This commit is contained in:
zhoujie 2026-03-14 13:06:30 +08:00
parent d53ab96f53
commit 58b98a1a3f
8 changed files with 3383 additions and 9 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

Binary file not shown.

View File

@ -31,26 +31,43 @@ void DVP_Init(void)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_8 | GPIO_Pin_9; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_8 | GPIO_Pin_9;
GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Step 1: Reset DVP logic */
DVP->CR1 = RB_DVP_ALL_CLR | RB_DVP_RCV_CLR;
DVP->CR1 = 0x00; /* release reset */
/* Step 2: Configure mode - 8-bit data, Video/RGB, continuous capture */
DVP->CR0 = 0; /* clear everything first */
DVP_Mode(RB_DVP_D8_MOD, Video_Mode);
/* Mini212G2 polarity: PCLK normal(rising), VSYNC high-active, HSYNC high-active */
DVP->CR0 &= ~(RB_DVP_P_POLAR | RB_DVP_V_POLAR | RB_DVP_H_POLAR);
DVP->CR0 |= RB_DVP_V_POLAR; /* VSYNC active-high (FIELD_VALID=HIGH) */
/* Step 3: Configure DMA buffers */
DVP->DMA_BUF0 = (uint32_t)DMA_LineBuf0;
DVP->DMA_BUF1 = (uint32_t)DMA_LineBuf1;
/* Step 4: Configure image dimensions */
DVP->ROW_NUM = 1;
DVP->COL_NUM = BYTES_PER_LINE;
/* Step 5: Enable interrupts */
DVP_INTCfg(ENABLE, RB_DVP_IE_STR_FRM | RB_DVP_IE_ROW_DONE);
NVIC_InitStructure.NVIC_IRQChannel = DVP_IRQn; NVIC_InitStructure.NVIC_IRQChannel = DVP_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure); NVIC_Init(&NVIC_InitStructure);
DVP_Cfg(DVP_DMA_Disable, DVP_FLAG_FIFO_RESET_Enable, DVP_RX_RESET_Enable); /* Step 6: Enable DMA, then enable DVP */
DVP_Mode(RB_DVP_D8_MOD, Video_Mode); DVP->CR1 = RB_DVP_DMA_EN; /* DMA on, CM=0 continuous, no reset bits */
DVP->CR0 &= ~(RB_DVP_P_POLAR | RB_DVP_V_POLAR | RB_DVP_H_POLAR);
DVP->DMA_BUF0 = (uint32_t)DMA_LineBuf0;
DVP->DMA_BUF1 = (uint32_t)DMA_LineBuf1;
DVP->ROW_NUM = 1;
DVP->COL_NUM = BYTES_PER_LINE;
DVP_INTCfg(ENABLE, RB_DVP_IE_STR_FRM | RB_DVP_IE_ROW_DONE);
DVP_Cfg(DVP_DMA_Enable, DVP_FLAG_FIFO_RESET_Disable, DVP_RX_RESET_Disable);
DVP->CR0 |= RB_DVP_ENABLE; DVP->CR0 |= RB_DVP_ENABLE;
printf("[DVP] Init done CR0=0x%02x CR1=0x%02x ROW=%d COL=%d\r\n", printf("[DVP] Init done CR0=0x%02x CR1=0x%02x ROW=%d COL=%d\r\n",
(int)(DVP->CR0 & 0xFF), (int)(DVP->CR1 & 0xFF), (int)(DVP->CR0 & 0xFF), (int)(DVP->CR1 & 0xFF),
(int)DVP->ROW_NUM, (int)DVP->COL_NUM); (int)DVP->ROW_NUM, (int)DVP->COL_NUM);
printf("[DVP] DMA_BUF0=0x%08x DMA_BUF1=0x%08x\r\n",
(int)DVP->DMA_BUF0, (int)DVP->DMA_BUF1);
} }
#define PROTOCOL_HEADER_RESERVE 64 #define PROTOCOL_HEADER_RESERVE 64

View File

