This commit is contained in:
zhoujie 2026-03-14 13:32:11 +08:00
parent 58b98a1a3f
commit cde0bd8243
3 changed files with 70 additions and 9 deletions

View File

@ -95,6 +95,15 @@ void USART_Printf_Init(uint32_t baudrate)
GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure; USART_InitTypeDef USART_InitStructure;
/* When sensor occupies USART3, skip debug UART init entirely */
#include "mini212g2.h"
#if SENSOR_USE_USART3 && (DEBUG == DEBUG_UART3)
(void)GPIO_InitStructure;
(void)USART_InitStructure;
(void)baudrate;
return; /* USART3 reserved for sensor, printf disabled */
#endif
#if(DEBUG == DEBUG_UART1) #if(DEBUG == DEBUG_UART1)
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
@ -221,8 +230,11 @@ __attribute__((used)) int _write(int fd, char *buf, int size)
while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET); while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
USART_SendData(USART2, *buf++); USART_SendData(USART2, *buf++);
#elif(DEBUG == DEBUG_UART3) #elif(DEBUG == DEBUG_UART3)
#include "mini212g2.h"
#if !SENSOR_USE_USART3
while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET); while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
USART_SendData(USART3, *buf++); USART_SendData(USART3, *buf++);
#endif
#endif #endif
} }
#endif #endif

View File

