437 lines
12 KiB
C
437 lines
12 KiB
C
/* USER CODE BEGIN Header */
|
|
/**
|
|
******************************************************************************
|
|
* @file : main.c
|
|
* @brief : Main program body
|
|
******************************************************************************
|
|
* @attention
|
|
*
|
|
* Copyright (c) 2026 STMicroelectronics.
|
|
* All rights reserved.
|
|
*
|
|
* This software is licensed under terms that can be found in the LICENSE file
|
|
* in the root directory of this software component.
|
|
* If no LICENSE file comes with this software, it is provided AS-IS.
|
|
*
|
|
******************************************************************************
|
|
*/
|
|
/* USER CODE END Header */
|
|
/* Includes ------------------------------------------------------------------*/
|
|
#include "main.h"
|
|
#include "dma.h"
|
|
#include "iwdg.h"
|
|
#include "spi.h"
|
|
#include "tim.h"
|
|
#include "usart.h"
|
|
#include "gpio.h"
|
|
|
|
/* Private includes ----------------------------------------------------------*/
|
|
/* USER CODE BEGIN Includes */
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#include "CH390.h"
|
|
#include "CH390_Interface.h"
|
|
#include "SEGGER_RTT.h"
|
|
#include "config.h"
|
|
#include "flash_param.h"
|
|
#include "ethernetif.h"
|
|
#include "ch390_runtime.h"
|
|
#include "lwip/init.h"
|
|
#include "lwip/timeouts.h"
|
|
#include "tcp_client.h"
|
|
#include "tcp_server.h"
|
|
#include "uart_trans.h"
|
|
/* USER CODE END Includes */
|
|
|
|
/* Private typedef -----------------------------------------------------------*/
|
|
/* USER CODE BEGIN PTD */
|
|
|
|
/* USER CODE END PTD */
|
|
|
|
/* Private define ------------------------------------------------------------*/
|
|
/* USER CODE BEGIN PD */
|
|
/* LED 指示灯 */
|
|
#define LED_PIN GPIO_PIN_13
|
|
#define LED_PORT GPIOC
|
|
/* USER CODE END PD */
|
|
|
|
/* Private macro -------------------------------------------------------------*/
|
|
/* USER CODE BEGIN PM */
|
|
|
|
/* USER CODE END PM */
|
|
|
|
/* Private variables ---------------------------------------------------------*/
|
|
|
|
/* USER CODE BEGIN PV */
|
|
static volatile uint16_t g_led_blink_ticks = 0;
|
|
static uint8_t g_clock_fallback_to_hsi = 0u;
|
|
volatile uint8_t g_uart1_rx_probe_byte = 0u;
|
|
|
|
/* USER CODE END PV */
|
|
|
|
/* Private function prototypes -----------------------------------------------*/
|
|
void SystemClock_Config(void);
|
|
/* USER CODE BEGIN PFP */
|
|
static void LED_Init(void);
|
|
static void LED_StartBlink(void);
|
|
static void BootDiag_ReportCh390(void);
|
|
static void App_PollUart1ConfigRx(void);
|
|
static void App_Init(void);
|
|
static void App_Poll(void);
|
|
/* USER CODE END PFP */
|
|
|
|
/* Private user code ---------------------------------------------------------*/
|
|
/* USER CODE BEGIN 0 */
|
|
|
|
/**
|
|
* @brief LED 初始化(点亮表示系统启动)
|
|
*/
|
|
static void LED_Init(void)
|
|
{
|
|
/* LED 灭(PC13 高电平灭,低电平亮) */
|
|
HAL_GPIO_WritePin(LED_PORT, LED_PIN, GPIO_PIN_SET);
|
|
}
|
|
|
|
static void LED_StartBlink(void)
|
|
{
|
|
if (HAL_TIM_Base_Start_IT(&htim4) != HAL_OK) {
|
|
Error_Handler();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief LED 闪烁(用于指示系统运行状态)
|
|
*/
|
|
void LED_Toggle(void)
|
|
{
|
|
HAL_GPIO_TogglePin(LED_PORT, LED_PIN);
|
|
}
|
|
|
|
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
|
|
{
|
|
if (htim->Instance == TIM4) {
|
|
g_led_blink_ticks++;
|
|
if (g_led_blink_ticks >= 1000u) {
|
|
g_led_blink_ticks = 0u;
|
|
LED_Toggle();
|
|
}
|
|
}
|
|
}
|
|
|
|
static void BootDiag_ReportCh390(void)
|
|
{
|
|
ch390_diag_t diag;
|
|
|
|
ch390_runtime_get_diag(&diag);
|
|
|
|
SEGGER_RTT_printf(0,
|
|
"CH390 VID=0x%04X PID=0x%04X REV=0x%02X NSR=0x%02X LINK=%d\r\n",
|
|
diag.vendor_id,
|
|
diag.product_id,
|
|
diag.revision,
|
|
diag.nsr,
|
|
diag.link_up);
|
|
SEGGER_RTT_printf(0,
|
|
"CH390 NCR=0x%02X RCR=0x%02X IMR=0x%02X INTCR=0x%02X GPR=0x%02X ISR=0x%02X\r\n",
|
|
diag.ncr,
|
|
diag.rcr,
|
|
diag.imr,
|
|
diag.intcr,
|
|
diag.gpr,
|
|
diag.isr);
|
|
}
|
|
|
|
static void App_PollUart1ConfigRx(void)
|
|
{
|
|
while (__HAL_UART_GET_FLAG(&huart1, UART_FLAG_RXNE) != RESET) {
|
|
uint8_t byte = (uint8_t)(huart1.Instance->DR & 0xFFu);
|
|
config_uart_rx_byte(byte);
|
|
}
|
|
}
|
|
|
|
static void App_Init(void)
|
|
{
|
|
const device_config_t *cfg;
|
|
ip4_addr_t ipaddr;
|
|
ip4_addr_t netmask;
|
|
ip4_addr_t gateway;
|
|
uart_config_t uart_cfg;
|
|
tcp_server_config_t server_cfg;
|
|
tcp_client_config_t client_cfg;
|
|
|
|
config_init();
|
|
cfg = config_get();
|
|
|
|
uart_trans_init();
|
|
|
|
uart_cfg.baudrate = cfg->uart2_baudrate;
|
|
uart_cfg.data_bits = cfg->uart2_databits;
|
|
uart_cfg.stop_bits = cfg->uart2_stopbits;
|
|
uart_cfg.parity = cfg->uart2_parity;
|
|
uart_trans_config(UART_CHANNEL_SERVER, &uart_cfg);
|
|
|
|
uart_cfg.baudrate = cfg->uart3_baudrate;
|
|
uart_cfg.data_bits = cfg->uart3_databits;
|
|
uart_cfg.stop_bits = cfg->uart3_stopbits;
|
|
uart_cfg.parity = cfg->uart3_parity;
|
|
uart_trans_config(UART_CHANNEL_CLIENT, &uart_cfg);
|
|
|
|
uart_trans_start(UART_CHANNEL_SERVER);
|
|
uart_trans_start(UART_CHANNEL_CLIENT);
|
|
|
|
SEGGER_RTT_Init();
|
|
SEGGER_RTT_WriteString(0, "\r\nTCP2UART boot\r\n");
|
|
if (g_clock_fallback_to_hsi != 0u) {
|
|
SEGGER_RTT_WriteString(0, "WARN: HSE start failed, fallback to HSI PLL\r\n");
|
|
}
|
|
|
|
lwip_init();
|
|
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(&gateway, cfg->gw[0], cfg->gw[1], cfg->gw[2], cfg->gw[3]);
|
|
lwip_netif_init(&ipaddr, &netmask, &gateway);
|
|
BootDiag_ReportCh390();
|
|
|
|
server_cfg.port = cfg->server_port;
|
|
server_cfg.auto_reconnect = true;
|
|
tcp_server_init(&server_cfg);
|
|
tcp_server_start();
|
|
|
|
memcpy(client_cfg.server_ip, cfg->remote_ip, sizeof(client_cfg.server_ip));
|
|
client_cfg.server_port = cfg->remote_port;
|
|
client_cfg.auto_reconnect = true;
|
|
client_cfg.reconnect_interval_ms = cfg->reconnect_interval;
|
|
tcp_client_init(&client_cfg);
|
|
tcp_client_connect();
|
|
|
|
/* Arm UART1 RX interrupt path so config commands can enter via USART1. */
|
|
if (HAL_UART_Receive_IT(&huart1, (uint8_t *)&g_uart1_rx_probe_byte, 1u) != HAL_OK) {
|
|
Error_Handler();
|
|
}
|
|
}
|
|
|
|
static void App_Poll(void)
|
|
{
|
|
uint8_t buffer[128];
|
|
int len;
|
|
|
|
ethernetif_poll();
|
|
ethernetif_check_link();
|
|
sys_check_timeouts();
|
|
tcp_client_poll();
|
|
uart_trans_poll();
|
|
App_PollUart1ConfigRx();
|
|
config_poll();
|
|
|
|
len = tcp_server_recv(buffer, sizeof(buffer), 0u);
|
|
if (len > 0) {
|
|
uart_trans_write(UART_CHANNEL_SERVER, buffer, (uint16_t)len);
|
|
}
|
|
|
|
len = tcp_client_recv(buffer, sizeof(buffer), 0u);
|
|
if (len > 0) {
|
|
uart_trans_write(UART_CHANNEL_CLIENT, buffer, (uint16_t)len);
|
|
}
|
|
|
|
len = (int)uart_trans_read(UART_CHANNEL_SERVER, buffer, sizeof(buffer));
|
|
if (len > 0) {
|
|
tcp_server_send(buffer, (uint16_t)len);
|
|
}
|
|
|
|
len = (int)uart_trans_read(UART_CHANNEL_CLIENT, buffer, sizeof(buffer));
|
|
if (len > 0) {
|
|
tcp_client_send(buffer, (uint16_t)len);
|
|
}
|
|
|
|
if (config_is_reset_requested()) {
|
|
config_clear_reset_requested();
|
|
NVIC_SystemReset();
|
|
}
|
|
|
|
if (hiwdg.Instance == IWDG) {
|
|
HAL_IWDG_Refresh(&hiwdg);
|
|
}
|
|
}
|
|
|
|
/* USER CODE END 0 */
|
|
|
|
/**
|
|
* @brief The application entry point.
|
|
* @retval int
|
|
*/
|
|
int main(void)
|
|
{
|
|
|
|
/* USER CODE BEGIN 1 */
|
|
|
|
/* USER CODE END 1 */
|
|
|
|
/* MCU Configuration--------------------------------------------------------*/
|
|
|
|
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
|
|
HAL_Init();
|
|
|
|
/* USER CODE BEGIN Init */
|
|
|
|
/* USER CODE END Init */
|
|
|
|
/* Configure the system clock */
|
|
SystemClock_Config();
|
|
|
|
/* USER CODE BEGIN SysInit */
|
|
|
|
/* USER CODE END SysInit */
|
|
|
|
/* Initialize all configured peripherals */
|
|
MX_GPIO_Init();
|
|
MX_DMA_Init();
|
|
// MX_IWDG_Init();
|
|
MX_USART1_UART_Init();
|
|
MX_USART2_UART_Init();
|
|
MX_USART3_UART_Init();
|
|
MX_SPI1_Init();
|
|
MX_TIM4_Init();
|
|
/* USER CODE BEGIN 2 */
|
|
|
|
/* LED 初始化 */
|
|
LED_Init();
|
|
LED_StartBlink();
|
|
|
|
App_Init();
|
|
|
|
/* USER CODE END 2 */
|
|
|
|
/* Infinite loop */
|
|
/* USER CODE BEGIN WHILE */
|
|
while (1)
|
|
{
|
|
/* USER CODE END WHILE */
|
|
|
|
/* USER CODE BEGIN 3 */
|
|
App_Poll();
|
|
}
|
|
/* USER CODE END 3 */
|
|
}
|
|
|
|
/**
|
|
* @brief System Clock Configuration
|
|
* @retval None
|
|
*/
|
|
void SystemClock_Config(void)
|
|
{
|
|
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
|
|
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
|
|
|
|
g_clock_fallback_to_hsi = 0u;
|
|
|
|
/** Initializes the RCC Oscillators according to the specified parameters
|
|
* in the RCC_OscInitTypeDef structure.
|
|
*/
|
|
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE;
|
|
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
|
|
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
|
|
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
|
|
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
|
|
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
|
|
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
|
|
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
|
|
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
|
|
/*
|
|
* Some bring-up boards fail to start the external crystal cleanly.
|
|
* Fall back to HSI-based PLL so the firmware can still boot and expose
|
|
* RTT / debugger evidence instead of trapping during clock init.
|
|
*/
|
|
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_HSI;
|
|
RCC_OscInitStruct.HSEState = RCC_HSE_OFF;
|
|
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
|
|
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
|
|
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
|
|
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
|
|
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
|
|
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
|
|
Error_Handler();
|
|
}
|
|
g_clock_fallback_to_hsi = 1u;
|
|
}
|
|
|
|
/** Initializes the CPU, AHB and APB buses clocks
|
|
*/
|
|
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|
|
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
|
|
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
|
|
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
|
|
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
|
|
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
|
|
|
|
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
}
|
|
|
|
/* USER CODE BEGIN 4 */
|
|
|
|
/**
|
|
* @brief 重定向 printf 到 UART1(调试输出)
|
|
*/
|
|
#ifdef __GNUC__
|
|
int _write(int file, char *ptr, int len)
|
|
{
|
|
(void)file;
|
|
return (int)SEGGER_RTT_Write(0, ptr, (unsigned)len);
|
|
}
|
|
#else
|
|
int fputc(int ch, FILE *f)
|
|
{
|
|
(void)f;
|
|
SEGGER_RTT_PutChar(0, (char)ch);
|
|
return ch;
|
|
}
|
|
#endif
|
|
|
|
void Debug_TrapWithRttHint(const char *tag)
|
|
{
|
|
SEGGER_RTT_WriteString(0, "\r\nTRAP: ");
|
|
SEGGER_RTT_WriteString(0, tag);
|
|
SEGGER_RTT_WriteString(0, "\r\n");
|
|
__disable_irq();
|
|
__BKPT(0);
|
|
while (1)
|
|
{
|
|
}
|
|
}
|
|
|
|
/* USER CODE END 4 */
|
|
|
|
/**
|
|
* @brief This function is executed in case of error occurrence.
|
|
* @retval None
|
|
*/
|
|
void Error_Handler(void)
|
|
{
|
|
/* USER CODE BEGIN Error_Handler_Debug */
|
|
/* User can add his own implementation to report the HAL error return state */
|
|
Debug_TrapWithRttHint("Error_Handler");
|
|
/* USER CODE END Error_Handler_Debug */
|
|
}
|
|
#ifdef USE_FULL_ASSERT
|
|
/**
|
|
* @brief Reports the name of the source file and the source line number
|
|
* where the assert_param error has occurred.
|
|
* @param file: pointer to the source file name
|
|
* @param line: assert_param error line source number
|
|
* @retval None
|
|
*/
|
|
void assert_failed(uint8_t *file, uint32_t line)
|
|
{
|
|
/* USER CODE BEGIN 6 */
|
|
(void)file;
|
|
(void)line;
|
|
Debug_TrapWithRttHint("assert_failed");
|
|
/* User can add his own implementation to report the file name and line number,
|
|
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
|
|
/* USER CODE END 6 */
|
|
}
|
|
#endif /* USE_FULL_ASSERT */
|