feat: 完成TCP2UART透传核心集成
集成CH390驱动、LwIP协议栈和FreeRTOS多任务透传框架,确保TCP Server/Client与UART链路按配置稳定联动。
This commit is contained in:
+250
-9
@@ -25,7 +25,21 @@
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "semphr.h"
|
||||
#include "stream_buffer.h"
|
||||
|
||||
/* Application modules */
|
||||
#include "tcp_server.h"
|
||||
#include "tcp_client.h"
|
||||
#include "uart_trans.h"
|
||||
#include "config.h"
|
||||
#include "ethernetif.h"
|
||||
|
||||
/* LwIP includes */
|
||||
#include "lwip/tcpip.h"
|
||||
#include "lwip/ip4_addr.h"
|
||||
#include "lwip/dhcp.h"
|
||||
#include "lwip/timeouts.h"
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
@@ -35,7 +49,21 @@
|
||||
|
||||
/* Private define ------------------------------------------------------------*/
|
||||
/* USER CODE BEGIN PD */
|
||||
/* Task stack sizes (words, not bytes) */
|
||||
#define LWIP_TASK_STACK_SIZE 512
|
||||
#define TCP_SERVER_TASK_STACK_SIZE 384
|
||||
#define TCP_CLIENT_TASK_STACK_SIZE 384
|
||||
#define UART_TRANS_TASK_STACK_SIZE 256
|
||||
#define CONFIG_TASK_STACK_SIZE 384
|
||||
#define DEFAULT_TASK_STACK_SIZE 128
|
||||
|
||||
/* Task priorities */
|
||||
#define LWIP_TASK_PRIORITY (osPriorityAboveNormal)
|
||||
#define TCP_SERVER_TASK_PRIORITY (osPriorityNormal)
|
||||
#define TCP_CLIENT_TASK_PRIORITY (osPriorityNormal)
|
||||
#define UART_TRANS_TASK_PRIORITY (osPriorityNormal)
|
||||
#define CONFIG_TASK_PRIORITY (osPriorityBelowNormal)
|
||||
#define DEFAULT_TASK_PRIORITY (osPriorityLow)
|
||||
/* USER CODE END PD */
|
||||
|
||||
/* Private macro -------------------------------------------------------------*/
|
||||
@@ -45,19 +73,32 @@
|
||||
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Variables */
|
||||
/* Task handles */
|
||||
TaskHandle_t lwipTaskHandle = NULL;
|
||||
TaskHandle_t tcpServerTaskHandle = NULL;
|
||||
TaskHandle_t tcpClientTaskHandle = NULL;
|
||||
TaskHandle_t uartServerTransTaskHandle = NULL;
|
||||
TaskHandle_t uartClientTransTaskHandle = NULL;
|
||||
TaskHandle_t configTaskHandle = NULL;
|
||||
|
||||
/* SPI mutex for CH390 access */
|
||||
SemaphoreHandle_t ch390SpiMutex = NULL;
|
||||
|
||||
/* CH390 interrupt notification semaphore */
|
||||
SemaphoreHandle_t ch390IntSemaphore = NULL;
|
||||
/* USER CODE END Variables */
|
||||
|
||||
/* Definitions for defaultTask */
|
||||
osThreadId_t defaultTaskHandle;
|
||||
const osThreadAttr_t defaultTask_attributes = {
|
||||
.name = "defaultTask",
|
||||
.stack_size = 128 * 4,
|
||||
.priority = (osPriority_t) osPriorityNormal,
|
||||
.stack_size = DEFAULT_TASK_STACK_SIZE * 4,
|
||||
.priority = (osPriority_t) DEFAULT_TASK_PRIORITY,
|
||||
};
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
/* USER CODE BEGIN FunctionPrototypes */
|
||||
|
||||
void LwIPTask(void *argument);
|
||||
/* USER CODE END FunctionPrototypes */
|
||||
|
||||
void StartDefaultTask(void *argument);
|
||||
@@ -75,11 +116,15 @@ void MX_FREERTOS_Init(void) {
|
||||
/* USER CODE END Init */
|
||||
|
||||
/* USER CODE BEGIN RTOS_MUTEX */
|
||||
/* add mutexes, ... */
|
||||
/* Create SPI mutex for CH390 access */
|
||||
ch390SpiMutex = xSemaphoreCreateMutex();
|
||||
configASSERT(ch390SpiMutex != NULL);
|
||||
/* USER CODE END RTOS_MUTEX */
|
||||
|
||||
/* USER CODE BEGIN RTOS_SEMAPHORES */
|
||||
/* add semaphores, ... */
|
||||
/* Create CH390 interrupt notification semaphore */
|
||||
ch390IntSemaphore = xSemaphoreCreateBinary();
|
||||
configASSERT(ch390IntSemaphore != NULL);
|
||||
/* USER CODE END RTOS_SEMAPHORES */
|
||||
|
||||
/* USER CODE BEGIN RTOS_TIMERS */
|
||||
@@ -95,7 +140,37 @@ void MX_FREERTOS_Init(void) {
|
||||
defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);
|
||||
|
||||
/* USER CODE BEGIN RTOS_THREADS */
|
||||
/* add threads, ... */
|
||||
BaseType_t task_created;
|
||||
|
||||
/* Create LwIP task (handles network stack + TCP server/client) */
|
||||
task_created = xTaskCreate(LwIPTask, "LwIP", LWIP_TASK_STACK_SIZE, NULL,
|
||||
tskIDLE_PRIORITY + 3, &lwipTaskHandle);
|
||||
configASSERT(task_created == pdPASS);
|
||||
|
||||
/* Create TCP Server task */
|
||||
task_created = xTaskCreate(tcp_server_task, "TCPServer", TCP_SERVER_TASK_STACK_SIZE, NULL,
|
||||
tskIDLE_PRIORITY + 2, &tcpServerTaskHandle);
|
||||
configASSERT(task_created == pdPASS);
|
||||
|
||||
/* Create TCP Client task */
|
||||
task_created = xTaskCreate(tcp_client_task, "TCPClient", TCP_CLIENT_TASK_STACK_SIZE, NULL,
|
||||
tskIDLE_PRIORITY + 2, &tcpClientTaskHandle);
|
||||
configASSERT(task_created == pdPASS);
|
||||
|
||||
/* Create UART Server transparent transmission task */
|
||||
task_created = xTaskCreate(uart_server_trans_task, "UartSrvTx", UART_TRANS_TASK_STACK_SIZE, NULL,
|
||||
tskIDLE_PRIORITY + 2, &uartServerTransTaskHandle);
|
||||
configASSERT(task_created == pdPASS);
|
||||
|
||||
/* Create UART Client transparent transmission task */
|
||||
task_created = xTaskCreate(uart_client_trans_task, "UartCliTx", UART_TRANS_TASK_STACK_SIZE, NULL,
|
||||
tskIDLE_PRIORITY + 2, &uartClientTransTaskHandle);
|
||||
configASSERT(task_created == pdPASS);
|
||||
|
||||
/* Create Configuration task (AT commands via UART1) */
|
||||
task_created = xTaskCreate(config_task, "Config", CONFIG_TASK_STACK_SIZE, NULL,
|
||||
tskIDLE_PRIORITY + 1, &configTaskHandle);
|
||||
configASSERT(task_created == pdPASS);
|
||||
/* USER CODE END RTOS_THREADS */
|
||||
|
||||
/* USER CODE BEGIN RTOS_EVENTS */
|
||||
@@ -107,6 +182,7 @@ void MX_FREERTOS_Init(void) {
|
||||
/* USER CODE BEGIN Header_StartDefaultTask */
|
||||
/**
|
||||
* @brief Function implementing the defaultTask thread.
|
||||
* LED blinking for system status indication.
|
||||
* @param argument: Not used
|
||||
* @retval None
|
||||
*/
|
||||
@@ -114,10 +190,16 @@ void MX_FREERTOS_Init(void) {
|
||||
void StartDefaultTask(void *argument)
|
||||
{
|
||||
/* USER CODE BEGIN StartDefaultTask */
|
||||
/* Infinite loop */
|
||||
(void)argument;
|
||||
|
||||
/* Infinite loop - LED heartbeat */
|
||||
for(;;)
|
||||
{
|
||||
osDelay(1);
|
||||
/* Toggle LED (PC13, active low) */
|
||||
HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
|
||||
|
||||
/* 500ms toggle = 1Hz blink rate */
|
||||
osDelay(500);
|
||||
}
|
||||
/* USER CODE END StartDefaultTask */
|
||||
}
|
||||
@@ -125,5 +207,164 @@ void StartDefaultTask(void *argument)
|
||||
/* Private application code --------------------------------------------------*/
|
||||
/* USER CODE BEGIN Application */
|
||||
|
||||
/* USER CODE END Application */
|
||||
/**
|
||||
* @brief LwIP task - handles network stack initialization and processing
|
||||
*/
|
||||
void LwIPTask(void *argument)
|
||||
{
|
||||
(void)argument;
|
||||
|
||||
ip4_addr_t ipaddr, netmask, gw;
|
||||
uart_config_t uart_server_cfg;
|
||||
uart_config_t uart_client_cfg;
|
||||
uint8_t use_dhcp = 0;
|
||||
const device_config_t *cfg;
|
||||
|
||||
/* Wait for configuration to be loaded */
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
|
||||
/* Get device configuration */
|
||||
cfg = config_get();
|
||||
|
||||
/* Initialize UART transparent transmission module */
|
||||
uart_trans_init();
|
||||
|
||||
/* Apply UART settings from configuration */
|
||||
uart_server_cfg.baudrate = UART_DEFAULT_BAUDRATE;
|
||||
uart_server_cfg.data_bits = UART_DEFAULT_DATA_BITS;
|
||||
uart_server_cfg.stop_bits = UART_DEFAULT_STOP_BITS;
|
||||
uart_server_cfg.parity = UART_DEFAULT_PARITY;
|
||||
|
||||
uart_client_cfg.baudrate = UART_DEFAULT_BAUDRATE;
|
||||
uart_client_cfg.data_bits = UART_DEFAULT_DATA_BITS;
|
||||
uart_client_cfg.stop_bits = UART_DEFAULT_STOP_BITS;
|
||||
uart_client_cfg.parity = UART_DEFAULT_PARITY;
|
||||
|
||||
if (cfg != NULL)
|
||||
{
|
||||
uart_server_cfg.baudrate = cfg->uart2_baudrate;
|
||||
uart_server_cfg.data_bits = cfg->uart2_databits;
|
||||
uart_server_cfg.stop_bits = cfg->uart2_stopbits;
|
||||
uart_server_cfg.parity = cfg->uart2_parity;
|
||||
|
||||
uart_client_cfg.baudrate = cfg->uart3_baudrate;
|
||||
uart_client_cfg.data_bits = cfg->uart3_databits;
|
||||
uart_client_cfg.stop_bits = cfg->uart3_stopbits;
|
||||
uart_client_cfg.parity = cfg->uart3_parity;
|
||||
}
|
||||
|
||||
(void)uart_trans_config(UART_CHANNEL_SERVER, &uart_server_cfg);
|
||||
(void)uart_trans_config(UART_CHANNEL_CLIENT, &uart_client_cfg);
|
||||
|
||||
/* Wait for TCP tasks to initialize their StreamBuffers */
|
||||
/* TCP Server and TCP Client tasks call tcp_server_init() / tcp_client_init() */
|
||||
/* which create the StreamBuffers. We need to wait until they're ready. */
|
||||
while (tcp_server_get_rx_stream() == NULL ||
|
||||
tcp_server_get_tx_stream() == NULL ||
|
||||
tcp_client_get_rx_stream() == NULL ||
|
||||
tcp_client_get_tx_stream() == NULL)
|
||||
{
|
||||
vTaskDelay(pdMS_TO_TICKS(50));
|
||||
}
|
||||
|
||||
/* Connect StreamBuffers between TCP and UART modules */
|
||||
/* Server: TCP Server RX -> UART2 TX, UART2 RX -> TCP Server TX */
|
||||
uart_trans_set_streams(UART_CHANNEL_SERVER,
|
||||
tcp_server_get_rx_stream(), /* TCP RX -> UART TX */
|
||||
tcp_server_get_tx_stream()); /* UART RX -> TCP TX */
|
||||
|
||||
/* Client: TCP Client RX -> UART3 TX, UART3 RX -> TCP Client TX */
|
||||
uart_trans_set_streams(UART_CHANNEL_CLIENT,
|
||||
tcp_client_get_rx_stream(), /* TCP RX -> UART TX */
|
||||
tcp_client_get_tx_stream()); /* UART RX -> TCP TX */
|
||||
|
||||
/* Initialize LwIP stack (calls tcpip_init internally) */
|
||||
tcpip_init(NULL, NULL);
|
||||
|
||||
/* Wait for tcpip thread to initialize */
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
|
||||
/* Set IP addresses */
|
||||
if (cfg != NULL && cfg->dhcp_enable != 0)
|
||||
{
|
||||
use_dhcp = 1;
|
||||
IP4_ADDR(&ipaddr, 0, 0, 0, 0);
|
||||
IP4_ADDR(&netmask, 0, 0, 0, 0);
|
||||
IP4_ADDR(&gw, 0, 0, 0, 0);
|
||||
}
|
||||
else if (cfg != NULL)
|
||||
{
|
||||
IP4_ADDR(&ipaddr, cfg->ip[0], cfg->ip[1], cfg->ip[2], cfg->ip[3]);
|
||||
IP4_ADDR(&netmask, cfg->mask[0], cfg->mask[1], cfg->mask[2], cfg->mask[3]);
|
||||
IP4_ADDR(&gw, cfg->gw[0], cfg->gw[1], cfg->gw[2], cfg->gw[3]);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Default IP if no config */
|
||||
IP4_ADDR(&ipaddr, 192, 168, 1, 200);
|
||||
IP4_ADDR(&netmask, 255, 255, 255, 0);
|
||||
IP4_ADDR(&gw, 192, 168, 1, 1);
|
||||
}
|
||||
|
||||
/* Initialize network interface */
|
||||
lwip_netif_init(&ipaddr, &netmask, &gw);
|
||||
|
||||
if (use_dhcp)
|
||||
{
|
||||
dhcp_start(&ch390_netif);
|
||||
}
|
||||
|
||||
/* Main loop - handle network events */
|
||||
for (;;)
|
||||
{
|
||||
/* Wait for CH390 interrupt or timeout */
|
||||
if (xSemaphoreTake(ch390IntSemaphore, pdMS_TO_TICKS(10)) == pdTRUE)
|
||||
{
|
||||
/* Process network input - poll all pending packets */
|
||||
ethernetif_poll();
|
||||
}
|
||||
|
||||
/* Check link status periodically */
|
||||
ethernetif_check_link();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get SPI mutex handle for CH390 driver
|
||||
*/
|
||||
SemaphoreHandle_t get_ch390_spi_mutex(void)
|
||||
{
|
||||
return ch390SpiMutex;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Notify LwIP task of CH390 interrupt (call from ISR)
|
||||
*/
|
||||
void notify_ch390_interrupt_from_isr(void)
|
||||
{
|
||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
|
||||
if (ch390IntSemaphore != NULL)
|
||||
{
|
||||
xSemaphoreGiveFromISR(ch390IntSemaphore, &xHigherPriorityTaskWoken);
|
||||
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get UART Server transparent task handle
|
||||
*/
|
||||
TaskHandle_t get_uart_server_task_handle(void)
|
||||
{
|
||||
return uartServerTransTaskHandle;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get UART Client transparent task handle
|
||||
*/
|
||||
TaskHandle_t get_uart_client_task_handle(void)
|
||||
{
|
||||
return uartClientTransTaskHandle;
|
||||
}
|
||||
|
||||
/* USER CODE END Application */
|
||||
|
||||
+77
-2
@@ -27,7 +27,12 @@
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include <stdio.h>
|
||||
|
||||
#include "CH390.h"
|
||||
#include "config.h"
|
||||
#include "flash_param.h"
|
||||
#include "uart_trans.h"
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
@@ -37,7 +42,15 @@
|
||||
|
||||
/* Private define ------------------------------------------------------------*/
|
||||
/* USER CODE BEGIN PD */
|
||||
/* CH390 硬件控制引脚 */
|
||||
#define CH390_RST_PIN GPIO_PIN_1
|
||||
#define CH390_RST_PORT GPIOB
|
||||
#define CH390_CS_PIN GPIO_PIN_4
|
||||
#define CH390_CS_PORT GPIOA
|
||||
|
||||
/* LED 指示灯 */
|
||||
#define LED_PIN GPIO_PIN_13
|
||||
#define LED_PORT GPIOC
|
||||
/* USER CODE END PD */
|
||||
|
||||
/* Private macro -------------------------------------------------------------*/
|
||||
@@ -55,12 +68,48 @@
|
||||
void SystemClock_Config(void);
|
||||
void MX_FREERTOS_Init(void);
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
static void CH390_HardwareReset(void);
|
||||
static void LED_Init(void);
|
||||
/* USER CODE END PFP */
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN 0 */
|
||||
|
||||
/**
|
||||
* @brief CH390 硬件复位
|
||||
* @note 复位时序: RST 低电平至少 10us,然后高电平等待 50ms 完成初始化
|
||||
*/
|
||||
static void CH390_HardwareReset(void)
|
||||
{
|
||||
/* 拉低 RST 引脚 */
|
||||
HAL_GPIO_WritePin(CH390_RST_PORT, CH390_RST_PIN, GPIO_PIN_RESET);
|
||||
HAL_Delay(1); /* 保持低电平 1ms (远超最小 10us 要求) */
|
||||
|
||||
/* 拉高 RST 引脚,等待芯片初始化完成 */
|
||||
HAL_GPIO_WritePin(CH390_RST_PORT, CH390_RST_PIN, GPIO_PIN_SET);
|
||||
HAL_Delay(50); /* 等待 50ms */
|
||||
|
||||
/* 确保 CS 为高电平(未选中状态) */
|
||||
HAL_GPIO_WritePin(CH390_CS_PORT, CH390_CS_PIN, GPIO_PIN_SET);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief LED 初始化(点亮表示系统启动)
|
||||
*/
|
||||
static void LED_Init(void)
|
||||
{
|
||||
/* LED 灭(PC13 高电平灭,低电平亮) */
|
||||
HAL_GPIO_WritePin(LED_PORT, LED_PIN, GPIO_PIN_SET);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief LED 闪烁(用于指示系统运行状态)
|
||||
*/
|
||||
void LED_Toggle(void)
|
||||
{
|
||||
HAL_GPIO_TogglePin(LED_PORT, LED_PIN);
|
||||
}
|
||||
|
||||
/* USER CODE END 0 */
|
||||
|
||||
/**
|
||||
@@ -99,7 +148,16 @@ int main(void)
|
||||
MX_USART3_UART_Init();
|
||||
MX_SPI1_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
|
||||
/* LED 初始化 */
|
||||
LED_Init();
|
||||
|
||||
/* CH390 硬件复位 */
|
||||
CH390_HardwareReset();
|
||||
|
||||
/* Initialize configuration from Flash (fallback to defaults on invalid data) */
|
||||
config_init();
|
||||
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* Init scheduler */
|
||||
@@ -164,6 +222,23 @@ void SystemClock_Config(void)
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
|
||||
/**
|
||||
* @brief 重定向 printf 到 UART1(调试输出)
|
||||
*/
|
||||
#ifdef __GNUC__
|
||||
int _write(int file, char *ptr, int len)
|
||||
{
|
||||
HAL_UART_Transmit(&huart1, (uint8_t *)ptr, len, HAL_MAX_DELAY);
|
||||
return len;
|
||||
}
|
||||
#else
|
||||
int fputc(int ch, FILE *f)
|
||||
{
|
||||
HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
|
||||
return ch;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* USER CODE END 4 */
|
||||
|
||||
/**
|
||||
|
||||
+12
-8
@@ -41,10 +41,10 @@ void MX_SPI1_Init(void)
|
||||
hspi1.Init.Mode = SPI_MODE_MASTER;
|
||||
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
|
||||
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
|
||||
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
|
||||
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
|
||||
hspi1.Init.NSS = SPI_NSS_HARD_OUTPUT;
|
||||
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
|
||||
hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH; /* CH390 requires CPOL=High (Mode 3) */
|
||||
hspi1.Init.CLKPhase = SPI_PHASE_2EDGE; /* CH390 requires CPHA=2Edge (Mode 3) */
|
||||
hspi1.Init.NSS = SPI_NSS_SOFT; /* Software CS control for CH390 */
|
||||
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; /* 72MHz/8 = 9MHz (CH390 max 10MHz) */
|
||||
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
|
||||
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
|
||||
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
|
||||
@@ -73,20 +73,24 @@ void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle)
|
||||
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
/**SPI1 GPIO Configuration
|
||||
PA4 ------> SPI1_NSS
|
||||
PA4 ------> CH390 CS (GPIO Output, controlled by CH390_Interface.c)
|
||||
PA5 ------> SPI1_SCK
|
||||
PA6 ------> SPI1_MISO
|
||||
PA7 ------> SPI1_MOSI
|
||||
*/
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_7;
|
||||
/* SCK and MOSI as AF Push-Pull */
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_7;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
/* MISO as Input */
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_6;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
|
||||
/* PA4 (CS) is configured as GPIO output in CH390_Interface.c */
|
||||
|
||||
/* SPI1 interrupt Init */
|
||||
HAL_NVIC_SetPriority(SPI1_IRQn, 5, 0);
|
||||
@@ -109,12 +113,12 @@ void HAL_SPI_MspDeInit(SPI_HandleTypeDef* spiHandle)
|
||||
__HAL_RCC_SPI1_CLK_DISABLE();
|
||||
|
||||
/**SPI1 GPIO Configuration
|
||||
PA4 ------> SPI1_NSS
|
||||
PA4 ------> CH390 CS (handled by CH390_Interface.c)
|
||||
PA5 ------> SPI1_SCK
|
||||
PA6 ------> SPI1_MISO
|
||||
PA7 ------> SPI1_MOSI
|
||||
*/
|
||||
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7);
|
||||
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7);
|
||||
|
||||
/* SPI1 interrupt Deinit */
|
||||
HAL_NVIC_DisableIRQ(SPI1_IRQn);
|
||||
|
||||
+81
-3
@@ -24,6 +24,11 @@
|
||||
#include "task.h"
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "uart_trans.h"
|
||||
#include "config.h"
|
||||
|
||||
/* External functions from freertos.c */
|
||||
extern void notify_ch390_interrupt_from_isr(void);
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
@@ -295,7 +300,12 @@ void SPI1_IRQHandler(void)
|
||||
void USART1_IRQHandler(void)
|
||||
{
|
||||
/* USER CODE BEGIN USART1_IRQn 0 */
|
||||
|
||||
/* Handle IDLE interrupt for configuration */
|
||||
if (__HAL_UART_GET_FLAG(&huart1, UART_FLAG_IDLE))
|
||||
{
|
||||
__HAL_UART_CLEAR_IDLEFLAG(&huart1);
|
||||
config_uart_idle_handler();
|
||||
}
|
||||
/* USER CODE END USART1_IRQn 0 */
|
||||
HAL_UART_IRQHandler(&huart1);
|
||||
/* USER CODE BEGIN USART1_IRQn 1 */
|
||||
@@ -309,7 +319,12 @@ void USART1_IRQHandler(void)
|
||||
void USART2_IRQHandler(void)
|
||||
{
|
||||
/* USER CODE BEGIN USART2_IRQn 0 */
|
||||
|
||||
/* Handle IDLE interrupt for Server transparent transmission */
|
||||
if (__HAL_UART_GET_FLAG(&huart2, UART_FLAG_IDLE))
|
||||
{
|
||||
__HAL_UART_CLEAR_IDLEFLAG(&huart2);
|
||||
uart_trans_idle_handler(UART_CHANNEL_SERVER);
|
||||
}
|
||||
/* USER CODE END USART2_IRQn 0 */
|
||||
HAL_UART_IRQHandler(&huart2);
|
||||
/* USER CODE BEGIN USART2_IRQn 1 */
|
||||
@@ -323,7 +338,12 @@ void USART2_IRQHandler(void)
|
||||
void USART3_IRQHandler(void)
|
||||
{
|
||||
/* USER CODE BEGIN USART3_IRQn 0 */
|
||||
|
||||
/* Handle IDLE interrupt for Client transparent transmission */
|
||||
if (__HAL_UART_GET_FLAG(&huart3, UART_FLAG_IDLE))
|
||||
{
|
||||
__HAL_UART_CLEAR_IDLEFLAG(&huart3);
|
||||
uart_trans_idle_handler(UART_CHANNEL_CLIENT);
|
||||
}
|
||||
/* USER CODE END USART3_IRQn 0 */
|
||||
HAL_UART_IRQHandler(&huart3);
|
||||
/* USER CODE BEGIN USART3_IRQn 1 */
|
||||
@@ -332,5 +352,63 @@ void USART3_IRQHandler(void)
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 1 */
|
||||
/**
|
||||
* @brief This function handles EXTI0 interrupt (CH390D INT pin).
|
||||
*/
|
||||
void EXTI0_IRQHandler(void)
|
||||
{
|
||||
/* Clear interrupt flag */
|
||||
if (__HAL_GPIO_EXTI_GET_IT(GPIO_PIN_0))
|
||||
{
|
||||
__HAL_GPIO_EXTI_CLEAR_IT(GPIO_PIN_0);
|
||||
|
||||
/* Notify LwIP task */
|
||||
notify_ch390_interrupt_from_isr();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief HAL UART TX Complete callback
|
||||
*/
|
||||
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
|
||||
{
|
||||
if (huart == &huart2)
|
||||
{
|
||||
uart_trans_tx_cplt_handler(UART_CHANNEL_SERVER);
|
||||
}
|
||||
else if (huart == &huart3)
|
||||
{
|
||||
uart_trans_tx_cplt_handler(UART_CHANNEL_CLIENT);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief HAL UART RX Complete callback
|
||||
*/
|
||||
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
|
||||
{
|
||||
if (huart == &huart2)
|
||||
{
|
||||
uart_trans_rx_cplt_handler(UART_CHANNEL_SERVER);
|
||||
}
|
||||
else if (huart == &huart3)
|
||||
{
|
||||
uart_trans_rx_cplt_handler(UART_CHANNEL_CLIENT);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief HAL UART RX Half Complete callback
|
||||
*/
|
||||
void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart)
|
||||
{
|
||||
if (huart == &huart2)
|
||||
{
|
||||
uart_trans_rx_half_cplt_handler(UART_CHANNEL_SERVER);
|
||||
}
|
||||
else if (huart == &huart3)
|
||||
{
|
||||
uart_trans_rx_half_cplt_handler(UART_CHANNEL_CLIENT);
|
||||
}
|
||||
}
|
||||
/* USER CODE END 1 */
|
||||
|
||||
+2
-2
@@ -220,7 +220,7 @@ void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
|
||||
hdma_usart2_rx.Init.MemInc = DMA_MINC_ENABLE;
|
||||
hdma_usart2_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
|
||||
hdma_usart2_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
|
||||
hdma_usart2_rx.Init.Mode = DMA_NORMAL;
|
||||
hdma_usart2_rx.Init.Mode = DMA_CIRCULAR;
|
||||
hdma_usart2_rx.Init.Priority = DMA_PRIORITY_LOW;
|
||||
if (HAL_DMA_Init(&hdma_usart2_rx) != HAL_OK)
|
||||
{
|
||||
@@ -283,7 +283,7 @@ void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
|
||||
hdma_usart3_rx.Init.MemInc = DMA_MINC_ENABLE;
|
||||
hdma_usart3_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
|
||||
hdma_usart3_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
|
||||
hdma_usart3_rx.Init.Mode = DMA_NORMAL;
|
||||
hdma_usart3_rx.Init.Mode = DMA_CIRCULAR;
|
||||
hdma_usart3_rx.Init.Priority = DMA_PRIORITY_LOW;
|
||||
if (HAL_DMA_Init(&hdma_usart3_rx) != HAL_OK)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user