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