]> mj.ucw.cz Git - home-hw.git/blob - Src/main.c
02220adda978003e7bc1dd0548c9177cb37f073a
[home-hw.git] / Src / main.c
1
2 /**
3   ******************************************************************************
4   * @file           : main.c
5   * @brief          : Main program body
6   ******************************************************************************
7   ** This notice applies to any and all portions of this file
8   * that are not between comment pairs USER CODE BEGIN and
9   * USER CODE END. Other portions of this file, whether 
10   * inserted by the user or by software development tools
11   * are owned by their respective copyright owners.
12   *
13   * COPYRIGHT(c) 2018 STMicroelectronics
14   *
15   * Redistribution and use in source and binary forms, with or without modification,
16   * are permitted provided that the following conditions are met:
17   *   1. Redistributions of source code must retain the above copyright notice,
18   *      this list of conditions and the following disclaimer.
19   *   2. Redistributions in binary form must reproduce the above copyright notice,
20   *      this list of conditions and the following disclaimer in the documentation
21   *      and/or other materials provided with the distribution.
22   *   3. Neither the name of STMicroelectronics nor the names of its contributors
23   *      may be used to endorse or promote products derived from this software
24   *      without specific prior written permission.
25   *
26   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
30   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
34   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36   *
37   ******************************************************************************
38   */
39 /* Includes ------------------------------------------------------------------*/
40 #include "main.h"
41
42 /* USER CODE BEGIN Includes */
43
44 /* USER CODE END Includes */
45
46 /* Private variables ---------------------------------------------------------*/
47
48 /* USER CODE BEGIN PV */
49 /* Private variables ---------------------------------------------------------*/
50
51 /* USER CODE END PV */
52
53 /* Private function prototypes -----------------------------------------------*/
54 static void LL_Init(void);
55 void SystemClock_Config(void);
56 static void MX_GPIO_Init(void);
57 static void MX_USART2_UART_Init(void);
58
59 /* USER CODE BEGIN PFP */
60 /* Private function prototypes -----------------------------------------------*/
61
62 /* USER CODE END PFP */
63
64 /* USER CODE BEGIN 0 */
65
66 /* USER CODE END 0 */
67
68 /**
69   * @brief  The application entry point.
70   *
71   * @retval None
72   */
73 int main(void)
74 {
75   /* USER CODE BEGIN 1 */
76
77   /* USER CODE END 1 */
78
79   /* MCU Configuration----------------------------------------------------------*/
80
81   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
82   LL_Init();
83
84   /* USER CODE BEGIN Init */
85
86   /* USER CODE END Init */
87
88   /* Configure the system clock */
89   SystemClock_Config();
90
91   /* USER CODE BEGIN SysInit */
92
93   /* USER CODE END SysInit */
94
95   /* Initialize all configured peripherals */
96   MX_GPIO_Init();
97   MX_USART2_UART_Init();
98   /* USER CODE BEGIN 2 */
99
100   /* USER CODE END 2 */
101
102   /* Infinite loop */
103   /* USER CODE BEGIN WHILE */
104   while (1)
105   {
106
107   /* USER CODE END WHILE */
108
109   /* USER CODE BEGIN 3 */
110
111   }
112   /* USER CODE END 3 */
113
114 }
115
116 static void LL_Init(void)
117 {
118   
119
120   LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_SYSCFG);
121
122   /* System interrupt init*/
123   /* SVC_IRQn interrupt configuration */
124   NVIC_SetPriority(SVC_IRQn, 0);
125   /* PendSV_IRQn interrupt configuration */
126   NVIC_SetPriority(PendSV_IRQn, 0);
127   /* SysTick_IRQn interrupt configuration */
128   NVIC_SetPriority(SysTick_IRQn, 0);
129
130 }
131
132 /**
133   * @brief System Clock Configuration
134   * @retval None
135   */
136 void SystemClock_Config(void)
137 {
138
139   LL_FLASH_SetLatency(LL_FLASH_LATENCY_1);
140
141   if(LL_FLASH_GetLatency() != LL_FLASH_LATENCY_1)
142   {
143   Error_Handler();  
144   }
145   LL_RCC_HSI_Enable();
146
147    /* Wait till HSI is ready */
148   while(LL_RCC_HSI_IsReady() != 1)
149   {
150     
151   }
152   LL_RCC_HSI_SetCalibTrimming(16);
153
154   LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSI_DIV_2, LL_RCC_PLL_MUL_8);
155
156   LL_RCC_PLL_Enable();
157
158    /* Wait till PLL is ready */
159   while(LL_RCC_PLL_IsReady() != 1)
160   {
161     
162   }
163   LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
164
165   LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1);
166
167   LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
168
169    /* Wait till System clock is ready */
170   while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
171   {
172   
173   }
174   LL_Init1msTick(32000000);
175
176   LL_SYSTICK_SetClkSource(LL_SYSTICK_CLKSOURCE_HCLK);
177
178   LL_SetSystemCoreClock(32000000);
179
180   /* SysTick_IRQn interrupt configuration */
181   NVIC_SetPriority(SysTick_IRQn, 0);
182 }
183
184 /* USART2 init function */
185 static void MX_USART2_UART_Init(void)
186 {
187
188   LL_USART_InitTypeDef USART_InitStruct;
189
190   LL_GPIO_InitTypeDef GPIO_InitStruct;
191
192   /* Peripheral clock enable */
193   LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_USART2);
194   
195   /**USART2 GPIO Configuration  
196   PA2   ------> USART2_TX
197   PA3   ------> USART2_RX 
198   */
199   GPIO_InitStruct.Pin = USART_TX_Pin;
200   GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
201   GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
202   GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
203   GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
204   GPIO_InitStruct.Alternate = LL_GPIO_AF_1;
205   LL_GPIO_Init(USART_TX_GPIO_Port, &GPIO_InitStruct);
206
207   GPIO_InitStruct.Pin = USART_RX_Pin;
208   GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
209   GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
210   GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
211   GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
212   GPIO_InitStruct.Alternate = LL_GPIO_AF_1;
213   LL_GPIO_Init(USART_RX_GPIO_Port, &GPIO_InitStruct);
214
215   USART_InitStruct.BaudRate = 38400;
216   USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B;
217   USART_InitStruct.StopBits = LL_USART_STOPBITS_1;
218   USART_InitStruct.Parity = LL_USART_PARITY_NONE;
219   USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX_RX;
220   USART_InitStruct.HardwareFlowControl = LL_USART_HWCONTROL_NONE;
221   USART_InitStruct.OverSampling = LL_USART_OVERSAMPLING_16;
222   LL_USART_Init(USART2, &USART_InitStruct);
223
224   LL_USART_DisableIT_CTS(USART2);
225
226   LL_USART_DisableOverrunDetect(USART2);
227
228   LL_USART_ConfigAsyncMode(USART2);
229
230   LL_USART_Enable(USART2);
231
232 }
233
234 /** Configure pins as 
235         * Analog 
236         * Input 
237         * Output
238         * EVENT_OUT
239         * EXTI
240 */
241 static void MX_GPIO_Init(void)
242 {
243
244   LL_EXTI_InitTypeDef EXTI_InitStruct;
245   LL_GPIO_InitTypeDef GPIO_InitStruct;
246
247   /* GPIO Ports Clock Enable */
248   LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOC);
249   LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOF);
250   LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOA);
251
252   /**/
253   LL_GPIO_ResetOutputPin(LD2_GPIO_Port, LD2_Pin);
254
255   /**/
256   LL_SYSCFG_SetEXTISource(LL_SYSCFG_EXTI_PORTC, LL_SYSCFG_EXTI_LINE13);
257
258   /**/
259   LL_GPIO_SetPinPull(B1_GPIO_Port, B1_Pin, LL_GPIO_PULL_NO);
260
261   /**/
262   LL_GPIO_SetPinMode(B1_GPIO_Port, B1_Pin, LL_GPIO_MODE_INPUT);
263
264   /**/
265   EXTI_InitStruct.Line_0_31 = LL_EXTI_LINE_13;
266   EXTI_InitStruct.LineCommand = ENABLE;
267   EXTI_InitStruct.Mode = LL_EXTI_MODE_IT;
268   EXTI_InitStruct.Trigger = LL_EXTI_TRIGGER_FALLING;
269   LL_EXTI_Init(&EXTI_InitStruct);
270
271   /**/
272   GPIO_InitStruct.Pin = LD2_Pin;
273   GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
274   GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
275   GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
276   GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
277   LL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
278
279 }
280
281 /* USER CODE BEGIN 4 */
282
283 /* USER CODE END 4 */
284
285 /**
286   * @brief  This function is executed in case of error occurrence.
287   * @param  file: The file name as string.
288   * @param  line: The line in file as a number.
289   * @retval None
290   */
291 void _Error_Handler(char *file, int line)
292 {
293   /* USER CODE BEGIN Error_Handler_Debug */
294   /* User can add his own implementation to report the HAL error return state */
295   while(1)
296   {
297   }
298   /* USER CODE END Error_Handler_Debug */
299 }
300
301 #ifdef  USE_FULL_ASSERT
302 /**
303   * @brief  Reports the name of the source file and the source line number
304   *         where the assert_param error has occurred.
305   * @param  file: pointer to the source file name
306   * @param  line: assert_param error line source number
307   * @retval None
308   */
309 void assert_failed(uint8_t* file, uint32_t line)
310
311   /* USER CODE BEGIN 6 */
312   /* User can add his own implementation to report the file name and line number,
313      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
314   /* USER CODE END 6 */
315 }
316 #endif /* USE_FULL_ASSERT */
317
318 /**
319   * @}
320   */
321
322 /**
323   * @}
324   */
325
326 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/