Files
RCU_C1P_Module/MCU_Driver/debug.c

364 lines
9.2 KiB
C
Raw Normal View History

/*
* 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)
{
/*<2A><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD>ȼ<EFBFBD>*/
NVIC_SetPriority(SysTick_IRQn, 0x00);
NVIC_EnableIRQ(SysTick_IRQn);
/*<2A><><EFBFBD>ö<EFBFBD>ʱ<EFBFBD><CAB1>*/
SysTick->CTLR= 0;
SysTick->SR = 0;
SysTick->CNT = 0;
SysTick->CMP = SystemCoreClock/10000;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1000<30><30><EFBFBD><EFBFBD>1000HZ(<28>Ǿ<EFBFBD><C7BE><EFBFBD>1ms<6D><73>һ<EFBFBD><D2BB><EFBFBD>ж<EFBFBD>)
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; //<2F><><EFBFBD><EFBFBD><EFBFBD>жϱ<D0B6>־
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);
//<2F>÷<EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD>ٴ<EFBFBD><D9B4><EFBFBD><EFBFBD>ռ<D5BC><E4A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> - <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ
* 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; //<2F><>һ<EFBFBD>δ<EFBFBD>ӡʱ<D3A1><CAB1><EFBFBD><EFBFBD>
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; //<2F><>һ<EFBFBD>δ<EFBFBD>ӡʱ<D3A1><CAB1><EFBFBD><EFBFBD>
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; //<2F><>һ<EFBFBD>δ<EFBFBD>ӡʱ<D3A1><CAB1><EFBFBD><EFBFBD>
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");
}
}