Rev 27 | Rev 29 | 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 ---------------------------------------------------------*/ |
||
28 | mjames | 46 | I2C_HandleTypeDef hi2c1; |
11 | mjames | 47 | I2C_HandleTypeDef hi2c2; |
48 | |||
27 | mjames | 49 | IWDG_HandleTypeDef hiwdg; |
50 | |||
13 | mjames | 51 | RTC_HandleTypeDef hrtc; |
52 | |||
2 | mjames | 53 | SPI_HandleTypeDef hspi1; |
54 | |||
9 | mjames | 55 | TIM_HandleTypeDef htim3; |
2 | mjames | 56 | TIM_HandleTypeDef htim4; |
57 | |||
58 | UART_HandleTypeDef huart1; |
||
59 | |||
60 | /* USER CODE BEGIN PV */ |
||
11 | mjames | 61 | typedef struct |
62 | { |
||
63 | uint8_t dev_addr; |
||
64 | } interface_t; |
||
2 | mjames | 65 | |
11 | mjames | 66 | static int8_t |
24 | mjames | 67 | user_i2c_write (uint8_t i2c_addr, uint8_t reg_addr, uint8_t *reg_data, |
68 | uint32_t len) |
||
11 | mjames | 69 | { |
24 | mjames | 70 | HAL_StatusTypeDef st = HAL_I2C_Mem_Write (&hi2c2, i2c_addr << 1, reg_addr, 1, |
28 | mjames | 71 | reg_data, len, 1000); |
11 | mjames | 72 | |
24 | mjames | 73 | return st != HAL_OK ? BMP280_E_COMM_FAIL : BMP280_OK; |
11 | mjames | 74 | |
75 | } |
||
76 | static int8_t |
||
24 | mjames | 77 | user_i2c_read (uint8_t i2c_addr, uint8_t reg_addr, uint8_t *reg_data, |
78 | uint32_t len) |
||
11 | mjames | 79 | { |
24 | mjames | 80 | HAL_StatusTypeDef st = HAL_I2C_Mem_Read (&hi2c2, i2c_addr << 1, reg_addr, 1, |
28 | mjames | 81 | reg_data, len, 1000); |
11 | mjames | 82 | |
24 | mjames | 83 | return st != HAL_OK ? BMP280_E_COMM_FAIL : BMP280_OK; |
11 | mjames | 84 | |
85 | } |
||
28 | mjames | 86 | // the second I2C bus is I2C1, this is used for the external I2C thermometer to avoid problems with noise pickup . |
87 | static int8_t |
||
88 | user_i2c2_write (uint8_t i2c_addr, uint8_t reg_addr, uint8_t *reg_data, |
||
89 | uint32_t len) |
||
90 | { |
||
91 | HAL_StatusTypeDef st = HAL_I2C_Mem_Write (&hi2c1, i2c_addr << 1, reg_addr, 1, |
||
92 | reg_data, len, 1000); |
||
11 | mjames | 93 | |
28 | mjames | 94 | return st != HAL_OK ? BMP280_E_COMM_FAIL : BMP280_OK; |
95 | |||
96 | } |
||
97 | static int8_t |
||
98 | user_i2c2_read (uint8_t i2c_addr, uint8_t reg_addr, uint8_t *reg_data, |
||
99 | uint32_t len) |
||
100 | { |
||
101 | HAL_StatusTypeDef st = HAL_I2C_Mem_Read (&hi2c1, i2c_addr << 1, reg_addr, 1, |
||
102 | reg_data, len, 1000); |
||
103 | |||
104 | return st != HAL_OK ? BMP280_E_COMM_FAIL : BMP280_OK; |
||
105 | |||
106 | } |
||
107 | |||
108 | static void reset_i2c2(void) |
||
109 | { |
||
110 | |||
111 | |||
112 | |||
113 | |||
114 | |||
115 | } |
||
116 | |||
11 | mjames | 117 | static void |
118 | user_delay_ms (uint32_t ms, void *handle) |
||
119 | { |
||
120 | HAL_Delay (ms); |
||
121 | |||
122 | } |
||
123 | |||
124 | struct bmp280_dev bmp = |
||
125 | { |
||
26 | mjames | 126 | .intf = BMP280_I2C_INTF, |
127 | .read = user_i2c_read, |
||
128 | .write = user_i2c_write, |
||
129 | .delay_ms = user_delay_ms, |
||
11 | mjames | 130 | |
26 | mjames | 131 | /* Update interface pointer with the structure that contains both device address and file descriptor */ |
132 | .dev_id = BMP280_I2C_ADDR_PRIM |
||
133 | }; |
||
11 | mjames | 134 | |
26 | mjames | 135 | struct bmp280_dev bmp2 = |
136 | { |
||
137 | .intf = BMP280_I2C_INTF, |
||
28 | mjames | 138 | .read = user_i2c2_read, |
139 | .write = user_i2c2_write, |
||
26 | mjames | 140 | .delay_ms = user_delay_ms, |
11 | mjames | 141 | |
26 | mjames | 142 | /* Update interface pointer with the structure that contains both device address and file descriptor */ |
28 | mjames | 143 | .dev_id = BMP280_I2C_ADDR_PRIM |
26 | mjames | 144 | }; |
145 | |||
11 | mjames | 146 | int8_t rslt; |
26 | mjames | 147 | |
148 | |||
11 | mjames | 149 | struct bmp280_config conf; |
26 | mjames | 150 | struct bmp280_config conf2; |
11 | mjames | 151 | |
2 | mjames | 152 | /* USER CODE END PV */ |
153 | |||
154 | /* Private function prototypes -----------------------------------------------*/ |
||
26 | mjames | 155 | void SystemClock_Config(void); |
156 | static void MX_GPIO_Init(void); |
||
157 | static void MX_SPI1_Init(void); |
||
158 | static void MX_TIM4_Init(void); |
||
159 | static void MX_USART1_UART_Init(void); |
||
160 | static void MX_TIM3_Init(void); |
||
161 | static void MX_I2C2_Init(void); |
||
162 | static void MX_RTC_Init(void); |
||
27 | mjames | 163 | static void MX_IWDG_Init(void); |
28 | mjames | 164 | static void MX_I2C1_Init(void); |
2 | mjames | 165 | /* USER CODE BEGIN PFP */ |
166 | |||
167 | /* USER CODE END PFP */ |
||
168 | |||
169 | /* Private user code ---------------------------------------------------------*/ |
||
170 | /* USER CODE BEGIN 0 */ |
||
171 | |||
26 | mjames | 172 | uint32_t const TICKS_LOOP = 50; // 50mS per loop. |
24 | mjames | 173 | |
26 | mjames | 174 | // initialise the BMP |
175 | uint8_t init_bmp(struct bmp280_config * conf, struct bmp280_dev * bmp ) |
||
176 | { |
||
177 | rslt = bmp280_init(bmp); |
||
178 | if (rslt != BMP280_OK) |
||
179 | return rslt; |
||
180 | |||
181 | rslt = bmp280_get_config (conf, bmp); |
||
182 | if (rslt != BMP280_OK) |
||
183 | return rslt; |
||
184 | /* configuring the temperature oversampling, filter coefficient and output data rate */ |
||
185 | /* Overwrite the desired settings */ |
||
186 | conf->filter = BMP280_FILTER_COEFF_2; |
||
187 | |||
188 | /* Temperature oversampling set at 4x */ |
||
189 | conf->os_temp = BMP280_OS_4X; |
||
190 | |||
191 | /* Pressure over sampling none (disabling pressure measurement) */ |
||
192 | conf->os_pres = BMP280_OS_4X; |
||
193 | |||
194 | /* Setting the output data rate as 2HZ(500ms) */ |
||
195 | conf->odr = BMP280_ODR_500_MS; |
||
196 | rslt = bmp280_set_config (conf, bmp); |
||
197 | if (rslt != BMP280_OK) |
||
198 | return rslt; |
||
199 | |||
200 | /* Always set the power mode after setting the configuration */ |
||
201 | rslt = bmp280_set_power_mode (BMP280_NORMAL_MODE, bmp); |
||
202 | if (rslt != BMP280_OK) |
||
203 | return rslt; |
||
204 | |||
205 | } |
||
206 | |||
28 | mjames | 207 | static void reset_I2C1(void) |
208 | { |
||
209 | HAL_GPIO_WritePin ( GPIOB, GPIO_PIN_9, GPIO_PIN_SET); |
||
210 | HAL_GPIO_WritePin ( GPIOB, GPIO_PIN_8, GPIO_PIN_SET); |
||
211 | __HAL_AFIO_REMAP_I2C1_DISABLE(); |
||
212 | int i; |
||
213 | // clock 18 times |
||
214 | for (i=0;i<18;++i) |
||
215 | { |
||
216 | HAL_Delay (1); |
||
217 | HAL_GPIO_WritePin ( GPIOB, GPIO_PIN_9, GPIO_PIN_SET); |
||
218 | HAL_Delay (1); |
||
219 | HAL_GPIO_WritePin ( GPIOB, GPIO_PIN_9, GPIO_PIN_SET); |
||
220 | } |
||
221 | __HAL_AFIO_REMAP_I2C1_ENABLE(); |
||
26 | mjames | 222 | |
28 | mjames | 223 | } |
26 | mjames | 224 | |
225 | |||
226 | |||
28 | mjames | 227 | |
2 | mjames | 228 | /* USER CODE END 0 */ |
229 | |||
230 | /** |
||
26 | mjames | 231 | * @brief The application entry point. |
232 | * @retval int |
||
233 | */ |
||
234 | int main(void) |
||
2 | mjames | 235 | { |
236 | /* USER CODE BEGIN 1 */ |
||
237 | |||
238 | /* USER CODE END 1 */ |
||
239 | |||
240 | /* MCU Configuration--------------------------------------------------------*/ |
||
241 | |||
242 | /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ |
||
26 | mjames | 243 | HAL_Init(); |
2 | mjames | 244 | |
245 | /* USER CODE BEGIN Init */ |
||
246 | |||
247 | /* USER CODE END Init */ |
||
248 | |||
249 | /* Configure the system clock */ |
||
26 | mjames | 250 | SystemClock_Config(); |
2 | mjames | 251 | |
252 | /* USER CODE BEGIN SysInit */ |
||
253 | |||
254 | /* USER CODE END SysInit */ |
||
255 | |||
256 | /* Initialize all configured peripherals */ |
||
26 | mjames | 257 | MX_GPIO_Init(); |
258 | MX_SPI1_Init(); |
||
259 | MX_TIM4_Init(); |
||
260 | MX_USART1_UART_Init(); |
||
261 | MX_TIM3_Init(); |
||
262 | MX_I2C2_Init(); |
||
263 | MX_RTC_Init(); |
||
264 | MX_USB_DEVICE_Init(); |
||
27 | mjames | 265 | MX_IWDG_Init(); |
28 | mjames | 266 | MX_I2C1_Init(); |
2 | mjames | 267 | /* USER CODE BEGIN 2 */ |
14 | mjames | 268 | |
269 | HAL_GPIO_WritePin ( USB_PULLUP_GPIO_Port, USB_PULLUP_Pin, GPIO_PIN_RESET); |
||
270 | HAL_Delay (1000); |
||
271 | HAL_GPIO_WritePin ( USB_PULLUP_GPIO_Port, USB_PULLUP_Pin, GPIO_PIN_SET); |
||
24 | mjames | 272 | /* setup the USART control blocks */ |
2 | mjames | 273 | |
22 | mjames | 274 | #if defined SERIAL_UART1 |
27 | mjames | 275 | init_usart_ctl (&uc1, &huart1); |
22 | mjames | 276 | |
25 | mjames | 277 | |
2 | mjames | 278 | EnableSerialRxInterrupt (&uc1); |
25 | mjames | 279 | |
22 | mjames | 280 | #endif |
2 | mjames | 281 | |
26 | mjames | 282 | init_bmp(&conf,&bmp); |
283 | init_bmp(&conf2,&bmp2); |
||
11 | mjames | 284 | |
26 | mjames | 285 | cc_init (); |
11 | mjames | 286 | |
287 | |||
24 | mjames | 288 | uint32_t timeNext = HAL_GetTick () + TICKS_LOOP; |
11 | mjames | 289 | |
2 | mjames | 290 | /* USER CODE END 2 */ |
291 | |||
292 | /* Infinite loop */ |
||
293 | /* USER CODE BEGIN WHILE */ |
||
294 | while (1) |
||
295 | { |
||
24 | mjames | 296 | uint32_t timeNow = HAL_GetTick (); |
22 | mjames | 297 | |
24 | mjames | 298 | if (timeNow < timeNext) |
299 | HAL_Delay (timeNext - timeNow); |
||
300 | timeNext += TICKS_LOOP; |
||
26 | mjames | 301 | cc_run (&bmp, &bmp2); |
2 | mjames | 302 | |
27 | mjames | 303 | // refresh watchdog timer |
304 | HAL_IWDG_Refresh(&hiwdg); |
||
305 | |||
306 | |||
26 | mjames | 307 | /* USER CODE END WHILE */ |
14 | mjames | 308 | |
26 | mjames | 309 | /* USER CODE BEGIN 3 */ |
2 | mjames | 310 | } |
311 | /* USER CODE END 3 */ |
||
312 | } |
||
313 | |||
314 | /** |
||
26 | mjames | 315 | * @brief System Clock Configuration |
316 | * @retval None |
||
317 | */ |
||
318 | void SystemClock_Config(void) |
||
2 | mjames | 319 | { |
26 | mjames | 320 | RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
321 | RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; |
||
322 | RCC_PeriphCLKInitTypeDef PeriphClkInit = {0}; |
||
2 | mjames | 323 | |
324 | /** Initializes the RCC Oscillators according to the specified parameters |
||
26 | mjames | 325 | * in the RCC_OscInitTypeDef structure. |
326 | */ |
||
27 | mjames | 327 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE |
328 | |RCC_OSCILLATORTYPE_LSE; |
||
2 | mjames | 329 | RCC_OscInitStruct.HSEState = RCC_HSE_ON; |
330 | RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; |
||
13 | mjames | 331 | RCC_OscInitStruct.LSEState = RCC_LSE_ON; |
2 | mjames | 332 | RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
27 | mjames | 333 | RCC_OscInitStruct.LSIState = RCC_LSI_ON; |
2 | mjames | 334 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
335 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; |
||
13 | mjames | 336 | RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; |
26 | mjames | 337 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) |
338 | { |
||
339 | Error_Handler(); |
||
340 | } |
||
2 | mjames | 341 | /** Initializes the CPU, AHB and APB buses clocks |
26 | mjames | 342 | */ |
343 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |
||
344 | |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; |
||
2 | mjames | 345 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
346 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
||
347 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; |
||
348 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; |
||
349 | |||
26 | mjames | 350 | if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) |
351 | { |
||
352 | Error_Handler(); |
||
353 | } |
||
354 | PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC|RCC_PERIPHCLK_USB; |
||
13 | mjames | 355 | PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE; |
356 | PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5; |
||
26 | mjames | 357 | if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) |
358 | { |
||
359 | Error_Handler(); |
||
360 | } |
||
2 | mjames | 361 | } |
362 | |||
363 | /** |
||
28 | mjames | 364 | * @brief I2C1 Initialization Function |
365 | * @param None |
||
366 | * @retval None |
||
367 | */ |
||
368 | static void MX_I2C1_Init(void) |
||
369 | { |
||
370 | |||
371 | /* USER CODE BEGIN I2C1_Init 0 */ |
||
372 | |||
373 | /* USER CODE END I2C1_Init 0 */ |
||
374 | |||
375 | /* USER CODE BEGIN I2C1_Init 1 */ |
||
376 | |||
377 | /* USER CODE END I2C1_Init 1 */ |
||
378 | hi2c1.Instance = I2C1; |
||
379 | hi2c1.Init.ClockSpeed = 100000; |
||
380 | hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2; |
||
381 | hi2c1.Init.OwnAddress1 = 0; |
||
382 | hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; |
||
383 | hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; |
||
384 | hi2c1.Init.OwnAddress2 = 0; |
||
385 | hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; |
||
386 | hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; |
||
387 | if (HAL_I2C_Init(&hi2c1) != HAL_OK) |
||
388 | { |
||
389 | Error_Handler(); |
||
390 | } |
||
391 | /* USER CODE BEGIN I2C1_Init 2 */ |
||
392 | |||
393 | /* USER CODE END I2C1_Init 2 */ |
||
394 | |||
395 | } |
||
396 | |||
397 | /** |
||
26 | mjames | 398 | * @brief I2C2 Initialization Function |
399 | * @param None |
||
400 | * @retval None |
||
401 | */ |
||
402 | static void MX_I2C2_Init(void) |
||
11 | mjames | 403 | { |
404 | |||
405 | /* USER CODE BEGIN I2C2_Init 0 */ |
||
406 | |||
407 | /* USER CODE END I2C2_Init 0 */ |
||
408 | |||
409 | /* USER CODE BEGIN I2C2_Init 1 */ |
||
410 | |||
411 | /* USER CODE END I2C2_Init 1 */ |
||
412 | hi2c2.Instance = I2C2; |
||
413 | hi2c2.Init.ClockSpeed = 100000; |
||
414 | hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2; |
||
415 | hi2c2.Init.OwnAddress1 = 0; |
||
416 | hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; |
||
417 | hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; |
||
418 | hi2c2.Init.OwnAddress2 = 0; |
||
419 | hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; |
||
420 | hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; |
||
26 | mjames | 421 | if (HAL_I2C_Init(&hi2c2) != HAL_OK) |
422 | { |
||
423 | Error_Handler(); |
||
424 | } |
||
11 | mjames | 425 | /* USER CODE BEGIN I2C2_Init 2 */ |
426 | |||
427 | /* USER CODE END I2C2_Init 2 */ |
||
428 | |||
429 | } |
||
430 | |||
431 | /** |
||
27 | mjames | 432 | * @brief IWDG Initialization Function |
433 | * @param None |
||
434 | * @retval None |
||
435 | */ |
||
436 | static void MX_IWDG_Init(void) |
||
437 | { |
||
438 | |||
439 | /* USER CODE BEGIN IWDG_Init 0 */ |
||
440 | |||
441 | /* USER CODE END IWDG_Init 0 */ |
||
442 | |||
443 | /* USER CODE BEGIN IWDG_Init 1 */ |
||
444 | |||
445 | /* USER CODE END IWDG_Init 1 */ |
||
446 | hiwdg.Instance = IWDG; |
||
447 | hiwdg.Init.Prescaler = IWDG_PRESCALER_16; |
||
448 | hiwdg.Init.Reload = 4095; |
||
449 | if (HAL_IWDG_Init(&hiwdg) != HAL_OK) |
||
450 | { |
||
451 | Error_Handler(); |
||
452 | } |
||
453 | /* USER CODE BEGIN IWDG_Init 2 */ |
||
454 | |||
455 | /* USER CODE END IWDG_Init 2 */ |
||
456 | |||
457 | } |
||
458 | |||
459 | /** |
||
26 | mjames | 460 | * @brief RTC Initialization Function |
461 | * @param None |
||
462 | * @retval None |
||
463 | */ |
||
464 | static void MX_RTC_Init(void) |
||
13 | mjames | 465 | { |
466 | |||
467 | /* USER CODE BEGIN RTC_Init 0 */ |
||
468 | |||
469 | /* USER CODE END RTC_Init 0 */ |
||
470 | |||
26 | mjames | 471 | RTC_TimeTypeDef sTime = {0}; |
472 | RTC_DateTypeDef DateToUpdate = {0}; |
||
13 | mjames | 473 | |
474 | /* USER CODE BEGIN RTC_Init 1 */ |
||
475 | |||
476 | /* USER CODE END RTC_Init 1 */ |
||
477 | /** Initialize RTC Only |
||
26 | mjames | 478 | */ |
13 | mjames | 479 | hrtc.Instance = RTC; |
480 | hrtc.Init.AsynchPrediv = RTC_AUTO_1_SECOND; |
||
481 | hrtc.Init.OutPut = RTC_OUTPUTSOURCE_ALARM; |
||
26 | mjames | 482 | if (HAL_RTC_Init(&hrtc) != HAL_OK) |
483 | { |
||
484 | Error_Handler(); |
||
485 | } |
||
13 | mjames | 486 | |
487 | /* USER CODE BEGIN Check_RTC_BKUP */ |
||
488 | |||
489 | /* USER CODE END Check_RTC_BKUP */ |
||
490 | |||
491 | /** Initialize RTC and set the Time and Date |
||
26 | mjames | 492 | */ |
13 | mjames | 493 | sTime.Hours = 0x0; |
494 | sTime.Minutes = 0x0; |
||
495 | sTime.Seconds = 0x0; |
||
496 | |||
26 | mjames | 497 | if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK) |
498 | { |
||
499 | Error_Handler(); |
||
500 | } |
||
13 | mjames | 501 | DateToUpdate.WeekDay = RTC_WEEKDAY_MONDAY; |
502 | DateToUpdate.Month = RTC_MONTH_JANUARY; |
||
503 | DateToUpdate.Date = 0x1; |
||
504 | DateToUpdate.Year = 0x0; |
||
505 | |||
26 | mjames | 506 | if (HAL_RTC_SetDate(&hrtc, &DateToUpdate, RTC_FORMAT_BCD) != HAL_OK) |
507 | { |
||
508 | Error_Handler(); |
||
509 | } |
||
13 | mjames | 510 | /* USER CODE BEGIN RTC_Init 2 */ |
511 | |||
512 | /* USER CODE END RTC_Init 2 */ |
||
513 | |||
514 | } |
||
515 | |||
516 | /** |
||
26 | mjames | 517 | * @brief SPI1 Initialization Function |
518 | * @param None |
||
519 | * @retval None |
||
520 | */ |
||
521 | static void MX_SPI1_Init(void) |
||
2 | mjames | 522 | { |
523 | |||
524 | /* USER CODE BEGIN SPI1_Init 0 */ |
||
525 | |||
526 | /* USER CODE END SPI1_Init 0 */ |
||
527 | |||
528 | /* USER CODE BEGIN SPI1_Init 1 */ |
||
529 | |||
530 | /* USER CODE END SPI1_Init 1 */ |
||
531 | /* SPI1 parameter configuration*/ |
||
532 | hspi1.Instance = SPI1; |
||
533 | hspi1.Init.Mode = SPI_MODE_MASTER; |
||
534 | hspi1.Init.Direction = SPI_DIRECTION_1LINE; |
||
535 | hspi1.Init.DataSize = SPI_DATASIZE_8BIT; |
||
5 | mjames | 536 | hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH; |
537 | hspi1.Init.CLKPhase = SPI_PHASE_2EDGE; |
||
2 | mjames | 538 | hspi1.Init.NSS = SPI_NSS_SOFT; |
539 | hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; |
||
540 | hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; |
||
541 | hspi1.Init.TIMode = SPI_TIMODE_DISABLE; |
||
542 | hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; |
||
543 | hspi1.Init.CRCPolynomial = 10; |
||
26 | mjames | 544 | if (HAL_SPI_Init(&hspi1) != HAL_OK) |
545 | { |
||
546 | Error_Handler(); |
||
547 | } |
||
2 | mjames | 548 | /* USER CODE BEGIN SPI1_Init 2 */ |
549 | |||
550 | /* USER CODE END SPI1_Init 2 */ |
||
551 | |||
552 | } |
||
553 | |||
554 | /** |
||
26 | mjames | 555 | * @brief TIM3 Initialization Function |
556 | * @param None |
||
557 | * @retval None |
||
558 | */ |
||
559 | static void MX_TIM3_Init(void) |
||
2 | mjames | 560 | { |
561 | |||
9 | mjames | 562 | /* USER CODE BEGIN TIM3_Init 0 */ |
563 | |||
564 | /* USER CODE END TIM3_Init 0 */ |
||
565 | |||
26 | mjames | 566 | TIM_MasterConfigTypeDef sMasterConfig = {0}; |
567 | TIM_OC_InitTypeDef sConfigOC = {0}; |
||
9 | mjames | 568 | |
569 | /* USER CODE BEGIN TIM3_Init 1 */ |
||
570 | |||
571 | /* USER CODE END TIM3_Init 1 */ |
||
572 | htim3.Instance = TIM3; |
||
21 | mjames | 573 | htim3.Init.Prescaler = 719; |
9 | mjames | 574 | htim3.Init.CounterMode = TIM_COUNTERMODE_UP; |
575 | htim3.Init.Period = 10000; |
||
576 | htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
||
577 | htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; |
||
26 | mjames | 578 | if (HAL_TIM_OC_Init(&htim3) != HAL_OK) |
579 | { |
||
580 | Error_Handler(); |
||
581 | } |
||
582 | if (HAL_TIM_OnePulse_Init(&htim3, TIM_OPMODE_SINGLE) != HAL_OK) |
||
583 | { |
||
584 | Error_Handler(); |
||
585 | } |
||
9 | mjames | 586 | sMasterConfig.MasterOutputTrigger = TIM_TRGO_ENABLE; |
587 | sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; |
||
26 | mjames | 588 | if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK) |
589 | { |
||
590 | Error_Handler(); |
||
591 | } |
||
9 | mjames | 592 | sConfigOC.OCMode = TIM_OCMODE_TIMING; |
593 | sConfigOC.Pulse = 9999; |
||
594 | sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; |
||
595 | sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; |
||
26 | mjames | 596 | if (HAL_TIM_OC_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK) |
597 | { |
||
598 | Error_Handler(); |
||
599 | } |
||
9 | mjames | 600 | /* USER CODE BEGIN TIM3_Init 2 */ |
601 | |||
602 | /* USER CODE END TIM3_Init 2 */ |
||
603 | |||
604 | } |
||
605 | |||
606 | /** |
||
26 | mjames | 607 | * @brief TIM4 Initialization Function |
608 | * @param None |
||
609 | * @retval None |
||
610 | */ |
||
611 | static void MX_TIM4_Init(void) |
||
9 | mjames | 612 | { |
613 | |||
2 | mjames | 614 | /* USER CODE BEGIN TIM4_Init 0 */ |
615 | |||
616 | /* USER CODE END TIM4_Init 0 */ |
||
617 | |||
26 | mjames | 618 | TIM_Encoder_InitTypeDef sConfig = {0}; |
619 | TIM_MasterConfigTypeDef sMasterConfig = {0}; |
||
2 | mjames | 620 | |
621 | /* USER CODE BEGIN TIM4_Init 1 */ |
||
622 | |||
623 | /* USER CODE END TIM4_Init 1 */ |
||
624 | htim4.Instance = TIM4; |
||
625 | htim4.Init.Prescaler = 0; |
||
626 | htim4.Init.CounterMode = TIM_COUNTERMODE_UP; |
||
627 | htim4.Init.Period = 65535; |
||
628 | htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
||
629 | htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; |
||
630 | sConfig.EncoderMode = TIM_ENCODERMODE_TI12; |
||
631 | sConfig.IC1Polarity = TIM_ICPOLARITY_RISING; |
||
632 | sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI; |
||
633 | sConfig.IC1Prescaler = TIM_ICPSC_DIV1; |
||
21 | mjames | 634 | sConfig.IC1Filter = 15; |
2 | mjames | 635 | sConfig.IC2Polarity = TIM_ICPOLARITY_RISING; |
636 | sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI; |
||
637 | sConfig.IC2Prescaler = TIM_ICPSC_DIV1; |
||
21 | mjames | 638 | sConfig.IC2Filter = 15; |
26 | mjames | 639 | if (HAL_TIM_Encoder_Init(&htim4, &sConfig) != HAL_OK) |
640 | { |
||
641 | Error_Handler(); |
||
642 | } |
||
2 | mjames | 643 | sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; |
644 | sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; |
||
26 | mjames | 645 | if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK) |
646 | { |
||
647 | Error_Handler(); |
||
648 | } |
||
2 | mjames | 649 | /* USER CODE BEGIN TIM4_Init 2 */ |
650 | |||
651 | /* USER CODE END TIM4_Init 2 */ |
||
652 | |||
653 | } |
||
654 | |||
655 | /** |
||
26 | mjames | 656 | * @brief USART1 Initialization Function |
657 | * @param None |
||
658 | * @retval None |
||
659 | */ |
||
660 | static void MX_USART1_UART_Init(void) |
||
2 | mjames | 661 | { |
662 | |||
663 | /* USER CODE BEGIN USART1_Init 0 */ |
||
664 | |||
665 | /* USER CODE END USART1_Init 0 */ |
||
666 | |||
667 | /* USER CODE BEGIN USART1_Init 1 */ |
||
668 | |||
669 | /* USER CODE END USART1_Init 1 */ |
||
670 | huart1.Instance = USART1; |
||
27 | mjames | 671 | huart1.Init.BaudRate = 9600; |
2 | mjames | 672 | huart1.Init.WordLength = UART_WORDLENGTH_8B; |
673 | huart1.Init.StopBits = UART_STOPBITS_1; |
||
674 | huart1.Init.Parity = UART_PARITY_NONE; |
||
675 | huart1.Init.Mode = UART_MODE_TX_RX; |
||
676 | huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; |
||
677 | huart1.Init.OverSampling = UART_OVERSAMPLING_16; |
||
26 | mjames | 678 | if (HAL_UART_Init(&huart1) != HAL_OK) |
679 | { |
||
680 | Error_Handler(); |
||
681 | } |
||
2 | mjames | 682 | /* USER CODE BEGIN USART1_Init 2 */ |
683 | |||
684 | /* USER CODE END USART1_Init 2 */ |
||
685 | |||
686 | } |
||
687 | |||
688 | /** |
||
26 | mjames | 689 | * @brief GPIO Initialization Function |
690 | * @param None |
||
691 | * @retval None |
||
692 | */ |
||
693 | static void MX_GPIO_Init(void) |
||
2 | mjames | 694 | { |
26 | mjames | 695 | GPIO_InitTypeDef GPIO_InitStruct = {0}; |
2 | mjames | 696 | |
697 | /* GPIO Ports Clock Enable */ |
||
5 | mjames | 698 | __HAL_RCC_GPIOC_CLK_ENABLE(); |
2 | mjames | 699 | __HAL_RCC_GPIOD_CLK_ENABLE(); |
700 | __HAL_RCC_GPIOA_CLK_ENABLE(); |
||
701 | __HAL_RCC_GPIOB_CLK_ENABLE(); |
||
702 | |||
703 | /*Configure GPIO pin Output Level */ |
||
27 | mjames | 704 | HAL_GPIO_WritePin(Green_LED_GPIO_Port, Green_LED_Pin, GPIO_PIN_RESET); |
705 | |||
706 | /*Configure GPIO pin Output Level */ |
||
26 | mjames | 707 | HAL_GPIO_WritePin(GPIOA, SPI_CD_Pin|SPI_RESET_Pin|SPI_NSS1_Pin, GPIO_PIN_RESET); |
2 | mjames | 708 | |
14 | mjames | 709 | /*Configure GPIO pin Output Level */ |
26 | mjames | 710 | HAL_GPIO_WritePin(USB_PULLUP_GPIO_Port, USB_PULLUP_Pin, GPIO_PIN_RESET); |
14 | mjames | 711 | |
27 | mjames | 712 | /*Configure GPIO pin : Green_LED_Pin */ |
713 | GPIO_InitStruct.Pin = Green_LED_Pin; |
||
714 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
||
715 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
716 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
||
717 | HAL_GPIO_Init(Green_LED_GPIO_Port, &GPIO_InitStruct); |
||
718 | |||
13 | mjames | 719 | /*Configure GPIO pins : SPI_CD_Pin SPI_RESET_Pin SPI_NSS1_Pin */ |
26 | mjames | 720 | GPIO_InitStruct.Pin = SPI_CD_Pin|SPI_RESET_Pin|SPI_NSS1_Pin; |
2 | mjames | 721 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
722 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
723 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
||
26 | mjames | 724 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
2 | mjames | 725 | |
14 | mjames | 726 | /*Configure GPIO pin : USB_PULLUP_Pin */ |
727 | GPIO_InitStruct.Pin = USB_PULLUP_Pin; |
||
728 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
||
729 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
730 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
||
26 | mjames | 731 | HAL_GPIO_Init(USB_PULLUP_GPIO_Port, &GPIO_InitStruct); |
14 | mjames | 732 | |
21 | mjames | 733 | /*Configure GPIO pin : encoder_push_Pin */ |
734 | GPIO_InitStruct.Pin = encoder_push_Pin; |
||
735 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT; |
||
736 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
||
26 | mjames | 737 | HAL_GPIO_Init(encoder_push_GPIO_Port, &GPIO_InitStruct); |
21 | mjames | 738 | |
2 | mjames | 739 | } |
740 | |||
741 | /* USER CODE BEGIN 4 */ |
||
742 | |||
743 | /* USER CODE END 4 */ |
||
744 | |||
745 | /** |
||
26 | mjames | 746 | * @brief This function is executed in case of error occurrence. |
747 | * @retval None |
||
748 | */ |
||
749 | void Error_Handler(void) |
||
2 | mjames | 750 | { |
751 | /* USER CODE BEGIN Error_Handler_Debug */ |
||
752 | /* User can add his own implementation to report the HAL error return state */ |
||
753 | |||
754 | /* USER CODE END Error_Handler_Debug */ |
||
755 | } |
||
756 | |||
757 | #ifdef USE_FULL_ASSERT |
||
758 | /** |
||
759 | * @brief Reports the name of the source file and the source line number |
||
760 | * where the assert_param error has occurred. |
||
761 | * @param file: pointer to the source file name |
||
762 | * @param line: assert_param error line source number |
||
763 | * @retval None |
||
764 | */ |
||
765 | void assert_failed(uint8_t *file, uint32_t line) |
||
766 | { |
||
767 | /* USER CODE BEGIN 6 */ |
||
768 | /* User can add his own implementation to report the file name and line number, |
||
769 | tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ |
||
770 | /* USER CODE END 6 */ |
||
771 | } |
||
772 | #endif /* USE_FULL_ASSERT */ |
||
773 | |||
774 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |