Rev 10 | 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 | * Copyright (c) 2022 STMicroelectronics. |
||
| 10 | * All rights reserved. |
||
| 11 | * |
||
| 12 | * This software is licensed under terms that can be found in the LICENSE file |
||
| 13 | * in the root directory of this software component. |
||
| 14 | * If no LICENSE file comes with this software, it is provided AS-IS. |
||
| 15 | * |
||
| 16 | ****************************************************************************** |
||
| 17 | */ |
||
| 18 | /* USER CODE END Header */ |
||
| 19 | /* Includes ------------------------------------------------------------------*/ |
||
| 20 | #include "main.h" |
||
| 21 | |||
| 22 | /* Private includes ----------------------------------------------------------*/ |
||
| 23 | /* USER CODE BEGIN Includes */ |
||
| 24 | #include "display.h" |
||
| 4 | mjames | 25 | #include "bmp280driver.h" |
| 11 | mjames | 26 | #include "libMisc/fixI2C.h" |
| 2 | mjames | 27 | /* USER CODE END Includes */ |
| 28 | |||
| 29 | /* Private typedef -----------------------------------------------------------*/ |
||
| 30 | /* USER CODE BEGIN PTD */ |
||
| 31 | |||
| 32 | /* USER CODE END PTD */ |
||
| 33 | |||
| 34 | /* Private define ------------------------------------------------------------*/ |
||
| 35 | /* USER CODE BEGIN PD */ |
||
| 36 | /* USER CODE END PD */ |
||
| 37 | |||
| 38 | /* Private macro -------------------------------------------------------------*/ |
||
| 39 | /* USER CODE BEGIN PM */ |
||
| 40 | |||
| 41 | /* USER CODE END PM */ |
||
| 42 | |||
| 43 | /* Private variables ---------------------------------------------------------*/ |
||
| 11 | mjames | 44 | I2C_HandleTypeDef hi2c1; |
| 2 | mjames | 45 | |
| 4 | mjames | 46 | SPI_HandleTypeDef hspi1; |
| 47 | |||
| 2 | mjames | 48 | /* USER CODE BEGIN PV */ |
| 49 | |||
| 50 | /* USER CODE END PV */ |
||
| 51 | |||
| 52 | /* Private function prototypes -----------------------------------------------*/ |
||
| 53 | void SystemClock_Config(void); |
||
| 54 | static void MX_GPIO_Init(void); |
||
| 55 | static void MX_SPI1_Init(void); |
||
| 4 | mjames | 56 | static void MX_I2C1_Init(void); |
| 2 | mjames | 57 | /* USER CODE BEGIN PFP */ |
| 58 | |||
| 59 | /* USER CODE END PFP */ |
||
| 60 | |||
| 61 | /* Private user code ---------------------------------------------------------*/ |
||
| 62 | /* USER CODE BEGIN 0 */ |
||
| 63 | |||
| 64 | /* USER CODE END 0 */ |
||
| 65 | |||
| 66 | /** |
||
| 11 | mjames | 67 | * @brief The application entry point. |
| 68 | * @retval int |
||
| 69 | */ |
||
| 2 | mjames | 70 | int main(void) |
| 8 | mjames | 71 | { |
| 2 | mjames | 72 | /* USER CODE BEGIN 1 */ |
| 73 | |||
| 74 | /* USER CODE END 1 */ |
||
| 75 | |||
| 76 | /* MCU Configuration--------------------------------------------------------*/ |
||
| 77 | |||
| 78 | /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ |
||
| 79 | HAL_Init(); |
||
| 80 | |||
| 81 | /* USER CODE BEGIN Init */ |
||
| 82 | |||
| 83 | /* USER CODE END Init */ |
||
| 84 | |||
| 85 | /* Configure the system clock */ |
||
| 86 | SystemClock_Config(); |
||
| 87 | |||
| 88 | /* USER CODE BEGIN SysInit */ |
||
| 9 | mjames | 89 | /* Peripheral clock enable */ |
| 2 | mjames | 90 | |
| 91 | /* USER CODE END SysInit */ |
||
| 92 | |||
| 93 | /* Initialize all configured peripherals */ |
||
| 94 | MX_GPIO_Init(); |
||
| 95 | MX_SPI1_Init(); |
||
| 4 | mjames | 96 | MX_I2C1_Init(); |
| 2 | mjames | 97 | /* USER CODE BEGIN 2 */ |
| 98 | cc_init(); |
||
| 4 | mjames | 99 | |
| 9 | mjames | 100 | HAL_I2C_ClearBusyFlagErrata_2_14_7(&hi2c1); |
| 101 | MX_I2C1_Init(); |
||
| 4 | mjames | 102 | init_bmp(&hi2c1); |
| 103 | uint32_t lastTick = HAL_GetTick(); |
||
| 2 | mjames | 104 | /* USER CODE END 2 */ |
| 105 | |||
| 106 | /* Infinite loop */ |
||
| 107 | /* USER CODE BEGIN WHILE */ |
||
| 108 | while (1) |
||
| 109 | { |
||
| 110 | cc_display(0); |
||
| 4 | mjames | 111 | |
| 11 | mjames | 112 | cc_feed_pushbutton(HAL_GPIO_ReadPin(PUSHBUTTON_GPIO_Port, PUSHBUTTON_Pin )==GPIO_PIN_SET); |
| 113 | |||
| 114 | |||
| 4 | mjames | 115 | if (HAL_GetTick() - lastTick > 200) |
| 116 | { |
||
| 117 | lastTick = HAL_GetTick(); |
||
| 118 | /* Reading the raw data from sensor */ |
||
| 119 | struct bmp280_uncomp_data ucomp_data; |
||
| 120 | uint8_t rslt = bmp280_get_uncomp_data(&ucomp_data, &bmp); |
||
| 121 | |||
| 122 | uint32_t comp_pres = 0; |
||
| 123 | int32_t comp_temp = -10000; |
||
| 124 | if (rslt == 0) |
||
| 125 | { |
||
| 126 | uint8_t rslt2 = bmp280_get_comp_pres_32bit(&comp_pres, ucomp_data.uncomp_press, &bmp); |
||
| 127 | |||
| 128 | uint8_t rslt3 = bmp280_get_comp_temp_32bit(&comp_temp, ucomp_data.uncomp_temp, &bmp); |
||
| 10 | mjames | 129 | |
| 130 | cc_feed(comp_pres, comp_temp); |
||
| 4 | mjames | 131 | } |
| 132 | } |
||
| 2 | mjames | 133 | /* USER CODE END WHILE */ |
| 134 | |||
| 135 | /* USER CODE BEGIN 3 */ |
||
| 136 | } |
||
| 137 | /* USER CODE END 3 */ |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 11 | mjames | 141 | * @brief System Clock Configuration |
| 142 | * @retval None |
||
| 143 | */ |
||
| 2 | mjames | 144 | void SystemClock_Config(void) |
| 145 | { |
||
| 146 | RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
||
| 147 | RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; |
||
| 148 | |||
| 149 | /** Initializes the RCC Oscillators according to the specified parameters |
||
| 11 | mjames | 150 | * in the RCC_OscInitTypeDef structure. |
| 151 | */ |
||
| 2 | mjames | 152 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; |
| 153 | RCC_OscInitStruct.HSEState = RCC_HSE_ON; |
||
| 154 | RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; |
||
| 155 | RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
||
| 156 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
||
| 157 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; |
||
| 9 | mjames | 158 | RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL4; |
| 2 | mjames | 159 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) |
| 160 | { |
||
| 161 | Error_Handler(); |
||
| 162 | } |
||
| 163 | |||
| 164 | /** Initializes the CPU, AHB and APB buses clocks |
||
| 11 | mjames | 165 | */ |
| 166 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |
||
| 167 | |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; |
||
| 2 | mjames | 168 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
| 169 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
||
| 170 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; |
||
| 171 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; |
||
| 172 | |||
| 9 | mjames | 173 | if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) |
| 2 | mjames | 174 | { |
| 175 | Error_Handler(); |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 11 | mjames | 180 | * @brief I2C1 Initialization Function |
| 181 | * @param None |
||
| 182 | * @retval None |
||
| 183 | */ |
||
| 4 | mjames | 184 | static void MX_I2C1_Init(void) |
| 185 | { |
||
| 186 | |||
| 187 | /* USER CODE BEGIN I2C1_Init 0 */ |
||
| 188 | |||
| 189 | /* USER CODE END I2C1_Init 0 */ |
||
| 190 | |||
| 191 | /* USER CODE BEGIN I2C1_Init 1 */ |
||
| 192 | |||
| 193 | /* USER CODE END I2C1_Init 1 */ |
||
| 194 | hi2c1.Instance = I2C1; |
||
| 195 | hi2c1.Init.ClockSpeed = 100000; |
||
| 196 | hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2; |
||
| 197 | hi2c1.Init.OwnAddress1 = 0; |
||
| 198 | hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; |
||
| 199 | hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; |
||
| 200 | hi2c1.Init.OwnAddress2 = 0; |
||
| 201 | hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; |
||
| 202 | hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; |
||
| 203 | if (HAL_I2C_Init(&hi2c1) != HAL_OK) |
||
| 204 | { |
||
| 205 | Error_Handler(); |
||
| 206 | } |
||
| 207 | /* USER CODE BEGIN I2C1_Init 2 */ |
||
| 208 | |||
| 209 | /* USER CODE END I2C1_Init 2 */ |
||
| 11 | mjames | 210 | |
| 4 | mjames | 211 | } |
| 212 | |||
| 213 | /** |
||
| 11 | mjames | 214 | * @brief SPI1 Initialization Function |
| 215 | * @param None |
||
| 216 | * @retval None |
||
| 217 | */ |
||
| 2 | mjames | 218 | static void MX_SPI1_Init(void) |
| 219 | { |
||
| 220 | |||
| 221 | /* USER CODE BEGIN SPI1_Init 0 */ |
||
| 222 | |||
| 223 | /* USER CODE END SPI1_Init 0 */ |
||
| 224 | |||
| 225 | /* USER CODE BEGIN SPI1_Init 1 */ |
||
| 226 | |||
| 227 | /* USER CODE END SPI1_Init 1 */ |
||
| 228 | /* SPI1 parameter configuration*/ |
||
| 229 | hspi1.Instance = SPI1; |
||
| 230 | hspi1.Init.Mode = SPI_MODE_MASTER; |
||
| 231 | hspi1.Init.Direction = SPI_DIRECTION_2LINES; |
||
| 232 | hspi1.Init.DataSize = SPI_DATASIZE_8BIT; |
||
| 233 | hspi1.Init.CLKPolarity = SPI_POLARITY_LOW; |
||
| 234 | hspi1.Init.CLKPhase = SPI_PHASE_1EDGE; |
||
| 235 | hspi1.Init.NSS = SPI_NSS_SOFT; |
||
| 236 | hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64; |
||
| 237 | hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; |
||
| 238 | hspi1.Init.TIMode = SPI_TIMODE_DISABLE; |
||
| 239 | hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; |
||
| 240 | hspi1.Init.CRCPolynomial = 10; |
||
| 241 | if (HAL_SPI_Init(&hspi1) != HAL_OK) |
||
| 242 | { |
||
| 243 | Error_Handler(); |
||
| 244 | } |
||
| 245 | /* USER CODE BEGIN SPI1_Init 2 */ |
||
| 246 | |||
| 247 | /* USER CODE END SPI1_Init 2 */ |
||
| 11 | mjames | 248 | |
| 2 | mjames | 249 | } |
| 250 | |||
| 251 | /** |
||
| 11 | mjames | 252 | * @brief GPIO Initialization Function |
| 253 | * @param None |
||
| 254 | * @retval None |
||
| 255 | */ |
||
| 2 | mjames | 256 | static void MX_GPIO_Init(void) |
| 257 | { |
||
| 258 | GPIO_InitTypeDef GPIO_InitStruct = {0}; |
||
| 259 | |||
| 260 | /* GPIO Ports Clock Enable */ |
||
| 261 | __HAL_RCC_GPIOD_CLK_ENABLE(); |
||
| 262 | __HAL_RCC_GPIOA_CLK_ENABLE(); |
||
| 263 | __HAL_RCC_GPIOB_CLK_ENABLE(); |
||
| 264 | |||
| 265 | /*Configure GPIO pin Output Level */ |
||
| 11 | mjames | 266 | HAL_GPIO_WritePin(GPIOA, SPI_NSS1_Pin|SPI_RESET_Pin, GPIO_PIN_RESET); |
| 2 | mjames | 267 | |
| 268 | /*Configure GPIO pin Output Level */ |
||
| 269 | HAL_GPIO_WritePin(SPI_CD_GPIO_Port, SPI_CD_Pin, GPIO_PIN_RESET); |
||
| 270 | |||
| 271 | /*Configure GPIO pins : SPI_NSS1_Pin SPI_RESET_Pin */ |
||
| 11 | mjames | 272 | GPIO_InitStruct.Pin = SPI_NSS1_Pin|SPI_RESET_Pin; |
| 2 | mjames | 273 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
| 274 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
| 275 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
||
| 276 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
||
| 277 | |||
| 278 | /*Configure GPIO pin : SPI_CD_Pin */ |
||
| 279 | GPIO_InitStruct.Pin = SPI_CD_Pin; |
||
| 280 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
||
| 281 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
| 282 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
||
| 283 | HAL_GPIO_Init(SPI_CD_GPIO_Port, &GPIO_InitStruct); |
||
| 11 | mjames | 284 | |
| 285 | /*Configure GPIO pin : PUSHBUTTON_Pin */ |
||
| 286 | GPIO_InitStruct.Pin = PUSHBUTTON_Pin; |
||
| 287 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT; |
||
| 288 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
||
| 289 | HAL_GPIO_Init(PUSHBUTTON_GPIO_Port, &GPIO_InitStruct); |
||
| 290 | |||
| 2 | mjames | 291 | } |
| 292 | |||
| 293 | /* USER CODE BEGIN 4 */ |
||
| 294 | |||
| 295 | /* USER CODE END 4 */ |
||
| 296 | |||
| 297 | /** |
||
| 11 | mjames | 298 | * @brief This function is executed in case of error occurrence. |
| 299 | * @retval None |
||
| 300 | */ |
||
| 2 | mjames | 301 | void Error_Handler(void) |
| 302 | { |
||
| 303 | /* USER CODE BEGIN Error_Handler_Debug */ |
||
| 304 | /* User can add his own implementation to report the HAL error return state */ |
||
| 305 | __disable_irq(); |
||
| 306 | while (1) |
||
| 307 | { |
||
| 308 | } |
||
| 309 | /* USER CODE END Error_Handler_Debug */ |
||
| 310 | } |
||
| 311 | |||
| 11 | mjames | 312 | #ifdef USE_FULL_ASSERT |
| 2 | mjames | 313 | /** |
| 11 | mjames | 314 | * @brief Reports the name of the source file and the source line number |
| 315 | * where the assert_param error has occurred. |
||
| 316 | * @param file: pointer to the source file name |
||
| 317 | * @param line: assert_param error line source number |
||
| 318 | * @retval None |
||
| 319 | */ |
||
| 2 | mjames | 320 | void assert_failed(uint8_t *file, uint32_t line) |
| 321 | { |
||
| 322 | /* USER CODE BEGIN 6 */ |
||
| 323 | /* User can add his own implementation to report the file name and line number, |
||
| 324 | ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ |
||
| 325 | /* USER CODE END 6 */ |
||
| 326 | } |
||
| 327 | #endif /* USE_FULL_ASSERT */ |