【平头哥RVB2601创意应用开发】配置串口1 UART1回显
本帖最后由 full_stack 于 2022-6-9 02:26 编辑<p>今天在调串口1回显程序,就是串口收到什么数据,再返回什么数据,</p>
<p>调了很久,总是会有问题,收到的数据有点乱,</p>
<p>写出来的程序偶尔会出错。可能控制不了时序。</p>
<p>没想到啊,没想到,我总以为官方没有给出串口1的驱动程序,</p>
<p>原来都是写的好好的,怪我没看手册,一个人瞎搞。</p>
<p>使用官方的接口文档,分分钟搞定串口1输入输出。</p>
<p>接口文档链接</p>
<p>https://yoc.docs.t-head.cn/yocbook/Chapter3-AliOS/HAL%E7%A1%AC%E4%BB%B6%E6%8A%BD%E8%B1%A1%E5%B1%82%E6%8E%A5%E5%8F%A3/</p>
<p>直接重新下载的新的helloworld代码</p>
<p>添加了串口的代码,如下:</p>
<pre>
<code class="language-cpp">/*
* Copyright (C) 2019-2020 Alibaba Group Holding Limited
*/
#include <stdlib.h>
#include <string.h>
#include <aos/aos.h>
#include "aos/cli.h"
#include "main.h"
#include "app_init.h"
#include "oled.h"
#include <aos/hal/uart.h>
#include "soc.h"
#include <drv/pin.h>
#define TAG "app"
uart_dev_t uart1;
#define UART1_PORT_NUM1
#define UART_BUF_SIZE 1
#define UART_TX_TIMEOUT 10
#define UART_RX_TIMEOUT 10
/* define dev */
uart_dev_t uart1;
/* data buffer */
char uart_data_buf;
int application_start(void)
{
//int count = 0;
int ret = -1;
int i = 0;
int rx_size = 0;
/* uart port set */
uart1.port = UART1_PORT_NUM;
/* uart attr config */
uart1.config.baud_rate = 115200;
uart1.config.data_width = DATA_WIDTH_8BIT;
uart1.config.parity = NO_PARITY;
uart1.config.stop_bits = STOP_BITS_1;
uart1.config.flow_control = FLOW_CONTROL_DISABLED;
uart1.config.mode = MODE_TX_RX;
/* init uart1 with the given settings */
ret = hal_uart_init(&uart1);
if (ret != 0) {
printf("uart1 init error !\n");
}
/* init the tx buffer */
for (i = 0; i < UART_BUF_SIZE; i++) {
uart_data_buf = i + 1;
}
/* send 0,1,2,3,4,5,6,7,8,9 by uart1 */
ret = hal_uart_send(&uart1, uart_data_buf, UART_BUF_SIZE, UART_TX_TIMEOUT);
if (ret == 0) {
printf("uart1 data send succeed !\n");
}
csi_pin_set_mux(PA28, PA28_UART1_TX);
csi_pin_set_mux(PA27, PA27_UART1_RX);
/* scan uart1 every 100ms, if data received send it back */
while(1) {
ret = hal_uart_recv_II(&uart1, uart_data_buf, UART_BUF_SIZE,
&rx_size, UART_RX_TIMEOUT);
if ((ret == 0) ) {
printf("uart1 data received succeed !\n");
hal_uart_send(&uart1, "the recv buf is: ", 16, UART_TX_TIMEOUT);
ret = hal_uart_send(&uart1, uart_data_buf, rx_size, UART_TX_TIMEOUT);
if (ret == 0) {
printf("uart1 data send succeed !\n");
}
}
//hal_uart_send(&uart1, uart_data_buf, UART_BUF_SIZE, UART_TX_TIMEOUT);
/* sleep 100ms */
aos_msleep(100);
};
}
int main(void)
{
board_yoc_init();
LOGD(TAG, "%s\n", aos_get_app_version());
oled_init();
application_start();
while (1) {
// LOGD(TAG, "Hello world! YoC");
aos_msleep(1000);
}
return 0;
}
</code></pre>
<p>另外我想知道,uart_dev_t这个结构体,为什么右键 goto declaration去的位置总是错的?</p>
<pre>
<code class="language-cpp">typedef struct {
aos_dev_t device;
int flowctrl;
csi_uart_t handle;
char *recv_buf;
dev_ringbuf_tread_buffer;
aos_mutex_t mutex;
aos_event_t event_write_read;
int event_state;
void (*write_event)(aos_dev_t *dev, int event_id, void *priv);
void *priv;
void *init_cfg;
int type;
} uart_dev_t;</code></pre>
<p> </p>
给你个建议,最好要换个IO别占用那个烧写的IO,要不死得难看。
页:
[1]