星闪Hi3863串口乱码问题(踩坑日记)

前言:

跟着星闪官网敲串口代码的时候在配置参数的时候,看官网数据位配置成3位,平常用串口正常都是8位,然后我就配置成了8位数据位,结果开串口调试助手起来看的时候打印不出来

正确代码:

#include "common_def.h"
#include "osal_debug.h"
#include "soc_osal.h"
#include "cmsis_os2.h"
#include "gpio.h"
#include "pinctrl.h"
#include "app_init.h"
#include "uart.h"
#define TASKS_TEST_TASK_STACK_SIZE      0x1000
#define TASKS_TEST_TASK_PRIO            (osPriority_t)(17)
#define TASK_TICK                       10

#define UART_TX_PIN     15
#define UART_RX_PIN     16
#define UART_ID         1

static uint8_t g_app_uart_rx_buff[16] = { 0 };

static uart_buffer_config_t g_app_uart_buffer_config = {
    .rx_buffer = g_app_uart_rx_buff,
    .rx_buffer_size = 16
};

/*串口初始化*/
void myuart_init(void)
{

    //复用引脚
    uapi_pin_set_mode(UART_TX_PIN, PIN_MODE_1);
    uapi_pin_set_mode(UART_RX_PIN, PIN_MODE_1);
    //配置UART
    uart_attr_t attr = {
        .baud_rate = 115200,
        .data_bits = 3, 
        .stop_bits = 1,
        .parity = 0
    };

    uart_pin_config_t pin_config = {
        .tx_pin = UART_TX_PIN,
        .rx_pin = UART_RX_PIN,
        .cts_pin = PIN_NONE,
        .rts_pin = PIN_NONE
    };
    uapi_uart_deinit(UART_ID); 
    uapi_uart_init(UART_ID, &pin_config, &attr, NULL, &g_app_uart_buffer_config);
}

static void *tasks_test_task(const char *arg)
{
    unused(arg);
    myuart_init();
    static uint8_t g_app_uart_tx_buff[512] = "Hello world";
    while (1)
    {   
        osal_msleep(2000);
        uapi_uart_write(UART_ID, g_app_uart_tx_buff, 512, 0);
        //printf("uart transimit");
        
    }

    return NULL;
}


static void tasks_test_entry(void)
{
    osThreadAttr_t attr;

    attr.name = "TasksTask";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = TASKS_TEST_TASK_STACK_SIZE;
    attr.priority = TASKS_TEST_TASK_PRIO;

    if (osThreadNew((osThreadFunc_t)tasks_test_task, NULL, &attr) == NULL) {
        /* Create task fail. */
    }
}   

/* Run the tasks_test_entry. */
app_run(tasks_test_entry);

数据位数改成3才能正常输出不会乱码,具体为什么8不是我也还没找到原因,有大佬知道的话帮忙指导一下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值