Files
HBshuiwuConcentrator/APP/debug_printf.c
2025-12-15 16:07:49 +08:00

895 lines
23 KiB
C

#include "include.h"
#include "uart.h"
#include <math.h>
#include "debug_printf.h"
#include "Cmd.h"
//信号量 队列 互斥信号量 定时器 进程同步 数据保护
void EdbugPrintProcess(u8* Pstr,u16 len);
#define PEINTFBUFF_LENGTH (800)
#define COM_DEBUG_BUF g_DebugRxBuffer
u8 g_DebugRxBuffer[COM_DEBUG_RX_BUFFER_SIZE];
struct st_uart_port COM_DEBUG_port = USART_PORT_PARAMS(COM_DEBUG, g_DebugRxBuffer,NULL);
//static QueueHandle_t printQueue;
static SemaphoreHandle_t xSemaphore;
static bool printf_switch_open = TRUE;
TaskHandle_t print_task_handle;
u16 head_index = 0;
u16 tail_index = 0;
u8 print_buf[Q_PRINT_LENGTH][Q_PRINT_SIZE + 1] = {0};
bool SendRuning = false;
u16 EdbugPrintBuffLength = 0x00;
u16 EdbugPrintBuffIndex = 0x00;
u16 EdbugPrintBuffRxLength = 0x00;
u16 EdbugPrintBuffRxIndex = 0x00;
u8 EdbugPrintBuff[PEINTFBUFF_LENGTH];
/*****************************************************************************
* Function : close_printf
* Description : none
* Input : void
* Output : None
* Return :
* Others :
* Record
* 1.Date : 20170314
* Author : barry
* Modification: Created function
*****************************************************************************/
void close_printf(void)
{
printf_switch_open = FALSE;
head_index = 0;
tail_index = 0;
}
/*****************************************************************************
* Function : open_printf
* Description : none
* Input : void
* Output : None
* Return :
* Others :
* Record
* 1.Date : 20170314
* Author : barry
* Modification: Created function
*****************************************************************************/
void open_printf(void)
{
printf_switch_open = TRUE;
head_index = 0;
tail_index = 0;
}
/*****************************************************************************
* Function : debug_Tx
* Description : none
* Input : u8 *buf
u8 len
* Output : None
* Return :
* Others :
* Record
* 1.Date : 20170314
* Author : barry
* Modification: Created function
*****************************************************************************/
void debug_Tx(u8 *buf, u8 len)
{
//hal_UartDMATx(&COM_DEBUG_port, buf, len);
EdbugPrintProcess(buf, len);
}
/*****************************************************************************
* Function : reset_debug_buf
* Description : none
* Input : void
* Output : None
* Return :
* Others :
* Record
* 1.Date : 20170314
* Author : barry
* Modification: Created function
*****************************************************************************/
void reset_debug_buf(void)
{
MemSet(COM_DEBUG_port.data.rxBuf, 0, sizeof(g_DebugRxBuffer));
COM_DEBUG_port.data.rx_len = 0;
}
/*****************************************************************************
* Function : get_debugBuf_len
* Description : none
* Input : void
* Output : None
* Return :
* Others :
* Record
* 1.Date : 20170314
* Author : barry
* Modification: Created function
*****************************************************************************/
u16 get_debugBuf_len(void)
{
return COM_DEBUG_port.data.rx_len;
}
/*****************************************************************************
* Function : get_debugBuf
* Description : none
* Input : void
* Output : None
* Return : u8
* Others :
* Record
* 1.Date : 20170314
* Author : barry
* Modification: Created function
*****************************************************************************/
u8 * get_debugBuf(void)
{
return COM_DEBUG_port.data.rxBuf;
}
void add_print_buf(u8*buf, u8 length)
{
u8 print_size;
u8 index = 0;
u8 valid_blockNum;
u8 blockNum = (length % Q_PRINT_SIZE == 0)? (length / Q_PRINT_SIZE): (length/ Q_PRINT_SIZE + 1);
if (head_index >= tail_index)
{
if (head_index == tail_index)
{
head_index = 0;
tail_index = 0;
}
valid_blockNum = Q_PRINT_LENGTH - head_index + tail_index;
blockNum = (blockNum >= valid_blockNum) ? valid_blockNum : blockNum;
}
else
{
valid_blockNum = tail_index - head_index;
blockNum = (blockNum >= valid_blockNum) ? valid_blockNum : blockNum;
}
for (u8 i = 0; i < blockNum; i++)
{
if (i == blockNum - 1)
{
print_size = (length % Q_PRINT_SIZE == 0)? Q_PRINT_SIZE: (length % Q_PRINT_SIZE);
}
else
{
print_size = Q_PRINT_SIZE;
}
print_buf[head_index][0] = print_size;
MemCpy(&print_buf[head_index][1], buf + index, print_size);
index += print_size;
head_index = (head_index + 1) % Q_PRINT_LENGTH;
}
}
void UART5_IRQHandler(void)
{
if (USART_GetITStatus(COM_DEBUG_NO, USART_IT_TC) != RESET)
{
if( EdbugPrintBuffIndex >= EdbugPrintBuffLength)
{
USART_ITConfig(COM_DEBUG_NO, USART_IT_TC, DISABLE);
EdbugPrintBuffIndex = 0;
SendRuning = false;
}
else
{
USART_SendData(COM_DEBUG_NO, EdbugPrintBuff[EdbugPrintBuffIndex]);
EdbugPrintBuffIndex ++;
}
}
else if (USART_GetITStatus(COM_DEBUG_NO, USART_IT_RXNE) != RESET)
{
u8 transparent_cmd[] = "transparent 00 ";
u16 len;
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
if (USART_GetITStatus(COM_DEBUG_NO, USART_IT_RXNE) != RESET)
{
u8 receivedByte = USART_ReceiveData(COM_DEBUG_NO);
if ( COM_DEBUG_port.data.rx_index >= COM_DEBUG_RX_BUFFER_SIZE)
{
COM_DEBUG_port.data.rx_index = 0;
}
if (receivedByte == '\b')//Backspace
{
if ( cmp_datas(transparent_cmd, COM_DEBUG_port.data.rxBuf, 11) )
{
COM_DEBUG_port.data.rxBuf[COM_DEBUG_port.data.rx_index++] = receivedByte;
}
else
{
if (COM_DEBUG_port.data.rx_index > 0)
{
COM_DEBUG_port.data.rx_index--;
}
}
}
else
{
COM_DEBUG_port.data.rxBuf[COM_DEBUG_port.data.rx_index ++] = receivedByte;
if ((receivedByte == '\r') || (receivedByte == '\n'))//Enter
{
if ( cmp_datas(transparent_cmd, COM_DEBUG_port.data.rxBuf, 11) )
{
len = COM_DEBUG_port.data.rxBuf[12] + COM_DEBUG_port.data.rxBuf[13]*256 + sizeof(transparent_cmd) - 1;
if (COM_DEBUG_port.data.rx_index < len)
{
return;
}
}
COM_DEBUG_port.data.rx_len = COM_DEBUG_port.data.rx_index;
COM_DEBUG_port.data.rx_index = 0;
xSemaphoreGiveFromISR( COM_DEBUG_port.xSemaphore, &xHigherPriorityTaskWoken );
portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
}
}
}
}
}
void WaitPrintfEnd( void )
{
if(SendRuning == true)
{
for(u8 tc = 0x0; ( tc < 20 ) & ( SendRuning == true) ; tc ++)
{
vTaskDelay(20 /portTICK_RATE_MS);
}
}
}
void EdbugPrintProcess(u8* Pstr,u16 len)
{
if(len > PEINTFBUFF_LENGTH) return ;
WaitPrintfEnd();
MemCpy(EdbugPrintBuff, Pstr, len);
EdbugPrintBuffLength = len;
EdbugPrintBuffIndex = 0x00;
while(USART_GetITStatus(COM_DEBUG_NO, USART_IT_TC) != RESET);
USART_ITConfig(COM_DEBUG_NO, USART_IT_TC, ENABLE);
}
/**
* @brief Retargets the C library printf function to the DEBUG_COM.
* @param None.
* @retval None.
*/
int printf(const char *format ,... )
{
u16 receData = 0;
// if (printf_switch_open)
// {
// va_list arg;
// u16 receNum = 0;
// static u8 buffer[100];
//
// va_start(arg, format);
// receNum = (u16)vsnprintf((char *)(buffer), sizeof(buffer), format, arg);
// va_end(arg);
//
// receData = receNum;
// if ((receNum > 0) && (receNum <= sizeof(buffer)))
// {
// add_print_buf(buffer, receNum);
//
// if (COM_DEBUG_port.data.tx_timeout == 0)
// {
// COM_DEBUG_port.data.tx_timeout = 1;
// hal_UartDMATx(&COM_DEBUG_port,&print_buf[tail_index][1], print_buf[tail_index][0]);
// }
// }
// }
if (printf_switch_open)
{
va_list arg;
u16 receNum = 0;
//static u8 buffer[100];
WaitPrintfEnd();
va_start(arg, format);
receNum = (u16)vsnprintf((char *)(EdbugPrintBuff), sizeof(EdbugPrintBuff), format, arg);
va_end(arg);
SendRuning = true;
EdbugPrintBuffLength = receNum;
EdbugPrintBuffIndex = 0x00;
while(USART_GetITStatus(COM_DEBUG_NO, USART_IT_TC) != RESET);
USART_ITConfig(COM_DEBUG_NO, USART_IT_TC, ENABLE);
}
return receData;
}
// memcpy(RS485_tx_buf, buf, length);
// COM_noDMA_tx_index = 0;
// COM_noDMA_len = length;
// COM_485_port.data.tx_timeout = xTaskGetTickCount() + (RS485_BAUD_COFF*length + 100)/portTICK_RATE_MS;;
// while(USART_GetITStatus(COM_RADIO_NO, USART_IT_TC) != RESET);
// USART_ITConfig(COM_RADIO_NO, USART_IT_TC, ENABLE);
/*****************************************************************************
* Function : printf_buf
* Description : none
* Input : u8* buf
u16 length
* Output : None
* Return :
* Others :
* Record
* 1.Date : 20170306
* Author : barry
* Modification: Created function
*****************************************************************************/
void printf_buf(u8* buf, u16 length)
{
// static u8 temp_buf[Q_PRINT_SIZE];
// u8 temp_count = 0;
//
// u8 max_pos = (sizeof(temp_buf) - 1) - (sizeof(temp_buf) - 1) % 3;
//
// if (Q_PRINT_SIZE < 4)
// {
// return;
// }
//
// MemSet(temp_buf, 0, Q_PRINT_SIZE);
//
// for (u8 i = 0; i < length; i++)
// {
// temp_buf[temp_count++] = ((buf[i] / 0x10) <= 9)? (buf[i] / 0x10) + '0': (buf[i] / 0x10) + 'A' - 10;
// temp_buf[temp_count++] = ((buf[i] % 0x10) <= 9)? (buf[i] % 0x10) + '0': (buf[i] % 0x10) + 'A' - 10;
// temp_buf[temp_count++] = ' ';
//
// if ( (temp_count >= max_pos ) || (i == length - 1))
// {
// add_print_buf(temp_buf, temp_count);
// temp_count = 0;
// MemSet(temp_buf, 0, Q_PRINT_SIZE);
// }
// }
//
// if (head_index != tail_index)
// {
// printf("\r\n");
// }
if(length >= 255 ) return ;
u16 index = 0x00;
u16 Printflength = 0x00;
//printf("%s"," ");
WaitPrintfEnd();
for(index = 0x00; index < length; index ++)
{
Printflength += sprintf((char*)&EdbugPrintBuff[Printflength],"%02X ",buf[index]);
}
Printflength += sprintf((char*)&EdbugPrintBuff[Printflength],"%s ","\r\n");
// EdbugPrintBuffLength = Printflength;
//
// EdbugPrintBuffIndex = 0x00;
//
// while(USART_GetITStatus(COM_DEBUG_NO, USART_IT_TC) != RESET);
//
// USART_ITConfig(COM_DEBUG_NO, USART_IT_TC, ENABLE);
printf("%s",EdbugPrintBuff);
}
/*****************************************************************************
* Function : vPrintf_task
* Description : none
* Input : void * ptr
* Output : None
* Return :
* Others :
* Record
* 1.Date : 20170314
* Author : barry
* Modification: Created function
*****************************************************************************/
void vPrintf_task(void * ptr)
{
for (;;)
{
vTaskDelay(pdMS_TO_TICKS(100));
while (SendRuning == true)
{
if (xSemaphoreTake( xSemaphore, 1000 ) == pdFALSE)
{
hal_InitCOM(&COM_DEBUG_port);
// COM_DEBUG_port.data.rx_index = 0;
// COM_DEBUG_port.data.rx_len = 0;
// COM_DEBUG_port.data.tx_timeout = 0;
//
SendRuning = false;
EdbugPrintBuffLength = 0x00;
EdbugPrintBuffIndex = 0x00;
}
}
}
}
/*****************************************************************************
* Function : vPrintf_rx_task
* Description : none
* Input : void * ptr
* Output : None
* Return :
* Others :
* Record
* 1.Date : 20170314
* Author : barry
* Modification: Created function
*****************************************************************************/
void vPrintf_rx_task(void * ptr)
{
for (;;)
{
if (xSemaphoreTake( COM_DEBUG_port.xSemaphore, portMAX_DELAY ) == pdPASS)
{
Cmd_Proc();
reset_debug_buf();
}
}
}
/*****************************************************************************
* Function : task_print_start
* Description : none
* Input : void
* Output : None
* Return :
* Others :
* Record
* 1.Date : 20170314
* Author : barry
* Modification: Created function
*****************************************************************************/
void task_print_start(void)
{
vSemaphoreCreateBinary( xSemaphore );
xQueueReset( (QueueHandle_t)xSemaphore);
init_uart_port(&COM_DEBUG_port);
xTaskCreate( vPrintf_task, "Task printf", 150, NULL, 4, &print_task_handle );
xTaskCreate( vPrintf_rx_task, "Task rx printf", 150, NULL, 2, NULL );
printf("hb app start 2021-12-14\r\n");
}
#if 0
///*****************************************************************************
// * Function : printf
// * Description : none
// * Input : const char *format
// ...
// * Output : None
// * Return :
// * Others :
// * Record
// * 1.Date : 20170314
// * Author : barry
// * Modification: Created function
//
//*****************************************************************************/
//int printf(const char *format ,... )
//{
// static va_list arg;
// static u16 receNum = 0;
// static u16 receData = 0;
// static u8 blockNum = 0;
// static u8 buffer[PRINT_MAX_BUF];
// static u8 print_buf[ Q_PRINT_SIZE + 1];
// static u8 index = 0;
// static u8 print_size;
//
// if (printf_switch_open)
// {
// MemSet(buffer, 0, sizeof(buffer));
// va_start(arg, format);
// receNum = (u16)vsnprintf((char *)(buffer), sizeof(buffer), format, arg);
// va_end(arg);
//
// index = 0;
//
// if ((receNum > 0) && (receNum <= PRINT_MAX_BUF))
// {
// blockNum = (receNum % Q_PRINT_SIZE == 0)? (receNum / Q_PRINT_SIZE): (receNum / Q_PRINT_SIZE + 1);
//
// taskENTER_CRITICAL(); //保护临界区代码,不被其他的任务在数据打印过程中调用
//
// for (u8 i = 0; i < blockNum; i++)
// {
// if (i == blockNum - 1)
// {
// print_size = (receNum % Q_PRINT_SIZE == 0)? Q_PRINT_SIZE: receNum % Q_PRINT_SIZE;
// }
// else
// {
// print_size = Q_PRINT_SIZE;
// }
// print_buf[0] = print_size;
// MemCpy(print_buf + 1, buffer + index, Q_PRINT_SIZE);
// index += print_size;
// xQueueSendToBack( printQueue, print_buf, 0 );
// }
//
// taskEXIT_CRITICAL();
// }
// }
//
// return receData;
// }
//
///*****************************************************************************
// * Function : printf_buf
// * Description : none
// * Input : u8* buf
// u16 length
// * Output : None
// * Return :
// * Others :
// * Record
// * 1.Date : 20170314
// * Author : barry
// * Modification: Created function
//
//*****************************************************************************/
//void printf_buf(u8* buf, u16 length)
//{
// static u8 temp_buf[Q_PRINT_SIZE];
// u8 temp_count = 0;
//
// u8 max_pos = (sizeof(temp_buf) - 1) - (sizeof(temp_buf) - 1) % 3;
//
// if (Q_PRINT_SIZE < 4)
// {
// return;
// }
//
// MemSet(temp_buf, 0, Q_PRINT_SIZE);
//
// taskENTER_CRITICAL();
//
// for (u8 i = 0; i < length; i++)
// {
// temp_buf[temp_count++] = ((buf[i] / 0x10) <= 9)? (buf[i] / 0x10) + '0': (buf[i] / 0x10) + 'a' - 10;
// temp_buf[temp_count++] = ((buf[i] % 0x10) <= 9)? (buf[i] % 0x10) + '0': (buf[i] % 0x10) + 'a' - 10;
// temp_buf[temp_count++] = ' ';
//
// if ( (temp_count >= max_pos ) || (i == length - 1))
// {
// temp_count = 0;
// printf("%s", temp_buf);
// MemSet(temp_buf, 0, Q_PRINT_SIZE);
// }
// }
//
// printf("\r\n");
//
// taskEXIT_CRITICAL();
//}
//
///*****************************************************************************
// * Function : vPrintf_task
// * Description : none
// * Input : void * ptr
// * Output : None
// * Return :
// * Others :
// * Record
// * 1.Date : 20170314
// * Author : barry
// * Modification: Created function
//
//*****************************************************************************/
//void vPrintf_task(void * ptr)
//{
// u8 buf[Q_PRINT_SIZE+ 1];
//
// for (;;)
// {
// if( xQueueReceive( printQueue, buf, portMAX_DELAY ) == pdPASS )
// {
// hal_UartDMATx(&COM_DEBUG_port, buf + 1, buf[0]);
// if (xSemaphoreTake( xSemaphore, 1000 ) == pdFALSE)
// {
// hal_InitCOM(&COM_DEBUG_port);
// COM_DEBUG_port.data.rx_index = 0;
// COM_DEBUG_port.data.rx_len = 0;
// }
// }
// }
//}
//
// /*****************************************************************************
// * Function : vPrintf_rx_task
// * Description : none
// * Input : void * ptr
// * Output : None
// * Return :
// * Others :
// * Record
// * 1.Date : 20170314
// * Author : barry
// * Modification: Created function
//
//*****************************************************************************/
//void vPrintf_rx_task(void * ptr)
//{
// for (;;)
// {
// if (xSemaphoreTake( COM_DEBUG_port.xSemaphore, portMAX_DELAY ) == pdPASS)
// {
// Cmd_Proc();
// reset_debug_buf();
// }
// }
//}
// /*****************************************************************************
// * Function : task_print_start
// * Description : none
// * Input : void
// * Output : None
// * Return :
// * Others :
// * Record
// * 1.Date : 20170314
// * Author : barry
// * Modification: Created function
//
// *****************************************************************************/
// void task_print_start(void)
// {
// hal_InitCOM(&COM_DEBUG_port);
//
// printQueue = xQueueCreate(Q_PRINT_LENGTH, Q_PRINT_SIZE + 1); // Q_PRINT_LENGTH 队列的条目数 Q_PRINT_SIZE 每个条目占多少字节
// vSemaphoreCreateBinary( xSemaphore );
// xQueueReset( (QueueHandle_t)xSemaphore);
//
// init_uart_port(&COM_DEBUG_port);
// xTaskCreate( vPrintf_task, "Task printf", 150, NULL, 4, &print_task_handle );
// xTaskCreate( vPrintf_rx_task, "Task rx printf", 150, NULL, 2, NULL );
//
// printf("app start\r\n");
// }
#endif
/*****************************************************************************
* Function : COM_DEBUG_TX_IRQHandler
* Description : none
* Input : void
* Output : None
* Return :
* Others :
* Record
* 1.Date : 20170314
* Author : barry
* Modification: Created function
*****************************************************************************/
//
//void COM_DEBUG_TX_IRQHandler(void)
//{
// BaseType_t xHigherPriorityTaskWoken = pdFALSE;
//
// if (DMA_GetITStatus(COM_DEBUG_TX_DMA_COMPLETE) != RESET)
// {
// DMA_ClearITPendingBit(COM_DEBUG_TX_DMA_COMPLETE);
//
// tail_index = (tail_index + 1) % Q_PRINT_LENGTH;
// if (tail_index != head_index)
// {
// hal_UartDMATx(&COM_DEBUG_port,&print_buf[tail_index][1], print_buf[tail_index][0]);
// }
// else
// {
// COM_DEBUG_port.data.tx_timeout = 0;
// }
// xSemaphoreGiveFromISR( xSemaphore, &xHigherPriorityTaskWoken );
// portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
// }
// else if (DMA_GetITStatus(COM_DEBUG_TX_DMA_ERROR) != RESET)
// {
// DMA_ClearITPendingBit(COM_DEBUG_TX_DMA_ERROR);
// }
//}
/*****************************************************************************
* Function : COM_DEBUG_RX_IRQHandler
* Description : none
* Input : void
* Output : None
* Return :
* Others :
* Record
* 1.Date : 20170314
* Author : barry
* Modification: Created function
*****************************************************************************/
//void COM_DEBUG_RX_IRQHandler(void)
//{
// u8 transparent_cmd[] = "transparent 00 ";
//
// u16 len;
//
// BaseType_t xHigherPriorityTaskWoken = pdFALSE;
//
// if (USART_GetITStatus(COM_DEBUG_NO, USART_IT_RXNE) != RESET)
// {
// u8 receivedByte = USART_ReceiveData(COM_DEBUG_NO);
//
// if ( COM_DEBUG_port.data.rx_index >= COM_DEBUG_RX_BUFFER_SIZE)
// {
// COM_DEBUG_port.data.rx_index = 0;
// }
//
// if (receivedByte == '\b')//Backspace
// {
// if ( cmp_datas(transparent_cmd, COM_DEBUG_port.data.rxBuf, 11) )
// {
// COM_DEBUG_port.data.rxBuf[COM_DEBUG_port.data.rx_index++] = receivedByte;
// }
// else
// {
// if (COM_DEBUG_port.data.rx_index > 0)
// {
// COM_DEBUG_port.data.rx_index--;
// }
// }
// }
// else
// {
// COM_DEBUG_port.data.rxBuf[COM_DEBUG_port.data.rx_index ++] = receivedByte;
//
// if ((receivedByte == '\r') || (receivedByte == '\n'))//Enter
// {
// if ( cmp_datas(transparent_cmd, COM_DEBUG_port.data.rxBuf, 11) )
// {
// len = COM_DEBUG_port.data.rxBuf[12] + COM_DEBUG_port.data.rxBuf[13]*256 + sizeof(transparent_cmd) - 1;
//
// if (COM_DEBUG_port.data.rx_index < len)
// {
// return;
// }
// }
//
// COM_DEBUG_port.data.rx_len = COM_DEBUG_port.data.rx_index;
// COM_DEBUG_port.data.rx_index = 0;
//
// xSemaphoreGiveFromISR( COM_DEBUG_port.xSemaphore, &xHigherPriorityTaskWoken );
// portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
// }
// }
// }
//}