Files
caocong d2d8800788 feat:新建项目文件
BLV主机C1P模块
2025-12-06 13:49:01 +08:00

364 lines
9.2 KiB
C

/*
* debug.c
*
* Created on: May 14, 2025
* Author: cc
*/
#include "debug.h"
#include <stddef.h>
#include <stdarg.h>
#include <stdio.h>
volatile uint32_t SysTick_100us = 0;
volatile uint32_t SysTick_1ms = 0;
volatile uint32_t SysTick_1s = 0;
__attribute__((section(".non_0_wait"))) void Systick_Init(void)
{
/*配置中断优先级*/
NVIC_SetPriority(SysTick_IRQn, 0x00);
NVIC_EnableIRQ(SysTick_IRQn);
/*配置定时器*/
SysTick->CTLR= 0;
SysTick->SR = 0;
SysTick->CNT = 0;
SysTick->CMP = SystemCoreClock/10000;//后面的1000代表1000HZ(那就是1ms进一次中断)
SysTick->CTLR= 0xf;
}
void SysTick_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void SysTick_Handler(void)
{
static uint8_t NUM = 0;
static uint16_t NUM_s = 0;
SysTick->SR = 0; //清除中断标志
SysTick_100us++;
NUM++;
if(NUM >= 10){
NUM = 0;
SysTick_1ms++;
NUM_s++;
if(NUM_s >= 1000){
NUM_s = 0x00;
SysTick_1s++;
}
}
}
/*********************************************************************
* @fn Delay_Us
*
* @brief Microsecond Delay Time.
*
* @param n - Microsecond number.
*
* @return None
*/
__attribute__((section(".non_0_wait"))) void Delay_Us(uint32_t n)
{
for(uint32_t i=0;i<n;i++){
for(uint32_t j=0;j<30;j++){
__NOP();
}
}
}
/*********************************************************************
* @fn Delay_Ms
*
* @brief Millisecond Delay Time.
*
* @param n - Millisecond number.
*
* @return None
*/
__attribute__((section(".non_0_wait"))) void Delay_Ms(uint32_t n)
{
for(uint32_t i=0;i<n;i++){
Delay_Us(1000);
}
}
/*********************************************************************
* @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;
for (i = 0; i < size; i++)
{
#if (DEBUG) == DEBUG_UART0
while ((R8_UART0_LSR & RB_LSR_TX_FIFO_EMP) == 0);
R8_UART0_THR = *(buf++);
#elif (DEBUG) == DEBUG_UART1
while ((R8_UART1_LSR & RB_LSR_TX_FIFO_EMP) == 0);
R8_UART1_THR = *(buf++);
#elif (DEBUG) == DEBUG_UART2
while ((R8_UART2_LSR & RB_LSR_TX_FIFO_EMP) == 0);
R8_UART2_THR = *(buf++);
#endif
}
return size;
}
/*********************************************************************
* @fn _sbrk
*
* @brief Change the spatial position of data segment.
*
* @return size: Data length
*/
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;
}
uint32_t SysTick_Now = 0, SysTick_Last = 0, SysTick_Diff = 0;
char Dbg_Buffer[100];
uint32_t Dbg_Switch = (DBG_OPT_ActCond_STATUS << DBG_BIT_ActCond_STATUS_bit) + \
(DBG_OPT_MQTT_STATUS << DBG_BIT_MQTT_STATUS_bit) + \
(DBG_OPT_Debug_STATUS << DBG_BIT_Debug_STATUS_bit) + \
(DBG_OPT_LOGIC_STATUS << DBG_BIT_LOGIC_STATUS_bit) + \
(DBG_OPT_DEVICE_STATUS << DBG_BIT_DEVICE_STATUS_bit) + \
(DBG_OPT_NET_STATUS << DBG_BIT_NET_STATUS_bit) + \
(DBG_OPT_SYS_STATUS << DBG_BIT_SYS_STATUS_bit);
//该方式会减少代码空间,待完成
void __putchar__ (char ch)
{
#if (DEBUG) == DEBUG_UART0
while ((R8_UART0_LSR & RB_LSR_TX_FIFO_EMP) == 0);
R8_UART0_THR = ch;
#elif (DEBUG) == DEBUG_UART1
while ((R8_UART1_LSR & RB_LSR_TX_FIFO_EMP) == 0);
R8_UART1_THR = ch;
#elif (DEBUG) == DEBUG_UART2
while ((R8_UART2_LSR & RB_LSR_TX_FIFO_EMP) == 0);
R8_UART2_THR = ch;
#elif (DEBUG) == DEBUG_UART3
while ((R8_UART3_LSR & RB_LSR_TX_FIFO_EMP) == 0);
R8_UART3_THR = ch;
#endif
}
/*******************************************************************************
* Function Name : Dbg_NoTick_Print
* Description : DEBUG调试信息输出 - 不带时间戳打印
* Input :
* Return :
*******************************************************************************/
__attribute__((section(".non_0_wait"))) void Dbg_NoTick_Print(int DbgOptBit ,const char *fmt, ...)
{
char ch;
va_list ap;
if (DBG_LOG_EN && (Dbg_Switch & (1 << DbgOptBit )))
{
va_start(ap, fmt);
while (*fmt) {
if (*fmt != '%') {
__putchar__(*fmt++);
continue;
}
switch (*++fmt) {
case 's':
{
char *str = va_arg(ap, char *);
printf("%s",str);
}
break;
case 'd':
{
int num = va_arg(ap, int);
printf("%d", num);
}
break;
case 'x':
case 'X':
{
int num = va_arg(ap, unsigned int);
printf("%x", num);
}
break;
// Add other specifiers here...
case 'c':
case 'C':
ch = (unsigned char)va_arg(ap, int);
printf("%c", ch);
break;
default:
__putchar__(*fmt);
break;
}
fmt++;
}
va_end(ap);
printf("\r\n");
}
}
__attribute__((section(".non_0_wait"))) void Dbg_Print(int DbgOptBit ,const char *fmt, ...)
{
char ch;
va_list ap;
if (DBG_LOG_EN && (Dbg_Switch & (1 << DbgOptBit )))
{
SysTick_Now = SysTick_1ms;
SysTick_Diff = SysTick_Now - SysTick_Last; //上一次打印时间差
SysTick_Last = SysTick_Now;
printf("%8d [%6d]: ",SysTick_Now,SysTick_Diff);
va_start(ap, fmt);
while (*fmt) {
if (*fmt != '%') {
__putchar__(*fmt++);
continue;
}
switch (*++fmt) {
case 's':
{
char *str = va_arg(ap, char *);
printf("%s",str);
}
break;
case 'd':
{
int num = va_arg(ap, int);
printf("%d", num);
}
break;
case 'x':
case 'X':
{
int num = va_arg(ap, unsigned int);
printf("%x", num);
}
break;
// Add other specifiers here...
case 'c':
case 'C':
ch = (unsigned char)va_arg(ap, int);
printf("%c", ch);
break;
default:
__putchar__(*fmt);
break;
}
fmt++;
}
va_end(ap);
}
}
__attribute__((section(".non_0_wait"))) void Dbg_Println(int DbgOptBit ,const char *fmt, ...)
{
char ch;
va_list ap;
if (DBG_LOG_EN && (Dbg_Switch & (1 << DbgOptBit )))
{
SysTick_Now = SysTick_1ms;
SysTick_Diff = SysTick_Now - SysTick_Last; //上一次打印时间差
SysTick_Last = SysTick_Now;
printf("%8d [%6d]: ",SysTick_Now,SysTick_Diff);
va_start(ap, fmt);
while (*fmt) {
if (*fmt != '%') {
__putchar__(*fmt++);
continue;
}
switch (*++fmt) {
case 's':
{
char *str = va_arg(ap, char *);
printf("%s",str);
}
break;
case 'd':
{
int num = va_arg(ap, int);
printf("%d", num);
}
break;
case 'x':
case 'X':
{
int num = va_arg(ap, unsigned int);
printf("%x", num);
}
break;
// Add other specifiers here...
case 'c':
case 'C':
ch = (unsigned char)va_arg(ap, int);
printf("%c", ch);
break;
default:
__putchar__(*fmt);
break;
}
fmt++;
}
va_end(ap);
printf("\r\n");
}
}
__attribute__((section(".non_0_wait"))) void Dbg_Print_Buff(int DbgOptBit ,const char *cmd ,uint8_t *buff,uint32_t len)
{
if (DBG_LOG_EN && (Dbg_Switch & (1 << DbgOptBit )))
{
SysTick_Now = SysTick_1ms;
SysTick_Diff = SysTick_Now - SysTick_Last; //上一次打印时间差
SysTick_Last = SysTick_Now;
DBG_Printf("%8d [%6d]: %s",SysTick_Now,SysTick_Diff,cmd);
for(uint32_t i=0;i<len;i++)
{
DBG_Printf("%02X ",buff[i]);
}
DBG_Printf("\n\r");
}
}