Files
kunlun/driver/src/hal/debug_uart.c
2024-09-28 14:24:04 +08:00

112 lines
2.9 KiB
C

/****************************************************************************
Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT
be copied by any method or incorporated into another program without
the express written consent of Aerospace C.Power. This Information or any portion
thereof remains the property of Aerospace C.Power. The Information contained herein
is believed to be accurate and Aerospace C.Power assumes no responsibility or
liability for its use in any way and conveys no license or title under
any patent or copyright and makes no representation or warranty that this
Information is free from patent or copyright infringement.
****************************************************************************/
/* os shim includes */
#include "os_types.h"
/* common includes */
#include "iot_config.h"
#include "iot_board.h"
#include "uart.h"
#include "strformat.h"
#include "iot_module.h"
#include "os_lock.h"
extern str_format_context log_ctxt;
extern struct uart_ctrl uart_e_ctrl;
uint8_t g_dbg_uart_port = 0;
uint32_t g_dbg_uart_baudrate = 0;
#define DEBUG_BAUD_RATE 3000000
uint32_t dbg_send_bytes_nolock(const uint8_t *seq, uint32_t len)
{
uint8_t c;
int i = 0;
while(i < len) {
c = *(seq+i);
if (c == '\n'){
g_uart_ctrl.try_putc(g_dbg_uart_port,'\r');
}
g_uart_ctrl.try_putc(g_dbg_uart_port,c);
i++;
}
return 0;
}
static StrFormatResult dbg_uart_early_printf(void *user_data, const char *data, unsigned int len)
{
if (len > 0) {
dbg_send_bytes_nolock((uint8_t*)data, len);
}
return STRFORMAT_OK;
}
void dbg1_uart_init()
{
g_uart_ctrl = uart_e_ctrl;
g_dbg_uart_port = UART_CLI_PORT;
g_uart_ctrl.init(UART_CLI_PORT);
log_ctxt.write_str = dbg_uart_early_printf;
}
void dbg_uart_init()
{
g_uart_ctrl = uart_e_ctrl;
g_dbg_uart_port = DEBUG_UART_PORT;
g_uart_ctrl.init(DEBUG_UART_PORT);
log_ctxt.write_str = dbg_uart_early_printf;
}
void dbg_uart_init_port(uint8_t port, uint8_t init)
{
g_uart_ctrl = uart_e_ctrl;
g_dbg_uart_port = port;
if(init){
g_uart_ctrl.init(port);
g_uart_ctrl.config(port, DEBUG_BAUD_RATE, UART_DATA_8_BITS, UART_STOP_BITS_1, UART_PARITY_DISABLE);
g_dbg_uart_baudrate = DEBUG_BAUD_RATE;
}
log_ctxt.write_str = dbg_uart_early_printf;
}
void dbg_putchar(const uint8_t ch)
{
dbg_send_bytes_nolock(&ch, 1);
}
void iot_dbg_uart_set_config(uint32_t baud, uint8_t parity, uint8_t data, uint8_t stop)
{
g_uart_ctrl.config(g_dbg_uart_port, baud, data, stop, parity);
g_dbg_uart_baudrate = baud;
}
void iot_dbg_uart_set_port(uint8_t port, uint32_t baud, uint8_t parity,
uint8_t data, uint8_t stop)
{
g_dbg_uart_port = port;
g_uart_ctrl.init(g_dbg_uart_port);
g_uart_ctrl.config(g_dbg_uart_port, baud, data, stop, parity);
}