Rev 23 | Rev 25 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | mjames | 1 | /* USER CODE BEGIN Header */ |
2 | /** |
||
3 | ****************************************************************************** |
||
4 | * @file : main.c |
||
5 | * @brief : Main program body |
||
6 | ****************************************************************************** |
||
7 | * @attention |
||
8 | * |
||
9 | * <h2><center>© Copyright (c) 2020 STMicroelectronics. |
||
10 | * All rights reserved.</center></h2> |
||
11 | * |
||
12 | * This software component is licensed by ST under BSD 3-Clause license, |
||
13 | * the "License"; You may not use this file except in compliance with the |
||
14 | * License. You may obtain a copy of the License at: |
||
15 | * opensource.org/licenses/BSD-3-Clause |
||
16 | * |
||
17 | ****************************************************************************** |
||
18 | */ |
||
19 | /* USER CODE END Header */ |
||
20 | /* Includes ------------------------------------------------------------------*/ |
||
21 | #include "main.h" |
||
13 | mjames | 22 | #include "usb_device.h" |
2 | mjames | 23 | |
24 | /* Private includes ----------------------------------------------------------*/ |
||
25 | /* USER CODE BEGIN Includes */ |
||
26 | #include "libSerial/serial.h" |
||
11 | mjames | 27 | #include "libBMP280/bmp280.h" |
6 | mjames | 28 | #include "display.h" |
2 | mjames | 29 | /* USER CODE END Includes */ |
30 | |||
31 | /* Private typedef -----------------------------------------------------------*/ |
||
32 | /* USER CODE BEGIN PTD */ |
||
33 | |||
34 | /* USER CODE END PTD */ |
||
35 | |||
36 | /* Private define ------------------------------------------------------------*/ |
||
37 | /* USER CODE BEGIN PD */ |
||
38 | /* USER CODE END PD */ |
||
39 | |||
40 | /* Private macro -------------------------------------------------------------*/ |
||
41 | /* USER CODE BEGIN PM */ |
||
42 | |||
43 | /* USER CODE END PM */ |
||
44 | |||
45 | /* Private variables ---------------------------------------------------------*/ |
||
11 | mjames | 46 | I2C_HandleTypeDef hi2c2; |
47 | |||
13 | mjames | 48 | RTC_HandleTypeDef hrtc; |
49 | |||
2 | mjames | 50 | SPI_HandleTypeDef hspi1; |
51 | |||
9 | mjames | 52 | TIM_HandleTypeDef htim3; |
2 | mjames | 53 | TIM_HandleTypeDef htim4; |
54 | |||
55 | UART_HandleTypeDef huart1; |
||
56 | |||
57 | /* USER CODE BEGIN PV */ |
||
11 | mjames | 58 | typedef struct |
59 | { |
||
60 | uint8_t dev_addr; |
||
61 | } interface_t; |
||
2 | mjames | 62 | |
11 | mjames | 63 | static int8_t |
24 | mjames | 64 | user_i2c_write (uint8_t i2c_addr, uint8_t reg_addr, uint8_t *reg_data, |
65 | uint32_t len) |
||
11 | mjames | 66 | { |
24 | mjames | 67 | HAL_StatusTypeDef st = HAL_I2C_Mem_Write (&hi2c2, i2c_addr << 1, reg_addr, 1, |
68 | reg_data, len, 10000); |
||
11 | mjames | 69 | |
24 | mjames | 70 | return st != HAL_OK ? BMP280_E_COMM_FAIL : BMP280_OK; |
11 | mjames | 71 | |
72 | } |
||
73 | static int8_t |
||
24 | mjames | 74 | user_i2c_read (uint8_t i2c_addr, uint8_t reg_addr, uint8_t *reg_data, |
75 | uint32_t len) |
||
11 | mjames | 76 | { |
24 | mjames | 77 | HAL_StatusTypeDef st = HAL_I2C_Mem_Read (&hi2c2, i2c_addr << 1, reg_addr, 1, |
78 | reg_data, len, 10000); |
||
11 | mjames | 79 | |
24 | mjames | 80 | return st != HAL_OK ? BMP280_E_COMM_FAIL : BMP280_OK; |
11 | mjames | 81 | |
82 | } |
||
83 | |||
84 | static void |
||
85 | user_delay_ms (uint32_t ms, void *handle) |
||
86 | { |
||
87 | HAL_Delay (ms); |
||
88 | |||
89 | } |
||
90 | |||
91 | struct bmp280_dev bmp = |
||
92 | { |
||
93 | |||
94 | .intf = BMP280_I2C_INTF, .read = user_i2c_read, .write = user_i2c_write, |
||
95 | .delay_ms = user_delay_ms, |
||
96 | |||
97 | /* Update interface pointer with the structure that contains both device address and file descriptor */ |
||
24 | mjames | 98 | .dev_id = BMP280_I2C_ADDR_PRIM }; |
11 | mjames | 99 | |
100 | int8_t rslt; |
||
101 | struct bmp280_config conf; |
||
102 | |||
2 | mjames | 103 | /* USER CODE END PV */ |
104 | |||
105 | /* Private function prototypes -----------------------------------------------*/ |
||
24 | mjames | 106 | void |
107 | SystemClock_Config (void); |
||
108 | static void |
||
109 | MX_GPIO_Init (void); |
||
110 | static void |
||
111 | MX_SPI1_Init (void); |
||
112 | static void |
||
113 | MX_TIM4_Init (void); |
||
114 | static void |
||
115 | MX_USART1_UART_Init (void); |
||
116 | static void |
||
117 | MX_TIM3_Init (void); |
||
118 | static void |
||
119 | MX_I2C2_Init (void); |
||
120 | static void |
||
121 | MX_RTC_Init (void); |
||
2 | mjames | 122 | /* USER CODE BEGIN PFP */ |
123 | |||
124 | /* USER CODE END PFP */ |
||
125 | |||
126 | /* Private user code ---------------------------------------------------------*/ |
||
127 | /* USER CODE BEGIN 0 */ |
||
128 | |||
24 | mjames | 129 | uint32_t const TICKS_LOOP = 50; // 100mS per loop. |
130 | |||
2 | mjames | 131 | /* USER CODE END 0 */ |
132 | |||
133 | /** |
||
24 | mjames | 134 | * @brief The application entry point. |
135 | * @retval int |
||
136 | */ |
||
137 | int |
||
138 | main (void) |
||
2 | mjames | 139 | { |
140 | /* USER CODE BEGIN 1 */ |
||
141 | |||
142 | /* USER CODE END 1 */ |
||
143 | |||
144 | /* MCU Configuration--------------------------------------------------------*/ |
||
145 | |||
146 | /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ |
||
24 | mjames | 147 | HAL_Init (); |
2 | mjames | 148 | |
149 | /* USER CODE BEGIN Init */ |
||
150 | |||
151 | /* USER CODE END Init */ |
||
152 | |||
153 | /* Configure the system clock */ |
||
24 | mjames | 154 | SystemClock_Config (); |
2 | mjames | 155 | |
156 | /* USER CODE BEGIN SysInit */ |
||
157 | |||
158 | /* USER CODE END SysInit */ |
||
159 | |||
160 | /* Initialize all configured peripherals */ |
||
24 | mjames | 161 | MX_GPIO_Init (); |
162 | MX_SPI1_Init (); |
||
163 | MX_TIM4_Init (); |
||
164 | MX_USART1_UART_Init (); |
||
165 | MX_TIM3_Init (); |
||
166 | MX_I2C2_Init (); |
||
167 | MX_RTC_Init (); |
||
168 | MX_USB_DEVICE_Init (); |
||
2 | mjames | 169 | /* USER CODE BEGIN 2 */ |
14 | mjames | 170 | |
171 | HAL_GPIO_WritePin ( USB_PULLUP_GPIO_Port, USB_PULLUP_Pin, GPIO_PIN_RESET); |
||
172 | HAL_Delay (1000); |
||
173 | HAL_GPIO_WritePin ( USB_PULLUP_GPIO_Port, USB_PULLUP_Pin, GPIO_PIN_SET); |
||
174 | |||
24 | mjames | 175 | /* setup the USART control blocks */ |
176 | # |
||
2 | mjames | 177 | |
22 | mjames | 178 | #if defined SERIAL_UART1 |
179 | init_usart_ctl (&uc1, huart1.Instance); |
||
180 | |||
2 | mjames | 181 | EnableSerialRxInterrupt (&uc1); |
22 | mjames | 182 | #endif |
2 | mjames | 183 | |
11 | mjames | 184 | /* Initialize the bmp280 */ |
24 | mjames | 185 | rslt = bmp280_init (&bmp); |
11 | mjames | 186 | // print_rslt(" bmp280_init status", rslt); |
187 | |||
188 | /* Always read the current settings before writing, especially when |
||
189 | * all the configuration is not modified |
||
190 | */ |
||
24 | mjames | 191 | rslt = bmp280_get_config (&conf, &bmp); |
192 | // print_rslt(" bmp280_get_config status", rslt); |
||
11 | mjames | 193 | |
194 | /* configuring the temperature oversampling, filter coefficient and output data rate */ |
||
195 | /* Overwrite the desired settings */ |
||
196 | conf.filter = BMP280_FILTER_COEFF_2; |
||
197 | |||
198 | /* Temperature oversampling set at 4x */ |
||
199 | conf.os_temp = BMP280_OS_4X; |
||
200 | |||
201 | /* Pressure over sampling none (disabling pressure measurement) */ |
||
202 | conf.os_pres = BMP280_OS_4X; |
||
203 | |||
12 | mjames | 204 | /* Setting the output data rate as 2HZ(500ms) */ |
205 | conf.odr = BMP280_ODR_500_MS; |
||
24 | mjames | 206 | rslt = bmp280_set_config (&conf, &bmp); |
11 | mjames | 207 | //print_rslt(" bmp280_set_config status", rslt); |
208 | |||
209 | /* Always set the power mode after setting the configuration */ |
||
24 | mjames | 210 | rslt = bmp280_set_power_mode (BMP280_NORMAL_MODE, &bmp); |
11 | mjames | 211 | //print_rslt(" bmp280_set_power_mode status", rslt); |
212 | |||
24 | mjames | 213 | cc_init (); |
11 | mjames | 214 | |
24 | mjames | 215 | uint32_t timeNext = HAL_GetTick () + TICKS_LOOP; |
11 | mjames | 216 | |
2 | mjames | 217 | /* USER CODE END 2 */ |
218 | |||
219 | /* Infinite loop */ |
||
220 | /* USER CODE BEGIN WHILE */ |
||
221 | while (1) |
||
222 | { |
||
24 | mjames | 223 | uint32_t timeNow = HAL_GetTick (); |
22 | mjames | 224 | |
24 | mjames | 225 | if (timeNow < timeNext) |
226 | HAL_Delay (timeNext - timeNow); |
||
227 | timeNext += TICKS_LOOP; |
||
11 | mjames | 228 | cc_run (&bmp); |
2 | mjames | 229 | |
24 | mjames | 230 | /* USER CODE END WHILE */ |
14 | mjames | 231 | |
24 | mjames | 232 | /* USER CODE BEGIN 3 */ |
2 | mjames | 233 | } |
234 | /* USER CODE END 3 */ |
||
235 | } |
||
236 | |||
237 | /** |
||
24 | mjames | 238 | * @brief System Clock Configuration |
239 | * @retval None |
||
240 | */ |
||
241 | void |
||
242 | SystemClock_Config (void) |
||
2 | mjames | 243 | { |
24 | mjames | 244 | RCC_OscInitTypeDef RCC_OscInitStruct = |
245 | { 0 }; |
||
246 | RCC_ClkInitTypeDef RCC_ClkInitStruct = |
||
247 | { 0 }; |
||
248 | RCC_PeriphCLKInitTypeDef PeriphClkInit = |
||
249 | { 0 }; |
||
2 | mjames | 250 | |
251 | /** Initializes the RCC Oscillators according to the specified parameters |
||
24 | mjames | 252 | * in the RCC_OscInitTypeDef structure. |
253 | */ |
||
254 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE |
||
255 | | RCC_OSCILLATORTYPE_LSE; |
||
2 | mjames | 256 | RCC_OscInitStruct.HSEState = RCC_HSE_ON; |
257 | RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; |
||
13 | mjames | 258 | RCC_OscInitStruct.LSEState = RCC_LSE_ON; |
2 | mjames | 259 | RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
260 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
||
261 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; |
||
13 | mjames | 262 | RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; |
24 | mjames | 263 | if (HAL_RCC_OscConfig (&RCC_OscInitStruct) != HAL_OK) |
264 | { |
||
265 | Error_Handler (); |
||
266 | } |
||
2 | mjames | 267 | /** Initializes the CPU, AHB and APB buses clocks |
24 | mjames | 268 | */ |
269 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK |
||
270 | | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; |
||
2 | mjames | 271 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
272 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
||
273 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; |
||
274 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; |
||
275 | |||
24 | mjames | 276 | if (HAL_RCC_ClockConfig (&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) |
277 | { |
||
278 | Error_Handler (); |
||
279 | } |
||
280 | PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC | RCC_PERIPHCLK_USB; |
||
13 | mjames | 281 | PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE; |
282 | PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5; |
||
24 | mjames | 283 | if (HAL_RCCEx_PeriphCLKConfig (&PeriphClkInit) != HAL_OK) |
284 | { |
||
285 | Error_Handler (); |
||
286 | } |
||
2 | mjames | 287 | } |
288 | |||
289 | /** |
||
24 | mjames | 290 | * @brief I2C2 Initialization Function |
291 | * @param None |
||
292 | * @retval None |
||
293 | */ |
||
294 | static void |
||
295 | MX_I2C2_Init (void) |
||
11 | mjames | 296 | { |
297 | |||
298 | /* USER CODE BEGIN I2C2_Init 0 */ |
||
299 | |||
300 | /* USER CODE END I2C2_Init 0 */ |
||
301 | |||
302 | /* USER CODE BEGIN I2C2_Init 1 */ |
||
303 | |||
304 | /* USER CODE END I2C2_Init 1 */ |
||
305 | hi2c2.Instance = I2C2; |
||
306 | hi2c2.Init.ClockSpeed = 100000; |
||
307 | hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2; |
||
308 | hi2c2.Init.OwnAddress1 = 0; |
||
309 | hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; |
||
310 | hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; |
||
311 | hi2c2.Init.OwnAddress2 = 0; |
||
312 | hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; |
||
313 | hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; |
||
24 | mjames | 314 | if (HAL_I2C_Init (&hi2c2) != HAL_OK) |
315 | { |
||
316 | Error_Handler (); |
||
317 | } |
||
11 | mjames | 318 | /* USER CODE BEGIN I2C2_Init 2 */ |
319 | |||
320 | /* USER CODE END I2C2_Init 2 */ |
||
321 | |||
322 | } |
||
323 | |||
324 | /** |
||
24 | mjames | 325 | * @brief RTC Initialization Function |
326 | * @param None |
||
327 | * @retval None |
||
328 | */ |
||
329 | static void |
||
330 | MX_RTC_Init (void) |
||
13 | mjames | 331 | { |
332 | |||
333 | /* USER CODE BEGIN RTC_Init 0 */ |
||
334 | |||
335 | /* USER CODE END RTC_Init 0 */ |
||
336 | |||
24 | mjames | 337 | RTC_TimeTypeDef sTime = |
338 | { 0 }; |
||
339 | RTC_DateTypeDef DateToUpdate = |
||
340 | { 0 }; |
||
13 | mjames | 341 | |
342 | /* USER CODE BEGIN RTC_Init 1 */ |
||
343 | |||
344 | /* USER CODE END RTC_Init 1 */ |
||
345 | /** Initialize RTC Only |
||
24 | mjames | 346 | */ |
13 | mjames | 347 | hrtc.Instance = RTC; |
348 | hrtc.Init.AsynchPrediv = RTC_AUTO_1_SECOND; |
||
349 | hrtc.Init.OutPut = RTC_OUTPUTSOURCE_ALARM; |
||
24 | mjames | 350 | if (HAL_RTC_Init (&hrtc) != HAL_OK) |
351 | { |
||
352 | Error_Handler (); |
||
353 | } |
||
13 | mjames | 354 | |
355 | /* USER CODE BEGIN Check_RTC_BKUP */ |
||
356 | |||
357 | /* USER CODE END Check_RTC_BKUP */ |
||
358 | |||
359 | /** Initialize RTC and set the Time and Date |
||
24 | mjames | 360 | */ |
13 | mjames | 361 | sTime.Hours = 0x0; |
362 | sTime.Minutes = 0x0; |
||
363 | sTime.Seconds = 0x0; |
||
364 | |||
24 | mjames | 365 | if (HAL_RTC_SetTime (&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK) |
366 | { |
||
367 | Error_Handler (); |
||
368 | } |
||
13 | mjames | 369 | DateToUpdate.WeekDay = RTC_WEEKDAY_MONDAY; |
370 | DateToUpdate.Month = RTC_MONTH_JANUARY; |
||
371 | DateToUpdate.Date = 0x1; |
||
372 | DateToUpdate.Year = 0x0; |
||
373 | |||
24 | mjames | 374 | if (HAL_RTC_SetDate (&hrtc, &DateToUpdate, RTC_FORMAT_BCD) != HAL_OK) |
375 | { |
||
376 | Error_Handler (); |
||
377 | } |
||
13 | mjames | 378 | /* USER CODE BEGIN RTC_Init 2 */ |
379 | |||
380 | /* USER CODE END RTC_Init 2 */ |
||
381 | |||
382 | } |
||
383 | |||
384 | /** |
||
24 | mjames | 385 | * @brief SPI1 Initialization Function |
386 | * @param None |
||
387 | * @retval None |
||
388 | */ |
||
389 | static void |
||
390 | MX_SPI1_Init (void) |
||
2 | mjames | 391 | { |
392 | |||
393 | /* USER CODE BEGIN SPI1_Init 0 */ |
||
394 | |||
395 | /* USER CODE END SPI1_Init 0 */ |
||
396 | |||
397 | /* USER CODE BEGIN SPI1_Init 1 */ |
||
398 | |||
399 | /* USER CODE END SPI1_Init 1 */ |
||
400 | /* SPI1 parameter configuration*/ |
||
401 | hspi1.Instance = SPI1; |
||
402 | hspi1.Init.Mode = SPI_MODE_MASTER; |
||
403 | hspi1.Init.Direction = SPI_DIRECTION_1LINE; |
||
404 | hspi1.Init.DataSize = SPI_DATASIZE_8BIT; |
||
5 | mjames | 405 | hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH; |
406 | hspi1.Init.CLKPhase = SPI_PHASE_2EDGE; |
||
2 | mjames | 407 | hspi1.Init.NSS = SPI_NSS_SOFT; |
408 | hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; |
||
409 | hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; |
||
410 | hspi1.Init.TIMode = SPI_TIMODE_DISABLE; |
||
411 | hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; |
||
412 | hspi1.Init.CRCPolynomial = 10; |
||
24 | mjames | 413 | if (HAL_SPI_Init (&hspi1) != HAL_OK) |
414 | { |
||
415 | Error_Handler (); |
||
416 | } |
||
2 | mjames | 417 | /* USER CODE BEGIN SPI1_Init 2 */ |
418 | |||
419 | /* USER CODE END SPI1_Init 2 */ |
||
420 | |||
421 | } |
||
422 | |||
423 | /** |
||
24 | mjames | 424 | * @brief TIM3 Initialization Function |
425 | * @param None |
||
426 | * @retval None |
||
427 | */ |
||
428 | static void |
||
429 | MX_TIM3_Init (void) |
||
2 | mjames | 430 | { |
431 | |||
9 | mjames | 432 | /* USER CODE BEGIN TIM3_Init 0 */ |
433 | |||
434 | /* USER CODE END TIM3_Init 0 */ |
||
435 | |||
24 | mjames | 436 | TIM_MasterConfigTypeDef sMasterConfig = |
437 | { 0 }; |
||
438 | TIM_OC_InitTypeDef sConfigOC = |
||
439 | { 0 }; |
||
9 | mjames | 440 | |
441 | /* USER CODE BEGIN TIM3_Init 1 */ |
||
442 | |||
443 | /* USER CODE END TIM3_Init 1 */ |
||
444 | htim3.Instance = TIM3; |
||
21 | mjames | 445 | htim3.Init.Prescaler = 719; |
9 | mjames | 446 | htim3.Init.CounterMode = TIM_COUNTERMODE_UP; |
447 | htim3.Init.Period = 10000; |
||
448 | htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
||
449 | htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; |
||
24 | mjames | 450 | if (HAL_TIM_OC_Init (&htim3) != HAL_OK) |
451 | { |
||
452 | Error_Handler (); |
||
453 | } |
||
454 | if (HAL_TIM_OnePulse_Init (&htim3, TIM_OPMODE_SINGLE) != HAL_OK) |
||
455 | { |
||
456 | Error_Handler (); |
||
457 | } |
||
9 | mjames | 458 | sMasterConfig.MasterOutputTrigger = TIM_TRGO_ENABLE; |
459 | sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; |
||
24 | mjames | 460 | if (HAL_TIMEx_MasterConfigSynchronization (&htim3, &sMasterConfig) != HAL_OK) |
461 | { |
||
462 | Error_Handler (); |
||
463 | } |
||
9 | mjames | 464 | sConfigOC.OCMode = TIM_OCMODE_TIMING; |
465 | sConfigOC.Pulse = 9999; |
||
466 | sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; |
||
467 | sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; |
||
24 | mjames | 468 | if (HAL_TIM_OC_ConfigChannel (&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK) |
469 | { |
||
470 | Error_Handler (); |
||
471 | } |
||
9 | mjames | 472 | /* USER CODE BEGIN TIM3_Init 2 */ |
473 | |||
474 | /* USER CODE END TIM3_Init 2 */ |
||
475 | |||
476 | } |
||
477 | |||
478 | /** |
||
24 | mjames | 479 | * @brief TIM4 Initialization Function |
480 | * @param None |
||
481 | * @retval None |
||
482 | */ |
||
483 | static void |
||
484 | MX_TIM4_Init (void) |
||
9 | mjames | 485 | { |
486 | |||
2 | mjames | 487 | /* USER CODE BEGIN TIM4_Init 0 */ |
488 | |||
489 | /* USER CODE END TIM4_Init 0 */ |
||
490 | |||
24 | mjames | 491 | TIM_Encoder_InitTypeDef sConfig = |
492 | { 0 }; |
||
493 | TIM_MasterConfigTypeDef sMasterConfig = |
||
494 | { 0 }; |
||
2 | mjames | 495 | |
496 | /* USER CODE BEGIN TIM4_Init 1 */ |
||
497 | |||
498 | /* USER CODE END TIM4_Init 1 */ |
||
499 | htim4.Instance = TIM4; |
||
500 | htim4.Init.Prescaler = 0; |
||
501 | htim4.Init.CounterMode = TIM_COUNTERMODE_UP; |
||
502 | htim4.Init.Period = 65535; |
||
503 | htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
||
504 | htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; |
||
505 | sConfig.EncoderMode = TIM_ENCODERMODE_TI12; |
||
506 | sConfig.IC1Polarity = TIM_ICPOLARITY_RISING; |
||
507 | sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI; |
||
508 | sConfig.IC1Prescaler = TIM_ICPSC_DIV1; |
||
21 | mjames | 509 | sConfig.IC1Filter = 15; |
2 | mjames | 510 | sConfig.IC2Polarity = TIM_ICPOLARITY_RISING; |
511 | sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI; |
||
512 | sConfig.IC2Prescaler = TIM_ICPSC_DIV1; |
||
21 | mjames | 513 | sConfig.IC2Filter = 15; |
24 | mjames | 514 | if (HAL_TIM_Encoder_Init (&htim4, &sConfig) != HAL_OK) |
515 | { |
||
516 | Error_Handler (); |
||
517 | } |
||
2 | mjames | 518 | sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; |
519 | sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; |
||
24 | mjames | 520 | if (HAL_TIMEx_MasterConfigSynchronization (&htim4, &sMasterConfig) != HAL_OK) |
521 | { |
||
522 | Error_Handler (); |
||
523 | } |
||
2 | mjames | 524 | /* USER CODE BEGIN TIM4_Init 2 */ |
525 | |||
526 | /* USER CODE END TIM4_Init 2 */ |
||
527 | |||
528 | } |
||
529 | |||
530 | /** |
||
24 | mjames | 531 | * @brief USART1 Initialization Function |
532 | * @param None |
||
533 | * @retval None |
||
534 | */ |
||
535 | static void |
||
536 | MX_USART1_UART_Init (void) |
||
2 | mjames | 537 | { |
538 | |||
539 | /* USER CODE BEGIN USART1_Init 0 */ |
||
540 | |||
541 | /* USER CODE END USART1_Init 0 */ |
||
542 | |||
543 | /* USER CODE BEGIN USART1_Init 1 */ |
||
544 | |||
545 | /* USER CODE END USART1_Init 1 */ |
||
546 | huart1.Instance = USART1; |
||
547 | huart1.Init.BaudRate = 115200; |
||
548 | huart1.Init.WordLength = UART_WORDLENGTH_8B; |
||
549 | huart1.Init.StopBits = UART_STOPBITS_1; |
||
550 | huart1.Init.Parity = UART_PARITY_NONE; |
||
551 | huart1.Init.Mode = UART_MODE_TX_RX; |
||
552 | huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; |
||
553 | huart1.Init.OverSampling = UART_OVERSAMPLING_16; |
||
24 | mjames | 554 | if (HAL_UART_Init (&huart1) != HAL_OK) |
555 | { |
||
556 | Error_Handler (); |
||
557 | } |
||
2 | mjames | 558 | /* USER CODE BEGIN USART1_Init 2 */ |
559 | |||
560 | /* USER CODE END USART1_Init 2 */ |
||
561 | |||
562 | } |
||
563 | |||
564 | /** |
||
24 | mjames | 565 | * @brief GPIO Initialization Function |
566 | * @param None |
||
567 | * @retval None |
||
568 | */ |
||
569 | static void |
||
570 | MX_GPIO_Init (void) |
||
2 | mjames | 571 | { |
24 | mjames | 572 | GPIO_InitTypeDef GPIO_InitStruct = |
573 | { 0 }; |
||
2 | mjames | 574 | |
575 | /* GPIO Ports Clock Enable */ |
||
5 | mjames | 576 | __HAL_RCC_GPIOC_CLK_ENABLE(); |
2 | mjames | 577 | __HAL_RCC_GPIOD_CLK_ENABLE(); |
578 | __HAL_RCC_GPIOA_CLK_ENABLE(); |
||
579 | __HAL_RCC_GPIOB_CLK_ENABLE(); |
||
580 | |||
581 | /*Configure GPIO pin Output Level */ |
||
24 | mjames | 582 | HAL_GPIO_WritePin (GPIOA, SPI_CD_Pin | SPI_RESET_Pin | SPI_NSS1_Pin, |
583 | GPIO_PIN_RESET); |
||
2 | mjames | 584 | |
14 | mjames | 585 | /*Configure GPIO pin Output Level */ |
24 | mjames | 586 | HAL_GPIO_WritePin (USB_PULLUP_GPIO_Port, USB_PULLUP_Pin, GPIO_PIN_RESET); |
14 | mjames | 587 | |
13 | mjames | 588 | /*Configure GPIO pins : SPI_CD_Pin SPI_RESET_Pin SPI_NSS1_Pin */ |
24 | mjames | 589 | GPIO_InitStruct.Pin = SPI_CD_Pin | SPI_RESET_Pin | SPI_NSS1_Pin; |
2 | mjames | 590 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
591 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
592 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
||
24 | mjames | 593 | HAL_GPIO_Init (GPIOA, &GPIO_InitStruct); |
2 | mjames | 594 | |
14 | mjames | 595 | /*Configure GPIO pin : USB_PULLUP_Pin */ |
596 | GPIO_InitStruct.Pin = USB_PULLUP_Pin; |
||
597 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
||
598 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
599 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
||
24 | mjames | 600 | HAL_GPIO_Init (USB_PULLUP_GPIO_Port, &GPIO_InitStruct); |
14 | mjames | 601 | |
21 | mjames | 602 | /*Configure GPIO pin : encoder_push_Pin */ |
603 | GPIO_InitStruct.Pin = encoder_push_Pin; |
||
604 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT; |
||
605 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
||
24 | mjames | 606 | HAL_GPIO_Init (encoder_push_GPIO_Port, &GPIO_InitStruct); |
21 | mjames | 607 | |
2 | mjames | 608 | } |
609 | |||
610 | /* USER CODE BEGIN 4 */ |
||
611 | |||
612 | /* USER CODE END 4 */ |
||
613 | |||
614 | /** |
||
24 | mjames | 615 | * @brief This function is executed in case of error occurrence. |
616 | * @retval None |
||
617 | */ |
||
618 | void |
||
619 | Error_Handler (void) |
||
2 | mjames | 620 | { |
621 | /* USER CODE BEGIN Error_Handler_Debug */ |
||
622 | /* User can add his own implementation to report the HAL error return state */ |
||
623 | |||
624 | /* USER CODE END Error_Handler_Debug */ |
||
625 | } |
||
626 | |||
627 | #ifdef USE_FULL_ASSERT |
||
628 | /** |
||
629 | * @brief Reports the name of the source file and the source line number |
||
630 | * where the assert_param error has occurred. |
||
631 | * @param file: pointer to the source file name |
||
632 | * @param line: assert_param error line source number |
||
633 | * @retval None |
||
634 | */ |
||
635 | void assert_failed(uint8_t *file, uint32_t line) |
||
636 | { |
||
637 | /* USER CODE BEGIN 6 */ |
||
638 | /* User can add his own implementation to report the file name and line number, |
||
639 | tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ |
||
640 | /* USER CODE END 6 */ |
||
641 | } |
||
642 | #endif /* USE_FULL_ASSERT */ |
||
643 | |||
644 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |