You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

97 lines
2.8 KiB
C

#include "p_usart.h"
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include "lvgl/src/lv_misc/lv_log.h"
#include "p_tcc.h"
#define DEBUG_USART_EX_BUFF_SIZE 16
struct usart_async_descriptor p_usart_debug_inst;
static volatile uint32_t data_arrived = 0;
static volatile bool b_tx_complete = true;
/* static uint8_t debug_buffer[DEBUG_MAX_BUFFER_SIZE]; */
static uint8_t debug_rx_buff[DEBUG_USART_EX_BUFF_SIZE];
static void p_lv_log_cb(lv_log_level_t level, const char * file, uint32_t line, const char * desc)
{
PDEBUG("[LVGL][%s][%s][%s][%d] --\t%s\r\n", p_get_time_str(), level == LV_LOG_LEVEL_TRACE ? "TRACE" :
level == LV_LOG_LEVEL_INFO ? "INFO" : level == LV_LOG_LEVEL_WARN ? "WARN" :
level == LV_LOG_LEVEL_ERROR ? "ERROR" : "NONE", file, (int)line, desc);
}
// usart debug callbacks
static void debug_tx_cb(const struct usart_async_descriptor *const io_descr)
{
/* Transfer completed */
b_tx_complete = true;
}
static void debug_rx_cb(const struct usart_async_descriptor *const io_descr)
{
p_set_data_arrived(1);
}
static void debug_err_cb(const struct usart_async_descriptor *const io_descr)
{
/* Transfer completed */
}
void p_usart_init(void)
{
// clock init
hri_gclk_write_PCHCTRL_reg(GCLK, SERCOM0_GCLK_ID_CORE, CONF_GCLK_SERCOM0_CORE_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));
hri_gclk_write_PCHCTRL_reg(GCLK, SERCOM0_GCLK_ID_SLOW, CONF_GCLK_SERCOM0_SLOW_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));
hri_mclk_set_APBAMASK_SERCOM0_bit(MCLK);
usart_async_init(&p_usart_debug_inst, USART_DEBUG_SERCOM, debug_rx_buff, DEBUG_USART_EX_BUFF_SIZE, (void*)NULL);
// port init
gpio_set_pin_function(USART_DEBUG_RX, USART_DEBUG_RX_MUX);
gpio_set_pin_function(USART_DEBUG_TX, USART_DEBUG_TX_MUX);
usart_async_register_callback(&p_usart_debug_inst, USART_ASYNC_TXC_CB, debug_tx_cb);
usart_async_register_callback(&p_usart_debug_inst, USART_ASYNC_RXC_CB, debug_rx_cb);
usart_async_register_callback(&p_usart_debug_inst, USART_ASYNC_ERROR_CB, debug_err_cb);
usart_async_enable(&p_usart_debug_inst);
lv_log_register_print_cb((lv_log_print_g_cb_t)p_lv_log_cb);
}
void p_write(struct usart_async_descriptor* const inst, const uint8_t* data, uint16_t len)
{
b_tx_complete = false;
io_write(&inst->io, data, len);
while(!b_tx_complete){}
}
int pprintf(const char* str, ...)
{
size_t size_str = strlen(str);
if (size_str >= DEBUG_MAX_BUFFER_SIZE)
{
return -1;
}
uint8_t printf_buffer[DEBUG_MAX_BUFFER_SIZE];
memset(printf_buffer, '\0', DEBUG_MAX_BUFFER_SIZE);
va_list args;
va_start(args, str);
vsprintf((char*)printf_buffer, str, args);
va_end(args);
p_write(&p_usart_debug_inst, (const uint8_t*)printf_buffer, strlen((const char*)printf_buffer));
return 0;
}
bool p_data_arrived(void)
{
return data_arrived;
}
void p_set_data_arrived(bool status)
{
data_arrived = status;
}