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