]> mj.ucw.cz Git - home-hw.git/blob - Src/main.c
5552021b51cf1363a354f4f95d78a65ea84fd830
[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 static void MX_I2C1_Init(void);
59
60 /* USER CODE BEGIN PFP */
61 /* Private function prototypes -----------------------------------------------*/
62
63 /* USER CODE END PFP */
64
65 /* USER CODE BEGIN 0 */
66
67 /* USER CODE END 0 */
68
69 /**
70   * @brief  The application entry point.
71   *
72   * @retval None
73   */
74 int main(void)
75 {
76   /* USER CODE BEGIN 1 */
77
78   /* USER CODE END 1 */
79
80   /* MCU Configuration----------------------------------------------------------*/
81
82   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
83   LL_Init();
84
85   /* USER CODE BEGIN Init */
86
87   /* USER CODE END Init */
88
89   /* Configure the system clock */
90   SystemClock_Config();
91
92   /* USER CODE BEGIN SysInit */
93
94   /* USER CODE END SysInit */
95
96   /* Initialize all configured peripherals */
97   MX_GPIO_Init();
98   MX_USART2_UART_Init();
99   MX_I2C1_Init();
100   /* USER CODE BEGIN 2 */
101
102   run_test();
103
104   /* USER CODE END 2 */
105
106   /* Infinite loop */
107   /* USER CODE BEGIN WHILE */
108   while (1)
109   {
110   /* USER CODE END WHILE */
111
112   /* USER CODE BEGIN 3 */
113   }
114   /* USER CODE END 3 */
115
116 }
117
118 static void LL_Init(void)
119 {
120   
121
122   LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_SYSCFG);
123
124   /* System interrupt init*/
125   /* SVC_IRQn interrupt configuration */
126   NVIC_SetPriority(SVC_IRQn, 0);
127   /* PendSV_IRQn interrupt configuration */
128   NVIC_SetPriority(PendSV_IRQn, 0);
129   /* SysTick_IRQn interrupt configuration */
130   NVIC_SetPriority(SysTick_IRQn, 0);
131
132 }
133
134 /**
135   * @brief System Clock Configuration
136   * @retval None
137   */
138 void SystemClock_Config(void)
139 {
140
141   LL_FLASH_SetLatency(LL_FLASH_LATENCY_1);
142
143   if(LL_FLASH_GetLatency() != LL_FLASH_LATENCY_1)
144   {
145   Error_Handler();  
146   }
147   LL_RCC_HSI_Enable();
148
149    /* Wait till HSI is ready */
150   while(LL_RCC_HSI_IsReady() != 1)
151   {
152     
153   }
154   LL_RCC_HSI_SetCalibTrimming(16);
155
156   LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSI_DIV_2, LL_RCC_PLL_MUL_8);
157
158   LL_RCC_PLL_Enable();
159
160    /* Wait till PLL is ready */
161   while(LL_RCC_PLL_IsReady() != 1)
162   {
163     
164   }
165   LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
166
167   LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1);
168
169   LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
170
171    /* Wait till System clock is ready */
172   while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
173   {
174   
175   }
176   LL_Init1msTick(32000000);
177
178   LL_SYSTICK_SetClkSource(LL_SYSTICK_CLKSOURCE_HCLK);
179
180   LL_SetSystemCoreClock(32000000);
181
182   LL_RCC_SetI2CClockSource(LL_RCC_I2C1_CLKSOURCE_HSI);
183
184   /* SysTick_IRQn interrupt configuration */
185   NVIC_SetPriority(SysTick_IRQn, 0);
186 }
187
188 /* I2C1 init function */
189 static void MX_I2C1_Init(void)
190 {
191
192   LL_I2C_InitTypeDef I2C_InitStruct;
193
194   LL_GPIO_InitTypeDef GPIO_InitStruct;
195
196   /**I2C1 GPIO Configuration  
197   PB6   ------> I2C1_SCL
198   PB7   ------> I2C1_SDA 
199   */
200   GPIO_InitStruct.Pin = LL_GPIO_PIN_6;
201   GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
202   GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
203   GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
204   GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
205   GPIO_InitStruct.Alternate = LL_GPIO_AF_1;
206   LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
207
208   GPIO_InitStruct.Pin = LL_GPIO_PIN_7;
209   GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
210   GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
211   GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
212   GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
213   GPIO_InitStruct.Alternate = LL_GPIO_AF_1;
214   LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
215
216   /* Peripheral clock enable */
217   LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_I2C1);
218
219     /**I2C Initialization 
220     */
221   I2C_InitStruct.PeripheralMode = LL_I2C_MODE_I2C;
222   I2C_InitStruct.Timing = 0x2000090E;
223   I2C_InitStruct.AnalogFilter = LL_I2C_ANALOGFILTER_ENABLE;
224   I2C_InitStruct.DigitalFilter = 0;
225   I2C_InitStruct.OwnAddress1 = 0;
226   I2C_InitStruct.TypeAcknowledge = LL_I2C_ACK;
227   I2C_InitStruct.OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT;
228   LL_I2C_Init(I2C1, &I2C_InitStruct);
229
230   LL_I2C_EnableAutoEndMode(I2C1);
231
232   LL_I2C_SetOwnAddress2(I2C1, 0, LL_I2C_OWNADDRESS2_NOMASK);
233
234   LL_I2C_DisableOwnAddress2(I2C1);
235
236   LL_I2C_DisableGeneralCall(I2C1);
237
238   LL_I2C_EnableClockStretching(I2C1);
239
240 }
241
242 /* USART2 init function */
243 static void MX_USART2_UART_Init(void)
244 {
245
246   LL_USART_InitTypeDef USART_InitStruct;
247
248   LL_GPIO_InitTypeDef GPIO_InitStruct;
249
250   /* Peripheral clock enable */
251   LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_USART2);
252   
253   /**USART2 GPIO Configuration  
254   PA2   ------> USART2_TX
255   PA3   ------> USART2_RX 
256   */
257   GPIO_InitStruct.Pin = USART_TX_Pin;
258   GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
259   GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
260   GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
261   GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
262   GPIO_InitStruct.Alternate = LL_GPIO_AF_1;
263   LL_GPIO_Init(USART_TX_GPIO_Port, &GPIO_InitStruct);
264
265   GPIO_InitStruct.Pin = USART_RX_Pin;
266   GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
267   GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
268   GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
269   GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
270   GPIO_InitStruct.Alternate = LL_GPIO_AF_1;
271   LL_GPIO_Init(USART_RX_GPIO_Port, &GPIO_InitStruct);
272
273   USART_InitStruct.BaudRate = 115200;
274   USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B;
275   USART_InitStruct.StopBits = LL_USART_STOPBITS_1;
276   USART_InitStruct.Parity = LL_USART_PARITY_NONE;
277   USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX_RX;
278   USART_InitStruct.HardwareFlowControl = LL_USART_HWCONTROL_NONE;
279   USART_InitStruct.OverSampling = LL_USART_OVERSAMPLING_16;
280   LL_USART_Init(USART2, &USART_InitStruct);
281
282   LL_USART_DisableIT_CTS(USART2);
283
284   LL_USART_DisableOverrunDetect(USART2);
285
286   LL_USART_ConfigAsyncMode(USART2);
287
288   LL_USART_Enable(USART2);
289
290 }
291
292 /** Configure pins as 
293         * Analog 
294         * Input 
295         * Output
296         * EVENT_OUT
297         * EXTI
298 */
299 static void MX_GPIO_Init(void)
300 {
301
302   LL_EXTI_InitTypeDef EXTI_InitStruct;
303   LL_GPIO_InitTypeDef GPIO_InitStruct;
304
305   /* GPIO Ports Clock Enable */
306   LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOC);
307   LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOF);
308   LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOA);
309   LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOB);
310
311   /**/
312   LL_GPIO_ResetOutputPin(LD2_GPIO_Port, LD2_Pin);
313
314   /**/
315   LL_SYSCFG_SetEXTISource(LL_SYSCFG_EXTI_PORTC, LL_SYSCFG_EXTI_LINE13);
316
317   /**/
318   LL_GPIO_SetPinPull(B1_GPIO_Port, B1_Pin, LL_GPIO_PULL_NO);
319
320   /**/
321   LL_GPIO_SetPinMode(B1_GPIO_Port, B1_Pin, LL_GPIO_MODE_INPUT);
322
323   /**/
324   EXTI_InitStruct.Line_0_31 = LL_EXTI_LINE_13;
325   EXTI_InitStruct.LineCommand = ENABLE;
326   EXTI_InitStruct.Mode = LL_EXTI_MODE_IT;
327   EXTI_InitStruct.Trigger = LL_EXTI_TRIGGER_FALLING;
328   LL_EXTI_Init(&EXTI_InitStruct);
329
330   /**/
331   GPIO_InitStruct.Pin = LD2_Pin;
332   GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
333   GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
334   GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
335   GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
336   LL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
337
338 }
339
340 /* USER CODE BEGIN 4 */
341
342 /* USER CODE END 4 */
343
344 /**
345   * @brief  This function is executed in case of error occurrence.
346   * @param  file: The file name as string.
347   * @param  line: The line in file as a number.
348   * @retval None
349   */
350 void _Error_Handler(char *file, int line)
351 {
352   /* USER CODE BEGIN Error_Handler_Debug */
353   /* User can add his own implementation to report the HAL error return state */
354   while(1)
355   {
356   }
357   /* USER CODE END Error_Handler_Debug */
358 }
359
360 #ifdef  USE_FULL_ASSERT
361 /**
362   * @brief  Reports the name of the source file and the source line number
363   *         where the assert_param error has occurred.
364   * @param  file: pointer to the source file name
365   * @param  line: assert_param error line source number
366   * @retval None
367   */
368 void assert_failed(uint8_t* file, uint32_t line)
369
370   /* USER CODE BEGIN 6 */
371   /* User can add his own implementation to report the file name and line number,
372      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
373   /* USER CODE END 6 */
374 }
375 #endif /* USE_FULL_ASSERT */
376
377 /**
378   * @}
379   */
380
381 /**
382   * @}
383   */
384
385 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/