This commit is contained in:
zj
2026-03-13 22:42:57 +08:00
parent ea4368bf15
commit e6e07e2f55
109 changed files with 63601 additions and 0 deletions
+327
View File
@@ -0,0 +1,327 @@
/********************************** (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;
#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)
while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
USART_SendData(USART3, *buf++);
#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
+99
View File
@@ -0,0 +1,99 @@
#include "dvp.h"
#include "ch32v30x_dvp.h"
#include "eth_driver.h"
#include "string.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;
static uint32_t frame_count = 0;
extern u8 socket[];
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);
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);
DVP_Cfg(DVP_DMA_Disable, DVP_FLAG_FIFO_RESET_Enable, DVP_RX_RESET_Enable);
DVP_Mode(RB_DVP_D8_MOD, Video_Mode);
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;
}
void DVP_Task(void)
{
if (!Line_Ready_Flag) return;
uint8_t *line = (uint8_t *)Ready_Line_Ptr;
uint32_t idx = current_line_idx;
Line_Ready_Flag = 0;
/* 仅发送图像行,跳过参数行,且 socket 已连接 */
if (idx < SENSOR_HEIGHT && socket[0] != 0xFF)
{
/* 4字节行头:帧号(2B) + 行号(2B) */
uint8_t hdr[4] = {
(frame_count >> 8) & 0xFF, frame_count & 0xFF,
(idx >> 8) & 0xFF, idx & 0xFF
};
u32 len = 4;
WCHNET_SocketSend(socket[0], hdr, &len);
len = BYTES_PER_LINE;
WCHNET_SocketSend(socket[0], line, &len);
/* 每帧第0行打印一次,确认帧号和socket状态 */
if (idx == 0)
printf("[DVP] frame=%lu socket=%d\r\n", frame_count, socket[0]);
}
else if (idx == 0 && socket[0] == 0xFF)
{
/* socket 未连接时提示 */
printf("[DVP] frame=%lu waiting for TCP...\r\n", frame_count);
}
memset(line, 0, BYTES_PER_LINE);
}
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;
frame_count++;
}
if (DVP->IFR & RB_DVP_IF_ROW_DONE)
{
DVP->IFR = RB_DVP_IF_ROW_DONE;
Ready_Line_Ptr = (DVP->CR1 & RB_DVP_BUF_TOG) ? DMA_LineBuf0 : DMA_LineBuf1;
current_line_idx++;
Line_Ready_Flag = 1;
}
}
+18
View File
@@ -0,0 +1,18 @@
#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 8000 // 80.00℃ (raw/100)
extern volatile uint8_t Line_Ready_Flag;
extern volatile uint8_t *Ready_Line_Ptr;
extern volatile uint32_t current_line_idx;
void DVP_Init(void);
void DVP_Task(void);
#endif