K210 - MaixBit开发板 裸机开发(2):串口通信

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 函数来进行串口映射,之后才能使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值