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