Maix Bit(K210) SDK 开发教程(二)串口通信
程序员文章站
2022-06-25 17:39:45
@[TOC](Maix Bit(K210) SDK )Maix Bit 开发板Kendryte IDE开发环境实现功能,在串口助手输入字符,通过Maix Bit回显在串口。 嵌入式应用通常要求一个简单的并且占用系统资源少的方法来传输数据。通用异步收发传输器(UART)即可以满足这些要求,它能够灵活地与外部设备进行全双工数据交换。1、使用API简介相关开发文档下载请看:https://blog.csdn.net/weixin_45488643/article/details/10923901...
@[TOC](Maix Bit(K210) SDK )
Maix Bit 开发板
Kendryte IDE开发环境
实现功能,在串口助手输入字符,通过Maix Bit回显在串口。
嵌入式应用通常要求一个简单的并且占用系统资源少的方法来传输数据。通用异步收发传输器(UART)即可以满足这些要求,它能够灵活地与外部设备进行全双工数据交换。
1、使用API简介
相关开发文档下载请看:https://blog.csdn.net/weixin_45488643/article/details/109239012
串口初始化:
void uart_init(uart_device_number_t channel)
设置接收:
void uart_set_send_trigger(uart_device_number_t channel, uart_send_trigger_t trigger)
设置发送:
void uart_set_send_trigger(uart_device_number_t channel, uart_send_trigger_t trigger)
设置uart相关参数:
void uart_configurev(uart_device_number_t channel, uint32_t baud_rate, uart_bitwidth_t data_width, uart_stopbit_t stopbit, uart_parity_t parity)
注册中断:
void uart_irq_register(uart_device_number_t channel, uart_interrupt_mode_t interrupt_mode, plic_irq_callback_t uart_callback, void *ctx, uint32_t priority)
更多相关函数的使用,见官网文档。
2、代码
#include <stdio.h>
#include <string.h>
#include "fpioa.h"
#include "gpiohs.h"
#include "sysctl.h"
#include "uart.h"
#define UART_NUM UART_DEVICE_3 //串口3
#define MAX_RECV_LEN 256 //设定可以接收的最大字节
int on_uart_send(void *ctx) {
uint8_t v_uart = *((uint32_t *)ctx) + 1 + 0x30;
uart_irq_unregister(UART_NUM, UART_SEND);
char *v_send_ok = "Send ok Uart";
uart_send_data(UART_NUM, v_send_ok, strlen(v_send_ok));
uart_send_data(UART_NUM, (char *)&v_uart, 1);
return 0;
}
int on_uart_recv(void *ctx) {
uint8_t msg_buff[MAX_RECV_LEN] = {0}, t = 0; //接收缓存区
char recv = 0;
while (uart_receive_data(UART_NUM, &recv, 1)) {
// uart_send_data(UART_NUM, &recv, 1); //接收一位发送一位
msg_buff[t] = recv;
t++;
}
uart_send_data(UART_NUM, msg_buff, strlen(msg_buff));//先保存,在发送
}
void io_mux_init(void) {
fpioa_set_function(4, FUNC_UART1_RX + UART_NUM * 2);
fpioa_set_function(5, FUNC_UART1_TX + UART_NUM * 2);
fpioa_set_function(12, FUNC_GPIOHS3);
}
int main() {
io_mux_init(); //初始化引脚
plic_init(); //初始化外部中断
sysctl_enable_irq(); //使能中断
gpiohs_set_drive_mode(3, GPIO_DM_OUTPUT); //设置驱动模式
gpio_pin_value_t value = GPIO_PV_LOW;
gpiohs_set_pin(3, value); // LED_G
uart_init(UART_NUM); //串口初始化
uart_configure(UART_NUM, 115200, 8, UART_STOP_1,
UART_PARITY_NONE); //设置uart 相关
uart_set_receive_trigger(UART_NUM, UART_RECEIVE_FIFO_8); //设置接收函数
uart_irq_register(UART_NUM, UART_RECEIVE, on_uart_recv, NULL,
2); //注册uart中断 接收中断 无中断函数参数 中断优先级2
uart_set_send_trigger(UART_NUM, UART_SEND_FIFO_0); //设置发送函数
uint32_t v_uart_num = UART_NUM;
uart_irq_register(UART_NUM, UART_SEND, on_uart_send, &v_uart_num,
2); //注册uart中断 发送中断 中断函数参数 中断优先级2
char *hel = {"hello world!\n"};
uart_send_data(UART_NUM, hel, strlen(hel));
while (1)
;
}
3、实现效果
上电串口先发送字符,并且LED_G点亮,之后串口发送的字符进行回显。
本文地址:https://blog.csdn.net/weixin_45488643/article/details/109257717