本帖最后由 hejunpeng 于 2016-5-12 14:35 编辑
NXP MAPS-KL43 FlexIO UART快速上手 —— 基于Keil uVision5 和 Kinetis SDK 2.0 FlexIO模块广泛支持各种协议,包括 UART、I2C、SPI、I2S、PWM/波形生成等,这里对FlexIO UART 使用做出简单说明,UART串口收发一直是MCU基本通信方式,此帖使用MAPS-KL43和官方SDK 2.0对KL43的FlexIO UART进行收发操作,使用串口调试助手,调试助手发送数据,MCU收到数据后再把数据传回来,一个完整测试过程,下面直接贴上程序代码和演示视频,并提供相关注释,编译环境采用 Keil uVision5。 MAPS-KL43开发套件: 原理图: 源码:
- #include "board.h"
- #include "clock_config.h"
- #include "fsl_common.h"
- #include "pin_mux.h"
- #include "fsl_port.h"
- #include "fsl_flexio_uart.h"
- /*******************************************************************************
- * Definitions
- ******************************************************************************/
- #define BOARD_FLEXIO_BASE FLEXIO
- #define FLEXIO_UART_TX_PIN 6
- #define FLEXIO_UART_RX_PIN 7
- #define FLEXIO_CLOCK_FREQUENCY 48000000
- /*******************************************************************************
- * Variables
- ******************************************************************************/
- FLEXIO_UART_Type uartDev;
- /*******************************************************************************
- * Main
- ******************************************************************************/
- int main(void)
- {
- uint8_t buffer;
-
- /* 定义 FlexIO串口结构体. */
- flexio_uart_config_t uart_config;
- /* 初始化系统时钟. */
- BOARD_BootClockRUN();
-
- /* 使能 FlexIO GPIO时钟. */
- CLOCK_EnableClock(kCLOCK_PortE);
-
- /* 初始化 FlexIO GPIO */
- PORT_SetPinMux(PORTE, 22U, kPORT_MuxAlt6);
- PORT_SetPinMux(PORTE, 23U, kPORT_MuxAlt6);
-
- /* 设置 FlexIO时钟源. */
- CLOCK_SetFlexio0Clock(1U);
- /* 获取 FlexIO UART初始配置. */
- FLEXIO_UART_GetDefaultConfig(&uart_config);
- uart_config.baudRate_Bps = 115200;
- uart_config.enableUart = true;
- /* FlexIO UART 配置 */
- uartDev.flexioBase = BOARD_FLEXIO_BASE;
- uartDev.TxPinIndex = FLEXIO_UART_TX_PIN;
- uartDev.RxPinIndex = FLEXIO_UART_RX_PIN;
- uartDev.shifterIndex[0] = 0U;
- uartDev.shifterIndex[1] = 1U;
- uartDev.timerIndex[0] = 0U;
- uartDev.timerIndex[1] = 1U;
- /* FlexIO UART初始化. */
- FLEXIO_UART_Init(&uartDev, &uart_config, FLEXIO_CLOCK_FREQUENCY);
- while (1)
- {
- /* 接收串口数据存放在 buffer中. */
- FLEXIO_UART_ReadBlocking(&uartDev, &buffer, 1);
-
- /* 将 buffer中的数据通过串口发送出去. */
- FLEXIO_UART_WriteBlocking(&uartDev, &buffer, 1);
- }
- }
复制代码
演示视频:
|