@ -57,19 +57,34 @@ static void Sensor_UART_Init(void)
GPIO_InitTypeDef gpio = {0}; GPIO_InitTypeDef gpio = {0};
USART_InitTypeDef usart = {0}; USART_InitTypeDef usart = {0};
#if SENSOR_USE_USART3
/* USART3: PB10=TX PB11=RX
* NOTE: This reuses the debug printf port. printf is disabled. */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
gpio.GPIO_Pin = GPIO_Pin_10;
gpio.GPIO_Speed = GPIO_Speed_50MHz;
gpio.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &gpio);
gpio.GPIO_Pin = GPIO_Pin_11;
gpio.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &gpio);
#else
/* USART2: PA2=TX PA3=RX */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/* TX: PA2 AF push-pull */
gpio.GPIO_Pin = GPIO_Pin_2; gpio.GPIO_Pin = GPIO_Pin_2;
gpio.GPIO_Speed = GPIO_Speed_50MHz; gpio.GPIO_Speed = GPIO_Speed_50MHz;
gpio.GPIO_Mode = GPIO_Mode_AF_PP; gpio.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &gpio); GPIO_Init(GPIOA, &gpio);
/* RX: PA3 floating input */
gpio.GPIO_Pin = GPIO_Pin_3; gpio.GPIO_Pin = GPIO_Pin_3;
gpio.GPIO_Mode = GPIO_Mode_IN_FLOATING; gpio.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &gpio); GPIO_Init(GPIOA, &gpio);
#endif
usart.USART_BaudRate = SENSOR_UART_BAUD; usart.USART_BaudRate = SENSOR_UART_BAUD;
usart.USART_WordLength = USART_WordLength_8b; usart.USART_WordLength = USART_WordLength_8b;
@ -122,6 +137,14 @@ static void Sensor_FlushRx(void)
* Public API * Public API
* ============================================================ */ * ============================================================ */
/* ============================================================
* Debug inspection variables (watch in JTAG debugger)
* ============================================================ */
volatile uint8_t sensor_init_ok = 0;
volatile uint8_t sensor_init_fail = 0;
volatile uint8_t sensor_last_resp[8] = {0};
volatile uint8_t sensor_last_resp_len = 0;
int Mini212G2_SendCmd(const uint8_t *cmd, uint8_t len) int Mini212G2_SendCmd(const uint8_t *cmd, uint8_t len)
{ {
uint8_t resp[8]; uint8_t resp[8];
@ -129,10 +152,14 @@ int Mini212G2_SendCmd(const uint8_t *cmd, uint8_t len)
Sensor_SendBytes(cmd, len); Sensor_SendBytes(cmd, len);
int n = Sensor_ReadResp(resp, sizeof(resp), 200); int n = Sensor_ReadResp(resp, sizeof(resp), 200);
/* Save last response for debugger inspection */
sensor_last_resp_len = (uint8_t)n;
for (int j = 0; j < n && j < 8; j++)
sensor_last_resp[j] = resp[j];
if (n == ACK_LEN && memcmp(resp, ACK_PATTERN, ACK_LEN) == 0) if (n == ACK_LEN && memcmp(resp, ACK_PATTERN, ACK_LEN) == 0)
return 0; return 0;
/* Debug: print what we got */
printf("[Sensor] resp(%d):", n); printf("[Sensor] resp(%d):", n);
for (int j = 0; j < n; j++) printf(" %02x", resp[j]); for (int j = 0; j < n; j++) printf(" %02x", resp[j]);
printf("\r\n"); printf("\r\n");
@ -151,7 +178,8 @@ int Mini212G2_Init(void)
int ok = 0, fail = 0; int ok = 0, fail = 0;
Sensor_UART_Init(); Sensor_UART_Init();
printf("[Sensor] UART2 init (PA2=TX PA3=RX, 115200)\r\n"); printf("[Sensor] UART init %d, baud=%d\r\n",
(int)(SENSOR_UART == USART3 ? 3 : 2), (int)SENSOR_UART_BAUD);
/* Sensor needs several seconds to boot after power-on */ /* Sensor needs several seconds to boot after power-on */
printf("[Sensor] Waiting 3s for sensor boot...\r\n"); printf("[Sensor] Waiting 3s for sensor boot...\r\n");
@ -202,6 +230,8 @@ int Mini212G2_Init(void)
Delay_Ms(500); Delay_Ms(500);
printf("[Sensor] Config result: %d ok, %d fail\r\n", ok, fail); printf("[Sensor] Config result: %d ok, %d fail\r\n", ok, fail);
sensor_init_ok = (uint8_t)ok;
sensor_init_fail = (uint8_t)fail;
/* Print current digital video status for verification */ /* Print current digital video status for verification */
Mini212G2_PrintDigitalVideoStatus(); Mini212G2_PrintDigitalVideoStatus();

View File

@ -3,13 +3,22 @@
#include "ch32v30x.h" #include "ch32v30x.h"
/* ---- Temporary debug wiring option ----
* Set to 1 to use USART3 (PB10=TX PB11=RX) for sensor communication.
* This steals the debug printf port use JTAG debugger for observation.
* Set to 0 for normal operation (USART2 / PA2+PA3). */
#define SENSOR_USE_USART3 1
/* Set to 1 to enable UART commands to configure sensor at boot. /* Set to 1 to enable UART commands to configure sensor at boot.
* Set to 0 if sensor is pre-configured via USB to auto-output CMOS/DVP. * Set to 0 if sensor is pre-configured via USB to auto-output CMOS/DVP.
* NOTE: PA2/PA3 may conflict with Ethernet keep 0 unless UART is wired. */ * Automatically forced to 1 when SENSOR_USE_USART3 is enabled. */
#define SENSOR_UART_ENABLE 0 #if SENSOR_USE_USART3
#define SENSOR_UART_ENABLE 1
/* Sensor UART: USART2 PA2=TX PA3=RX */ #define SENSOR_UART USART3
#define SENSOR_UART USART2 #else
#define SENSOR_UART_ENABLE 0
#define SENSOR_UART USART2
#endif
#define SENSOR_UART_BAUD 115200 #define SENSOR_UART_BAUD 115200
/** /**
@ -19,6 +28,16 @@
*/ */
int Mini212G2_Init(void); int Mini212G2_Init(void);
/* Debug variables — inspect via JTAG debugger when printf is unavailable.
* sensor_init_ok: number of commands that succeeded
* sensor_init_fail: number of commands that failed
* sensor_last_resp[]: raw bytes of last failed response
* sensor_last_resp_len: length of last response */
extern volatile uint8_t sensor_init_ok;
extern volatile uint8_t sensor_init_fail;
extern volatile uint8_t sensor_last_resp[8];
extern volatile uint8_t sensor_last_resp_len;
#if SENSOR_UART_ENABLE #if SENSOR_UART_ENABLE
/** /**
* Send a raw protocol command and wait for ACK. * Send a raw protocol command and wait for ACK.