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