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