]> mj.ucw.cz Git - home-hw.git/blob - Src/main.c
Experiments with USBD
[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 International N.V. 
14   * All rights reserved.
15   *
16   * Redistribution and use in source and binary forms, with or without 
17   * modification, are permitted, provided that the following conditions are met:
18   *
19   * 1. Redistribution of source code must retain the above copyright notice, 
20   *    this list of conditions and the following disclaimer.
21   * 2. Redistributions in binary form must reproduce the above copyright notice,
22   *    this list of conditions and the following disclaimer in the documentation
23   *    and/or other materials provided with the distribution.
24   * 3. Neither the name of STMicroelectronics nor the names of other 
25   *    contributors to this software may be used to endorse or promote products 
26   *    derived from this software without specific written permission.
27   * 4. This software, including modifications and/or derivative works of this 
28   *    software, must execute solely and exclusively on microcontroller or
29   *    microprocessor devices manufactured by or for STMicroelectronics.
30   * 5. Redistribution and use of this software other than as permitted under 
31   *    this license is void and will automatically terminate your rights under 
32   *    this license. 
33   *
34   * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS" 
35   * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT 
36   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
37   * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
38   * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT 
39   * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
40   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
42   * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
43   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
44   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
45   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46   *
47   ******************************************************************************
48   */
49 /* Includes ------------------------------------------------------------------*/
50 #include "main.h"
51 #include "stm32f1xx_hal.h"
52 #include "usb_device.h"
53
54 /* USER CODE BEGIN Includes */
55
56 /* USER CODE END Includes */
57
58 /* Private variables ---------------------------------------------------------*/
59
60 /* USER CODE BEGIN PV */
61 /* Private variables ---------------------------------------------------------*/
62 USBD_HandleTypeDef USBD_Device;
63
64 /* USER CODE END PV */
65
66 /* Private function prototypes -----------------------------------------------*/
67 void SystemClock_Config(void);
68 static void MX_GPIO_Init(void);
69 static void MX_I2C1_Init(void);
70 static void MX_I2C2_Init(void);
71
72 /* USER CODE BEGIN PFP */
73 /* Private function prototypes -----------------------------------------------*/
74
75 /* USER CODE END PFP */
76
77 /* USER CODE BEGIN 0 */
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
106   /* USER CODE END SysInit */
107
108   /* Initialize all configured peripherals */
109   MX_GPIO_Init();
110   MX_I2C1_Init();
111   MX_I2C2_Init();
112   MX_USB_DEVICE_Init();
113   /* USER CODE BEGIN 2 */
114
115   /* USER CODE END 2 */
116
117   /* Infinite loop */
118   /* USER CODE BEGIN WHILE */
119   while (1)
120   {
121     LL_GPIO_SetOutputPin(LED_GPIO_Port, LED_Pin);
122     LL_mDelay(1000);
123     LL_GPIO_ResetOutputPin(LED_GPIO_Port, LED_Pin);
124     LL_mDelay(1000);
125
126   /* USER CODE END WHILE */
127
128   /* USER CODE BEGIN 3 */
129
130   }
131   /* USER CODE END 3 */
132
133 }
134
135 /**
136   * @brief System Clock Configuration
137   * @retval None
138   */
139 void SystemClock_Config(void)
140 {
141
142   LL_FLASH_SetLatency(LL_FLASH_LATENCY_2);
143
144    if(LL_FLASH_GetLatency() != LL_FLASH_LATENCY_2)
145   {
146     Error_Handler();  
147   }
148   LL_RCC_HSE_Enable();
149
150    /* Wait till HSE is ready */
151   while(LL_RCC_HSE_IsReady() != 1)
152   {
153     
154   }
155   LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSE_DIV_1, LL_RCC_PLL_MUL_9);
156
157   LL_RCC_PLL_Enable();
158
159    /* Wait till PLL is ready */
160   while(LL_RCC_PLL_IsReady() != 1)
161   {
162     
163   }
164   LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
165
166   LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_2);
167
168   LL_RCC_SetAPB2Prescaler(LL_RCC_APB2_DIV_1);
169
170   LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
171
172    /* Wait till System clock is ready */
173   while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
174   {
175   
176   }
177   LL_Init1msTick(72000000);
178
179   LL_SYSTICK_SetClkSource(LL_SYSTICK_CLKSOURCE_HCLK);
180
181   LL_SetSystemCoreClock(72000000);
182
183   LL_RCC_SetUSBClockSource(LL_RCC_USB_CLKSOURCE_PLL_DIV_1_5);
184
185   /* SysTick_IRQn interrupt configuration */
186   NVIC_SetPriority(SysTick_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
187 }
188
189 /* I2C1 init function */
190 static void MX_I2C1_Init(void)
191 {
192
193   LL_I2C_InitTypeDef I2C_InitStruct;
194
195   LL_GPIO_InitTypeDef GPIO_InitStruct;
196
197   /**I2C1 GPIO Configuration  
198   PB6   ------> I2C1_SCL
199   PB7   ------> I2C1_SDA 
200   */
201   GPIO_InitStruct.Pin = LL_GPIO_PIN_6|LL_GPIO_PIN_7;
202   GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
203   GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
204   GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
205   LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
206
207   /* Peripheral clock enable */
208   LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_I2C1);
209
210     /**I2C Initialization 
211     */
212   I2C_InitStruct.PeripheralMode = LL_I2C_MODE_I2C;
213   I2C_InitStruct.ClockSpeed = 100000;
214   I2C_InitStruct.DutyCycle = LL_I2C_DUTYCYCLE_2;
215   I2C_InitStruct.OwnAddress1 = 0;
216   I2C_InitStruct.TypeAcknowledge = LL_I2C_ACK;
217   I2C_InitStruct.OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT;
218   LL_I2C_Init(I2C1, &I2C_InitStruct);
219
220   LL_I2C_SetOwnAddress2(I2C1, 0);
221
222   LL_I2C_DisableOwnAddress2(I2C1);
223
224   LL_I2C_DisableGeneralCall(I2C1);
225
226   LL_I2C_EnableClockStretching(I2C1);
227
228 }
229
230 /* I2C2 init function */
231 static void MX_I2C2_Init(void)
232 {
233
234   LL_I2C_InitTypeDef I2C_InitStruct;
235
236   LL_GPIO_InitTypeDef GPIO_InitStruct;
237
238   /**I2C2 GPIO Configuration  
239   PB10   ------> I2C2_SCL
240   PB11   ------> I2C2_SDA 
241   */
242   GPIO_InitStruct.Pin = LL_GPIO_PIN_10|LL_GPIO_PIN_11;
243   GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
244   GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
245   GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
246   LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
247
248   /* Peripheral clock enable */
249   LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_I2C2);
250
251     /**I2C Initialization 
252     */
253   I2C_InitStruct.PeripheralMode = LL_I2C_MODE_I2C;
254   I2C_InitStruct.ClockSpeed = 400000;
255   I2C_InitStruct.DutyCycle = LL_I2C_DUTYCYCLE_2;
256   I2C_InitStruct.OwnAddress1 = 0;
257   I2C_InitStruct.TypeAcknowledge = LL_I2C_ACK;
258   I2C_InitStruct.OwnAddrSize = LL_I2C_OWNADDRESS1_7BIT;
259   LL_I2C_Init(I2C2, &I2C_InitStruct);
260
261   LL_I2C_SetOwnAddress2(I2C2, 0);
262
263   LL_I2C_DisableOwnAddress2(I2C2);
264
265   LL_I2C_DisableGeneralCall(I2C2);
266
267   LL_I2C_EnableClockStretching(I2C2);
268
269 }
270
271 /** Configure pins as 
272         * Analog 
273         * Input 
274         * Output
275         * EVENT_OUT
276         * EXTI
277 */
278 static void MX_GPIO_Init(void)
279 {
280
281   LL_GPIO_InitTypeDef GPIO_InitStruct;
282
283   /* GPIO Ports Clock Enable */
284   LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOC);
285   LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOD);
286   LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOB);
287   LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA);
288
289   /**/
290   LL_GPIO_ResetOutputPin(LED_GPIO_Port, LED_Pin);
291
292   /**/
293   GPIO_InitStruct.Pin = LED_Pin;
294   GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
295   GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
296   GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
297   LL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);
298
299   /**/
300   GPIO_InitStruct.Pin = BMP_DONE_Pin;
301   GPIO_InitStruct.Mode = LL_GPIO_MODE_FLOATING;
302   LL_GPIO_Init(BMP_DONE_GPIO_Port, &GPIO_InitStruct);
303
304 }
305
306 /* USER CODE BEGIN 4 */
307
308 /* USER CODE END 4 */
309
310 /**
311   * @brief  This function is executed in case of error occurrence.
312   * @param  file: The file name as string.
313   * @param  line: The line in file as a number.
314   * @retval None
315   */
316 void _Error_Handler(char *file, int line)
317 {
318   /* USER CODE BEGIN Error_Handler_Debug */
319   /* User can add his own implementation to report the HAL error return state */
320   while(1)
321   {
322   }
323   /* USER CODE END Error_Handler_Debug */
324 }
325
326 #ifdef  USE_FULL_ASSERT
327 /**
328   * @brief  Reports the name of the source file and the source line number
329   *         where the assert_param error has occurred.
330   * @param  file: pointer to the source file name
331   * @param  line: assert_param error line source number
332   * @retval None
333   */
334 void assert_failed(uint8_t* file, uint32_t line)
335
336   /* USER CODE BEGIN 6 */
337   /* User can add his own implementation to report the file name and line number,
338      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
339   /* USER CODE END 6 */
340 }
341 #endif /* USE_FULL_ASSERT */
342
343 /**
344   * @}
345   */
346
347 /**
348   * @}
349   */
350
351 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/