/* * debug.c * * Created on: May 14, 2025 * Author: cc */ #include "debug.h" #include #include #include #include "watchdog.h" volatile uint32_t SysTick_100us = 0; volatile uint32_t SysTick_1ms = 0; volatile uint32_t SysTick_1s = 0; 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 = 0; SysTick_1s++; } } } /********************************************************************* * @fn Delay_Us * * @brief Microsecond Delay Time. * * @param n - Microsecond number. * * @return None */ void Delay_Us(uint32_t n) { for(uint32_t i=0;i _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 1 #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 #else //select debug serial Pane volatile unsigned int *pdata = (unsigned int *)LDCC_DATA_P; while (*pdata & LDCC_BIT_STATUS); //Waiting for data read. *pdata = ch; #endif } int *myitoa(int value, int* string, int radix) { int tmp[33]; int* tp = tmp; int i; unsigned v; int sign; int* sp; if (radix > 36 || radix <= 1) { return 0; } sign = (radix == 10 && value < 0); if (sign) v = -value; else v = (unsigned)value; while (v || tp == tmp) { i = v % radix; v = v / radix; if (i < 10) { *tp++ = i+'0'; } else { *tp++ = i + 'a' - 10; } } sp = string; if (sign) *sp++ = '-'; while (tp > tmp) *sp++ = *--tp; *sp = 0; return string; } void my_printf(const char *fmt, ...) { const int *s; int d; char ch, *pbuf; int buf[16]; va_list ap; va_start(ap, fmt); while (*fmt) { if (*fmt != '%') { __putchar__(*fmt++); continue; } switch (*++fmt) { case 's': s = va_arg(ap, const int *); for ( ; *s; s++) { __putchar__(*s); } break; case 'd': d = va_arg(ap, int); myitoa(d, buf, 10); for (s = buf; *s; s++) { __putchar__(*s); } break; case 'x': case 'X': d = va_arg(ap, int); myitoa(d, buf, 16); for (s = buf; *s; s++) { __putchar__(*s); } break; // Add other specifiers here... case 'c': case 'C': ch = (unsigned char)va_arg(ap, int); pbuf = &ch; __putchar__(*pbuf); break; default: __putchar__(*fmt); break; } fmt++; } va_end(ap); } void Dbg_Println(int DbgOptBit ,const char *fmt, ...) { #if DBG_LOG_EN char ch; va_list ap; if ( (Dbg_Switch & DbgOptBit ) != 0x00 ) { 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) { WDT_Feed(); 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"); } #endif } /******************************************************************************* * Function Name : Dbg_Print_Buff * Description : DEBUG调试信息输出 - 输出Buff中的数据内容 * Input : * Return : *******************************************************************************/ void Dbg_Print_Buff(int DbgOptBit ,const char *cmd ,uint8_t *buff,uint32_t len) { #if DBG_LOG_EN if ( (Dbg_Switch & DbgOptBit ) != 0x00 ) { SysTick_Now = SysTick_1ms; SysTick_Diff = SysTick_Now - SysTick_Last; //上一次打印时间差 SysTick_Last = SysTick_Now; printf("%8d [%6d]: %s",SysTick_Now,SysTick_Diff,cmd); for(uint32_t i=0;i