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