62 lines
2.0 KiB
C
62 lines
2.0 KiB
C
/**
|
|
* @file bsp_usart.c
|
|
* @brief USART1 init + printf retarget (fputc)
|
|
*/
|
|
#include "bsp_usart.h"
|
|
#include <stdio.h>
|
|
|
|
/**
|
|
* @brief Initialize USART1 (115200 8N1) on PB6(TX)/PB7(RX).
|
|
*/
|
|
void bsp_usart_init(void)
|
|
{
|
|
GPIO_InitType GPIO_InitStructure;
|
|
USART_InitType USART_InitStructure;
|
|
|
|
/* Enable GPIOB and USART1 clocks */
|
|
RCC_EnableAPB2PeriphClk(BSP_USART_GPIO_CLK, ENABLE);
|
|
RCC_EnableAPB2PeriphClk(BSP_USART_CLK, ENABLE);
|
|
|
|
/* Configure USART1 TX (PB6) as alternate function push-pull */
|
|
GPIO_InitStruct(&GPIO_InitStructure);
|
|
GPIO_InitStructure.Pin = BSP_USART_TX_PIN;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF_PP;
|
|
GPIO_InitStructure.GPIO_Alternate = BSP_USART_TX_GPIO_AF;
|
|
GPIO_InitPeripheral(BSP_USART_GPIO, &GPIO_InitStructure);
|
|
|
|
/* Configure USART1 RX (PB7) as alternate function */
|
|
GPIO_InitStructure.Pin = BSP_USART_RX_PIN;
|
|
GPIO_InitStructure.GPIO_Alternate = BSP_USART_RX_GPIO_AF;
|
|
GPIO_InitPeripheral(BSP_USART_GPIO, &GPIO_InitStructure);
|
|
|
|
USART_InitStructure.BaudRate = BSP_USART_BAUDRATE;
|
|
USART_InitStructure.WordLength = USART_WL_8B;
|
|
USART_InitStructure.StopBits = USART_STPB_1;
|
|
USART_InitStructure.Parity = USART_PE_NO;
|
|
USART_InitStructure.HardwareFlowControl = USART_HFCTRL_NONE;
|
|
USART_InitStructure.Mode = USART_MODE_RX | USART_MODE_TX;
|
|
|
|
USART_Init(BSP_USARTx, &USART_InitStructure);
|
|
USART_Enable(BSP_USARTx, ENABLE);
|
|
}
|
|
|
|
/* Retarget the C library printf function to USART1 */
|
|
int fputc(int ch, FILE* f)
|
|
{
|
|
/* Send '\r' before '\n' so that serial terminals display correctly */
|
|
if (ch == '\n')
|
|
{
|
|
USART_SendData(BSP_USARTx, (uint8_t)'\r');
|
|
while (USART_GetFlagStatus(BSP_USARTx, USART_FLAG_TXDE) == RESET)
|
|
{
|
|
}
|
|
}
|
|
|
|
USART_SendData(BSP_USARTx, (uint8_t)ch);
|
|
while (USART_GetFlagStatus(BSP_USARTx, USART_FLAG_TXDE) == RESET)
|
|
{
|
|
}
|
|
|
|
return ch;
|
|
}
|