]> mj.ucw.cz Git - home-hw.git/blob - nucleo-test/Src/main.c
First bits of DS18B20 communication
[home-hw.git] / nucleo-test / 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_DMA_Init(void);
58 static void MX_USART2_UART_Init(void);
59 static void MX_I2C1_Init(void);
60 static void MX_TIM3_Init(void);
61
62 /* USER CODE BEGIN PFP */
63 /* Private function prototypes -----------------------------------------------*/
64
65 /* USER CODE END PFP */
66
67 /* USER CODE BEGIN 0 */
68
69 /* USER CODE END 0 */
70
71 /**
72   * @brief  The application entry point.
73   *
74   * @retval None
75   */
76 int main(void)
77 {
78   /* USER CODE BEGIN 1 */
79
80   /* USER CODE END 1 */
81
82   /* MCU Configuration----------------------------------------------------------*/
83
84   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
85   LL_Init();
86
87   /* USER CODE BEGIN Init */
88
89   /* USER CODE END Init */
90
91   /* Configure the system clock */
92   SystemClock_Config();
93
94   /* USER CODE BEGIN SysInit */
95
96   /* USER CODE END SysInit */
97
98   /* Initialize all configured peripherals */
99   MX_GPIO_Init();
100   MX_DMA_Init();
101   MX_USART2_UART_Init();
102   MX_I2C1_Init();
103   MX_TIM3_Init();
104   /* USER CODE BEGIN 2 */
105
106   run_test();
107
108   /* USER CODE END 2 */
109
110   /* Infinite loop */
111   /* USER CODE BEGIN WHILE */
112   while (1)
113   {
114   /* USER CODE END WHILE */
115
116   /* USER CODE BEGIN 3 */
117   }
118   /* USER CODE END 3 */
119
120 }
121
122 static void LL_Init(void)
123 {
124   
125
126   LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_SYSCFG);
127
128   /* System interrupt init*/
129   /* SVC_IRQn interrupt configuration */
130   NVIC_SetPriority(SVC_IRQn, 0);
131   /* PendSV_IRQn interrupt configuration */
132   NVIC_SetPriority(PendSV_IRQn, 0);
133   /* SysTick_IRQn interrupt configuration */
134   NVIC_SetPriority(SysTick_IRQn, 0);
135
136 }
137
138 /**
139   * @brief System Clock Configuration
140   * @retval None
141   */
142 void SystemClock_Config(void)
143 {
144
145   LL_FLASH_SetLatency(LL_FLASH_LATENCY_1);
146
147   if(LL_FLASH_GetLatency() != LL_FLASH_LATENCY_1)
148   {
149   Error_Handler();  
150   }
151   LL_RCC_HSI_Enable();
152
153    /* Wait till HSI is ready */
154   while(LL_RCC_HSI_IsReady() != 1)
155   {
156     
157   }
158   LL_RCC_HSI_SetCalibTrimming(16);
159
160   LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSI_DIV_2, LL_RCC_PLL_MUL_8);
161
162   LL_RCC_PLL_Enable();
163
164    /* Wait till PLL is ready */
165   while(LL_RCC_PLL_IsReady() != 1)
166   {
167     
168   }
169   LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
170
171   LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1);
172
173   LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
174
175    /* Wait till System clock is ready */
176   while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
177   {
178   
179   }
180   LL_Init1msTick(32000000);
181
182   LL_SYSTICK_SetClkSource(LL_SYSTICK_CLKSOURCE_HCLK);
183
184   LL_SetSystemCoreClock(32000000);
185
186   LL_RCC_SetI2CClockSource(LL_RCC_I2C1_CLKSOURCE_HSI);
187
188   /* SysTick_IRQn interrupt configuration */
189   NVIC_SetPriority(SysTick_IRQn, 0);
190 }
191
192 /* I2C1 init function */
193 static void MX_I2C1_Init(void)
194 {
195
196   LL_I2C_InitTypeDef I2C_InitStruct;
197
198   LL_GPIO_InitTypeDef GPIO_InitStruct;
199
200   /**I2C1 GPIO Configuration  
201   PB8   ------> I2C1_SCL
202   PB9   ------> I2C1_SDA 
203   */
204   GPIO_InitStruct.Pin = LL_GPIO_PIN_8;
205   GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
206   GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
207   GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
208   GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
209   GPIO_InitStruct.Alternate = LL_GPIO_AF_1;
210   LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
211
212   GPIO_InitStruct.Pin = LL_GPIO_PIN_9;
213   GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
214   GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
215   GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
216   GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
217   GPIO_InitStruct.Alternate = LL_GPIO_AF_1;
218   LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
219
220   /* Peripheral clock enable */
221   LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_I2C1);
222
223     /**I2C Initialization 
224     */
225   I2C_InitStruct.PeripheralMode = LL_I2C_MODE_I2C;
226   I2C_InitStruct.Timing = 0x2000090E;
227   I2C_InitStruct.AnalogFilter = LL_I2C_ANALOGFILTER_ENABLE;
228   I2C_InitStruct.DigitalFilter = 0;
229   I2C_InitStruct.OwnAddress1 = 0;
230   I2C_InitStruct.TypeAcknowledge = LL_I2C_ACK;
231   I2C_InitStruct.OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT;
232   LL_I2C_Init(I2C1, &I2C_InitStruct);
233
234   LL_I2C_EnableAutoEndMode(I2C1);
235
236   LL_I2C_SetOwnAddress2(I2C1, 0, LL_I2C_OWNADDRESS2_NOMASK);
237
238   LL_I2C_DisableOwnAddress2(I2C1);
239
240   LL_I2C_DisableGeneralCall(I2C1);
241
242   LL_I2C_EnableClockStretching(I2C1);
243
244 }
245
246 /* TIM3 init function */
247 static void MX_TIM3_Init(void)
248 {
249
250   LL_TIM_InitTypeDef TIM_InitStruct;
251   LL_TIM_OC_InitTypeDef TIM_OC_InitStruct;
252
253   LL_GPIO_InitTypeDef GPIO_InitStruct;
254
255   /* Peripheral clock enable */
256   LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM3);
257
258   /* TIM3 DMA Init */
259   
260   /* TIM3_CH1_TRIG Init */
261   LL_DMA_SetDataTransferDirection(DMA1, LL_DMA_CHANNEL_4, LL_DMA_DIRECTION_PERIPH_TO_MEMORY);
262
263   LL_DMA_SetChannelPriorityLevel(DMA1, LL_DMA_CHANNEL_4, LL_DMA_PRIORITY_VERYHIGH);
264
265   LL_DMA_SetMode(DMA1, LL_DMA_CHANNEL_4, LL_DMA_MODE_NORMAL);
266
267   LL_DMA_SetPeriphIncMode(DMA1, LL_DMA_CHANNEL_4, LL_DMA_PERIPH_NOINCREMENT);
268
269   LL_DMA_SetMemoryIncMode(DMA1, LL_DMA_CHANNEL_4, LL_DMA_MEMORY_NOINCREMENT);
270
271   LL_DMA_SetPeriphSize(DMA1, LL_DMA_CHANNEL_4, LL_DMA_PDATAALIGN_WORD);
272
273   LL_DMA_SetMemorySize(DMA1, LL_DMA_CHANNEL_4, LL_DMA_MDATAALIGN_WORD);
274
275   /* TIM3 interrupt Init */
276   NVIC_SetPriority(TIM3_IRQn, 0);
277   NVIC_EnableIRQ(TIM3_IRQn);
278
279   TIM_InitStruct.Prescaler = 31;
280   TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
281   TIM_InitStruct.Autoreload = 0;
282   TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
283   LL_TIM_Init(TIM3, &TIM_InitStruct);
284
285   LL_TIM_DisableARRPreload(TIM3);
286
287   LL_TIM_SetClockSource(TIM3, LL_TIM_CLOCKSOURCE_INTERNAL);
288
289   TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_FROZEN;
290   TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE;
291   TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_DISABLE;
292   TIM_OC_InitStruct.CompareValue = 0;
293   TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH;
294   LL_TIM_OC_Init(TIM3, LL_TIM_CHANNEL_CH1, &TIM_OC_InitStruct);
295
296   LL_TIM_OC_DisableFast(TIM3, LL_TIM_CHANNEL_CH1);
297
298   TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_ACTIVE;
299   TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE;
300   TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_DISABLE;
301   LL_TIM_OC_Init(TIM3, LL_TIM_CHANNEL_CH2, &TIM_OC_InitStruct);
302
303   LL_TIM_OC_DisableFast(TIM3, LL_TIM_CHANNEL_CH2);
304
305   LL_TIM_SetTriggerOutput(TIM3, LL_TIM_TRGO_RESET);
306
307   LL_TIM_DisableMasterSlaveMode(TIM3);
308
309   /**TIM3 GPIO Configuration  
310   PA7   ------> TIM3_CH2 
311   */
312   GPIO_InitStruct.Pin = THERMO_Pin;
313   GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
314   GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
315   GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
316   GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
317   GPIO_InitStruct.Alternate = LL_GPIO_AF_1;
318   LL_GPIO_Init(THERMO_GPIO_Port, &GPIO_InitStruct);
319
320 }
321
322 /* USART2 init function */
323 static void MX_USART2_UART_Init(void)
324 {
325
326   LL_USART_InitTypeDef USART_InitStruct;
327
328   LL_GPIO_InitTypeDef GPIO_InitStruct;
329
330   /* Peripheral clock enable */
331   LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_USART2);
332   
333   /**USART2 GPIO Configuration  
334   PA2   ------> USART2_TX
335   PA3   ------> USART2_RX 
336   */
337   GPIO_InitStruct.Pin = USART_TX_Pin;
338   GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
339   GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
340   GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
341   GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
342   GPIO_InitStruct.Alternate = LL_GPIO_AF_1;
343   LL_GPIO_Init(USART_TX_GPIO_Port, &GPIO_InitStruct);
344
345   GPIO_InitStruct.Pin = USART_RX_Pin;
346   GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
347   GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
348   GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
349   GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
350   GPIO_InitStruct.Alternate = LL_GPIO_AF_1;
351   LL_GPIO_Init(USART_RX_GPIO_Port, &GPIO_InitStruct);
352
353   USART_InitStruct.BaudRate = 115200;
354   USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B;
355   USART_InitStruct.StopBits = LL_USART_STOPBITS_1;
356   USART_InitStruct.Parity = LL_USART_PARITY_NONE;
357   USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX_RX;
358   USART_InitStruct.HardwareFlowControl = LL_USART_HWCONTROL_NONE;
359   USART_InitStruct.OverSampling = LL_USART_OVERSAMPLING_16;
360   LL_USART_Init(USART2, &USART_InitStruct);
361
362   LL_USART_DisableIT_CTS(USART2);
363
364   LL_USART_DisableOverrunDetect(USART2);
365
366   LL_USART_ConfigAsyncMode(USART2);
367
368   LL_USART_Enable(USART2);
369
370 }
371
372 /** 
373   * Enable DMA controller clock
374   */
375 static void MX_DMA_Init(void) 
376 {
377   /* Init with LL driver */
378   /* DMA controller clock enable */
379   LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_DMA1);
380
381   /* DMA interrupt init */
382   /* DMA1_Channel4_5_IRQn interrupt configuration */
383   NVIC_SetPriority(DMA1_Channel4_5_IRQn, 0);
384   NVIC_EnableIRQ(DMA1_Channel4_5_IRQn);
385
386 }
387
388 /** Configure pins as 
389         * Analog 
390         * Input 
391         * Output
392         * EVENT_OUT
393         * EXTI
394 */
395 static void MX_GPIO_Init(void)
396 {
397
398   LL_GPIO_InitTypeDef GPIO_InitStruct;
399   LL_EXTI_InitTypeDef EXTI_InitStruct;
400
401   /* GPIO Ports Clock Enable */
402   LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOC);
403   LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOF);
404   LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOA);
405   LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOB);
406
407   /**/
408   LL_GPIO_ResetOutputPin(LD2_GPIO_Port, LD2_Pin);
409
410   /**/
411   LL_SYSCFG_SetEXTISource(LL_SYSCFG_EXTI_PORTC, LL_SYSCFG_EXTI_LINE13);
412
413   /**/
414   LL_GPIO_SetPinPull(BLUE_BUTTON_GPIO_Port, BLUE_BUTTON_Pin, LL_GPIO_PULL_NO);
415
416   /**/
417   LL_GPIO_SetPinMode(BLUE_BUTTON_GPIO_Port, BLUE_BUTTON_Pin, LL_GPIO_MODE_INPUT);
418
419   /**/
420   EXTI_InitStruct.Line_0_31 = LL_EXTI_LINE_13;
421   EXTI_InitStruct.LineCommand = ENABLE;
422   EXTI_InitStruct.Mode = LL_EXTI_MODE_IT;
423   EXTI_InitStruct.Trigger = LL_EXTI_TRIGGER_FALLING;
424   LL_EXTI_Init(&EXTI_InitStruct);
425
426   /**/
427   GPIO_InitStruct.Pin = LD2_Pin;
428   GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
429   GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
430   GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
431   GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
432   LL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
433
434   /**/
435   GPIO_InitStruct.Pin = BMP_DONE_Pin;
436   GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT;
437   GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
438   LL_GPIO_Init(BMP_DONE_GPIO_Port, &GPIO_InitStruct);
439
440 }
441
442 /* USER CODE BEGIN 4 */
443
444 /* USER CODE END 4 */
445
446 /**
447   * @brief  This function is executed in case of error occurrence.
448   * @param  file: The file name as string.
449   * @param  line: The line in file as a number.
450   * @retval None
451   */
452 void _Error_Handler(char *file, int line)
453 {
454   /* USER CODE BEGIN Error_Handler_Debug */
455   /* User can add his own implementation to report the HAL error return state */
456   while(1)
457   {
458   }
459   /* USER CODE END Error_Handler_Debug */
460 }
461
462 #ifdef  USE_FULL_ASSERT
463 /**
464   * @brief  Reports the name of the source file and the source line number
465   *         where the assert_param error has occurred.
466   * @param  file: pointer to the source file name
467   * @param  line: assert_param error line source number
468   * @retval None
469   */
470 void assert_failed(uint8_t* file, uint32_t line)
471
472   /* USER CODE BEGIN 6 */
473   /* User can add his own implementation to report the file name and line number,
474      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
475   /* USER CODE END 6 */
476 }
477 #endif /* USE_FULL_ASSERT */
478
479 /**
480   * @}
481   */
482
483 /**
484   * @}
485   */
486
487 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/