]> mj.ucw.cz Git - home-hw.git/blob - Src/main.c
We have a working timer interrupt!
[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 #include "stm32f1xx_hal.h"
42
43 /* USER CODE BEGIN Includes */
44 #include "util.h"
45 #include "usb.h"
46 #include "app.h"
47
48 /* USER CODE END Includes */
49
50 /* Private variables ---------------------------------------------------------*/
51
52 PCD_HandleTypeDef hpcd_USB_FS;
53
54 /* USER CODE BEGIN PV */
55 /* Private variables ---------------------------------------------------------*/
56 struct usb usb;
57
58 /* USER CODE END PV */
59
60 /* Private function prototypes -----------------------------------------------*/
61 void SystemClock_Config(void);
62 static void MX_GPIO_Init(void);
63 static void MX_I2C1_Init(void);
64 static void MX_I2C2_Init(void);
65 static void MX_USB_PCD_Init(void);
66 static void MX_TIM4_Init(void);
67
68 /* USER CODE BEGIN PFP */
69 /* Private function prototypes -----------------------------------------------*/
70
71 /* USER CODE END PFP */
72
73 /* USER CODE BEGIN 0 */
74
75 /* USER CODE END 0 */
76
77 /**
78   * @brief  The application entry point.
79   *
80   * @retval None
81   */
82 int main(void)
83 {
84   /* USER CODE BEGIN 1 */
85
86   /* USER CODE END 1 */
87
88   /* MCU Configuration----------------------------------------------------------*/
89
90   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
91   HAL_Init();
92
93   /* USER CODE BEGIN Init */
94
95   /* USER CODE END Init */
96
97   /* Configure the system clock */
98   SystemClock_Config();
99
100   /* USER CODE BEGIN SysInit */
101   usb_init(&usb, &hpcd_USB_FS);
102
103   /* USER CODE END SysInit */
104
105   /* Initialize all configured peripherals */
106   MX_GPIO_Init();
107
108   // A hack to let USB host reset us
109   LL_GPIO_InitTypeDef gpio;
110   gpio.Pin = LL_GPIO_PIN_12 | LL_GPIO_PIN_13;
111   gpio.Mode = LL_GPIO_MODE_OUTPUT;
112   gpio.Speed = LL_GPIO_SPEED_FREQ_HIGH;
113   gpio.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
114   LL_GPIO_Init(GPIOA, &gpio);
115   LL_GPIO_ResetOutputPin(GPIOA, LL_GPIO_PIN_12);
116   LL_GPIO_ResetOutputPin(GPIOA, LL_GPIO_PIN_13);
117   LL_mDelay(1000);
118
119   MX_I2C1_Init();
120   MX_I2C2_Init();
121   MX_USB_PCD_Init();
122   MX_TIM4_Init();
123   /* USER CODE BEGIN 2 */
124   display_init();
125   usb_start(&usb);
126
127   LL_TIM_EnableCounter(TIM4);
128   LL_TIM_EnableIT_UPDATE(TIM4);
129   LL_TIM_GenerateEvent_UPDATE(TIM4);
130
131   /* USER CODE END 2 */
132
133   /* Infinite loop */
134   /* USER CODE BEGIN WHILE */
135   int cnt = 0;
136   while (1)
137   {
138     debug_printf("Counter = %d\n", cnt);
139     display_counter(cnt);
140     LL_mDelay(1000);
141     cnt++;
142
143   /* USER CODE END WHILE */
144
145   /* USER CODE BEGIN 3 */
146
147   }
148   /* USER CODE END 3 */
149
150 }
151
152 /**
153   * @brief System Clock Configuration
154   * @retval None
155   */
156 void SystemClock_Config(void)
157 {
158
159   LL_FLASH_SetLatency(LL_FLASH_LATENCY_2);
160
161    if(LL_FLASH_GetLatency() != LL_FLASH_LATENCY_2)
162   {
163     Error_Handler();  
164   }
165   LL_RCC_HSE_Enable();
166
167    /* Wait till HSE is ready */
168   while(LL_RCC_HSE_IsReady() != 1)
169   {
170     
171   }
172   LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE_DIV_1, LL_RCC_PLL_MUL_9);
173
174   LL_RCC_PLL_Enable();
175
176    /* Wait till PLL is ready */
177   while(LL_RCC_PLL_IsReady() != 1)
178   {
179     
180   }
181   LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
182
183   LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_2);
184
185   LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1);
186
187   LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
188
189    /* Wait till System clock is ready */
190   while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
191   {
192   
193   }
194   LL_Init1msTick(72000000);
195
196   LL_SYSTICK_SetClkSource(LL_SYSTICK_CLKSOURCE_HCLK);
197
198   LL_SetSystemCoreClock(72000000);
199
200   LL_RCC_SetUSBClockSource(LL_RCC_USB_CLKSOURCE_PLL_DIV_1_5);
201
202   /* SysTick_IRQn interrupt configuration */
203   NVIC_SetPriority(SysTick_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
204 }
205
206 /* I2C1 init function */
207 static void MX_I2C1_Init(void)
208 {
209
210   LL_I2C_InitTypeDef I2C_InitStruct;
211
212   LL_GPIO_InitTypeDef GPIO_InitStruct;
213
214   /**I2C1 GPIO Configuration  
215   PB6   ------> I2C1_SCL
216   PB7   ------> I2C1_SDA 
217   */
218   GPIO_InitStruct.Pin = LL_GPIO_PIN_6|LL_GPIO_PIN_7;
219   GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
220   GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
221   GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
222   LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
223
224   /* Peripheral clock enable */
225   LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_I2C1);
226
227     /**I2C Initialization 
228     */
229   I2C_InitStruct.PeripheralMode = LL_I2C_MODE_I2C;
230   I2C_InitStruct.ClockSpeed = 100000;
231   I2C_InitStruct.DutyCycle = LL_I2C_DUTYCYCLE_2;
232   I2C_InitStruct.OwnAddress1 = 0;
233   I2C_InitStruct.TypeAcknowledge = LL_I2C_ACK;
234   I2C_InitStruct.OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT;
235   LL_I2C_Init(I2C1, &I2C_InitStruct);
236
237   LL_I2C_SetOwnAddress2(I2C1, 0);
238
239   LL_I2C_DisableOwnAddress2(I2C1);
240
241   LL_I2C_DisableGeneralCall(I2C1);
242
243   LL_I2C_EnableClockStretching(I2C1);
244
245 }
246
247 /* I2C2 init function */
248 static void MX_I2C2_Init(void)
249 {
250
251   LL_I2C_InitTypeDef I2C_InitStruct;
252
253   LL_GPIO_InitTypeDef GPIO_InitStruct;
254
255   /**I2C2 GPIO Configuration  
256   PB10   ------> I2C2_SCL
257   PB11   ------> I2C2_SDA 
258   */
259   GPIO_InitStruct.Pin = LL_GPIO_PIN_10|LL_GPIO_PIN_11;
260   GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
261   GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
262   GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
263   LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
264
265   /* Peripheral clock enable */
266   LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_I2C2);
267
268     /**I2C Initialization 
269     */
270   I2C_InitStruct.PeripheralMode = LL_I2C_MODE_I2C;
271   I2C_InitStruct.ClockSpeed = 400000;
272   I2C_InitStruct.DutyCycle = LL_I2C_DUTYCYCLE_2;
273   I2C_InitStruct.OwnAddress1 = 0;
274   I2C_InitStruct.TypeAcknowledge = LL_I2C_ACK;
275   I2C_InitStruct.OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT;
276   LL_I2C_Init(I2C2, &I2C_InitStruct);
277
278   LL_I2C_SetOwnAddress2(I2C2, 0);
279
280   LL_I2C_DisableOwnAddress2(I2C2);
281
282   LL_I2C_DisableGeneralCall(I2C2);
283
284   LL_I2C_EnableClockStretching(I2C2);
285
286 }
287
288 /* TIM4 init function */
289 static void MX_TIM4_Init(void)
290 {
291
292   LL_TIM_InitTypeDef TIM_InitStruct;
293   LL_TIM_OC_InitTypeDef TIM_OC_InitStruct;
294
295   /* Peripheral clock enable */
296   LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM4);
297
298   /* TIM4 interrupt Init */
299   NVIC_SetPriority(TIM4_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
300   NVIC_EnableIRQ(TIM4_IRQn);
301
302   TIM_InitStruct.Prescaler = 7200;
303   TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
304   TIM_InitStruct.Autoreload = 1000;
305   TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
306   LL_TIM_Init(TIM4, &TIM_InitStruct);
307
308   LL_TIM_DisableARRPreload(TIM4);
309
310   LL_TIM_SetClockSource(TIM4, LL_TIM_CLOCKSOURCE_INTERNAL);
311
312   TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_FROZEN;
313   TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE;
314   TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_DISABLE;
315   TIM_OC_InitStruct.CompareValue = 0;
316   TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH;
317   LL_TIM_OC_Init(TIM4, LL_TIM_CHANNEL_CH1, &TIM_OC_InitStruct);
318
319   LL_TIM_OC_DisableFast(TIM4, LL_TIM_CHANNEL_CH1);
320
321   LL_TIM_SetTriggerOutput(TIM4, LL_TIM_TRGO_RESET);
322
323   LL_TIM_DisableMasterSlaveMode(TIM4);
324
325 }
326
327 /* USB init function */
328 static void MX_USB_PCD_Init(void)
329 {
330
331   hpcd_USB_FS.Instance = USB;
332   hpcd_USB_FS.Init.dev_endpoints = 8;
333   hpcd_USB_FS.Init.speed = PCD_SPEED_FULL;
334   hpcd_USB_FS.Init.ep0_mps = DEP0CTL_MPS_64;
335   hpcd_USB_FS.Init.low_power_enable = DISABLE;
336   hpcd_USB_FS.Init.lpm_enable = DISABLE;
337   hpcd_USB_FS.Init.battery_charging_enable = DISABLE;
338   if (HAL_PCD_Init(&hpcd_USB_FS) != HAL_OK)
339   {
340     _Error_Handler(__FILE__, __LINE__);
341   }
342
343 }
344
345 /** Configure pins as 
346         * Analog 
347         * Input 
348         * Output
349         * EVENT_OUT
350         * EXTI
351 */
352 static void MX_GPIO_Init(void)
353 {
354
355   LL_GPIO_InitTypeDef GPIO_InitStruct;
356
357   /* GPIO Ports Clock Enable */
358   LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOC);
359   LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOD);
360   LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOB);
361   LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA);
362
363   /**/
364   LL_GPIO_ResetOutputPin(LED_GPIO_Port, LED_Pin);
365
366   /**/
367   GPIO_InitStruct.Pin = LED_Pin;
368   GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
369   GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
370   GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
371   LL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);
372
373   /**/
374   GPIO_InitStruct.Pin = BMP_DONE_Pin;
375   GPIO_InitStruct.Mode = LL_GPIO_MODE_FLOATING;
376   LL_GPIO_Init(BMP_DONE_GPIO_Port, &GPIO_InitStruct);
377
378 }
379
380 /* USER CODE BEGIN 4 */
381
382 /* USER CODE END 4 */
383
384 /**
385   * @brief  This function is executed in case of error occurrence.
386   * @param  file: The file name as string.
387   * @param  line: The line in file as a number.
388   * @retval None
389   */
390 void _Error_Handler(char *file, int line)
391 {
392   /* USER CODE BEGIN Error_Handler_Debug */
393   /* User can add his own implementation to report the HAL error return state */
394   while(1)
395   {
396   }
397   /* USER CODE END Error_Handler_Debug */
398 }
399
400 #ifdef  USE_FULL_ASSERT
401 /**
402   * @brief  Reports the name of the source file and the source line number
403   *         where the assert_param error has occurred.
404   * @param  file: pointer to the source file name
405   * @param  line: assert_param error line source number
406   * @retval None
407   */
408 void assert_failed(uint8_t* file, uint32_t line)
409
410   /* USER CODE BEGIN 6 */
411   /* User can add his own implementation to report the file name and line number,
412      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
413   /* USER CODE END 6 */
414 }
415 #endif /* USE_FULL_ASSERT */
416
417 /**
418   * @}
419   */
420
421 /**
422   * @}
423   */
424
425 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/