Rev 29 | Rev 31 | 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 */ |
| 28 | mjames | 135 | .dev_id = BMP280_I2C_ADDR_PRIM |
| 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 | |||
| 28 | mjames | 199 | static void reset_I2C1(void) |
| 200 | { |
||
| 201 | HAL_GPIO_WritePin ( GPIOB, GPIO_PIN_9, GPIO_PIN_SET); |
||
| 202 | HAL_GPIO_WritePin ( GPIOB, GPIO_PIN_8, GPIO_PIN_SET); |
||
| 203 | __HAL_AFIO_REMAP_I2C1_DISABLE(); |
||
| 204 | int i; |
||
| 205 | // clock 18 times |
||
| 206 | for (i=0;i<18;++i) |
||
| 207 | { |
||
| 208 | HAL_Delay (1); |
||
| 209 | HAL_GPIO_WritePin ( GPIOB, GPIO_PIN_9, GPIO_PIN_SET); |
||
| 210 | HAL_Delay (1); |
||
| 211 | HAL_GPIO_WritePin ( GPIOB, GPIO_PIN_9, GPIO_PIN_SET); |
||
| 212 | } |
||
| 213 | __HAL_AFIO_REMAP_I2C1_ENABLE(); |
||
| 26 | mjames | 214 | |
| 28 | mjames | 215 | } |
| 26 | mjames | 216 | |
| 29 | mjames | 217 | void resetBmp2(void) |
| 218 | { |
||
| 219 | HAL_GPIO_WritePin (I2C1_BusPower_GPIO_Port, I2C1_BusPower_Pin, GPIO_PIN_RESET); |
||
| 30 | mjames | 220 | HAL_Delay (400); |
| 29 | mjames | 221 | HAL_GPIO_WritePin (I2C1_BusPower_GPIO_Port, I2C1_BusPower_Pin, GPIO_PIN_SET); |
| 222 | HAL_Delay (200); |
||
| 223 | init_bmp(&conf2,&bmp2); |
||
| 26 | mjames | 224 | |
| 29 | mjames | 225 | } |
| 26 | mjames | 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 */ |
| 29 | mjames | 710 | HAL_GPIO_WritePin(I2C1_BusPower_GPIO_Port, I2C1_BusPower_Pin, GPIO_PIN_SET); |
| 711 | |||
| 712 | /*Configure GPIO pin Output Level */ |
||
| 26 | mjames | 713 | HAL_GPIO_WritePin(USB_PULLUP_GPIO_Port, USB_PULLUP_Pin, GPIO_PIN_RESET); |
| 14 | mjames | 714 | |
| 27 | mjames | 715 | /*Configure GPIO pin : Green_LED_Pin */ |
| 716 | GPIO_InitStruct.Pin = Green_LED_Pin; |
||
| 717 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
||
| 718 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
| 719 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
||
| 720 | HAL_GPIO_Init(Green_LED_GPIO_Port, &GPIO_InitStruct); |
||
| 721 | |||
| 13 | mjames | 722 | /*Configure GPIO pins : SPI_CD_Pin SPI_RESET_Pin SPI_NSS1_Pin */ |
| 26 | mjames | 723 | GPIO_InitStruct.Pin = SPI_CD_Pin|SPI_RESET_Pin|SPI_NSS1_Pin; |
| 2 | mjames | 724 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
| 725 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
| 726 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
||
| 26 | mjames | 727 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
| 2 | mjames | 728 | |
| 29 | mjames | 729 | /*Configure GPIO pin : I2C1_BusPower_Pin */ |
| 730 | GPIO_InitStruct.Pin = I2C1_BusPower_Pin; |
||
| 731 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
||
| 732 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
| 733 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; |
||
| 734 | HAL_GPIO_Init(I2C1_BusPower_GPIO_Port, &GPIO_InitStruct); |
||
| 735 | |||
| 14 | mjames | 736 | /*Configure GPIO pin : USB_PULLUP_Pin */ |
| 737 | GPIO_InitStruct.Pin = USB_PULLUP_Pin; |
||
| 738 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
||
| 739 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
| 740 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
||
| 26 | mjames | 741 | HAL_GPIO_Init(USB_PULLUP_GPIO_Port, &GPIO_InitStruct); |
| 14 | mjames | 742 | |
| 21 | mjames | 743 | /*Configure GPIO pin : encoder_push_Pin */ |
| 744 | GPIO_InitStruct.Pin = encoder_push_Pin; |
||
| 745 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT; |
||
| 746 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
||
| 26 | mjames | 747 | HAL_GPIO_Init(encoder_push_GPIO_Port, &GPIO_InitStruct); |
| 21 | mjames | 748 | |
| 2 | mjames | 749 | } |
| 750 | |||
| 751 | /* USER CODE BEGIN 4 */ |
||
| 752 | |||
| 753 | /* USER CODE END 4 */ |
||
| 754 | |||
| 755 | /** |
||
| 26 | mjames | 756 | * @brief This function is executed in case of error occurrence. |
| 757 | * @retval None |
||
| 758 | */ |
||
| 759 | void Error_Handler(void) |
||
| 2 | mjames | 760 | { |
| 761 | /* USER CODE BEGIN Error_Handler_Debug */ |
||
| 762 | /* User can add his own implementation to report the HAL error return state */ |
||
| 763 | |||
| 764 | /* USER CODE END Error_Handler_Debug */ |
||
| 765 | } |
||
| 766 | |||
| 767 | #ifdef USE_FULL_ASSERT |
||
| 768 | /** |
||
| 769 | * @brief Reports the name of the source file and the source line number |
||
| 770 | * where the assert_param error has occurred. |
||
| 771 | * @param file: pointer to the source file name |
||
| 772 | * @param line: assert_param error line source number |
||
| 773 | * @retval None |
||
| 774 | */ |
||
| 775 | void assert_failed(uint8_t *file, uint32_t line) |
||
| 776 | { |
||
| 777 | /* USER CODE BEGIN 6 */ |
||
| 778 | /* User can add his own implementation to report the file name and line number, |
||
| 779 | tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ |
||
| 780 | /* USER CODE END 6 */ |
||
| 781 | } |
||
| 782 | #endif /* USE_FULL_ASSERT */ |
||
| 783 | |||
| 784 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |