reformat from main to components
This commit is contained in:
parent
5dd9b9dad8
commit
8e3b541d26
|
@ -1,6 +1,15 @@
|
|||
set(COMPONENT_ADD_INCLUDEDIRS "config include $ENV{IDF_PATH}/components/esp_ringbuf/include/ $ENV{IDF_PATH}/components/")
|
||||
set(COMPONENT_SRCS "./source/DAP.c ./source/DAP_vendor.c ./source/JTAG_DP.c ./source/SW_DP.c ./source/SWO.c ./source/spi_op.c ./source/spi_switch.c ./source/dap_utility.c")
|
||||
file(GLOB SOURCES
|
||||
./cmsis-dap/source/DAP.c
|
||||
./cmsis-dap/source/DAP_vendor.c
|
||||
./cmsis-dap/source/JTAG_DP.c
|
||||
./cmsis-dap/source/SW_DP.c
|
||||
./cmsis-dap/source/SWO.c
|
||||
./cmsis-dap/source/spi_op.c
|
||||
./cmsis-dap/source/spi_switch.c
|
||||
./cmsis-dap/source/dap_utility.c
|
||||
)
|
||||
|
||||
|
||||
|
||||
register_component()
|
||||
idf_component_register(
|
||||
SRCS ${SOURCES}
|
||||
INCLUDE_DIRS "."
|
||||
)
|
||||
|
|
|
@ -25,20 +25,6 @@
|
|||
*
|
||||
*---------------------------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
* @file DAP_config.h
|
||||
* @author windowsair
|
||||
* @brief Adaptation of GPIO and SPI pin
|
||||
* @change: 2021-2-10 Support GPIO and SPI
|
||||
* 2021-2-18 Try to support SWO
|
||||
* @version 0.1
|
||||
* @date 2021-2-10
|
||||
*
|
||||
* @copyright Copyright (c) 2021
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef __DAP_CONFIG_H__
|
||||
#define __DAP_CONFIG_H__
|
||||
|
||||
|
@ -47,12 +33,12 @@
|
|||
|
||||
#include "main/dap_configuration.h"
|
||||
#include "main/wifi_configuration.h"
|
||||
#include "main/timer.h"
|
||||
|
||||
#include "components/DAP/include/cmsis_compiler.h"
|
||||
#include "components/DAP/include/gpio_op.h"
|
||||
#include "components/DAP/include/spi_switch.h"
|
||||
#include "cmsis-dap/include/cmsis_compiler.h"
|
||||
#include "cmsis-dap/include/gpio_op.h"
|
||||
#include "cmsis-dap/include/spi_switch.h"
|
||||
|
||||
#include <soc/rtc.h>
|
||||
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
#include "gpio.h"
|
||||
|
@ -811,7 +797,7 @@ default, the DWT timer is used. The frequency of this timer is configured with
|
|||
*/
|
||||
__STATIC_INLINE uint32_t TIMESTAMP_GET(void)
|
||||
{
|
||||
return get_timer_count();
|
||||
return rtc_time_get();
|
||||
}
|
||||
|
||||
///@}
|
|
@ -0,0 +1,22 @@
|
|||
#ifndef __CMSIS_COMPILER_H__
|
||||
#define __CMSIS_COMPILER_H__
|
||||
|
||||
#ifndef __STATIC_FORCEINLINE
|
||||
#define __STATIC_FORCEINLINE static inline __attribute__((always_inline))
|
||||
#endif
|
||||
#ifndef __STATIC_INLINE
|
||||
#define __STATIC_INLINE static inline __attribute__((always_inline))
|
||||
#endif
|
||||
#ifndef __FORCEINLINE
|
||||
#define __FORCEINLINE inline __attribute__((always_inline))
|
||||
#endif
|
||||
#ifndef __WEAK
|
||||
#define __WEAK __attribute__((weak))
|
||||
#endif
|
||||
|
||||
#ifndef __UNUSED
|
||||
#define __UNUSED __attribute__((unused))
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
|
@ -29,16 +29,16 @@
|
|||
#endif
|
||||
#include "hal/gpio_types.h"
|
||||
#elif defined CONFIG_IDF_TARGET_ESP32C3
|
||||
#include "soc/esp32c3/include/soc/gpio_struct.h"
|
||||
#include "hal/esp32c3/include/hal/gpio_ll.h"
|
||||
#include "hal/esp32c3/include/hal/clk_gate_ll.h"
|
||||
#include "soc/esp32c3/include/soc/gpio_struct.h"
|
||||
#include "soc/esp32c3/include/soc/dport_access.h"
|
||||
#include "soc/esp32c3/include/soc/periph_defs.h"
|
||||
#include "soc/esp32c3/include/soc/usb_serial_jtag_reg.h"
|
||||
#include "soc/esp32c3/include/soc/io_mux_reg.h"
|
||||
#include "soc/esp32c3/include/soc/spi_struct.h"
|
||||
#include "soc/esp32c3/include/soc/spi_reg.h"
|
||||
#include "soc/gpio_struct.h"
|
||||
#include "hal/gpio_ll.h"
|
||||
#include "hal/clk_gate_ll.h"
|
||||
#include "soc/gpio_struct.h"
|
||||
#include "soc/dport_access.h"
|
||||
#include "soc/periph_defs.h"
|
||||
#include "soc/usb_serial_jtag_reg.h"
|
||||
#include "soc/io_mux_reg.h"
|
||||
#include "soc/spi_struct.h"
|
||||
#include "soc/spi_reg.h"
|
||||
#else
|
||||
#error unknown hardware
|
||||
#endif
|
|
@ -13,8 +13,8 @@
|
|||
#define __GPIO_OP_H__
|
||||
|
||||
#include "sdkconfig.h"
|
||||
#include "components/DAP/include/cmsis_compiler.h"
|
||||
#include "components/DAP/include/gpio_common.h"
|
||||
#include "cmsis-dap/include/cmsis_compiler.h"
|
||||
#include "cmsis-dap/include/gpio_common.h"
|
||||
|
||||
|
||||
|
|
@ -27,9 +27,9 @@
|
|||
|
||||
#include <string.h>
|
||||
|
||||
#include "components/DAP/config/DAP_config.h"
|
||||
#include "components/DAP/include/DAP.h"
|
||||
#include "components/DAP/include/spi_switch.h"
|
||||
#include "DAP_config.h"
|
||||
#include "cmsis-dap/include/DAP.h"
|
||||
#include "cmsis-dap/include/spi_switch.h"
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
|
@ -25,8 +25,8 @@
|
|||
*
|
||||
*---------------------------------------------------------------------------*/
|
||||
|
||||
#include "components/DAP/config/DAP_config.h"
|
||||
#include "components/DAP/include/DAP.h"
|
||||
#include "DAP_config.h"
|
||||
#include "cmsis-dap/include/DAP.h"
|
||||
|
||||
//**************************************************************************************************
|
||||
/**
|
|
@ -25,8 +25,8 @@
|
|||
*
|
||||
*---------------------------------------------------------------------------*/
|
||||
|
||||
#include "components/DAP/config/DAP_config.h"
|
||||
#include "components/DAP/include/DAP.h"
|
||||
#include "DAP_config.h"
|
||||
#include "cmsis-dap/include/DAP.h"
|
||||
|
||||
|
||||
// JTAG Macros
|
|
@ -38,10 +38,10 @@
|
|||
*
|
||||
*---------------------------------------------------------------------------*/
|
||||
|
||||
#include "components/DAP/config/DAP_config.h"
|
||||
#include "components/DAP/include/DAP.h"
|
||||
#include "components/DAP/include/uart_modify.h"
|
||||
#include "components/DAP/include/swo.h"
|
||||
#include "DAP_config.h"
|
||||
#include "cmsis-dap/include/DAP.h"
|
||||
#include "cmsis-dap/include/uart_modify.h"
|
||||
#include "cmsis-dap/include/swo.h"
|
||||
|
||||
#include "esp_err.h"
|
||||
|
|
@ -25,27 +25,13 @@
|
|||
*
|
||||
*---------------------------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
* @file SW_DP.c
|
||||
* @author windowsair
|
||||
* @brief Adaptation of GPIO and SPI
|
||||
* @change:
|
||||
* 2021-2-10 Support GPIO and SPI for SWD sequence / SWJ sequence / SWD transfer
|
||||
* Note: SWD sequence not yet tested
|
||||
* @version 0.1
|
||||
* @date 2021-2-10
|
||||
*
|
||||
* @copyright Copyright (c) 2021
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "components/DAP/config/DAP_config.h"
|
||||
#include "components/DAP/include/DAP.h"
|
||||
#include "components/DAP/include/spi_op.h"
|
||||
#include "components/DAP/include/spi_switch.h"
|
||||
#include "components/DAP/include/dap_utility.h"
|
||||
#include "DAP_config.h"
|
||||
#include "cmsis-dap/include/DAP.h"
|
||||
#include "cmsis-dap/include/spi_op.h"
|
||||
#include "cmsis-dap/include/spi_switch.h"
|
||||
#include "cmsis-dap/include/dap_utility.h"
|
||||
|
||||
|
||||
// Debug
|
|
@ -0,0 +1,10 @@
|
|||
#include "cmsis-dap/include/dap_utility.h"
|
||||
|
||||
const uint8_t kParityByteTable[256] =
|
||||
{
|
||||
#define P2(n) n, (n)^1, (n)^1, n
|
||||
#define P4(n) P2(n), P2((n)^1), P2((n)^1), P2(n)
|
||||
#define P6(n) P4(n), P4((n)^1), P4((n)^1), P4(n)
|
||||
|
||||
P6(0), P6(1), P6(1), P6(0)
|
||||
};
|
|
@ -12,17 +12,16 @@
|
|||
* @copyright MIT License
|
||||
*
|
||||
*/
|
||||
#include "sdkconfig.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "main/dap_configuration.h"
|
||||
|
||||
#include "components/DAP/include/cmsis_compiler.h"
|
||||
#include "components/DAP/include/spi_op.h"
|
||||
#include "components/DAP/include/spi_switch.h"
|
||||
#include "components/DAP/include/gpio_common.h"
|
||||
#include "cmsis-dap/include/cmsis_compiler.h"
|
||||
#include "cmsis-dap/include/spi_op.h"
|
||||
#include "cmsis-dap/include/spi_switch.h"
|
||||
#include "cmsis-dap/include/gpio_common.h"
|
||||
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
#define DAP_SPI SPI1
|
|
@ -15,9 +15,9 @@
|
|||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "components/DAP/include/cmsis_compiler.h"
|
||||
#include "components/DAP/include/spi_switch.h"
|
||||
#include "components/DAP/include/gpio_common.h"
|
||||
#include "cmsis-dap/include/cmsis_compiler.h"
|
||||
#include "cmsis-dap/include/spi_switch.h"
|
||||
#include "cmsis-dap/include/gpio_common.h"
|
||||
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
#define DAP_SPI SPI1
|
|
@ -1,63 +0,0 @@
|
|||
#ifndef __CMSIS_COMPILER_H__
|
||||
#define __CMSIS_COMPILER_H__
|
||||
|
||||
#ifndef __STATIC_FORCEINLINE
|
||||
#define __STATIC_FORCEINLINE static inline __attribute__((always_inline))
|
||||
#endif
|
||||
#ifndef __STATIC_INLINE
|
||||
#define __STATIC_INLINE static inline __attribute__((always_inline))
|
||||
#endif
|
||||
#ifndef __FORCEINLINE
|
||||
#define __FORCEINLINE inline __attribute__((always_inline))
|
||||
#endif
|
||||
#ifndef __WEAK
|
||||
#define __WEAK __attribute__((weak))
|
||||
#endif
|
||||
|
||||
#ifndef __UNUSED
|
||||
#define __UNUSED __attribute__((unused))
|
||||
#endif
|
||||
|
||||
#include "sdkconfig.h"
|
||||
|
||||
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
#define GPIO_PIN_REG_0 PERIPHS_IO_MUX_GPIO0_U
|
||||
#define GPIO_PIN_REG_1 PERIPHS_IO_MUX_U0TXD_U
|
||||
#define GPIO_PIN_REG_2 PERIPHS_IO_MUX_GPIO2_U
|
||||
#define GPIO_PIN_REG_3 PERIPHS_IO_MUX_U0RXD_U
|
||||
#define GPIO_PIN_REG_4 PERIPHS_IO_MUX_GPIO4_U
|
||||
#define GPIO_PIN_REG_5 PERIPHS_IO_MUX_GPIO5_U
|
||||
#define GPIO_PIN_REG_6 PERIPHS_IO_MUX_SD_CLK_U
|
||||
#define GPIO_PIN_REG_7 PERIPHS_IO_MUX_SD_DATA0_U
|
||||
#define GPIO_PIN_REG_8 PERIPHS_IO_MUX_SD_DATA1_U
|
||||
#define GPIO_PIN_REG_9 PERIPHS_IO_MUX_SD_DATA2_U
|
||||
#define GPIO_PIN_REG_10 PERIPHS_IO_MUX_SD_DATA3_U
|
||||
#define GPIO_PIN_REG_11 PERIPHS_IO_MUX_SD_CMD_U
|
||||
#define GPIO_PIN_REG_12 PERIPHS_IO_MUX_MTDI_U
|
||||
#define GPIO_PIN_REG_13 PERIPHS_IO_MUX_MTCK_U
|
||||
#define GPIO_PIN_REG_14 PERIPHS_IO_MUX_MTMS_U
|
||||
#define GPIO_PIN_REG_15 PERIPHS_IO_MUX_MTDO_U
|
||||
#define GPIO_PIN_REG_16 PAD_XPD_DCDC_CONF
|
||||
#define GPIO_PIN_REG(i) \
|
||||
(i==0) ? GPIO_PIN_REG_0: \
|
||||
(i==1) ? GPIO_PIN_REG_1: \
|
||||
(i==2) ? GPIO_PIN_REG_2: \
|
||||
(i==3) ? GPIO_PIN_REG_3: \
|
||||
(i==4) ? GPIO_PIN_REG_4: \
|
||||
(i==5) ? GPIO_PIN_REG_5: \
|
||||
(i==6) ? GPIO_PIN_REG_6: \
|
||||
(i==7) ? GPIO_PIN_REG_7: \
|
||||
(i==8) ? GPIO_PIN_REG_8: \
|
||||
(i==9) ? GPIO_PIN_REG_9: \
|
||||
(i==10)? GPIO_PIN_REG_10: \
|
||||
(i==11)? GPIO_PIN_REG_11: \
|
||||
(i==12)? GPIO_PIN_REG_12: \
|
||||
(i==13)? GPIO_PIN_REG_13: \
|
||||
(i==14)? GPIO_PIN_REG_14: \
|
||||
(i==15)? GPIO_PIN_REG_15: \
|
||||
GPIO_PIN_REG_16
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
|
@ -1,10 +0,0 @@
|
|||
#include "components/DAP/include/dap_utility.h"
|
||||
|
||||
const uint8_t kParityByteTable[256] =
|
||||
{
|
||||
#define P2(n) n, n^1, n^1, n
|
||||
#define P4(n) P2(n), P2(n^1), P2(n^1), P2(n)
|
||||
#define P6(n) P4(n), P4(n^1), P4(n^1), P4(n)
|
||||
|
||||
P6(0), P6(1), P6(1), P6(0)
|
||||
};
|
|
@ -1,4 +1,7 @@
|
|||
set(COMPONENT_ADD_INCLUDEDIRS ". ../../main")
|
||||
set(COMPONENT_SRCS "MSOS20_descriptor.c usb_handle.c usb_descriptor.c")
|
||||
file(GLOB SOURCES *.c)
|
||||
|
||||
register_component()
|
||||
idf_component_register(
|
||||
SRCS ${SOURCES}
|
||||
INCLUDE_DIRS "."
|
||||
PRIV_REQUIRES dap_proxy
|
||||
)
|
|
@ -10,7 +10,7 @@
|
|||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "main/usbip_server.h"
|
||||
#include "usbip_server.h"
|
||||
#include "main/wifi_configuration.h"
|
||||
|
||||
#include "components/USBIP/usb_handle.h"
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
file(GLOB SOURCES *.c)
|
||||
|
||||
idf_component_register(
|
||||
SRCS ${SOURCES}
|
||||
INCLUDE_DIRS "."
|
||||
PRIV_REQUIRES DAP USBIP esp_ringbuf
|
||||
)
|
|
@ -14,19 +14,19 @@
|
|||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "main/usbip_server.h"
|
||||
#include "main/DAP_handle.h"
|
||||
#include "usbip_server.h"
|
||||
#include "DAP_handle.h"
|
||||
#include "main/dap_configuration.h"
|
||||
#include "main/wifi_configuration.h"
|
||||
|
||||
#include "components/USBIP/usb_descriptor.h"
|
||||
#include "components/DAP/include/DAP.h"
|
||||
#include "components/DAP/include/swo.h"
|
||||
#include "cmsis-dap/include/DAP.h"
|
||||
#include "cmsis-dap/include/swo.h"
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/ringbuf.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
#include <freertos/ringbuf.h>
|
||||
#include <freertos/semphr.h>
|
||||
|
||||
#include "lwip/err.h"
|
||||
#include "lwip/sockets.h"
|
|
@ -16,5 +16,6 @@ void handle_swo_trace_response(usbip_stage2_header *header);
|
|||
void handle_dap_unlink();
|
||||
|
||||
int fast_reply(uint8_t *buf, uint32_t length);
|
||||
void DAP_Thread(void *argument);
|
||||
|
||||
#endif
|
|
@ -7,29 +7,20 @@
|
|||
* @copyright Copyright (c) 2020
|
||||
*
|
||||
*/
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/param.h>
|
||||
|
||||
#include "main/wifi_configuration.h"
|
||||
#include "main/usbip_server.h"
|
||||
#include "main/DAP_handle.h"
|
||||
#include "usbip_server.h"
|
||||
#include "DAP_handle.h"
|
||||
|
||||
#include "components/elaphureLink/elaphureLink_protocol.h"
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
#include "nvs_flash.h"
|
||||
|
||||
#include "lwip/err.h"
|
||||
#include "lwip/sockets.h"
|
||||
#include "lwip/sys.h"
|
||||
#include <lwip/netdb.h>
|
||||
|
||||
extern TaskHandle_t kDAPTaskHandle;
|
||||
extern int kRestartDAPHandle;
|
|
@ -1,10 +1,8 @@
|
|||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "main/usbip_server.h"
|
||||
#include "main/kcp_server.h"
|
||||
#include "main/tcp_netconn.h"
|
||||
#include "main/DAP_handle.h"
|
||||
#include "usbip_server.h"
|
||||
#include "DAP_handle.h"
|
||||
#include "main/wifi_configuration.h"
|
||||
|
||||
#include "components/USBIP/usb_handle.h"
|
||||
|
@ -36,13 +34,7 @@ static void handle_unlink(usbip_stage2_header *header);
|
|||
static void send_stage2_unlink(usbip_stage2_header *req_header);
|
||||
|
||||
int usbip_network_send(int s, const void *dataptr, size_t size, int flags) {
|
||||
#if (USE_KCP == 1)
|
||||
return kcp_network_send(dataptr, size);
|
||||
#elif (USE_TCP_NETCONN == 1)
|
||||
return tcp_netconn_send(dataptr, size);
|
||||
#else // BSD style
|
||||
return send(s, dataptr, size, flags);
|
||||
#endif
|
||||
}
|
||||
|
||||
int attach(uint8_t *buffer, uint32_t length)
|
|
@ -1,6 +1,7 @@
|
|||
set(COMPONENT_ADD_INCLUDEDIRS "${PROJECT_PATH}")
|
||||
set(COMPONENT_SRCS
|
||||
main.c timer.c tcp_server.c usbip_server.c DAP_handle.c
|
||||
kcp_server.c tcp_netconn.c uart_bridge.c wifi_handle.c)
|
||||
file(GLOB SOURCES *.c)
|
||||
|
||||
register_component()
|
||||
|
||||
idf_component_register(
|
||||
SRCS ${SOURCES}
|
||||
INCLUDE_DIRS "."
|
||||
)
|
|
@ -1,187 +0,0 @@
|
|||
/**
|
||||
* @file kcp_server.c
|
||||
* @author windows
|
||||
* @brief usbip KCP port
|
||||
* @version 0.1
|
||||
* @date 2021-10-08
|
||||
*
|
||||
* @copyright Copyright (c) 2021
|
||||
*
|
||||
*/
|
||||
|
||||
#include "main/kcp_server.h"
|
||||
#include "main/usbip_server.h"
|
||||
#include "main/wifi_configuration.h"
|
||||
|
||||
#include "components/kcp/ikcp.h"
|
||||
#include "components/kcp/ikcp_util.h"
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
|
||||
#include "lwip/err.h"
|
||||
#include "lwip/sockets.h"
|
||||
#include "lwip/sys.h"
|
||||
#include <lwip/netdb.h>
|
||||
|
||||
extern TaskHandle_t kDAPTaskHandle;
|
||||
extern int kRestartDAPHandle;
|
||||
extern int kSock;
|
||||
extern uint8_t kState;
|
||||
|
||||
static struct sockaddr_in client_addr = { 0 };
|
||||
static char kcp_buffer[MTU_SIZE];
|
||||
static ikcpcb *kcp1 = NULL;
|
||||
|
||||
|
||||
static void set_non_blocking(int sockfd) {
|
||||
int flag = fcntl(sockfd, F_GETFL, 0);
|
||||
if (flag < 0) {
|
||||
os_printf("fcntl F_GETFL fail\n");
|
||||
return;
|
||||
}
|
||||
if (fcntl(sockfd, F_SETFL, flag | O_NONBLOCK) < 0) {
|
||||
os_printf("fcntl F_SETFL fail\n");
|
||||
}
|
||||
}
|
||||
|
||||
static int udp_output(const char *buf, int len, ikcpcb *kcp, void *user)
|
||||
{
|
||||
int ret = -1;
|
||||
int time = 10;
|
||||
// Unfortunately, esp8266 often fails due to lack of memory
|
||||
while (ret < 0) {
|
||||
ret = sendto(kSock, buf, len, 0, (struct sockaddr *)&client_addr, sizeof(client_addr));
|
||||
if (ret < 0) {
|
||||
// os_printf("fail to send, retry\r\n");
|
||||
int errcode = errno;
|
||||
if (errno != ENOMEM)
|
||||
os_printf("unknown errcode %d\r\n", errcode);
|
||||
vTaskDelay(pdMS_TO_TICKS(time));
|
||||
time += 10;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int kcp_network_send(const char *buffer, int len) {
|
||||
ikcp_send(kcp1, buffer, len);
|
||||
ikcp_flush(kcp1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void kcp_server_task()
|
||||
{
|
||||
TickType_t xLastWakeTime = xTaskGetTickCount();
|
||||
|
||||
while (1) {
|
||||
kSock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
|
||||
if (kSock < 0) {
|
||||
os_printf("Unable to create socket: errno %d", errno);
|
||||
break;
|
||||
}
|
||||
os_printf("Socket created\r\n");
|
||||
|
||||
set_non_blocking(kSock);
|
||||
|
||||
|
||||
struct sockaddr_in server_addr;
|
||||
memset(&server_addr, 0, sizeof(server_addr));
|
||||
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
server_addr.sin_family = AF_INET;
|
||||
server_addr.sin_port = htons(PORT);
|
||||
|
||||
socklen_t socklen = sizeof(client_addr);
|
||||
|
||||
|
||||
int err = bind(kSock, (struct sockaddr *)&server_addr, sizeof(server_addr));
|
||||
if (err < 0) {
|
||||
os_printf("Socket unable to bind: errno %d\r\n", errno);
|
||||
}
|
||||
os_printf("Socket binded\r\n");
|
||||
|
||||
// KCP init
|
||||
if (kcp1 == NULL) {
|
||||
kcp1 = ikcp_create(1, (void *)0);
|
||||
}
|
||||
if (kcp1 == NULL) {
|
||||
os_printf("can not create kcp control block\r\n");
|
||||
break;
|
||||
}
|
||||
kcp1->output = udp_output;
|
||||
|
||||
ikcp_wndsize(kcp1, 4096, 4096);
|
||||
|
||||
ikcp_nodelay(kcp1, 2, 2, 2, 1); // set fast mode
|
||||
kcp1->interval = 0;
|
||||
kcp1->rx_minrto = 1;
|
||||
kcp1->fastresend = 1;
|
||||
|
||||
ikcp_setmtu(kcp1, 768);
|
||||
|
||||
|
||||
|
||||
int ret = -1;
|
||||
// KCP task main loop
|
||||
while (1) {
|
||||
vTaskDelayUntil(&xLastWakeTime, pdMS_TO_TICKS(1)); // we wanna sleep absolute time
|
||||
ikcp_update(kcp1, iclock());
|
||||
|
||||
// recv data from udp
|
||||
while (1) {
|
||||
ret = recvfrom(kSock, kcp_buffer, MTU_SIZE, 0, (struct sockaddr *)&client_addr, &socklen);
|
||||
if (ret < 0) {
|
||||
break;
|
||||
}
|
||||
ikcp_input(kcp1, kcp_buffer, ret);
|
||||
}
|
||||
|
||||
// recv data from kdp
|
||||
while (1) {
|
||||
ret = ikcp_recv(kcp1, kcp_buffer, MTU_SIZE);
|
||||
if (ret < 0) {
|
||||
break;
|
||||
}
|
||||
// recv user data, then handle it
|
||||
switch (kState)
|
||||
{
|
||||
case EMULATING:
|
||||
emulate((uint8_t *)kcp_buffer, ret);
|
||||
break;
|
||||
|
||||
case ACCEPTING:
|
||||
kState = ATTACHING;
|
||||
__attribute__((fallthrough));
|
||||
case ATTACHING:
|
||||
attach((uint8_t *)kcp_buffer, ret);
|
||||
break;
|
||||
|
||||
default:
|
||||
os_printf("unkonw kstate!\r\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
if (kcp1) {
|
||||
ikcp_release(kcp1);
|
||||
}
|
||||
if (kSock != -1) {
|
||||
os_printf("Shutting down socket and restarting...\r\n");
|
||||
shutdown(kSock, 0);
|
||||
close(kSock);
|
||||
|
||||
if (kState == EMULATING)
|
||||
kState = ACCEPTING;
|
||||
// Restart DAP Handle
|
||||
kRestartDAPHandle = 1;
|
||||
if (kDAPTaskHandle)
|
||||
xTaskNotifyGive(kDAPTaskHandle);
|
||||
}
|
||||
}
|
||||
vTaskDelete(NULL);
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
#ifndef __KCP_SERVER_H__
|
||||
#define __KCP_SERVER_H__
|
||||
|
||||
void kcp_server_task();
|
||||
int kcp_network_send(const char *buffer, int len);
|
||||
|
||||
#endif
|
68
main/main.c
68
main/main.c
|
@ -10,16 +10,11 @@
|
|||
#include <stdint.h>
|
||||
#include <sys/param.h>
|
||||
|
||||
#include "sdkconfig.h"
|
||||
#include "main/tcp_server.h"
|
||||
#include "main/tcp_netconn.h"
|
||||
#include "main/kcp_server.h"
|
||||
#include "main/uart_bridge.h"
|
||||
#include "main/timer.h"
|
||||
#include "tcp_server.h"
|
||||
#include "main/wifi_configuration.h"
|
||||
#include "main/wifi_handle.h"
|
||||
|
||||
#include "components/corsacOTA/src/corsacOTA.h"
|
||||
#include "cmsis-dap/include/DAP.h"
|
||||
#include "DAP_handle.h"
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
@ -37,13 +32,9 @@
|
|||
|
||||
#include "mdns.h"
|
||||
|
||||
extern void DAP_Setup(void);
|
||||
extern void DAP_Thread(void *argument);
|
||||
extern void SWO_Thread();
|
||||
|
||||
TaskHandle_t kDAPTaskHandle = NULL;
|
||||
|
||||
|
||||
static const char *MDNS_TAG = "server_common";
|
||||
|
||||
void mdns_setup() {
|
||||
|
@ -73,70 +64,17 @@ void mdns_setup() {
|
|||
}
|
||||
|
||||
void app_main() {
|
||||
// struct rst_info *rtc_info = system_get_rst_info();
|
||||
|
||||
// os_printf("reset reason: %x\n", rtc_info->reason);
|
||||
|
||||
// if (rtc_info->reason == REASON_WDT_RST ||
|
||||
// rtc_info->reason == REASON_EXCEPTION_RST ||
|
||||
// rtc_info->reason == REASON_SOFT_WDT_RST)
|
||||
// {
|
||||
// if (rtc_info->reason == REASON_EXCEPTION_RST)
|
||||
// {
|
||||
// os_printf("Fatal exception (%d):\n", rtc_info->exccause);
|
||||
// }
|
||||
// os_printf("epc1=0x%08x, epc2=0x%08x, epc3=0x%08x,excvaddr=0x%08x, depc=0x%08x\n",
|
||||
// rtc_info->epc1, rtc_info->epc2, rtc_info->epc3,
|
||||
// rtc_info->excvaddr, rtc_info->depc);
|
||||
// }
|
||||
|
||||
ESP_ERROR_CHECK(nvs_flash_init());
|
||||
|
||||
#if (USE_UART_BRIDGE == 1)
|
||||
uart_bridge_init();
|
||||
#endif
|
||||
wifi_init();
|
||||
DAP_Setup();
|
||||
timer_init();
|
||||
|
||||
#if (USE_MDNS == 1)
|
||||
mdns_setup();
|
||||
#endif
|
||||
|
||||
|
||||
#if (USE_OTA == 1)
|
||||
co_handle_t handle;
|
||||
co_config_t config = {
|
||||
.thread_name = "corsacOTA",
|
||||
.stack_size = 3192,
|
||||
.thread_prio = 8,
|
||||
.listen_port = 3241,
|
||||
.max_listen_num = 2,
|
||||
.wait_timeout_sec = 60,
|
||||
.wait_timeout_usec = 0,
|
||||
};
|
||||
|
||||
corsacOTA_init(&handle, &config);
|
||||
#endif
|
||||
|
||||
// Specify the usbip server task
|
||||
#if (USE_TCP_NETCONN == 1)
|
||||
xTaskCreate(tcp_netconn_task, "tcp_server", 4096, NULL, 14, NULL);
|
||||
#else // BSD style
|
||||
xTaskCreate(tcp_server_task, "tcp_server", 4096, NULL, 14, NULL);
|
||||
#endif
|
||||
|
||||
// DAP handle task
|
||||
xTaskCreate(DAP_Thread, "DAP_Task", 2048, NULL, 10, &kDAPTaskHandle);
|
||||
|
||||
#if defined CONFIG_IDF_TARGET_ESP8266
|
||||
#define UART_BRIDGE_TASK_STACK_SIZE 1024
|
||||
#else
|
||||
#define UART_BRIDGE_TASK_STACK_SIZE 2048
|
||||
#endif
|
||||
|
||||
//// FIXME: potential stack overflow
|
||||
#if (USE_UART_BRIDGE == 1)
|
||||
xTaskCreate(uart_bridge_task, "uart_server", UART_BRIDGE_TASK_STACK_SIZE, NULL, 2, NULL);
|
||||
#endif
|
||||
}
|
||||
|
|
|
@ -1,24 +0,0 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_system.h"
|
||||
|
||||
void esp_print_tasks(void)
|
||||
{
|
||||
char *pbuffer = (char *)calloc(1, 2048);
|
||||
os_printf("--------------- heap:%u ---------------------\r\n", esp_get_free_heap_size());
|
||||
vTaskGetRunTimeStats(pbuffer);
|
||||
os_printf("%s", pbuffer);
|
||||
os_printf("----------------------------------------------\r\n");
|
||||
free(pbuffer);
|
||||
}
|
||||
|
||||
void monitor_task()
|
||||
{
|
||||
while(1) {
|
||||
esp_print_tasks();
|
||||
vTaskDelay(3000 / portTICK_RATE_MS);
|
||||
}
|
||||
}
|
|
@ -1,212 +0,0 @@
|
|||
/*
|
||||
Copyright (c) 2015, SuperHouse Automation Pty Ltd
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/param.h>
|
||||
|
||||
#include "main/wifi_configuration.h"
|
||||
#include "main/usbip_server.h"
|
||||
#include "main/tcp_netconn.h"
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
#include "nvs_flash.h"
|
||||
|
||||
#include "lwip/err.h"
|
||||
#include "lwip/sockets.h"
|
||||
#include "lwip/sys.h"
|
||||
#include "lwip/netbuf.h"
|
||||
#include "lwip/api.h"
|
||||
#include "lwip/tcp.h"
|
||||
#include <lwip/netdb.h>
|
||||
|
||||
#define PORT 3240
|
||||
#define EVENTS_QUEUE_SIZE 50
|
||||
|
||||
#ifdef CALLBACK_DEBUG
|
||||
#define debug(s, ...) os_printf("%s: " s "\n", "Cb:", ##__VA_ARGS__)
|
||||
#else
|
||||
#define debug(s, ...)
|
||||
#endif
|
||||
|
||||
QueueHandle_t xQueue_events;
|
||||
typedef struct
|
||||
{
|
||||
struct netconn *nc;
|
||||
uint8_t type;
|
||||
} netconn_events;
|
||||
|
||||
extern TaskHandle_t kDAPTaskHandle;
|
||||
extern int kRestartDAPHandle;
|
||||
extern uint8_t kState;
|
||||
|
||||
struct netconn *kNetconn = NULL;
|
||||
|
||||
int tcp_netconn_send(const void *buffer, size_t len)
|
||||
{
|
||||
return netconn_write(kNetconn, buffer, len, NETCONN_COPY);
|
||||
}
|
||||
|
||||
/*
|
||||
* This function will be call in Lwip in each event on netconn
|
||||
*/
|
||||
static void netCallback(struct netconn *conn, enum netconn_evt evt, uint16_t length)
|
||||
{
|
||||
// Show some callback information (debug)
|
||||
debug("sock:%u\tsta:%u\tevt:%u\tlen:%u\ttyp:%u\tfla:%02x\terr:%d",
|
||||
(uint32_t)conn, conn->state, evt, length, conn->type, conn->flags, conn->pending_err);
|
||||
|
||||
netconn_events events;
|
||||
|
||||
// If netconn got error, it is close or deleted, dont do treatments on it.
|
||||
if (conn->pending_err)
|
||||
{
|
||||
return;
|
||||
}
|
||||
// Treatments only on rcv events.
|
||||
switch (evt)
|
||||
{
|
||||
case NETCONN_EVT_RCVPLUS:
|
||||
events.nc = conn;
|
||||
events.type = evt;
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
break;
|
||||
}
|
||||
|
||||
// Send the event to the queue
|
||||
xQueueSend(xQueue_events, &events, 1000);
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize a server netconn and listen port
|
||||
*/
|
||||
static void set_tcp_server_netconn(struct netconn **nc, uint16_t port, netconn_callback callback)
|
||||
{
|
||||
if (nc == NULL)
|
||||
{
|
||||
os_printf("%s: netconn missing .\n", __FUNCTION__);
|
||||
return;
|
||||
}
|
||||
*nc = netconn_new_with_callback(NETCONN_TCP, netCallback);
|
||||
if (!*nc)
|
||||
{
|
||||
os_printf("Status monitor: Failed to allocate netconn.\n");
|
||||
return;
|
||||
}
|
||||
netconn_set_nonblocking(*nc, NETCONN_FLAG_NON_BLOCKING);
|
||||
// netconn_set_recvtimeout(*nc, 10);
|
||||
netconn_bind(*nc, IP_ADDR_ANY, port);
|
||||
netconn_listen(*nc);
|
||||
}
|
||||
|
||||
/*
|
||||
* Close and delete a socket properly
|
||||
*/
|
||||
static void close_tcp_netconn(struct netconn *nc)
|
||||
{
|
||||
nc->pending_err = ERR_CLSD; // It is hacky way to be sure than callback will don't do treatment on a netconn closed and deleted
|
||||
netconn_close(nc);
|
||||
netconn_delete(nc);
|
||||
}
|
||||
|
||||
void tcp_netconn_task()
|
||||
{
|
||||
xQueue_events = xQueueCreate(EVENTS_QUEUE_SIZE, sizeof(netconn_events));
|
||||
struct netconn *nc = NULL; // To create servers
|
||||
|
||||
set_tcp_server_netconn(&nc, PORT, netCallback);
|
||||
os_printf("Server netconn %u ready on port %u.\n", (uint32_t)nc, PORT);
|
||||
|
||||
struct netbuf *netbuf = NULL; // To store incoming Data
|
||||
struct netconn *nc_in = NULL; // To accept incoming netconn
|
||||
//
|
||||
char *buffer;
|
||||
uint16_t len_buf;
|
||||
|
||||
while (1)
|
||||
{
|
||||
netconn_events events;
|
||||
xQueueReceive(xQueue_events, &events, portMAX_DELAY); // Wait here an event on netconn
|
||||
|
||||
if (events.nc->state == NETCONN_LISTEN) // If netconn is a server and receive incoming event on it
|
||||
{
|
||||
os_printf("Client incoming on server %u.\n", (uint32_t)events.nc);
|
||||
int err = netconn_accept(events.nc, &nc_in);
|
||||
if (err != ERR_OK)
|
||||
{
|
||||
if (nc_in)
|
||||
netconn_delete(nc_in);
|
||||
}
|
||||
os_printf("New client is %u.\n", (uint32_t)nc_in);
|
||||
ip_addr_t client_addr; // Address port
|
||||
uint16_t client_port; // Client port
|
||||
netconn_peer(nc_in, &client_addr, &client_port);
|
||||
// tcp_nagle_disable(events.nc->pcb.tcp); // crash! DO NOT USE
|
||||
}
|
||||
else if (events.nc->state != NETCONN_LISTEN) // If netconn is the client and receive data
|
||||
{
|
||||
// tcp_nagle_disable(events.nc->pcb.tcp);
|
||||
if ((netconn_recv(events.nc, &netbuf)) == ERR_OK) // data incoming ?
|
||||
{
|
||||
do
|
||||
{
|
||||
netbuf_data(netbuf, (void *)&buffer, &len_buf);
|
||||
kNetconn = events.nc;
|
||||
switch (kState)
|
||||
{
|
||||
case ACCEPTING:
|
||||
kState = ATTACHING;
|
||||
__attribute__((fallthrough));
|
||||
case ATTACHING:
|
||||
attach((uint8_t *)buffer, len_buf);
|
||||
kState = EMULATING;
|
||||
break;
|
||||
|
||||
case EMULATING:
|
||||
emulate((uint8_t *)buffer, len_buf);
|
||||
break;
|
||||
default:
|
||||
os_printf("unkonw kstate!\r\n");
|
||||
}
|
||||
} while (netbuf_next(netbuf) >= 0);
|
||||
netbuf_delete(netbuf);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (events.nc->pending_err == ERR_CLSD)
|
||||
{
|
||||
continue; // The same hacky way to treat a closed connection
|
||||
}
|
||||
os_printf("Shutting down socket and restarting...\r\n");
|
||||
close_tcp_netconn(events.nc);
|
||||
if (kState == EMULATING)
|
||||
kState = ACCEPTING;
|
||||
// Restart DAP Handle
|
||||
kRestartDAPHandle = 1;
|
||||
if (kDAPTaskHandle)
|
||||
xTaskNotifyGive(kDAPTaskHandle);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
#ifndef __TCP_NETCONN_H__
|
||||
#define __TCP_NETCONN_H__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
int tcp_netconn_send(const void *buffer, size_t len);
|
||||
void tcp_netconn_task();
|
||||
|
||||
#endif
|
63
main/timer.c
63
main/timer.c
|
@ -1,63 +0,0 @@
|
|||
/**
|
||||
* @file timer.c
|
||||
* @brief Hardware timer for DAP timestamp
|
||||
* @change: 2021-02-18 Using the FRC2 timer
|
||||
*
|
||||
* @version 0.2
|
||||
* @date 2020-01-22
|
||||
*
|
||||
* @copyright Copyright (c) 2021
|
||||
*
|
||||
*/
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "main/timer.h"
|
||||
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
#include "hw_timer.h"
|
||||
#endif
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/event_groups.h"
|
||||
|
||||
/*
|
||||
Note:
|
||||
System bus frequency is 80MHz, will not be affected by CPU frequency. The
|
||||
frequency of UART, SPI, or other peripheral devices, are divided from system
|
||||
bus frequency, so they will not be affected by CPU frequency either.
|
||||
|
||||
*/
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
#define TIMER_BASE 0x60000600
|
||||
volatile frc2_struct_t * frc2 = (frc2_struct_t *)(TIMER_BASE + (1) * 0x20);
|
||||
#endif
|
||||
|
||||
void timer_init()
|
||||
{
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
vPortEnterCritical();
|
||||
frc2->ctrl.div = TIMER_CLKDIV_16; // 80MHz / 16 = 5MHz
|
||||
frc2->ctrl.intr_type = TIMER_EDGE_INT;
|
||||
frc2->ctrl.reload = 0x01; // enable auto reload
|
||||
frc2->load.val = 0x7FFFFFFF; // 31bit max
|
||||
frc2->ctrl.en = 0x01;
|
||||
vPortExitCritical();
|
||||
#endif
|
||||
}
|
||||
|
||||
// Timing up to 2147483647(0x7FFFFFFF) / 5000000(5MHz) = 429s
|
||||
// 0.2 micro-second resolution
|
||||
uint32_t get_timer_count()
|
||||
{
|
||||
|
||||
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
return (uint32_t)frc2->count.data;
|
||||
#elif defined CONFIG_IDF_TARGET_ESP32 || defined CONFIG_IDF_TARGET_ESP32C3
|
||||
return 0;
|
||||
#else
|
||||
#error unknown hardware
|
||||
#endif
|
||||
}
|
74
main/timer.h
74
main/timer.h
|
@ -1,74 +0,0 @@
|
|||
/**
|
||||
* @file timer.h
|
||||
* @author windowsair
|
||||
* @brief esp8266 hardware timer
|
||||
* @change: 2021-02-18 Add frc2 timer
|
||||
* @note
|
||||
* FRC2 is not officially documented. There is no guarantee of its behavior.
|
||||
* FRC2 may be used for RTC functions. May be reserved for other functions in the future.
|
||||
*
|
||||
*
|
||||
* @version 0.1
|
||||
* @date 2021-02-18
|
||||
*
|
||||
* @copyright Copyright (c) 2021
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
#ifndef __TIMER_H__
|
||||
#define __TIMER_H__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "sdkconfig.h"
|
||||
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
// FRC2 is a 32-bit countup timer
|
||||
typedef struct {
|
||||
union {
|
||||
struct {
|
||||
uint32_t data: 32;
|
||||
};
|
||||
uint32_t val;
|
||||
} load;
|
||||
|
||||
union {
|
||||
struct {
|
||||
uint32_t data: 31;
|
||||
uint32_t reserved23: 1;
|
||||
};
|
||||
uint32_t val;
|
||||
} count;
|
||||
|
||||
union {
|
||||
struct {
|
||||
uint32_t div: 6;
|
||||
uint32_t reload: 1;
|
||||
uint32_t en: 1;
|
||||
uint32_t intr_type: 1;
|
||||
uint32_t reserved24: 23;
|
||||
};
|
||||
uint32_t val;
|
||||
} ctrl;
|
||||
|
||||
union {
|
||||
struct {
|
||||
uint32_t clr: 1;
|
||||
uint32_t reserved1: 31;
|
||||
};
|
||||
uint32_t val;
|
||||
} intr;
|
||||
} frc2_struct_t;
|
||||
|
||||
extern volatile frc2_struct_t* frc2;
|
||||
#endif
|
||||
|
||||
extern void timer_init();
|
||||
extern uint32_t get_timer_count();
|
||||
|
||||
#endif
|
|
@ -1,315 +0,0 @@
|
|||
/*
|
||||
Copyright (c) 2015, SuperHouse Automation Pty Ltd
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
|
||||
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file uart_bridge.c
|
||||
* @author windowsair
|
||||
* @brief UART TCP bridge
|
||||
* @version 0.1
|
||||
* @date 2021-11-16
|
||||
*
|
||||
* @copyright Copyright (c) 2021
|
||||
*
|
||||
*/
|
||||
#include "sdkconfig.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/param.h>
|
||||
#include <stdatomic.h>
|
||||
|
||||
#include "main/wifi_configuration.h"
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "driver/uart.h"
|
||||
|
||||
#include "lwip/err.h"
|
||||
#include "lwip/sockets.h"
|
||||
#include "lwip/sys.h"
|
||||
#include "lwip/netbuf.h"
|
||||
#include "lwip/api.h"
|
||||
#include "lwip/tcp.h"
|
||||
#include <lwip/netdb.h>
|
||||
|
||||
#if (USE_UART_BRIDGE == 1)
|
||||
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
#define UART_BRIDGE_TX UART_NUM_0
|
||||
#define UART_BRIDGE_RX UART_NUM_1
|
||||
#elif defined CONFIG_IDF_TARGET_ESP32
|
||||
#define UART_BRIDGE_TX UART_NUM_2
|
||||
#define UART_BRIDGE_RX UART_NUM_2
|
||||
#define UART_BRIDGE_TX_PIN 23
|
||||
#define UART_BRIDGE_RX_PIN 22
|
||||
#elif defined CONFIG_IDF_TARGET_ESP32C3
|
||||
#define UART_BRIDGE_TX UART_NUM_1
|
||||
#define UART_BRIDGE_RX UART_NUM_1
|
||||
#define UART_BRIDGE_TX_PIN 19
|
||||
#define UART_BRIDGE_RX_PIN 18 // PIN18 has 50000ns glitch during the power-up
|
||||
#else
|
||||
#error unknown hardware
|
||||
#endif
|
||||
|
||||
#define EVENTS_QUEUE_SIZE 10
|
||||
#define UART_BUF_SIZE 512
|
||||
|
||||
#ifdef CALLBACK_DEBUG
|
||||
#define debug(s, ...) os_printf("%s: " s "\n", "Cb:", ##__VA_ARGS__)
|
||||
#else
|
||||
#define debug(s, ...)
|
||||
#endif
|
||||
|
||||
static const char *UART_TAG = "UART";
|
||||
#define NETCONN_EVT_WIFI_DISCONNECTED (NETCONN_EVT_ERROR + 1)
|
||||
|
||||
static QueueHandle_t uart_server_events = NULL;
|
||||
typedef struct
|
||||
{
|
||||
struct netconn *nc;
|
||||
uint8_t type;
|
||||
} netconn_events;
|
||||
|
||||
static uint8_t uart_read_buffer[UART_BUF_SIZE];
|
||||
// use lwip buffer to write back
|
||||
static struct netconn *uart_netconn = NULL;
|
||||
static bool is_conn_valid = false; // lock free
|
||||
static bool is_first_time_recv = false;
|
||||
|
||||
void uart_bridge_close() {
|
||||
netconn_events events;
|
||||
events.type = NETCONN_EVT_WIFI_DISCONNECTED;
|
||||
xQueueSend(uart_server_events, &events, 1000);
|
||||
}
|
||||
|
||||
static void uart_bridge_reset() {
|
||||
uart_netconn = NULL;
|
||||
is_conn_valid = false;
|
||||
}
|
||||
|
||||
static int num_digits(int n) {
|
||||
if (n < 10)
|
||||
return 1;
|
||||
if (n < 100)
|
||||
return 2;
|
||||
if (n < 1000)
|
||||
return 3;
|
||||
if (n < 10000)
|
||||
return 4;
|
||||
if (n < 100000)
|
||||
return 5;
|
||||
if (n < 1000000)
|
||||
return 6;
|
||||
|
||||
return 7;
|
||||
}
|
||||
|
||||
/*
|
||||
* This function will be call in Lwip in each event on netconn
|
||||
*/
|
||||
static void netCallback(struct netconn *conn, enum netconn_evt evt, uint16_t length) {
|
||||
// Show some callback information (debug)
|
||||
debug("sock:%u\tsta:%u\tevt:%u\tlen:%u\ttyp:%u\tfla:%02x\terr:%d",
|
||||
(uint32_t)conn, conn->state, evt, length, conn->type, conn->flags, conn->pending_err);
|
||||
|
||||
netconn_events events;
|
||||
|
||||
// If netconn got error, it is close or deleted, dont do treatments on it.
|
||||
if (conn->pending_err) {
|
||||
return;
|
||||
}
|
||||
// Treatments only on rcv events.
|
||||
switch (evt) {
|
||||
case NETCONN_EVT_RCVPLUS:
|
||||
events.nc = conn;
|
||||
events.type = evt;
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
break;
|
||||
}
|
||||
|
||||
// Send the event to the queue
|
||||
xQueueSend(uart_server_events, &events, pdMS_TO_TICKS(1000));
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize a server netconn and listen port
|
||||
*/
|
||||
static void set_tcp_server_netconn(struct netconn **nc, uint16_t port, netconn_callback callback) {
|
||||
if (nc == NULL) {
|
||||
ESP_LOGE(UART_TAG, "%s: netconn missing .\n", __FUNCTION__);
|
||||
return;
|
||||
}
|
||||
*nc = netconn_new_with_callback(NETCONN_TCP, netCallback);
|
||||
if (!*nc) {
|
||||
ESP_LOGE(UART_TAG, "Status monitor: Failed to allocate netconn.\n");
|
||||
return;
|
||||
}
|
||||
netconn_set_nonblocking(*nc, NETCONN_FLAG_NON_BLOCKING);
|
||||
// netconn_set_recvtimeout(*nc, 10);
|
||||
netconn_bind(*nc, IP_ADDR_ANY, port);
|
||||
netconn_listen(*nc);
|
||||
}
|
||||
|
||||
/*
|
||||
* Close and delete a socket properly
|
||||
*/
|
||||
static void close_tcp_netconn(struct netconn *nc) {
|
||||
nc->pending_err = ERR_CLSD; // It is hacky way to be sure than callback will don't do treatment on a netconn closed and deleted
|
||||
netconn_close(nc);
|
||||
netconn_delete(nc);
|
||||
}
|
||||
|
||||
static void uart_bridge_setup() {
|
||||
uart_config_t uart_config = {
|
||||
.baud_rate = UART_BRIDGE_BAUDRATE,
|
||||
.data_bits = UART_DATA_8_BITS,
|
||||
.parity = UART_PARITY_DISABLE,
|
||||
.stop_bits = UART_STOP_BITS_1,
|
||||
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE};
|
||||
|
||||
if (UART_BRIDGE_TX == UART_BRIDGE_RX) {
|
||||
uart_param_config(UART_BRIDGE_RX, &uart_config);
|
||||
uart_driver_install(UART_BRIDGE_RX, UART_BUF_SIZE, UART_BUF_SIZE, 0, NULL, 0);
|
||||
} else {
|
||||
uart_param_config(UART_BRIDGE_RX, &uart_config);
|
||||
uart_param_config(UART_BRIDGE_TX, &uart_config);
|
||||
|
||||
uart_driver_install(UART_BRIDGE_RX, UART_BUF_SIZE, 0, 0, NULL, 0); // RX only
|
||||
uart_driver_install(UART_BRIDGE_TX, 0, UART_BUF_SIZE, 0, NULL, 0); // TX only
|
||||
}
|
||||
|
||||
#if defined CONFIG_IDF_TARGET_ESP32 || defined CONFIG_IDF_TARGET_ESP32C3
|
||||
uart_set_pin(UART_BRIDGE_TX, UART_BRIDGE_TX_PIN, UART_BRIDGE_RX_PIN, -1, -1);
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
void uart_bridge_init() {
|
||||
uart_server_events = xQueueCreate(EVENTS_QUEUE_SIZE, sizeof(netconn_events));
|
||||
}
|
||||
|
||||
void uart_bridge_task() {
|
||||
uart_bridge_setup();
|
||||
uart_server_events = xQueueCreate(EVENTS_QUEUE_SIZE, sizeof(netconn_events));
|
||||
|
||||
struct netconn *nc = NULL; // To create servers
|
||||
|
||||
set_tcp_server_netconn(&nc, UART_BRIDGE_PORT, netCallback);
|
||||
|
||||
struct netbuf *netbuf = NULL; // To store incoming Data
|
||||
struct netconn *nc_in = NULL; // To accept incoming netconn
|
||||
//
|
||||
char *buffer;
|
||||
uint16_t len_buf;
|
||||
size_t uart_buf_len;
|
||||
|
||||
while (1) {
|
||||
netconn_events events;
|
||||
int ret = xQueueReceive(uart_server_events, &events, pdMS_TO_TICKS(100));
|
||||
|
||||
if (ret != pdTRUE) {
|
||||
// timeout
|
||||
if (is_conn_valid) {
|
||||
ESP_ERROR_CHECK(uart_get_buffered_data_len(UART_BRIDGE_RX, &uart_buf_len));
|
||||
uart_buf_len = uart_buf_len > UART_BUF_SIZE ? UART_BUF_SIZE : uart_buf_len;
|
||||
uart_buf_len = uart_read_bytes(UART_BRIDGE_RX, uart_read_buffer, uart_buf_len, pdMS_TO_TICKS(5));
|
||||
// then send data
|
||||
netconn_write(uart_netconn, uart_read_buffer, uart_buf_len, NETCONN_COPY);
|
||||
}
|
||||
} else if (events.type == NETCONN_EVT_WIFI_DISCONNECTED) { // WIFI disconnected
|
||||
if (is_conn_valid) {
|
||||
close_tcp_netconn(uart_netconn);
|
||||
uart_bridge_reset();
|
||||
}
|
||||
} else if (events.nc->state == NETCONN_LISTEN) {
|
||||
if (is_conn_valid) {
|
||||
netconn_accept(events.nc, &nc_in);
|
||||
if (nc_in)
|
||||
close_tcp_netconn(nc_in);
|
||||
continue;
|
||||
}
|
||||
|
||||
int err = netconn_accept(events.nc, &nc_in);
|
||||
if (err != ERR_OK) {
|
||||
if (nc_in)
|
||||
netconn_delete(nc_in);
|
||||
continue;
|
||||
}
|
||||
|
||||
ip_addr_t client_addr; // Address port
|
||||
uint16_t client_port; // Client port
|
||||
netconn_peer(nc_in, &client_addr, &client_port);
|
||||
|
||||
uart_netconn = nc_in;
|
||||
is_conn_valid = true;
|
||||
is_first_time_recv = true;
|
||||
} else if (events.nc->state != NETCONN_LISTEN) {
|
||||
// if (events.nc && events.nc->pcb.tcp)
|
||||
// tcp_nagle_disable(events.nc->pcb.tcp);
|
||||
|
||||
uart_netconn = events.nc;
|
||||
// read data from UART
|
||||
ESP_ERROR_CHECK(uart_get_buffered_data_len(UART_BRIDGE_RX, &uart_buf_len));
|
||||
uart_buf_len = uart_buf_len > UART_BUF_SIZE ? UART_BUF_SIZE : uart_buf_len;
|
||||
uart_buf_len = uart_read_bytes(UART_BRIDGE_RX, uart_read_buffer, uart_buf_len, pdMS_TO_TICKS(5));
|
||||
// then send data
|
||||
netconn_write(events.nc, uart_read_buffer, uart_buf_len, NETCONN_COPY);
|
||||
|
||||
// try to get data
|
||||
if ((netconn_recv(events.nc, &netbuf)) == ERR_OK) // data incoming ?
|
||||
{
|
||||
do {
|
||||
netbuf_data(netbuf, (void *)&buffer, &len_buf);
|
||||
// write to uart
|
||||
if (is_first_time_recv) { // change bard rate
|
||||
if (len_buf > 1 && len_buf < 8) {
|
||||
char tmp_buff[8];
|
||||
memcpy(tmp_buff, buffer, len_buf);
|
||||
tmp_buff[len_buf] = '\0';
|
||||
int baudrate = atoi(tmp_buff);
|
||||
if (baudrate > 0 && baudrate < 2000000 && num_digits(baudrate) == len_buf) {
|
||||
ESP_LOGI(UART_TAG, "change baud:%d", baudrate);
|
||||
uart_set_baudrate(UART_BRIDGE_RX, baudrate);
|
||||
uart_set_baudrate(UART_BRIDGE_TX, baudrate);
|
||||
is_first_time_recv = false;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
is_first_time_recv = false;
|
||||
}
|
||||
uart_write_bytes(UART_BRIDGE_TX, (const char *)buffer, len_buf);
|
||||
} while (netbuf_next(netbuf) >= 0);
|
||||
netbuf_delete(netbuf);
|
||||
} else {
|
||||
if (events.nc->pending_err == ERR_CLSD) {
|
||||
continue; // The same hacky way to treat a closed connection
|
||||
}
|
||||
close_tcp_netconn(events.nc);
|
||||
uart_bridge_reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif // (USE_UART_BRIDGE == 1)
|
|
@ -1,10 +0,0 @@
|
|||
#ifndef _UART_BRIDGE_H_
|
||||
#define _UART_BRIDGE_H_
|
||||
|
||||
|
||||
void uart_bridge_init();
|
||||
void uart_bridge_task();
|
||||
void uart_bridge_close();
|
||||
|
||||
|
||||
#endif
|
|
@ -15,7 +15,7 @@ static struct {
|
|||
const char *ssid;
|
||||
const char *password;
|
||||
} wifi_list[] __attribute__((unused)) = {
|
||||
{.ssid = "OTA", .password = "12345678"},
|
||||
{.ssid = "MOWiFi_2.4G_A7F0", .password = "00112288"},
|
||||
{.ssid = "DAP", .password = "12345678"},
|
||||
// Add your WAP like this:
|
||||
// {.ssid = "your ssid", .password = "your password"},
|
||||
|
@ -29,7 +29,7 @@ static struct {
|
|||
#define MDNS_INSTANCE "DAP mDNS"
|
||||
//
|
||||
|
||||
#define USE_STATIC_IP 1
|
||||
#define USE_STATIC_IP 0
|
||||
// If you don't want to specify the ip configuration, then ignore the following items.
|
||||
#define DAP_IP_ADDRESS 192, 168, 137, 123
|
||||
#define DAP_IP_GATEWAY 192, 168, 137, 1
|
||||
|
@ -52,13 +52,6 @@ static struct {
|
|||
#define MTU_SIZE 1500
|
||||
//
|
||||
|
||||
#if (USE_TCP_NETCONN == 1 && USE_KCP == 1)
|
||||
#error Can not use KCP and TCP at the same time!
|
||||
#endif
|
||||
|
||||
#if (USE_KCP == 1)
|
||||
#warning KCP is a very experimental feature, and it should not be used under any circumstances. Please make sure what you are doing. Related usbip version: https://github.com/windowsair/usbip-win
|
||||
#endif
|
||||
|
||||
|
||||
extern int printf(const char *, ...);
|
||||
|
|
|
@ -8,9 +8,6 @@
|
|||
#endif
|
||||
|
||||
#include "main/wifi_configuration.h"
|
||||
#include "main/uart_bridge.h"
|
||||
|
||||
#include "components/DAP/include/gpio_op.h"
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
@ -58,14 +55,14 @@ static void event_handler(void *handler_arg __attribute__((unused)),
|
|||
break;
|
||||
case IP_EVENT_STA_GOT_IP: {
|
||||
ip_event_got_ip_t *event = event_data;
|
||||
GPIO_SET_LEVEL_HIGH(PIN_LED_WIFI_STATUS);
|
||||
// GPIO_SET_LEVEL_HIGH(PIN_LED_WIFI_STATUS);
|
||||
xEventGroupSetBits(wifi_event_group, IPV4_GOTIP_BIT);
|
||||
os_printf("SYSTEM EVENT STA GOT IP : %s\r\n", ip4addr_ntoa((const ip4_addr_t *) &event->ip_info.ip));
|
||||
break;
|
||||
}
|
||||
case WIFI_EVENT_STA_DISCONNECTED: {
|
||||
wifi_event_sta_disconnected_t *event = event_data;
|
||||
GPIO_SET_LEVEL_LOW(PIN_LED_WIFI_STATUS);
|
||||
// GPIO_SET_LEVEL_LOW(PIN_LED_WIFI_STATUS);
|
||||
os_printf("Disconnect reason : %d\r\n", event->reason);
|
||||
|
||||
#ifdef CONFIG_IDF_TARGET_ESP8266
|
||||
|
@ -132,8 +129,8 @@ static void wait_for_ip() {
|
|||
}
|
||||
|
||||
void wifi_init(void) {
|
||||
GPIO_FUNCTION_SET(PIN_LED_WIFI_STATUS);
|
||||
GPIO_SET_DIRECTION_NORMAL_OUT(PIN_LED_WIFI_STATUS);
|
||||
// GPIO_FUNCTION_SET(PIN_LED_WIFI_STATUS);
|
||||
// GPIO_SET_DIRECTION_NORMAL_OUT(PIN_LED_WIFI_STATUS);
|
||||
|
||||
esp_netif_init();
|
||||
wifi_event_group = xEventGroupCreate();
|
||||
|
|
Loading…
Reference in New Issue