sidestep
[mTask.git] / int / nucleo-f767-blinky / cube / nucleo-f767-blinky / Src / usart.c
1 /**
2 ******************************************************************************
3 * File Name : USART.c
4 * Description : This file provides code for the configuration
5 * of the USART instances.
6 ******************************************************************************
7 *
8 * COPYRIGHT(c) 2016 STMicroelectronics
9 *
10 * Redistribution and use in source and binary forms, with or without modification,
11 * are permitted provided that the following conditions are met:
12 * 1. Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 * 3. Neither the name of STMicroelectronics nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 ******************************************************************************
33 */
34
35 /* Includes ------------------------------------------------------------------*/
36 #include "usart.h"
37
38 #include "gpio.h"
39 #include "dma.h"
40
41 /* USER CODE BEGIN 0 */
42
43 /* USER CODE END 0 */
44
45 UART_HandleTypeDef huart3;
46 DMA_HandleTypeDef hdma_usart3_tx;
47
48 /* USART3 init function */
49
50 void MX_USART3_UART_Init(void)
51 {
52
53 huart3.Instance = USART3;
54 huart3.Init.BaudRate = 9600;
55 huart3.Init.WordLength = UART_WORDLENGTH_8B;
56 huart3.Init.StopBits = UART_STOPBITS_1;
57 huart3.Init.Parity = UART_PARITY_NONE;
58 huart3.Init.Mode = UART_MODE_TX_RX;
59 huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
60 huart3.Init.OverSampling = UART_OVERSAMPLING_16;
61 huart3.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
62 huart3.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
63 if (HAL_UART_Init(&huart3) != HAL_OK)
64 {
65 Error_Handler();
66 }
67
68 }
69
70 void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
71 {
72
73 GPIO_InitTypeDef GPIO_InitStruct;
74 if(uartHandle->Instance==USART3)
75 {
76 /* USER CODE BEGIN USART3_MspInit 0 */
77
78 /* USER CODE END USART3_MspInit 0 */
79 /* Peripheral clock enable */
80 __HAL_RCC_USART3_CLK_ENABLE();
81
82 /**USART3 GPIO Configuration
83 PD8 ------> USART3_TX
84 PD9 ------> USART3_RX
85 */
86 GPIO_InitStruct.Pin = STLK_RX_Pin|STLK_TX_Pin;
87 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
88 GPIO_InitStruct.Pull = GPIO_PULLUP;
89 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
90 GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
91 HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
92
93 /* Peripheral DMA init*/
94
95 hdma_usart3_tx.Instance = DMA1_Stream3;
96 hdma_usart3_tx.Init.Channel = DMA_CHANNEL_4;
97 hdma_usart3_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
98 hdma_usart3_tx.Init.PeriphInc = DMA_PINC_DISABLE;
99 hdma_usart3_tx.Init.MemInc = DMA_MINC_ENABLE;
100 hdma_usart3_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
101 hdma_usart3_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
102 hdma_usart3_tx.Init.Mode = DMA_NORMAL;
103 hdma_usart3_tx.Init.Priority = DMA_PRIORITY_LOW;
104 hdma_usart3_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
105 if (HAL_DMA_Init(&hdma_usart3_tx) != HAL_OK)
106 {
107 Error_Handler();
108 }
109
110 __HAL_LINKDMA(uartHandle,hdmatx,hdma_usart3_tx);
111
112 /* Peripheral interrupt init */
113 HAL_NVIC_SetPriority(USART3_IRQn, 2, 0);
114 HAL_NVIC_EnableIRQ(USART3_IRQn);
115 /* USER CODE BEGIN USART3_MspInit 1 */
116
117 /* USER CODE END USART3_MspInit 1 */
118 }
119 }
120
121 void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
122 {
123
124 if(uartHandle->Instance==USART3)
125 {
126 /* USER CODE BEGIN USART3_MspDeInit 0 */
127
128 /* USER CODE END USART3_MspDeInit 0 */
129 /* Peripheral clock disable */
130 __HAL_RCC_USART3_CLK_DISABLE();
131
132 /**USART3 GPIO Configuration
133 PD8 ------> USART3_TX
134 PD9 ------> USART3_RX
135 */
136 HAL_GPIO_DeInit(GPIOD, STLK_RX_Pin|STLK_TX_Pin);
137
138 /* Peripheral DMA DeInit*/
139 HAL_DMA_DeInit(uartHandle->hdmatx);
140
141 /* Peripheral interrupt Deinit*/
142 HAL_NVIC_DisableIRQ(USART3_IRQn);
143
144 }
145 /* USER CODE BEGIN USART3_MspDeInit 1 */
146
147 /* USER CODE END USART3_MspDeInit 1 */
148 }
149
150 /* USER CODE BEGIN 1 */
151
152 /* USER CODE END 1 */
153
154 /**
155 * @}
156 */
157
158 /**
159 * @}
160 */
161
162 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/