@ -0,0 +1,263 @@
#include "mini212g2.h"
#include "debug.h"
#include "string.h"
#if SENSOR_UART_ENABLE
/* ============================================================
* Protocol constants
* ============================================================ */
#define FRAME_HEAD1 0x55
#define FRAME_HEAD2 0xAA
#define FRAME_TAIL 0xF0
/* ACK response: 55 AA 01 00 01 F0 */
#define ACK_LEN 6
static const uint8_t ACK_PATTERN[ACK_LEN] = {0x55, 0xAA, 0x01, 0x00, 0x01, 0xF0};
/* ============================================================
* DVP output configuration commands
* (from Mini212G2 serial protocol document)
* ============================================================ */
/* Digital port type → CMOS */
static const uint8_t CMD_DIGITAL_CMOS[] =
{0x55, 0xAA, 0x07, 0x02, 0x01, 0x02, 0x00, 0x00, 0x00, 0x02, 0x04, 0xF0};
/* CMOS interface → CMOS8(MSB) */
static const uint8_t CMD_CMOS8_MSB[] =
{0x55, 0xAA, 0x07, 0x02, 0x01, 0x04, 0x00, 0x00, 0x00, 0x01, 0x01, 0xF0};
/* CMOS content → Y16 */
static const uint8_t CMD_CONTENT_Y16[] =
{0x55, 0xAA, 0x07, 0x02, 0x01, 0x03, 0x00, 0x00, 0x00, 0x02, 0x05, 0xF0};
/* Frame rate → 30Hz */
static const uint8_t CMD_FPS_30HZ[] =
{0x55, 0xAA, 0x07, 0x02, 0x01, 0x05, 0x00, 0x00, 0x00, 0x00, 0x01, 0xF0};
/* Save settings */
static const uint8_t CMD_SAVE[] =
{0x55, 0xAA, 0x07, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x03, 0xF0};
/* Shutter compensation (NUC) */
static const uint8_t CMD_SHUTTER[] =
{0x55, 0xAA, 0x07, 0x02, 0x01, 0x08, 0x00, 0x00, 0x00, 0x01, 0x0D, 0xF0};
/* Query: digital video page */
static const uint8_t CMD_QUERY_DIGITAL[] =
{0x55, 0xAA, 0x07, 0x02, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x84, 0xF0};
/* ============================================================
* Low-level UART helpers
* ============================================================ */
static void Sensor_UART_Init(void)
{
GPIO_InitTypeDef gpio = {0};
USART_InitTypeDef usart = {0};
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/* TX: PA2 AF push-pull */
gpio.GPIO_Pin = GPIO_Pin_2;
gpio.GPIO_Speed = GPIO_Speed_50MHz;
gpio.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &gpio);
/* RX: PA3 floating input */
gpio.GPIO_Pin = GPIO_Pin_3;
gpio.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &gpio);
usart.USART_BaudRate = SENSOR_UART_BAUD;
usart.USART_WordLength = USART_WordLength_8b;
usart.USART_StopBits = USART_StopBits_1;
usart.USART_Parity = USART_Parity_No;
usart.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
usart.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_Init(SENSOR_UART, &usart);
USART_Cmd(SENSOR_UART, ENABLE);
}
static void Sensor_SendBytes(const uint8_t *data, uint8_t len)
{
for (uint8_t i = 0; i < len; i++) {
while (!USART_GetFlagStatus(SENSOR_UART, USART_FLAG_TXE));
USART_SendData(SENSOR_UART, data[i]);
}
while (!USART_GetFlagStatus(SENSOR_UART, USART_FLAG_TC));
}
/**
* Read response bytes until FRAME_TAIL seen or timeout.
* timeout_ms approximate milliseconds (uses Delay_Us polling).
* Returns number of bytes actually read.
*/
static int Sensor_ReadResp(uint8_t *buf, uint8_t max_len, uint16_t timeout_ms)
{
uint8_t idx = 0;
/* Poll with ~0.1 ms granularity */
for (uint32_t i = 0; i < (uint32_t)timeout_ms * 10; i++) {
if (USART_GetFlagStatus(SENSOR_UART, USART_FLAG_RXNE)) {
buf[idx++] = (uint8_t)USART_ReceiveData(SENSOR_UART);
if (idx >= max_len) return idx;
if (idx >= 2 && buf[idx - 1] == FRAME_TAIL) return idx;
} else {
Delay_Us(100);
}
}
return idx;
}
static void Sensor_FlushRx(void)
{
while (USART_GetFlagStatus(SENSOR_UART, USART_FLAG_RXNE))
(void)USART_ReceiveData(SENSOR_UART);
}
/* ============================================================
* Public API
* ============================================================ */
int Mini212G2_SendCmd(const uint8_t *cmd, uint8_t len)
{
uint8_t resp[8];
Sensor_FlushRx();
Sensor_SendBytes(cmd, len);
int n = Sensor_ReadResp(resp, sizeof(resp), 200);
if (n == ACK_LEN && memcmp(resp, ACK_PATTERN, ACK_LEN) == 0)
return 0;
/* Debug: print what we got */
printf("[Sensor] resp(%d):", n);
for (int j = 0; j < n; j++) printf(" %02x", resp[j]);
printf("\r\n");
return -1;
}
#endif /* SENSOR_UART_ENABLE (UART helpers + SendCmd) */
int Mini212G2_Init(void)
{
#if !SENSOR_UART_ENABLE
printf("[Sensor] UART disabled, assuming pre-configured via USB\r\n");
printf("[Sensor] Expected: CMOS output, CMOS8(MSB), Y16, 30Hz\r\n");
return 0;
#else
int ok = 0, fail = 0;
Sensor_UART_Init();
printf("[Sensor] UART2 init (PA2=TX PA3=RX, 115200)\r\n");
/* Sensor needs several seconds to boot after power-on */
printf("[Sensor] Waiting 3s for sensor boot...\r\n");
Delay_Ms(3000);
printf("[Sensor] Configuring CMOS/DVP output...\r\n");
/* 1. Shutter compensation */
if (Mini212G2_SendCmd(CMD_SHUTTER, sizeof(CMD_SHUTTER)) == 0)
{ ok++; printf("[Sensor] Shutter OK\r\n"); }
else
{ fail++; printf("[Sensor] Shutter FAIL\r\n"); }
Delay_Ms(200);
/* 2. Digital port → CMOS */
if (Mini212G2_SendCmd(CMD_DIGITAL_CMOS, sizeof(CMD_DIGITAL_CMOS)) == 0)
{ ok++; printf("[Sensor] Digital->CMOS OK\r\n"); }
else
{ fail++; printf("[Sensor] Digital->CMOS FAIL\r\n"); }
Delay_Ms(200);
/* 3. CMOS interface → 8-bit MSB */
if (Mini212G2_SendCmd(CMD_CMOS8_MSB, sizeof(CMD_CMOS8_MSB)) == 0)
{ ok++; printf("[Sensor] CMOS8(MSB) OK\r\n"); }
else
{ fail++; printf("[Sensor] CMOS8(MSB) FAIL\r\n"); }
Delay_Ms(200);
/* 4. CMOS content → Y16 (raw 16-bit thermal) */
if (Mini212G2_SendCmd(CMD_CONTENT_Y16, sizeof(CMD_CONTENT_Y16)) == 0)
{ ok++; printf("[Sensor] Y16 OK\r\n"); }
else
{ fail++; printf("[Sensor] Y16 FAIL\r\n"); }
Delay_Ms(200);
/* 5. Frame rate → 30Hz */
if (Mini212G2_SendCmd(CMD_FPS_30HZ, sizeof(CMD_FPS_30HZ)) == 0)
{ ok++; printf("[Sensor] 30Hz OK\r\n"); }
else
{ fail++; printf("[Sensor] 30Hz FAIL\r\n"); }
Delay_Ms(200);
/* 6. Save settings to flash */
if (Mini212G2_SendCmd(CMD_SAVE, sizeof(CMD_SAVE)) == 0)
{ ok++; printf("[Sensor] Save OK\r\n"); }
else
{ fail++; printf("[Sensor] Save FAIL\r\n"); }
Delay_Ms(500);
printf("[Sensor] Config result: %d ok, %d fail\r\n", ok, fail);
/* Print current digital video status for verification */
Mini212G2_PrintDigitalVideoStatus();
return (fail == 0) ? 0 : -1;
#endif /* SENSOR_UART_ENABLE */
}
#if SENSOR_UART_ENABLE
void Mini212G2_PrintDigitalVideoStatus(void)
{
uint8_t resp[32];
Sensor_FlushRx();
Sensor_SendBytes(CMD_QUERY_DIGITAL, sizeof(CMD_QUERY_DIGITAL));
int n = Sensor_ReadResp(resp, sizeof(resp), 500);
if (n < 24) {
printf("[Sensor] Query failed (got %d bytes)\r\n", n);
return;
}
/* Parse per protocol spec (Page 11-12):
* Byte5 = ext-sync, Byte6 = digital port type, Byte7 = CMOS content,
* Byte8 = CMOS interface, Byte9 = frame rate, Byte11 = clock phase */
static const char *port_names[] = {
"OFF", "USB2.0", "CMOS", "BT1120", "BT656",
"USB+UART", "LCD", "LVDS", "LCD+DVP", "UVC+CDC"
};
static const char *content_names[] = {
"YUV422", "YUV422+Param", "Y16", "Y16+Param",
"Y16+YUV422", "Y16+Param+YUV422"
};
static const char *iface_names[] = {"CMOS16", "CMOS8(MSB)", "CMOS8(LSB)"};
static const char *fps_names[] = {"30Hz", "25Hz", "9Hz", "50Hz"};
uint8_t port_type = resp[6];
uint8_t content = resp[7];
uint8_t iface = resp[8];
uint8_t fps = resp[9];
uint8_t clk_phase = resp[11];
printf("[Sensor] Digital Video Status:\r\n");
printf(" Port: %s (%d)\r\n",
(port_type < 10) ? port_names[port_type] : "?", (int)port_type);
printf(" Content: %s (%d)\r\n",
(content < 6) ? content_names[content] : "?", (int)content);
printf(" Interface: %s (%d)\r\n",
(iface < 3) ? iface_names[iface] : "?", (int)iface);
printf(" FPS: %s (%d)\r\n",
(fps < 4) ? fps_names[fps] : "?", (int)fps);
printf(" ClkPhase: %s (%d)\r\n",
(clk_phase == 0) ? "Rising" : "Falling", (int)clk_phase);
}
int Mini212G2_ShutterCompensation(void)
{
return Mini212G2_SendCmd(CMD_SHUTTER, sizeof(CMD_SHUTTER));
}
#endif /* SENSOR_UART_ENABLE */

