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