This commit is contained in:
2026-03-15 16:22:19 +08:00
parent 2e76d7b4db
commit e834928457
252 changed files with 725 additions and 37992 deletions
+339
View File
@@ -0,0 +1,339 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : debug.c
* Author : WCH
* Version : V1.0.0
* Date : 2021/06/06
* Description : This file contains all the functions prototypes for UART
* Printf , Delay functions.
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
#include "debug.h"
static uint8_t p_us = 0;
static uint16_t p_ms = 0;
#define DEBUG_DATA0_ADDRESS ((volatile uint32_t*)0xE0000380)
#define DEBUG_DATA1_ADDRESS ((volatile uint32_t*)0xE0000384)
/*********************************************************************
* @fn Delay_Init
*
* @brief Initializes Delay Funcation.
*
* @return none
*/
void Delay_Init(void)
{
p_us = SystemCoreClock / 8000000;
p_ms = (uint16_t)p_us * 1000;
}
/*********************************************************************
* @fn Delay_Us
*
* @brief Microsecond Delay Time.
*
* @param n - Microsecond number.
*
* @return None
*/
void Delay_Us(uint32_t n)
{
uint32_t i;
SysTick->SR &= ~(1 << 0);
i = (uint32_t)n * p_us;
SysTick->CMP = i;
SysTick->CTLR |= (1 << 4);
SysTick->CTLR |= (1 << 5) | (1 << 0);
while((SysTick->SR & (1 << 0)) != (1 << 0))
;
SysTick->CTLR &= ~(1 << 0);
}
/*********************************************************************
* @fn Delay_Ms
*
* @brief Millisecond Delay Time.
*
* @param n - Millisecond number.
*
* @return None
*/
void Delay_Ms(uint32_t n)
{
uint32_t i;
SysTick->SR &= ~(1 << 0);
i = (uint32_t)n * p_ms;
SysTick->CMP = i;
SysTick->CTLR |= (1 << 4);
SysTick->CTLR |= (1 << 5) | (1 << 0);
while((SysTick->SR & (1 << 0)) != (1 << 0))
;
SysTick->CTLR &= ~(1 << 0);
}
/*********************************************************************
* @fn USART_Printf_Init
*
* @brief Initializes the USARTx peripheral.
*
* @param baudrate - USART communication baud rate.
*
* @return None
*/
void USART_Printf_Init(uint32_t baudrate)
{
GPIO_InitTypeDef GPIO_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)
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
#elif(DEBUG == DEBUG_UART2)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
#elif(DEBUG == DEBUG_UART3)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
#endif
USART_InitStructure.USART_BaudRate = baudrate;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx;
#if(DEBUG == DEBUG_UART1)
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
#elif(DEBUG == DEBUG_UART2)
USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
#elif(DEBUG == DEBUG_UART3)
USART_Init(USART3, &USART_InitStructure);
USART_Cmd(USART3, ENABLE);
#endif
}
/*********************************************************************
* @fn SDI_Printf_Enable
*
* @brief Initializes the SDI printf Function.
*
* @param None
*
* @return None
*/
void SDI_Printf_Enable(void)
{
*(DEBUG_DATA0_ADDRESS) = 0;
Delay_Init();
Delay_Ms(1);
}
/*********************************************************************
* @fn _write
*
* @brief Support Printf Function
*
* @param *buf - UART send Data.
* size - Data length
*
* @return size: Data length
*/
__attribute__((used)) int _write(int fd, char *buf, int size)
{
int i = 0;
#if (SDI_PRINT == SDI_PR_OPEN)
int writeSize = size;
do
{
/**
* data0 data1 8 bytes
* data0 The lowest byte storage length, the maximum is 7
*
*/
while( (*(DEBUG_DATA0_ADDRESS) != 0u))
{
}
if(writeSize>7)
{
*(DEBUG_DATA1_ADDRESS) = (*(buf+i+3)) | (*(buf+i+4)<<8) | (*(buf+i+5)<<16) | (*(buf+i+6)<<24);
*(DEBUG_DATA0_ADDRESS) = (7u) | (*(buf+i)<<8) | (*(buf+i+1)<<16) | (*(buf+i+2)<<24);
i += 7;
writeSize -= 7;
}
else
{
*(DEBUG_DATA1_ADDRESS) = (*(buf+i+3)) | (*(buf+i+4)<<8) | (*(buf+i+5)<<16) | (*(buf+i+6)<<24);
*(DEBUG_DATA0_ADDRESS) = (writeSize) | (*(buf+i)<<8) | (*(buf+i+1)<<16) | (*(buf+i+2)<<24);
writeSize = 0;
}
} while (writeSize);
#else
for(i = 0; i < size; i++)
{
#if(DEBUG == DEBUG_UART1)
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
USART_SendData(USART1, *buf++);
#elif(DEBUG == DEBUG_UART2)
while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
USART_SendData(USART2, *buf++);
#elif(DEBUG == DEBUG_UART3)
#include "mini212g2.h"
#if !SENSOR_USE_USART3
while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
USART_SendData(USART3, *buf++);
#endif
#endif
}
#endif
return size;
}
/*********************************************************************
* @fn _sbrk
*
* @brief Change the spatial position of data segment.
*
* @return size: Data length
*/
__attribute__((used)) void *_sbrk(ptrdiff_t incr)
{
extern char _end[];
extern char _heap_end[];
static char *curbrk = _end;
if ((curbrk + incr < _end) || (curbrk + incr > _heap_end))
return NULL - 1;
curbrk += incr;
return curbrk - incr;
}
/**
* @brief 初始化LED相关IO口, 并使能时钟
* @param 无
* @retval 无
*/
void led_init(void)
{
GPIO_InitTypeDef gpio_init_struct;
LED0_GPIO_CLK_ENABLE(); /* LED0时钟使能 */
LED1_GPIO_CLK_ENABLE(); /* LED1时钟使能 */
LED2_GPIO_CLK_ENABLE(); /* LED2时钟使能 */
INPUT_GPIO_CLK_ENABLE(); /* 输入引脚时钟使能 */
OUTPUT1_GPIO_CLK_ENABLE(); /* 输出1引脚时钟使能 */
OUTPUT2_GPIO_CLK_ENABLE(); /* 输出2引脚时钟使能 */
gpio_init_struct.GPIO_Pin = LED0_GPIO_PIN; /* LED0引脚 */
gpio_init_struct.GPIO_Mode = GPIO_Mode_Out_PP; /* 推挽输出 */
gpio_init_struct.GPIO_Speed = GPIO_Speed_50MHz; /* 高速 */
GPIO_Init(LED0_GPIO_PORT, &gpio_init_struct); /* 初始化LED0引脚 */
gpio_init_struct.GPIO_Pin = LED1_GPIO_PIN; /* LED1引脚 */
GPIO_Init(LED1_GPIO_PORT, &gpio_init_struct); /* 初始化LED1引脚 */
gpio_init_struct.GPIO_Pin = LED2_GPIO_PIN; /* LED2引脚 */
GPIO_Init(LED2_GPIO_PORT, &gpio_init_struct); /* 初始化LED2引脚 */
gpio_init_struct.GPIO_Pin = INPUT_GPIO_PIN; /* INPUT引脚 */
gpio_init_struct.GPIO_Mode = GPIO_Mode_IPU; /* 上拉输入 */
GPIO_Init(INPUT_GPIO_PORT, &gpio_init_struct); /* 初始化INPUT引脚 */
gpio_init_struct.GPIO_Pin = OUTPUT1_GPIO_PIN | OUTPUT2_GPIO_PIN; /* OUTPUT引脚 */
gpio_init_struct.GPIO_Mode = GPIO_Mode_Out_PP; /* 推挽输出 */
GPIO_Init(OUTPUT1_GPIO_PORT, &gpio_init_struct); /* 初始化OUTPUT引脚 */
LED0(1); /* 关闭 LED0 */
LED1(1); /* 关闭 LED1 */
LED2(1); /* 关闭 LED2 */
OUTPUT1(0); /* 拉低DO1 */
OUTPUT2(0); /* 拉低DO2 */
}
void check_input(void)
{
if (GPIO_ReadInputDataBit(INPUT_GPIO_PORT, INPUT_GPIO_PIN) == Bit_RESET) {
LED0(0);
} else {
LED0(1);
}
}
void set_outpot(uint8_t pd8_state, uint8_t pd9_state)
{
if(pd8_state)
GPIO_SetBits(OUTPUT1_GPIO_PORT, OUTPUT1_GPIO_PIN); /* PD8拉高 */
else
GPIO_ResetBits(OUTPUT1_GPIO_PORT, OUTPUT1_GPIO_PIN); /* PD8拉低 */
if(pd9_state)
GPIO_SetBits(OUTPUT2_GPIO_PORT, OUTPUT2_GPIO_PIN); /* PD9拉高 */
else
GPIO_ResetBits(OUTPUT2_GPIO_PORT, OUTPUT2_GPIO_PIN); /* PD9拉低 */
}
+128
View File
@@ -0,0 +1,128 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : debug.h
* Author : WCH
* Version : V1.0.0
* Date : 2021/06/06
* Description : This file contains all the functions prototypes for UART
* Printf , Delay functions.
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
#ifndef __DEBUG_H
#define __DEBUG_H
#ifdef __cplusplus
extern "C" {
#endif
#include "stdio.h"
#include "ch32v30x.h"
/* UART Printf Definition */
#define DEBUG_UART1 1
#define DEBUG_UART2 2
#define DEBUG_UART3 3
/* DEBUG UATR Definition */
#ifndef DEBUG
#define DEBUG DEBUG_UART3
#endif
/* SDI Printf Definition */
#define SDI_PR_CLOSE 0
#define SDI_PR_OPEN 1
#ifndef SDI_PRINT
#define SDI_PRINT SDI_PR_CLOSE
#endif
/* 引脚 定义 */
#define LED0_GPIO_PORT GPIOB
#define LED0_GPIO_PIN GPIO_Pin_5
#define LED0_GPIO_CLK_ENABLE() do{ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); }while(0) /* PB口时钟使能 */
#define LED1_GPIO_PORT GPIOB
#define LED1_GPIO_PIN GPIO_Pin_6
#define LED1_GPIO_CLK_ENABLE() do{ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); }while(0) /* PB口时钟使能 */
#define LED2_GPIO_PORT GPIOB
#define LED2_GPIO_PIN GPIO_Pin_7
#define LED2_GPIO_CLK_ENABLE() do{ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); }while(0) /* PC口时钟使能 */
#define INPUT_GPIO_PORT GPIOB
#define INPUT_GPIO_PIN GPIO_Pin_14
#define INPUT_GPIO_CLK_ENABLE() do{ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); }while(0) /* PA口时钟使能 */
#define OUTPUT1_GPIO_PORT GPIOD
#define OUTPUT1_GPIO_PIN GPIO_Pin_8
#define OUTPUT1_GPIO_CLK_ENABLE() do{ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); }while(0) /* PD口时钟使能 */
#define OUTPUT2_GPIO_PORT GPIOD
#define OUTPUT2_GPIO_PIN GPIO_Pin_9
#define OUTPUT2_GPIO_CLK_ENABLE() do{ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); }while(0) /* PD口时钟使能 */
/******************************************************************************************/
/* LED端口定义 */
#define LED0(x) do{ x ? \
GPIO_SetBits(LED0_GPIO_PORT,LED0_GPIO_PIN): \
GPIO_ResetBits(LED0_GPIO_PORT,LED0_GPIO_PIN); \
}while(0) /* LED0 = BLUE */
#define LED1(x) do{ x ? \
GPIO_SetBits(LED1_GPIO_PORT,LED1_GPIO_PIN): \
GPIO_ResetBits(LED1_GPIO_PORT,LED1_GPIO_PIN); \
}while(0) /* LED1 = YELLOW */
#define LED2(x) do{ x ? \
GPIO_SetBits(LED2_GPIO_PORT,LED2_GPIO_PIN): \
GPIO_ResetBits(LED2_GPIO_PORT,LED2_GPIO_PIN); \
}while(0) /* LED2 = GREEN */
#define INPUT(x) do{ x ? \
GPIO_SetBits(INPUT_GPIO_PORT,INPUT_GPIO_PIN): \
GPIO_ResetBits(INPUT_GPIO_PORT,INPUT_GPIO_PIN); \
}while(0)
#define OUTPUT1(x) do{ x ? \
GPIO_SetBits(OUTPUT1_GPIO_PORT,OUTPUT1_GPIO_PIN): \
GPIO_ResetBits(OUTPUT1_GPIO_PORT,OUTPUT1_GPIO_PIN); \
}while(0)
#define OUTPUT2(x) do{ x ? \
GPIO_SetBits(OUTPUT2_GPIO_PORT,OUTPUT2_GPIO_PIN): \
GPIO_ResetBits(OUTPUT2_GPIO_PORT,OUTPUT2_GPIO_PIN); \
}while(0)
/* LED取反定义 */
#define LED0_TOGGLE() do{ gpio_toggle_pin(LED0_GPIO_PORT, LED0_GPIO_PIN); }while(0) /* 翻转LED0 */
#define LED1_TOGGLE() do{ gpio_toggle_pin(LED1_GPIO_PORT, LED1_GPIO_PIN); }while(0) /* 翻转LED1 */
#define LED2_TOGGLE() do{ gpio_toggle_pin(LED2_GPIO_PORT, LED2_GPIO_PIN); }while(0) /* 翻转LED2 */
/******************************************************************************************/
void led_init(void); /* 初始化LED */
void check_input(void); /*检查PA8口的输入电平*/
void set_outpot(uint8_t pd8_state, uint8_t pd9_state); /*设置PD8\PD9输出电平*/
void Delay_Init(void);
void Delay_Us (uint32_t n);
void Delay_Ms (uint32_t n);
void USART_Printf_Init(uint32_t baudrate);
void SDI_Printf_Enable(void);
#ifdef __cplusplus
}
#endif
#endif
+115
View File
@@ -0,0 +1,115 @@
#include "dvp.h"
#include "ch32v30x_dvp.h"
#include "eth_driver.h"
#include "string.h"
#include "qdx_port.h"
__attribute__((aligned(4))) uint8_t DMA_LineBuf0[BYTES_PER_LINE];
__attribute__((aligned(4))) uint8_t DMA_LineBuf1[BYTES_PER_LINE];
volatile uint8_t Line_Ready_Flag = 0;
volatile uint8_t *Ready_Line_Ptr = NULL;
volatile uint32_t current_line_idx = 0;
volatile uint32_t dvp_frame_count = 0;
volatile uint32_t dvp_row_irq_cnt = 0;
extern u8 socket[];
extern volatile uint32_t sys_tick_ms;
void DVP_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure = {0};
NVIC_InitTypeDef NVIC_InitStructure = {0};
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DVP, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_9 | GPIO_Pin_10;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_11;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_8 | GPIO_Pin_9;
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_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Step 6: Enable DMA, then enable DVP */
DVP->CR1 = RB_DVP_DMA_EN; /* DMA on, CM=0 continuous, no reset bits */
DVP->CR0 |= RB_DVP_ENABLE;
DBG_INIT("DVP CR0=0x%02x CR1=0x%02x ROW=%d COL=%d\r\n",
(int)(DVP->CR0 & 0xFF), (int)(DVP->CR1 & 0xFF),
(int)DVP->ROW_NUM, (int)DVP->COL_NUM);
DBG_INIT("DVP DMA_BUF0=0x%08x DMA_BUF1=0x%08x\r\n",
(int)DVP->DMA_BUF0, (int)DVP->DMA_BUF1);
}
#define PROTOCOL_HEADER_RESERVE 64
__attribute__((aligned(4))) uint8_t FrameBuffer[SENSOR_HEIGHT][BYTES_PER_LINE];
volatile uint8_t Frame_Ready_Flag = 0;
volatile uint32_t Ready_Frame_Count = 0;
void DVP_Task(void)
{
/* Line copying is done directly in DVP_IRQHandler to prevent
* line drops when the task runs slower than the DVP pixel clock.
* This function is intentionally empty. */
}
void DVP_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void DVP_IRQHandler(void)
{
if (DVP->IFR & RB_DVP_IF_STR_FRM)
{
DVP->IFR = RB_DVP_IF_STR_FRM;
current_line_idx = 0;
dvp_frame_count++;
}
if (DVP->IFR & RB_DVP_IF_ROW_DONE)
{
DVP->IFR = RB_DVP_IF_ROW_DONE;
/* Capture src and idx before incrementing to fix off-by-one:
* STR_FRM sets idx=0, so first ROW_DONE copies to FrameBuffer[0]. */
uint8_t *src = (DVP->CR1 & RB_DVP_BUF_TOG) ? DMA_LineBuf0 : DMA_LineBuf1;
uint32_t idx = current_line_idx;
current_line_idx++;
dvp_row_irq_cnt++;
if (idx < SENSOR_HEIGHT)
{
memcpy(FrameBuffer[idx], src, BYTES_PER_LINE);
if (idx == SENSOR_HEIGHT - 1)
{
Frame_Ready_Flag = 1;
Ready_Frame_Count = dvp_frame_count;
}
}
}
}
+25
View File
@@ -0,0 +1,25 @@
#ifndef DVP_H_
#define DVP_H_
#include "ch32v30x.h"
#define SENSOR_WIDTH 256
#define SENSOR_HEIGHT 192
#define SENSOR_TOTAL_LINES (SENSOR_HEIGHT + 1) // +1 ²ÎÊýÐÐ
#define BYTES_PER_LINE (SENSOR_WIDTH * 2) // ÿÐÐ512×Ö½Ú
#define ALARM_TEMP_RAW 800 // 80.0¡æ (raw/10)
extern volatile uint8_t Line_Ready_Flag;
extern volatile uint8_t *Ready_Line_Ptr;
extern volatile uint32_t current_line_idx;
extern volatile uint32_t dvp_frame_count;
extern volatile uint32_t dvp_row_irq_cnt;
void DVP_Init(void);
void DVP_Task(void);
extern __attribute__((aligned(4))) uint8_t FrameBuffer[SENSOR_HEIGHT][BYTES_PER_LINE];
extern volatile uint8_t Frame_Ready_Flag;
extern volatile uint32_t Ready_Frame_Count;
#endif
+313
View File
@@ -0,0 +1,313 @@
#include "mini212g2.h"
#include "debug.h"
#include "string.h"
#include "qdx_port.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};
/* Query: status page (simplest query, always works) */
static const uint8_t CMD_QUERY_STATUS[] =
{0x55, 0xAA, 0x07, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x87, 0xF0};
/* ============================================================
* Low-level UART helpers
* ============================================================ */
static void Sensor_UART_Init(void)
{
GPIO_InitTypeDef gpio = {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_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
gpio.GPIO_Pin = GPIO_Pin_2;
gpio.GPIO_Speed = GPIO_Speed_50MHz;
gpio.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &gpio);
gpio.GPIO_Pin = GPIO_Pin_3;
gpio.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &gpio);
#endif
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
* ============================================================ */
/* ============================================================
* 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[32] = {0};
volatile uint8_t sensor_last_resp_len = 0;
volatile uint8_t sensor_comm_test = 0;
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);
/* Save last response for debugger inspection */
sensor_last_resp_len = (uint8_t)n;
for (int j = 0; j < n && j < 32; j++)
sensor_last_resp[j] = resp[j];
if (n == ACK_LEN && memcmp(resp, ACK_PATTERN, ACK_LEN) == 0)
return 0;
DBG_INIT("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
DBG_INIT("Sensor: UART disabled, pre-configured via USB\r\n");
DBG_INIT("Sensor: CMOS output, CMOS8(MSB), Y16, 30Hz\r\n");
return 0;
#else
int ok = 0, fail = 0;
Sensor_UART_Init();
DBG_INIT("Sensor: UART%d baud=%d\r\n",
(int)(SENSOR_UART == USART3 ? 3 : 2), (int)SENSOR_UART_BAUD);
/* Sensor needs several seconds to boot after power-on */
DBG_INIT("Sensor: Waiting 3s for boot...\r\n");
Delay_Ms(3000);
/* === Communication test: send status query === */
{
uint8_t test_resp[32];
Sensor_FlushRx();
Sensor_SendBytes(CMD_QUERY_STATUS, sizeof(CMD_QUERY_STATUS));
int tn = Sensor_ReadResp(test_resp, sizeof(test_resp), 500);
sensor_last_resp_len = (uint8_t)tn;
for (int j = 0; j < tn && j < 32; j++)
sensor_last_resp[j] = test_resp[j];
sensor_comm_test = (tn > 0) ? 1 : 0xFF;
/* Breakpoint here to check sensor_comm_test and sensor_last_resp */
}
Delay_Ms(200);
DBG_INIT("Sensor: Configuring CMOS/DVP output...\r\n");
/* 1. Shutter compensation */
if (Mini212G2_SendCmd(CMD_SHUTTER, sizeof(CMD_SHUTTER)) == 0)
{ ok++; DBG_INIT("Sensor: Shutter OK\r\n"); }
else
{ fail++; DBG_ERR("Sensor: Shutter FAIL\r\n"); }
Delay_Ms(200);
/* 2. Digital port → CMOS */
if (Mini212G2_SendCmd(CMD_DIGITAL_CMOS, sizeof(CMD_DIGITAL_CMOS)) == 0)
{ ok++; DBG_INIT("Sensor: Digital->CMOS OK\r\n"); }
else
{ fail++; DBG_ERR("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++; DBG_INIT("Sensor: CMOS8(MSB) OK\r\n"); }
else
{ fail++; DBG_ERR("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++; DBG_INIT("Sensor: Y16 OK\r\n"); }
else
{ fail++; DBG_ERR("Sensor: Y16 FAIL\r\n"); }
Delay_Ms(200);
/* 5. Frame rate → 30Hz */
if (Mini212G2_SendCmd(CMD_FPS_30HZ, sizeof(CMD_FPS_30HZ)) == 0)
{ ok++; DBG_INIT("Sensor: 30Hz OK\r\n"); }
else
{ fail++; DBG_ERR("Sensor: 30Hz FAIL\r\n"); }
Delay_Ms(200);
/* 6. Save settings to flash */
if (Mini212G2_SendCmd(CMD_SAVE, sizeof(CMD_SAVE)) == 0)
{ ok++; DBG_INIT("Sensor: Save OK\r\n"); }
else
{ fail++; DBG_ERR("Sensor: Save FAIL\r\n"); }
Delay_Ms(500);
DBG_INIT("Sensor: %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 */
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) {
DBG_ERR("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];
DBG_INIT("Sensor Digital Video Status:\r\n");
DBG_INIT(" Port: %s (%d)\r\n",
(port_type < 10) ? port_names[port_type] : "?", (int)port_type);
DBG_INIT(" Content: %s (%d)\r\n",
(content < 6) ? content_names[content] : "?", (int)content);
DBG_INIT(" Interface: %s (%d)\r\n",
(iface < 3) ? iface_names[iface] : "?", (int)iface);
DBG_INIT(" FPS: %s (%d)\r\n",
(fps < 4) ? fps_names[fps] : "?", (int)fps);
DBG_INIT(" 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 */
+59
View File
@@ -0,0 +1,59 @@
#ifndef MINI212G2_H_
#define MINI212G2_H_
#include "ch32v30x.h"
/* ---- Sensor UART configuration ----
* 模组已通过 USB 预配置,不需要 MCU UART 配置。
* 注意:USART2 (PA2/PA3) 引脚已被占用,不可使用。
* 如需重新启用 MCU UART 配置传感器,将下面的 #if 0 改为 #if 1。
* 启用后仅可使用 USART3 (PB10/PB11),但会占用 printf 调试口。 */
#if 0 /* MCU UART 配置已禁用 */
#define SENSOR_USE_USART3 1 /* 必须用 USART3,USART2 引脚已占用 */
#define SENSOR_UART_ENABLE 1
#define SENSOR_UART USART3
#define SENSOR_UART_BAUD 115200
#else
#define SENSOR_USE_USART3 0
#define SENSOR_UART_ENABLE 0
#define SENSOR_UART_BAUD 115200
#endif
/**
* 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);
/* 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[32];
extern volatile uint8_t sensor_last_resp_len;
/* sensor_comm_test: 0=not run, 1=got response, 0xFF=no response (timeout) */
extern volatile uint8_t sensor_comm_test;
#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