View File

@ -0,0 +1,40 @@
#ifndef MINI212G2_H_
#define MINI212G2_H_
#include "ch32v30x.h"
/* 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.
* NOTE: PA2/PA3 may conflict with Ethernet keep 0 unless UART is wired. */
#define SENSOR_UART_ENABLE 0
/* Sensor UART: USART2 PA2=TX PA3=RX */
#define SENSOR_UART USART2
#define SENSOR_UART_BAUD 115200
/**
* Initialize sensor. When SENSOR_UART_ENABLE=1, configures via UART.
* When SENSOR_UART_ENABLE=0, assumes sensor is pre-configured via USB.
* Call BEFORE DVP_Init(). Returns 0 on success.
*/
int Mini212G2_Init(void);
#if SENSOR_UART_ENABLE
/**
* Send a raw protocol command and wait for ACK.
* Returns 0 on ACK received, -1 on timeout/bad response.
*/
int Mini212G2_SendCmd(const uint8_t *cmd, uint8_t len);
/**
* Query current digital video settings. Prints result to debug console.
*/
void Mini212G2_PrintDigitalVideoStatus(void);
/**
* Trigger shutter compensation (NUC).
*/
int Mini212G2_ShutterCompensation(void);
#endif /* SENSOR_UART_ENABLE */
#endif

View File

@ -2,6 +2,7 @@
#include "string.h" #include "string.h"
#include "eth_driver.h" #include "eth_driver.h"
#include "dvp.h" #include "dvp.h"
#include "mini212g2.h"
#include "qdx_port.h" #include "qdx_port.h"
#include "qdx_preprocess.h" #include "qdx_preprocess.h"
#include "qdx_tcp_logic.h" #include "qdx_tcp_logic.h"
@ -347,6 +348,7 @@ int main(void)
for (i = 0; i < 6; i++) printf("%x ", MACAddr[i]); for (i = 0; i < 6; i++) printf("%x ", MACAddr[i]);
printf("\n"); printf("\n");
Mini212G2_Init(); /* Configure sensor for CMOS/DVP Y16 output */
DVP_Init(); DVP_Init();
TIM2_Init(); TIM2_Init();
NG_GPIO_Init(); NG_GPIO_Init();