EFR32FG1开发教程2--串口

前言:直接上代码吧

bsp_usart.c

/**
  ******************************************************************************
  * @file    bsp_usart.c
  * @author  Garen
  * @version V1.0.0
  * @date    2018-04-19
  * @brief   对USART操作,功能如下:   
  *           + USART初始化
  *           + USART发送一个字节
  *           + USART发送字符串
  *
  ******************************************************************************
  */
/* Includes ------------------------------------------------------------------*/
#include "bsp_usart.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/**
  * @brief  初始化usart
  * @retval None
  */
void BSP_UsartInit(void)
{
  /* Enable peripheral clocks */
  CMU_ClockEnable(cmuClock_HFPER, true);
  /* Configure GPIO pins */
  CMU_ClockEnable(cmuClock_GPIO, true);
  /* To avoid false start, configure output as high */
  GPIO_PinModeSet(BSP_SERIAL_APP_TX_PORT, BSP_SERIAL_APP_TX_PIN, gpioModePushPull, 1);
  GPIO_PinModeSet(BSP_SERIAL_APP_RX_PORT, BSP_SERIAL_APP_RX_PIN, gpioModeInput, 0);
  USART_TypeDef           *usart = RETARGET_UART;
  USART_InitAsync_TypeDef init   = USART_INITASYNC_DEFAULT;
  CMU_ClockEnable(RETARGET_CLK, true);
  /* Configure USART for basic async operation */
  init.enable = usartDisable;
  USART_InitAsync(usart, &init);
  /* Enable pins at correct UART/USART location. */
  usart->ROUTEPEN = USART_ROUTEPEN_RXPEN | USART_ROUTEPEN_TXPEN;
  usart->ROUTELOC0 = (usart->ROUTELOC0& ~(_USART_ROUTELOC0_TXLOC_MASK| _USART_ROUTELOC0_RXLOC_MASK))\
                     | (BSP_SERIAL_APP_TX_LOC << _USART_ROUTELOC0_TXLOC_SHIFT) \
                     | (BSP_SERIAL_APP_RX_LOC << _USART_ROUTELOC0_RXLOC_SHIFT);
  /* Clear previous RX interrupts */
  USART_IntClear(RETARGET_UART, USART_IF_RXDATAV);
  NVIC_ClearPendingIRQ(RETARGET_IRQn);
  /* Enable RX interrupts */
  USART_IntDisable(RETARGET_UART, USART_IF_RXDATAV);
  NVIC_DisableIRQ(RETARGET_IRQn);
  /* Finally enable it */
  USART_Enable(usart, usartEnable);
}

/**
  * @brief  发送一个字节
  * @param  pUSARTx:表示采用哪个串口发送.如USART0.USART1.
  * @param  ch:待发送的char
  * @retval None
  */
void BSP_UsartSendByte(uint8_t ch)
{
  USART_Tx(RETARGET_UART, ch);
}

/**
  * @brief  发送字符串
  * @param  pUSARTx:表示采用哪个串口发送.如USART0.USART1.
  * @param  str:待发送字符串地址
  * @retval None
  */
void BSP_UsartSendString(char *str)
{
  uint32_t k = 0;
  do
  {
    BSP_UsartSendByte((uint8_t)*(str+k));
    k++;
  }
  while(*(str + k) != '\0');
}

bsap_usart.h

/**
  ******************************************************************************
  * @file    bsp_usart.h
  * @author  Garen
  * @version V1.0.0
  * @date    2018-04-19
  * @brief   bsp_usart.c文件对应的头文件,函数声明.结构体定义等作用
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __BSP_USART_H
#define __BSP_USART_H

#ifdef __cplusplus
 extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "em_chip.h"
#include "em_cmu.h"
#include "em_emu.h"
#include "em_usart.h"
/* Exported types ------------------------------------------------------------*/
// $[SERIAL]
#define RETARGET_UART                         USART0
#define RETARGET_CLK                          cmuClock_USART0
#define RETARGET_IRQ_NAME                     USART0_RX_IRQHandler
#define RETARGET_IRQn                         USART0_RX_IRQn
   
#define HAL_SERIAL_USART0_ENABLE              (0)
#define BSP_SERIAL_APP_PORT                   (HAL_SERIAL_PORT_USART0)
#define HAL_SERIAL_LEUART0_ENABLE             (0)
#define HAL_SERIAL_USART1_ENABLE              (0)
#define HAL_SERIAL_RXWAKE_ENABLE              (0)
#define BSP_SERIAL_APP_RX_PIN                 (1U)
#define BSP_SERIAL_APP_RX_PORT                (gpioPortA)
#define BSP_SERIAL_APP_RX_LOC                 (0U)

#define BSP_SERIAL_APP_TX_PIN                 (0U)
#define BSP_SERIAL_APP_TX_PORT                (gpioPortA)
#define BSP_SERIAL_APP_TX_LOC                 (0U)

#define HAL_SERIAL_APP_RX_QUEUE_SIZE          (128UL)
#define HAL_SERIAL_APP_BAUD_RATE              (115200UL)
#define HAL_SERIAL_APP_RXSTOP                 (16UL)
#define HAL_SERIAL_APP_RXSTART                (16UL)
#define HAL_SERIAL_APP_TX_QUEUE_SIZE          (128UL)
#define HAL_SERIAL_APP_FLOW_CONTROL           (HAL_USART_FLOW_CONTROL_NONE)
// [SERIAL]$
/* Exported functions prototypes---- -----------------------------------------*/
void BSP_UsartInit(void);
void BSP_UsartSendByte(uint8_t ch);
void BSP_UsartSendString(char *str);

#ifdef __cplusplus
}
#endif

#endif /*__BSP_USART_H */

/******************************** (C)END OF FILE********************************/

你可能感兴趣的:(无线通信--EFR32FG1)