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