Subversion Repositories testOled

Rev

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