/**************************************************************************//**
 * @item     CosyOS-III 例程（系统构建）
 * @file     demo_debug_hal.c
 * @brief    例程-DEBUG for STM32 use HAL Driver
 * @author   迟凯峰
 * @version  V1.0.2
 * @date     2026.06.28
 ******************************************************************************/

#if 1 /* 编译开关 */ \
 && defined (USE_HAL_DRIVER)

#include "CosyOS.H"
#if (SYSCFG_DEBUGGING == __ENABLED__)

/*
 * 用户定义
 */

/*-------------------------------------------------------------*
 | DEBUG_MODE | 输出（UART SEND）| 输入（UART RECV）| 推荐等级 |
 |------------+------------------+------------------+----------|
 | 0          | printf（查询）   | 串口接收中断     | *        |
 |------------+------------------+------------------+----------|
 | 1          | 串口发送中断     | 串口接收中断     | ***      |
 |------------+------------------+------------------+----------|
 | 2          | DMA串口发送中断  | DMA串口接收中断  | *****    |
 *-------------------------------------------------------------*/
#define DEBUG_MODE          0

/* DEBUG RECV 缓存SIZE */
#define DEBUG_RECV_BUFF     32

////////////////////////////////////////////////////////////////////////////////

// 1、例程中使用了UART1做为DEBUG专用串口，用户如需更换其它串口请自行调整。
// 2、下方的DEBUG配置函数 Debug_Config()，不能够取代基本的串口初始化工作，
//    用户在main函数中，在调用该函数之前，需先行初始化串口。

////////////////////////////////////////////////////////////////////////////////

extern UART_HandleTypeDef huart1;
static char  u_debug_recv_buff[DEBUG_RECV_BUFF];
static char *u_debug_recv_p = u_debug_recv_buff;
#if (DEBUG_MODE == 0)
#include <stdio.h>
#endif

void Debug_Config(void)
{
    #if   (DEBUG_MODE == 0)
    HAL_UART_Receive_IT (&huart1, (uint8_t *)u_debug_recv_p, 1);
    
    #elif (DEBUG_MODE == 1)
    HAL_UART_Receive_IT (&huart1, (uint8_t *)u_debug_recv_p, 1);
    
    #elif (DEBUG_MODE == 2)
    HAL_UART_Receive_DMA(&huart1, (uint8_t *)u_debug_recv_p, 1);
    #endif
}

#if (SYSCFG_DEBUG_SENDLEN == __DISABLED__)
void debug_hook(char _XDATA_MEM_ *p)
#else
void debug_hook(char _XDATA_MEM_ *p, s_u16_t len)
#endif
{
    #if   (DEBUG_MODE == 0)
    printf("%s", p);
    xDebugSend_Complete();
    
    #elif (DEBUG_MODE == 1)
    HAL_UART_Transmit_IT (&huart1, (uint8_t *)p, len);
    
    #elif (DEBUG_MODE == 2)
    HAL_UART_Transmit_DMA(&huart1, (uint8_t *)p, len);
    #endif
}

#if (DEBUG_MODE >= 1)
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
    if(huart->Instance == USART1){
        xDebugSend_Complete();
    }
}
#endif

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    if(huart->Instance == USART1){
        if(u_debug_recv_p - u_debug_recv_buff < DEBUG_RECV_BUFF - 1){
            if(*u_debug_recv_p++ == '\n'){
                xDebugRecv_Handler(u_debug_recv_buff, u_debug_recv_p - u_debug_recv_buff);
                u_debug_recv_p = u_debug_recv_buff;
            }
        }
        else{
            u_debug_recv_p = u_debug_recv_buff;
        }
        #if   (DEBUG_MODE == 0)
        HAL_UART_Receive_IT (&huart1, (uint8_t *)u_debug_recv_p, 1);
        
        #elif (DEBUG_MODE == 1)
        HAL_UART_Receive_IT (&huart1, (uint8_t *)u_debug_recv_p, 1);
        
        #elif (DEBUG_MODE == 2)
        HAL_UART_Receive_DMA(&huart1, (uint8_t *)u_debug_recv_p, 1);
        #endif
    }
}

#endif

#endif
