内容简介
本文利用星闪BS25开发板NL001测试一下官方sdk中的外设程序uart,以便熟悉外设的基本操作。
测试工具
Operating system:Windows 10 home
deveco-device-tool-all-in-one:1.1.1_beta2
DevTools_CFBB:1.0.5
Compiler:CFBB IDE 1.0.3
Sdk:20230928
BurnTool:5.0.34
DebugKits_CFBB:3.0.98
Python:3.8.10
VSCode:1.66.2
Here we go
首先用“python build.py standard-bs25-app-evb menuconfig”配置工程为uart:
这里配置为interrupt方式。
编译后烧录到板子上测试,默认程序就能跑起来。首先保持烧录口(AP15和AP14)连接到电脑的UART口,打开串口调试助手查看log,上电后如下:
尝试在串口发送123后返回数据:
明显默认程序运行正常,并且收到的数据也正常,默认程序是通过A_P15(TXD)、A_P14(RXD)与串口调试助手连接的,参数为115200、8、1、N。
想要自己整一下代码,把AP12(tx)、AP13(rx)两个引脚注册到UART1上面,然后uart1收到数据后再重新在uart1上发送出去,uart0(AP15 TX, AP14 RX)继续保留打印系统log的功能(该部分在os其他地方初始化了,并不是在uart_demo.c中初始化的)。
整了一下代码:
#include "pinctrl.h"
#include "uart.h"
#include "watchdog.h"
#include "osal_debug.h"
#include "cmsis_os2.h"
#include "app_init.h"
#define UART_BAUDRATE115200
#define UART_DATA_BITS3 //3实际上是对应了UART_DATA_BIT_8,见uart_data_bit_t
#define UART_STOP_BITS1
#define UART_PARITY_BIT0 //UART_PARITY_NONE,见uart_parity_t
#define UART_TRANSFER_SIZE16
#define CONFIG_UART_INT_WAIT_MS5
#define UART_TASK_STACK_SIZE0x1000
#define UART_TASK_DURATION_MS500
#define UART_TASK_PRIO(osPriority_t)(17)
static uint8_t g_app_uart_rx_buff[UART_TRANSFER_SIZE] = { 0 };
static uint8_t g_app_uart_int_rx_flag = 0;
static uint8_t g_app_uart_int_rx_len = 0;
static uart_buffer_config_t g_app_uart_buffer_config = {
.rx_buffer =g_app_uart_rx_buff,
.rx_buffer_size =UART_TRANSFER_SIZE
};
static void app_uart_init_pin(void)
{
uapi_pin_set_mode(S_AGPIO12,6);
uapi_pin_set_mode(S_AGPIO13,6);
}
static void app_uart_init_config(void)
{
uart_attr_t attr = {
.baud_rate = UART_BAUDRATE,//115200
.data_bits =UART_DATA_BITS, //8
.stop_bits =UART_STOP_BITS, //1
.parity =UART_PARITY_BIT //UART_PARITY_NONE
};
uart_pin_config_t pin_config ={
.tx_pin =S_AGPIO12,//S_MGPIO0,
.rx_pin =S_AGPIO13,//S_MGPIO1,
.cts_pin = PIN_NONE,
.rts_pin = PIN_NONE
};
uapi_uart_init(UART_BUS_1,&pin_config, &attr, NULL, &g_app_uart_buffer_config);
}
static void app_uart_read_int_handler(const void *buffer, uint16_tlength, bool error)
{
unused(error);
if (buffer == NULL || length ==0)
{
osal_printk("uart%dint mode transfer illegal data! ", UART_BUS_1);
return;
}
uint8_t *buff = (uint8_t*)buffer;
if(memcpy_s(g_app_uart_rx_buff, length, buff, length) != EOK)
{
osal_printk("uart%dint mode data copy fail! ", UART_BUS_1);
return;
}
g_app_uart_int_rx_flag = 1;
g_app_uart_int_rx_len = length;
}
static void app_uart_write_int_handler(const void *buffer, uint32_tlength, const void *params)
{
unused(buffer);
unused(length);
unused(params);
uint8_t *buff = (void *)buffer;
osal_printk("uart%d writedata[] =", UART_BUS_1);
for (uint8_t i = 0; i
{
osal_printk("%d", buff[i]);
}
osal_printk(" ");
}
static void *uart_task(const char *arg)
{
unused(arg);
/* UART pinmux. */
app_uart_init_pin();
/* UART init config. */
app_uart_init_config();
osal_printk("uart%d intmode register receive callback start! ", UART_BUS_1);
if(uapi_uart_register_rx_callback(UART_BUS_1,UART_RX_CONDITION_FULL_OR_SUFFICIENT_DATA_OR_IDLE,
1, app_uart_read_int_handler) == ERRCODE_SUCC) {
osal_printk("uart%dint mode register receive callback succ! ", UART_BUS_1);
}
while (1)
{
osDelay(UART_TASK_DURATION_MS);
while(g_app_uart_int_rx_flag != 1) {
osDelay(CONFIG_UART_INT_WAIT_MS);
}
g_app_uart_int_rx_flag = 0;
osal_printk("uart%dint mode send back! ", UART_BUS_1);
if(uapi_uart_write_int(UART_BUS_1, g_app_uart_rx_buff, g_app_uart_int_rx_len, 0,
app_uart_write_int_handler) == ERRCODE_SUCC)
{
osal_printk("uart%d int mode send back succ! ",UART_BUS_1);
}
g_app_uart_int_rx_len = 0;
}
return NULL;
}
static void uart_entry(void)
{
osThreadAttr_t attr;
attr.name ="UartTask";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size =UART_TASK_STACK_SIZE;
attr.priority = UART_TASK_PRIO;
if(osThreadNew((osThreadFunc_t)uart_task, NULL, &attr) == NULL) {
/* Create task fail. */
}
}
/* Run the uart_entry. */
app_run(uart_entry);
由于电脑只有一个串口,不能同时连接AP15,AP14和AP12,AP13两个UART口进行测试,所以进行了以下测试:
从测试结果来看,UART0和UART1都已经能正常工作了,并且UART0是在引脚AP15和AP14上,UART1是工作在AP12和AP13上面,本例子程序只是对UART1进行了初始化,所以看来UART0是在系统其他地方进行了配置,并且通过调用osal_prink函数可以往UART0进行debug log发送。
所以个人感觉,UART0在万不得已的情况下就不要去动它了,串口就用其他的UART BUS好了,除非其他的都不够用了,那就再去研究一下怎么去使用UART0,对自己好一点,别乱折腾。
审核编辑:刘清
全部0条评论
快来发表一下你的评论吧 !