Files
cosyos/Demo-Build/RISC-V32/demo_debug.c
T
2026-07-31 12:36:33 +00:00

107 lines
3.5 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**************************************************************************//**
* @item CosyOS-III 例程(系统构建)
* @file demo_debug.c
* @brief 例程-DEBUG for CH32V30x use firmware library
* @author 迟凯峰
* @version v1.0.1
* @date 2026.07.31
******************************************************************************/
#if 0 /* 编译开关 */
#include "CosyOS.H"
#if (SYSCFG_DEBUGGING == __ENABLED__)
/*
* 用户定义
*/
/*-------------------------------------------------------------*
| DEBUG_MODE | 输出(UART SEND| 输入(UART RECV| 推荐等级 |
|------------+------------------+------------------+----------|
| 0 | printf(查询) | 串口接收中断 | * |
|------------+------------------+------------------+----------|
| 1 | 串口发送中断 | 串口接收中断 | *** |
*-------------------------------------------------------------*/
#define DEBUG_MODE 1 // 建议不要配置为0,否则极易导致 系统调试器(Debugger)的任务栈需求激增!
/* DEBUG RECV 缓存SIZE */
#define DEBUG_RECV_BUFF 32
////////////////////////////////////////////////////////////////////////////////
// 1、例程中使用了UART1做为DEBUG专用串口,用户如需更换其它串口请自行调整。
// 2、下方的DEBUG配置函数 Debug_Config(),不能够取代基本的串口初始化工作,
// 用户在main函数中,在调用该函数之前,需先行初始化串口。
////////////////////////////////////////////////////////////////////////////////
#include "ch32v30x_usart.h"
static char u_debug_recv_buff[DEBUG_RECV_BUFF];
static char *u_debug_recv_p = u_debug_recv_buff;
#if (DEBUG_MODE == 0)
#include <stdio.h>
#elif (DEBUG_MODE == 1)
static char *u_debug_send_p;
#endif
void Debug_Config(void)
{
#if (DEBUG_MODE == 0)
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
#elif (DEBUG_MODE == 1)
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
#endif
}
#if (SYSCFG_DEBUG_SENDLEN == __DISABLED__)
void debug_hook(char _XDATA_MEM_ *p)
#else
void debug_hook(char _XDATA_MEM_ *p, s_u16_t len)
#endif
{
#if (DEBUG_MODE == 0)
printf("%s", p);
xDebugSend_Complete();
#elif (DEBUG_MODE == 1)
u_debug_send_p = p;
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
#endif
}
// 根据是否启用硬件压栈及串口中断优先级,选择相匹配的声明方式:
void USART1_IRQHandler(void) __attribute__((interrupt)); // 声明为软件压栈
//void USART1_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast"))); // 声明为硬件压栈
void USART1_IRQHandler(void)
{
#if (DEBUG_MODE == 1)
if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET){
if(*u_debug_send_p){
USART_SendData(USART1, *u_debug_send_p++);
}
else{
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
xDebugSend_Complete();
}
}
#endif
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET){
if(u_debug_recv_p - u_debug_recv_buff < DEBUG_RECV_BUFF - 1){
*u_debug_recv_p = USART_ReceiveData(USART1);
if(*u_debug_recv_p++ == '\n'){
xDebugRecv_Handler(u_debug_recv_buff, u_debug_recv_p - u_debug_recv_buff);
u_debug_recv_p = u_debug_recv_buff;
}
}
else{
u_debug_recv_p = u_debug_recv_buff;
}
}
}
#endif
#endif