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