refactor: 清理UART调试代码并保留RTT诊断
This commit is contained in:
+121
-13
@@ -31,6 +31,7 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "CH390.h"
|
||||
#include "CH390_Interface.h"
|
||||
#include "SEGGER_RTT.h"
|
||||
#include "config.h"
|
||||
#include "flash_param.h"
|
||||
@@ -69,6 +70,8 @@
|
||||
|
||||
/* 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 */
|
||||
|
||||
@@ -78,6 +81,8 @@ void SystemClock_Config(void);
|
||||
static void CH390_HardwareReset(void);
|
||||
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 */
|
||||
@@ -138,6 +143,77 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
|
||||
}
|
||||
}
|
||||
|
||||
static void BootDiag_ReportCh390(void)
|
||||
{
|
||||
uint16_t vendor_id;
|
||||
uint16_t product_id;
|
||||
uint8_t revision;
|
||||
uint8_t nsr;
|
||||
uint8_t ncr;
|
||||
uint8_t rcr;
|
||||
uint8_t imr;
|
||||
uint8_t intcr;
|
||||
uint8_t gpr;
|
||||
uint8_t isr;
|
||||
uint8_t ncr_before;
|
||||
uint8_t ncr_after;
|
||||
uint8_t intcr_before;
|
||||
uint8_t intcr_after;
|
||||
int link_status;
|
||||
|
||||
vendor_id = ch390_get_vendor_id();
|
||||
product_id = ch390_get_product_id();
|
||||
revision = ch390_get_revision();
|
||||
nsr = ch390_read_reg(CH390_NSR);
|
||||
ncr = ch390_read_reg(CH390_NCR);
|
||||
rcr = ch390_read_reg(CH390_RCR);
|
||||
imr = ch390_read_reg(CH390_IMR);
|
||||
intcr = ch390_read_reg(CH390_INTCR);
|
||||
gpr = ch390_read_reg(CH390_GPR);
|
||||
isr = ch390_read_reg(CH390_ISR);
|
||||
link_status = ch390_get_link_status();
|
||||
|
||||
ncr_before = ncr;
|
||||
ch390_write_reg(CH390_NCR, (uint8_t)(ncr_before ^ NCR_FDX));
|
||||
ncr_after = ch390_read_reg(CH390_NCR);
|
||||
ch390_write_reg(CH390_NCR, ncr_before);
|
||||
|
||||
intcr_before = intcr;
|
||||
ch390_write_reg(CH390_INTCR, (uint8_t)(INCR_TYPE_OD | INCR_POL_L));
|
||||
intcr_after = ch390_read_reg(CH390_INTCR);
|
||||
ch390_write_reg(CH390_INTCR, intcr_before);
|
||||
|
||||
SEGGER_RTT_printf(0,
|
||||
"CH390 VID=0x%04X PID=0x%04X REV=0x%02X NSR=0x%02X LINK=%d\r\n",
|
||||
vendor_id,
|
||||
product_id,
|
||||
revision,
|
||||
nsr,
|
||||
link_status);
|
||||
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",
|
||||
ncr,
|
||||
rcr,
|
||||
imr,
|
||||
intcr,
|
||||
gpr,
|
||||
isr);
|
||||
SEGGER_RTT_printf(0,
|
||||
"CH390 WRCHK NCR:0x%02X->0x%02X INTCR:0x%02X->0x%02X\r\n",
|
||||
ncr_before,
|
||||
ncr_after,
|
||||
intcr_before,
|
||||
intcr_after);
|
||||
}
|
||||
|
||||
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;
|
||||
@@ -167,16 +243,19 @@ static void App_Init(void)
|
||||
|
||||
uart_trans_start(UART_CHANNEL_SERVER);
|
||||
uart_trans_start(UART_CHANNEL_CLIENT);
|
||||
config_start_reception();
|
||||
|
||||
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;
|
||||
@@ -189,6 +268,11 @@ static void App_Init(void)
|
||||
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)
|
||||
@@ -201,6 +285,7 @@ static void App_Poll(void)
|
||||
sys_check_timeouts();
|
||||
tcp_client_poll();
|
||||
uart_trans_poll();
|
||||
App_PollUart1ConfigRx();
|
||||
config_poll();
|
||||
|
||||
len = tcp_server_recv(buffer, sizeof(buffer), 0u);
|
||||
@@ -275,9 +360,6 @@ int main(void)
|
||||
LED_Init();
|
||||
LED_StartBlink();
|
||||
|
||||
/* CH390 硬件复位 */
|
||||
CH390_HardwareReset();
|
||||
|
||||
App_Init();
|
||||
|
||||
/* USER CODE END 2 */
|
||||
@@ -303,6 +385,8 @@ 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.
|
||||
*/
|
||||
@@ -314,9 +398,23 @@ void SystemClock_Config(void)
|
||||
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)
|
||||
{
|
||||
Error_Handler();
|
||||
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
|
||||
@@ -354,6 +452,18 @@ int fputc(int ch, FILE *f)
|
||||
}
|
||||
#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 */
|
||||
|
||||
/**
|
||||
@@ -364,12 +474,7 @@ void Error_Handler(void)
|
||||
{
|
||||
/* USER CODE BEGIN Error_Handler_Debug */
|
||||
/* User can add his own implementation to report the HAL error return state */
|
||||
SEGGER_RTT_WriteString(0, "\r\nError_Handler hit\r\n");
|
||||
__disable_irq();
|
||||
__BKPT(0);
|
||||
while (1)
|
||||
{
|
||||
}
|
||||
Debug_TrapWithRttHint("Error_Handler");
|
||||
/* USER CODE END Error_Handler_Debug */
|
||||
}
|
||||
#ifdef USE_FULL_ASSERT
|
||||
@@ -383,6 +488,9 @@ void Error_Handler(void)
|
||||
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 */
|
||||
|
||||
+16
-32
@@ -23,6 +23,7 @@
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "ethernetif.h"
|
||||
#include "SEGGER_RTT.h"
|
||||
#include "uart_trans.h"
|
||||
#include "config.h"
|
||||
/* USER CODE END Includes */
|
||||
@@ -85,9 +86,7 @@ void NMI_Handler(void)
|
||||
|
||||
/* USER CODE END NonMaskableInt_IRQn 0 */
|
||||
/* USER CODE BEGIN NonMaskableInt_IRQn 1 */
|
||||
while (1)
|
||||
{
|
||||
}
|
||||
Debug_TrapWithRttHint("NMI_Handler");
|
||||
/* USER CODE END NonMaskableInt_IRQn 1 */
|
||||
}
|
||||
|
||||
@@ -99,11 +98,7 @@ void HardFault_Handler(void)
|
||||
/* USER CODE BEGIN HardFault_IRQn 0 */
|
||||
|
||||
/* USER CODE END HardFault_IRQn 0 */
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE BEGIN W1_HardFault_IRQn 0 */
|
||||
/* USER CODE END W1_HardFault_IRQn 0 */
|
||||
}
|
||||
Debug_TrapWithRttHint("HardFault_Handler");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -114,11 +109,7 @@ void MemManage_Handler(void)
|
||||
/* USER CODE BEGIN MemoryManagement_IRQn 0 */
|
||||
|
||||
/* USER CODE END MemoryManagement_IRQn 0 */
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */
|
||||
/* USER CODE END W1_MemoryManagement_IRQn 0 */
|
||||
}
|
||||
Debug_TrapWithRttHint("MemManage_Handler");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -129,11 +120,7 @@ void BusFault_Handler(void)
|
||||
/* USER CODE BEGIN BusFault_IRQn 0 */
|
||||
|
||||
/* USER CODE END BusFault_IRQn 0 */
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE BEGIN W1_BusFault_IRQn 0 */
|
||||
/* USER CODE END W1_BusFault_IRQn 0 */
|
||||
}
|
||||
Debug_TrapWithRttHint("BusFault_Handler");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -144,11 +131,7 @@ void UsageFault_Handler(void)
|
||||
/* USER CODE BEGIN UsageFault_IRQn 0 */
|
||||
|
||||
/* USER CODE END UsageFault_IRQn 0 */
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE BEGIN W1_UsageFault_IRQn 0 */
|
||||
/* USER CODE END W1_UsageFault_IRQn 0 */
|
||||
}
|
||||
Debug_TrapWithRttHint("UsageFault_Handler");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -303,12 +286,6 @@ 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 */
|
||||
@@ -355,9 +332,11 @@ void USART3_IRQHandler(void)
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 1 */
|
||||
extern volatile uint8_t g_uart1_rx_probe_byte;
|
||||
|
||||
/**
|
||||
* @brief This function handles EXTI0 interrupt (CH390D INT pin).
|
||||
*/
|
||||
* @brief This function handles EXTI0 interrupt (CH390D INT pin).
|
||||
*/
|
||||
void EXTI0_IRQHandler(void)
|
||||
{
|
||||
/* Clear interrupt flag */
|
||||
@@ -390,7 +369,12 @@ void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
|
||||
*/
|
||||
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
|
||||
{
|
||||
if (huart == &huart2)
|
||||
if (huart == &huart1)
|
||||
{
|
||||
config_uart_rx_byte(g_uart1_rx_probe_byte);
|
||||
HAL_UART_Receive_IT(&huart1, (uint8_t *)&g_uart1_rx_probe_byte, 1u);
|
||||
}
|
||||
else if (huart == &huart2)
|
||||
{
|
||||
uart_trans_rx_cplt_handler(UART_CHANNEL_SERVER);
|
||||
}
|
||||
|
||||
+2
-1
@@ -21,6 +21,7 @@
|
||||
*/
|
||||
|
||||
/* Includes */
|
||||
#include "main.h"
|
||||
#include <sys/stat.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
@@ -61,7 +62,7 @@ int _kill(int pid, int sig)
|
||||
void _exit (int status)
|
||||
{
|
||||
_kill(status, -1);
|
||||
while (1) {} /* Make sure we hang here */
|
||||
Debug_TrapWithRttHint("_exit");
|
||||
}
|
||||
|
||||
__attribute__((weak)) int _read(int file, char *ptr, int len)
|
||||
|
||||
Reference in New Issue
Block a user