ECE 486 Support Libraries
usart.c
Go to the documentation of this file.
1 /*!
2  * @file
3  *
4  * @brief USART Initialization to stream printf() output back to the ST-LINK
5  *
6  * @author Hummels... really stolen from Sheaff.
7  *
8  * @date Jan 2016
9  *
10  * ST-LINK V2 provides a serial port to the host computer, which is accessed
11  * through a serial connector configured here...
12  */
13 
14 #include "stm32l4xx_hal.h"
15 #include "usart.h"
16 
17 UART_HandleTypeDef UsartHandle;
18 
19 // Setup L4 serial port to connect the STM debug IC
20 // Standard settings - 115200 8N1
21 // Be sure to get the updated syscalls.c file to redirect write to the USART
22 void usart_init(void)
23 {
24  UsartHandle.Instance = USART2;
25  UsartHandle.Init.BaudRate = 115200;
26  UsartHandle.Init.WordLength = UART_WORDLENGTH_8B;
27  UsartHandle.Init.StopBits = UART_STOPBITS_1;
28  UsartHandle.Init.Parity = UART_PARITY_NONE;
29  UsartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
30  UsartHandle.Init.Mode = UART_MODE_TX_RX;
31  UsartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
32  HAL_UART_Init(&UsartHandle);
33 
34  return;
35 }
36 
37 
38 void HAL_UART_MspInit(UART_HandleTypeDef * huart)
39 {
40  GPIO_InitTypeDef GPIO_InitStruct;
41 
42  __HAL_RCC_GPIOD_CLK_ENABLE();
43  __HAL_RCC_USART2_CLK_ENABLE();
44 
45  GPIO_InitStruct.Pin = GPIO_PIN_5;
46  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
47  GPIO_InitStruct.Pull = GPIO_PULLUP;
48  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
49  GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
50  HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
51 
52  /* UART RX GPIO pin configuration */
53  GPIO_InitStruct.Pin = GPIO_PIN_6;
54  GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
55 
56  HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
57 }
void HAL_UART_MspInit(UART_HandleTypeDef *huart)
Definition: usart.c:38
void usart_init(void)
USART Configuration to talk to ST-LINK.
Definition: usart.c:22
USART Initialization to stream printf() output back to the ST-LINK.
UART_HandleTypeDef UsartHandle
Definition: usart.c:17