Files
RCU_C1P_Launcher/MCU_Driver/debug.c

374 lines
8.4 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>
#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)
{
/*<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 = 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<n;i++){
for(uint32_t j=0;j<30;j++){
__NOP();
WDT_Feed();
}
}
}
/*********************************************************************
* @fn Delay_Ms
*
* @brief Millisecond Delay Time.
*
* @param n - Millisecond number.
*
* @return None
*/
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++);
#elif (DEBUG) == DEBUG_UART3
while ((R8_UART3_LSR & RB_LSR_TX_FIFO_EMP) == 0);
R8_UART3_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 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, ...)
{
char ch;
va_list ap;
if ( (DBG_LOG_EN & DBG_BIT_SYS_STATUS) != 0x00 )
{
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) {
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");
}
}
/*******************************************************************************
* Function Name : Dbg_Print_Buff
* Description : DEBUG<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> - <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Buff<EFBFBD>е<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* 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; //<2F><>һ<EFBFBD>δ<EFBFBD>ӡʱ<D3A1><CAB1><EFBFBD><EFBFBD>
SysTick_Last = SysTick_Now;
printf("%8d [%6d]: %s",SysTick_Now,SysTick_Diff,cmd);
for(uint32_t i=0;i<len;i++)
{
WDT_Feed();
printf("%02X ",buff[i]);
}
printf("\r\n");
}
#endif
}