K210 串口通信
目标
实现K210串口回显功能
API介绍
// 用于初始化串口, channle:指定的串口号
void uart_init(uart_device_number_t channel);
// 配置串口的相关参数
void uart_config(uart_device_number_t channel, uint32_t baud_rate, uart_bitwidth_t
data_width, uart_stopbit_t stopbit, uart_parity_t parity);
// 串口数据接收
int uart_receive_data(uart_device_number_t channel, char *buffer, size_t buf_len);
// 串口数据发送
int uart_send_data(uart_device_number_t channel, const char *buffer, size_t buf_len);
// IO脚映射
int fpioa_set_function(int number, fpioa_function_t function);
代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysctl.h>
#include "fpioa.h"
#include "gpio.h"
#include "uart.h"
#define UART_NUM UART_DEVICE_2
#define UART_RX_PIN 7
#define UART_TX_PIN 8
void uart_init_function();
void uart_pin_init();
char RecBuf[128];
int main() {
uart_pin_init();
uart_init_function();
int len = 0;
while (1) {
len = uart_receive_data(UART_NUM, RecBuf, 1);
if (len > 0) {
uart_send_data(UART_NUM, RecBuf, len);
}
}
return 1;
}
void uart_init_function() {
uart_init(UART_NUM);
uart_config(UART_NUM, 115200, UART_BITWIDTH_8BIT, UART_STOP_1,
UART_PARITY_NONE);
}
void uart_pin_init() {
fpioa_set_function(UART_RX_PIN, FUNC_UART1_RX + UART_NUM * 2);
fpioa_set_function(UART_TX_PIN, FUNC_UART1_TX + UART_NUM * 2);
}
注意
- 使用串口时,由于串口的RX和TX脚不固定,需要使用 fpioa_set_function 函数来进行串口映射,之后才能使用。