Rev 8 | Rev 10 | 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 | |||
9 | mjames | 63 | void HAL_GPIO_WRITE_ODR(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin) |
64 | { |
||
65 | /* Check the parameters */ |
||
66 | assert_param(IS_GPIO_PIN(GPIO_Pin)); |
||
67 | |||
68 | GPIOx->ODR |= GPIO_Pin; |
||
69 | } |
||
70 | |||
71 | |||
72 | /** Errata code from Stack Overflow |
||
73 | 1. Disable the I2C peripheral by clearing the PE bit in I2Cx_CR1 register. |
||
74 | 2. Configure the SCL and SDA I/Os as General Purpose Output Open-Drain, High level |
||
75 | (Write 1 to GPIOx_ODR). |
||
76 | 3. Check SCL and SDA High level in GPIOx_IDR. |
||
77 | 4. Configure the SDA I/O as General Purpose Output Open-Drain, Low level (Write 0 to |
||
78 | GPIOx_ODR). |
||
79 | 5. Check SDA Low level in GPIOx_IDR. |
||
80 | 6. Configure the SCL I/O as General Purpose Output Open-Drain, Low level (Write 0 to |
||
81 | GPIOx_ODR). |
||
82 | 7. Check SCL Low level in GPIOx_IDR. |
||
83 | 8. Configure the SCL I/O as General Purpose Output Open-Drain, High level (Write 1 to |
||
84 | GPIOx_ODR). |
||
85 | 9. Check SCL High level in GPIOx_IDR. |
||
86 | 10. Configure the SDA I/O as General Purpose Output Open-Drain , High level (Write 1 to |
||
87 | GPIOx_ODR). |
||
88 | 11. Check SDA High level in GPIOx_IDR. |
||
89 | 12. Configure the SCL and SDA I/Os as Alternate function Open-Drain. |
||
90 | 13. Set SWRST bit in I2Cx_CR1 register. |
||
91 | 14. Clear SWRST bit in I2Cx_CR1 register. |
||
92 | 15. Enable the I2C peripheral by setting the PE bit in I2Cx_CR1 register. |
||
93 | **/ |
||
94 | |||
95 | void HAL_I2C_ClearBusyFlagErrata_2_14_7(I2C_HandleTypeDef *hi2c) |
||
96 | { |
||
97 | |||
98 | static uint8_t resetTried = 0; |
||
99 | if (resetTried == 1) |
||
100 | { |
||
101 | return; |
||
102 | } |
||
103 | uint32_t const SDA_PIN = GPIO_PIN_7; |
||
104 | uint32_t const SCL_PIN = GPIO_PIN_6; |
||
105 | GPIO_InitTypeDef GPIO_InitStruct; |
||
106 | |||
107 | // 1 |
||
108 | __HAL_I2C_DISABLE(hi2c); |
||
109 | |||
110 | // 2 |
||
111 | GPIO_InitStruct.Pin = SDA_PIN | SCL_PIN; |
||
112 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD; |
||
113 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
114 | GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; |
||
115 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
||
116 | |||
117 | HAL_GPIO_WRITE_ODR(GPIOB, SDA_PIN); |
||
118 | HAL_GPIO_WRITE_ODR(GPIOB, SCL_PIN); |
||
119 | |||
120 | // 3 |
||
121 | GPIO_PinState pinState; |
||
122 | if (HAL_GPIO_ReadPin(GPIOB, SDA_PIN) == GPIO_PIN_RESET) |
||
123 | { |
||
124 | for (;;) |
||
125 | { |
||
126 | } |
||
127 | } |
||
128 | if (HAL_GPIO_ReadPin(GPIOB, SCL_PIN) == GPIO_PIN_RESET) |
||
129 | { |
||
130 | for (;;) |
||
131 | { |
||
132 | } |
||
133 | } |
||
134 | |||
135 | // 4 |
||
136 | GPIO_InitStruct.Pin = SDA_PIN; |
||
137 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
||
138 | |||
139 | HAL_GPIO_TogglePin(GPIOB, SDA_PIN); |
||
140 | |||
141 | // 5 |
||
142 | if (HAL_GPIO_ReadPin(GPIOB, SDA_PIN) == GPIO_PIN_SET) |
||
143 | { |
||
144 | for (;;) |
||
145 | { |
||
146 | } |
||
147 | } |
||
148 | |||
149 | // 6 |
||
150 | GPIO_InitStruct.Pin = SCL_PIN; |
||
151 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
||
152 | |||
153 | HAL_GPIO_TogglePin(GPIOB, SCL_PIN); |
||
154 | |||
155 | // 7 |
||
156 | if (HAL_GPIO_ReadPin(GPIOB, SCL_PIN) == GPIO_PIN_SET) |
||
157 | { |
||
158 | for (;;) |
||
159 | { |
||
160 | } |
||
161 | } |
||
162 | |||
163 | // 8 |
||
164 | GPIO_InitStruct.Pin = SDA_PIN; |
||
165 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
||
166 | |||
167 | HAL_GPIO_WRITE_ODR(GPIOB, SDA_PIN); |
||
168 | |||
169 | // 9 |
||
170 | if (HAL_GPIO_ReadPin(GPIOB, SDA_PIN) == GPIO_PIN_RESET) |
||
171 | { |
||
172 | for (;;) |
||
173 | { |
||
174 | } |
||
175 | } |
||
176 | |||
177 | // 10 |
||
178 | GPIO_InitStruct.Pin = SCL_PIN; |
||
179 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
||
180 | |||
181 | HAL_GPIO_WRITE_ODR(GPIOB, SCL_PIN); |
||
182 | |||
183 | // 11 |
||
184 | if (HAL_GPIO_ReadPin(GPIOB, SCL_PIN) == GPIO_PIN_RESET) |
||
185 | { |
||
186 | for (;;) |
||
187 | { |
||
188 | } |
||
189 | } |
||
190 | |||
191 | // 12 |
||
192 | GPIO_InitStruct.Pin = SDA_PIN | SCL_PIN; |
||
193 | GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; |
||
194 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
||
195 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
||
196 | |||
197 | // 13 |
||
198 | hi2c->Instance->CR1 |= I2C_CR1_SWRST; |
||
199 | |||
200 | // 14 |
||
201 | hi2c->Instance->CR1 ^= I2C_CR1_SWRST; |
||
202 | |||
203 | // 15 |
||
204 | __HAL_I2C_ENABLE(hi2c); |
||
205 | |||
206 | resetTried = 1; |
||
207 | } |
||
208 | |||
2 | mjames | 209 | /* USER CODE END 0 */ |
210 | |||
211 | /** |
||
8 | mjames | 212 | * @brief The application entry point. |
213 | * @retval int |
||
214 | */ |
||
2 | mjames | 215 | int main(void) |
8 | mjames | 216 | { |
2 | mjames | 217 | /* USER CODE BEGIN 1 */ |
218 | |||
219 | /* USER CODE END 1 */ |
||
220 | |||
221 | /* MCU Configuration--------------------------------------------------------*/ |
||
222 | |||
223 | /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ |
||
224 | HAL_Init(); |
||
225 | |||
226 | /* USER CODE BEGIN Init */ |
||
227 | |||
228 | /* USER CODE END Init */ |
||
229 | |||
230 | /* Configure the system clock */ |
||
231 | SystemClock_Config(); |
||
232 | |||
233 | /* USER CODE BEGIN SysInit */ |
||
9 | mjames | 234 | /* Peripheral clock enable */ |
2 | mjames | 235 | |
236 | /* USER CODE END SysInit */ |
||
237 | |||
238 | /* Initialize all configured peripherals */ |
||
239 | MX_GPIO_Init(); |
||
240 | MX_SPI1_Init(); |
||
4 | mjames | 241 | MX_I2C1_Init(); |
2 | mjames | 242 | /* USER CODE BEGIN 2 */ |
243 | cc_init(); |
||
4 | mjames | 244 | |
9 | mjames | 245 | HAL_I2C_ClearBusyFlagErrata_2_14_7(&hi2c1); |
246 | MX_I2C1_Init(); |
||
4 | mjames | 247 | init_bmp(&hi2c1); |
248 | uint32_t lastTick = HAL_GetTick(); |
||
2 | mjames | 249 | /* USER CODE END 2 */ |
250 | |||
251 | /* Infinite loop */ |
||
252 | /* USER CODE BEGIN WHILE */ |
||
253 | while (1) |
||
254 | { |
||
255 | cc_display(0); |
||
4 | mjames | 256 | |
257 | if (HAL_GetTick() - lastTick > 200) |
||
258 | { |
||
259 | lastTick = HAL_GetTick(); |
||
260 | /* Reading the raw data from sensor */ |
||
261 | struct bmp280_uncomp_data ucomp_data; |
||
262 | uint8_t rslt = bmp280_get_uncomp_data(&ucomp_data, &bmp); |
||
263 | |||
264 | uint32_t comp_pres = 0; |
||
265 | int32_t comp_temp = -10000; |
||
266 | if (rslt == 0) |
||
267 | { |
||
268 | uint8_t rslt2 = bmp280_get_comp_pres_32bit(&comp_pres, ucomp_data.uncomp_press, &bmp); |
||
269 | |||
270 | uint8_t rslt3 = bmp280_get_comp_temp_32bit(&comp_temp, ucomp_data.uncomp_temp, &bmp); |
||
271 | } |
||
272 | } |
||
2 | mjames | 273 | /* USER CODE END WHILE */ |
274 | |||
275 | /* USER CODE BEGIN 3 */ |
||
276 | } |
||
277 | /* USER CODE END 3 */ |
||
278 | } |
||
279 | |||
280 | /** |
||
8 | mjames | 281 | * @brief System Clock Configuration |
282 | * @retval None |
||
283 | */ |
||
2 | mjames | 284 | void SystemClock_Config(void) |
285 | { |
||
286 | RCC_OscInitTypeDef RCC_OscInitStruct = {0}; |
||
287 | RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; |
||
288 | |||
289 | /** Initializes the RCC Oscillators according to the specified parameters |
||
8 | mjames | 290 | * in the RCC_OscInitTypeDef structure. |
291 | */ |
||
2 | mjames | 292 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; |
293 | RCC_OscInitStruct.HSEState = RCC_HSE_ON; |
||
294 | RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; |
||
295 | RCC_OscInitStruct.HSIState = RCC_HSI_ON; |
||
296 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; |
||
297 | RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; |
||
9 | mjames | 298 | RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL4; |
2 | mjames | 299 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) |
300 | { |
||
301 | Error_Handler(); |
||
302 | } |
||
303 | |||
304 | /** Initializes the CPU, AHB and APB buses clocks |
||
8 | mjames | 305 | */ |
306 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |
||
307 | |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; |
||
2 | mjames | 308 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; |
309 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; |
||
310 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; |
||
311 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; |
||
312 | |||
9 | mjames | 313 | if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) |
2 | mjames | 314 | { |
315 | Error_Handler(); |
||
316 | } |
||
317 | } |
||
318 | |||
319 | /** |
||
8 | mjames | 320 | * @brief I2C1 Initialization Function |
321 | * @param None |
||
322 | * @retval None |
||
323 | */ |
||
4 | mjames | 324 | static void MX_I2C1_Init(void) |
325 | { |
||
326 | |||
327 | /* USER CODE BEGIN I2C1_Init 0 */ |
||
328 | |||
329 | /* USER CODE END I2C1_Init 0 */ |
||
330 | |||
331 | /* USER CODE BEGIN I2C1_Init 1 */ |
||
332 | |||
333 | /* USER CODE END I2C1_Init 1 */ |
||
334 | hi2c1.Instance = I2C1; |
||
335 | hi2c1.Init.ClockSpeed = 100000; |
||
336 | hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2; |
||
337 | hi2c1.Init.OwnAddress1 = 0; |
||
338 | hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; |
||
339 | hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; |
||
340 | hi2c1.Init.OwnAddress2 = 0; |
||
341 | hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; |
||
342 | hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; |
||
343 | if (HAL_I2C_Init(&hi2c1) != HAL_OK) |
||
344 | { |
||
345 | Error_Handler(); |
||
346 | } |
||
347 | /* USER CODE BEGIN I2C1_Init 2 */ |
||
348 | |||
349 | /* USER CODE END I2C1_Init 2 */ |
||
8 | mjames | 350 | |
4 | mjames | 351 | } |
352 | |||
353 | /** |
||
8 | mjames | 354 | * @brief SPI1 Initialization Function |
355 | * @param None |
||
356 | * @retval None |
||
357 | */ |
||
2 | mjames | 358 | static void MX_SPI1_Init(void) |
359 | { |
||
360 | |||
361 | /* USER CODE BEGIN SPI1_Init 0 */ |
||
362 | |||
363 | /* USER CODE END SPI1_Init 0 */ |
||
364 | |||
365 | /* USER CODE BEGIN SPI1_Init 1 */ |
||
366 | |||
367 | /* USER CODE END SPI1_Init 1 */ |
||
368 | /* SPI1 parameter configuration*/ |
||
369 | hspi1.Instance = SPI1; |
||
370 | hspi1.Init.Mode = SPI_MODE_MASTER; |
||
371 | hspi1.Init.Direction = SPI_DIRECTION_2LINES; |
||
372 | hspi1.Init.DataSize = SPI_DATASIZE_8BIT; |
||
373 | hspi1.Init.CLKPolarity = SPI_POLARITY_LOW; |
||
374 | hspi1.Init.CLKPhase = SPI_PHASE_1EDGE; |
||
375 | hspi1.Init.NSS = SPI_NSS_SOFT; |
||
376 | hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64; |
||
377 | hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; |
||
378 | hspi1.Init.TIMode = SPI_TIMODE_DISABLE; |
||
379 | hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; |
||
380 | hspi1.Init.CRCPolynomial = 10; |
||
381 | if (HAL_SPI_Init(&hspi1) != HAL_OK) |
||
382 | { |
||
383 | Error_Handler(); |
||
384 | } |
||
385 | /* USER CODE BEGIN SPI1_Init 2 */ |
||
386 | |||
387 | /* USER CODE END SPI1_Init 2 */ |
||
8 | mjames | 388 | |
2 | mjames | 389 | } |
390 | |||
391 | /** |
||
8 | mjames | 392 | * @brief GPIO Initialization Function |
393 | * @param None |
||
394 | * @retval None |
||
395 | */ |
||
2 | mjames | 396 | static void MX_GPIO_Init(void) |
397 | { |
||
398 | GPIO_InitTypeDef GPIO_InitStruct = {0}; |
||
399 | |||
400 | /* GPIO Ports Clock Enable */ |
||
401 | __HAL_RCC_GPIOD_CLK_ENABLE(); |
||
402 | __HAL_RCC_GPIOA_CLK_ENABLE(); |
||
403 | __HAL_RCC_GPIOB_CLK_ENABLE(); |
||
404 | |||
405 | /*Configure GPIO pin Output Level */ |
||
8 | mjames | 406 | HAL_GPIO_WritePin(GPIOA, SPI_NSS1_Pin|SPI_RESET_Pin, GPIO_PIN_RESET); |
2 | mjames | 407 | |
408 | /*Configure GPIO pin Output Level */ |
||
409 | HAL_GPIO_WritePin(SPI_CD_GPIO_Port, SPI_CD_Pin, GPIO_PIN_RESET); |
||
410 | |||
411 | /*Configure GPIO pins : SPI_NSS1_Pin SPI_RESET_Pin */ |
||
8 | mjames | 412 | GPIO_InitStruct.Pin = SPI_NSS1_Pin|SPI_RESET_Pin; |
2 | mjames | 413 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
414 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
415 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
||
416 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
||
417 | |||
418 | /*Configure GPIO pin : SPI_CD_Pin */ |
||
419 | GPIO_InitStruct.Pin = SPI_CD_Pin; |
||
420 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
||
421 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
||
422 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; |
||
423 | HAL_GPIO_Init(SPI_CD_GPIO_Port, &GPIO_InitStruct); |
||
8 | mjames | 424 | |
2 | mjames | 425 | } |
426 | |||
427 | /* USER CODE BEGIN 4 */ |
||
428 | |||
429 | /* USER CODE END 4 */ |
||
430 | |||
431 | /** |
||
8 | mjames | 432 | * @brief This function is executed in case of error occurrence. |
433 | * @retval None |
||
434 | */ |
||
2 | mjames | 435 | void Error_Handler(void) |
436 | { |
||
437 | /* USER CODE BEGIN Error_Handler_Debug */ |
||
438 | /* User can add his own implementation to report the HAL error return state */ |
||
439 | __disable_irq(); |
||
440 | while (1) |
||
441 | { |
||
442 | } |
||
443 | /* USER CODE END Error_Handler_Debug */ |
||
444 | } |
||
445 | |||
8 | mjames | 446 | #ifdef USE_FULL_ASSERT |
2 | mjames | 447 | /** |
8 | mjames | 448 | * @brief Reports the name of the source file and the source line number |
449 | * where the assert_param error has occurred. |
||
450 | * @param file: pointer to the source file name |
||
451 | * @param line: assert_param error line source number |
||
452 | * @retval None |
||
453 | */ |
||
2 | mjames | 454 | void assert_failed(uint8_t *file, uint32_t line) |
455 | { |
||
456 | /* USER CODE BEGIN 6 */ |
||
457 | /* User can add his own implementation to report the file name and line number, |
||
458 | ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ |
||
459 | /* USER CODE END 6 */ |
||
460 | } |
||
461 | #endif /* USE_FULL_ASSERT */ |