Subversion Repositories dashGPS

Rev

Rev 31 | 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>&copy; 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
32 mjames 67
user_i2c_write(uint8_t i2c_addr, uint8_t reg_addr, uint8_t *reg_data,
68
               uint32_t len)
11 mjames 69
{
32 mjames 70
  HAL_StatusTypeDef st = HAL_I2C_Mem_Write(&hi2c2, i2c_addr << 1, reg_addr, 1,
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
static int8_t
32 mjames 76
user_i2c_read(uint8_t i2c_addr, uint8_t reg_addr, uint8_t *reg_data,
77
              uint32_t len)
11 mjames 78
{
32 mjames 79
  HAL_StatusTypeDef st = HAL_I2C_Mem_Read(&hi2c2, i2c_addr << 1, reg_addr, 1,
80
                                          reg_data, len, 1000);
11 mjames 81
 
24 mjames 82
  return st != HAL_OK ? BMP280_E_COMM_FAIL : BMP280_OK;
11 mjames 83
}
28 mjames 84
// the second I2C bus is I2C1, this is used for the external I2C thermometer to avoid problems with noise pickup .
85
static int8_t
32 mjames 86
user_i2c2_write(uint8_t i2c_addr, uint8_t reg_addr, uint8_t *reg_data,
87
                uint32_t len)
28 mjames 88
{
32 mjames 89
  HAL_StatusTypeDef st = HAL_I2C_Mem_Write(&hi2c1, i2c_addr << 1, reg_addr, 1,
90
                                           reg_data, len, 1000);
11 mjames 91
 
28 mjames 92
  return st != HAL_OK ? BMP280_E_COMM_FAIL : BMP280_OK;
93
}
94
static int8_t
32 mjames 95
user_i2c2_read(uint8_t i2c_addr, uint8_t reg_addr, uint8_t *reg_data,
96
               uint32_t len)
28 mjames 97
{
32 mjames 98
  HAL_StatusTypeDef st = HAL_I2C_Mem_Read(&hi2c1, i2c_addr << 1, reg_addr, 1,
99
                                          reg_data, len, 1000);
28 mjames 100
 
101
  return st != HAL_OK ? BMP280_E_COMM_FAIL : BMP280_OK;
102
}
103
 
11 mjames 104
static void
32 mjames 105
user_delay_ms(uint32_t ms, void *handle)
11 mjames 106
{
32 mjames 107
  HAL_Delay(ms);
11 mjames 108
}
109
 
110
struct bmp280_dev bmp =
32 mjames 111
    {
112
        .intf = BMP280_I2C_INTF,
113
        .read = user_i2c_read,
114
        .write = user_i2c_write,
115
        .delay_ms = user_delay_ms,
11 mjames 116
 
32 mjames 117
        /* Update interface pointer with the structure that contains both device address and file descriptor */
118
        .dev_id = BMP280_I2C_ADDR_PRIM};
11 mjames 119
 
26 mjames 120
struct bmp280_dev bmp2 =
32 mjames 121
    {
122
        .intf = BMP280_I2C_INTF,
123
        .read = user_i2c2_read,
124
        .write = user_i2c2_write,
125
        .delay_ms = user_delay_ms,
11 mjames 126
 
32 mjames 127
        /* Update interface pointer with the structure that contains both device address and file descriptor */
128
        .dev_id = BMP280_I2C_ADDR_SEC};
26 mjames 129
 
11 mjames 130
int8_t rslt;
26 mjames 131
 
11 mjames 132
struct bmp280_config conf;
26 mjames 133
struct bmp280_config conf2;
11 mjames 134
 
2 mjames 135
/* USER CODE END PV */
136
 
137
/* Private function prototypes -----------------------------------------------*/
26 mjames 138
void SystemClock_Config(void);
139
static void MX_GPIO_Init(void);
140
static void MX_SPI1_Init(void);
141
static void MX_TIM4_Init(void);
142
static void MX_USART1_UART_Init(void);
143
static void MX_TIM3_Init(void);
144
static void MX_I2C2_Init(void);
145
static void MX_RTC_Init(void);
27 mjames 146
static void MX_IWDG_Init(void);
28 mjames 147
static void MX_I2C1_Init(void);
2 mjames 148
/* USER CODE BEGIN PFP */
149
 
150
/* USER CODE END PFP */
151
 
152
/* Private user code ---------------------------------------------------------*/
153
/* USER CODE BEGIN 0 */
154
 
26 mjames 155
uint32_t const TICKS_LOOP = 50; // 50mS per loop.
24 mjames 156
 
26 mjames 157
// initialise the BMP
32 mjames 158
uint8_t init_bmp(struct bmp280_config *conf, struct bmp280_dev *bmp)
26 mjames 159
{
160
  rslt = bmp280_init(bmp);
161
  if (rslt != BMP280_OK)
162
    return rslt;
163
 
32 mjames 164
  rslt = bmp280_get_config(conf, bmp);
26 mjames 165
  if (rslt != BMP280_OK)
166
    return rslt;
167
  /* configuring the temperature oversampling, filter coefficient and output data rate */
168
  /* Overwrite the desired settings */
169
  conf->filter = BMP280_FILTER_COEFF_2;
170
 
171
  /* Temperature oversampling set at 4x */
172
  conf->os_temp = BMP280_OS_4X;
173
 
174
  /* Pressure over sampling none (disabling pressure measurement) */
175
  conf->os_pres = BMP280_OS_4X;
176
 
177
  /* Setting the output data rate as 2HZ(500ms) */
178
  conf->odr = BMP280_ODR_500_MS;
32 mjames 179
  rslt = bmp280_set_config(conf, bmp);
26 mjames 180
  if (rslt != BMP280_OK)
181
    return rslt;
182
 
183
  /* Always set the power mode after setting the configuration */
32 mjames 184
  rslt = bmp280_set_power_mode(BMP280_NORMAL_MODE, bmp);
26 mjames 185
  if (rslt != BMP280_OK)
186
    return rslt;
187
}
188
 
31 mjames 189
///////////////////////////////////////
190
// code dealing with I2C1
28 mjames 191
static void reset_I2C1(void)
192
{
32 mjames 193
  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_SET);
194
  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_SET);
28 mjames 195
  __HAL_AFIO_REMAP_I2C1_DISABLE();
196
  int i;
197
  // clock 18 times
32 mjames 198
  for (i = 0; i < 18; ++i)
199
  {
200
    HAL_Delay(1);
201
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_SET);
202
    HAL_Delay(1);
203
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_SET);
204
  }
205
  __HAL_AFIO_REMAP_I2C1_ENABLE();
28 mjames 206
}
26 mjames 207
 
31 mjames 208
// the bus power also takes down the pullup power
209
static void power_I2C1(uint8_t power)
29 mjames 210
{
32 mjames 211
  if (power)
212
  {
213
    HAL_GPIO_WritePin(I2C1_BusPower_GPIO_Port, I2C1_BusPower_Pin, GPIO_PIN_SET);
214
  }
31 mjames 215
  else
32 mjames 216
  {
217
    HAL_GPIO_WritePin(I2C1_BusPower_GPIO_Port, I2C1_BusPower_Pin, GPIO_PIN_RESET);
218
  }
31 mjames 219
}
220
 
221
typedef enum
222
{
32 mjames 223
  BMP_NORMAL,       // normal operations
224
  BMP_RESET_ACTIVE, // powering down
225
  BMP_PAUSE         // pause after powerup
31 mjames 226
} bmp2state_t;
227
 
228
bmp2state_t bmp2state;
32 mjames 229
unsigned bmp2nextTime;
230
unsigned bmp2offset;
31 mjames 231
 
232
void initBmp2(void)
233
{
234
  power_I2C1(1);
32 mjames 235
  init_bmp(&conf2, &bmp2);
31 mjames 236
  bmp2state = BMP_NORMAL;
237
  bmp2nextTime = 0;
32 mjames 238
  bmp2offset = 0;
31 mjames 239
}
26 mjames 240
 
31 mjames 241
void resetBmp2(void)
242
{
32 mjames 243
  reset_I2C1();                 // physically reset the hardware
244
  power_I2C1(0);                // power down the bus and devices
31 mjames 245
  bmp2nextTime = HAL_GetTick(); // 600 milliseconds hold the bus reset for
246
  bmp2offset = 600;
247
  bmp2state = BMP_RESET_ACTIVE;
29 mjames 248
}
26 mjames 249
 
31 mjames 250
uint8_t pollBmp2State(void)
251
{
252
  if (bmp2state == BMP_NORMAL)
253
    return 1;
28 mjames 254
 
32 mjames 255
  if (HAL_GetTick() - bmp2offset < bmp2nextTime)
31 mjames 256
    return 0; // currently timer running
257
  switch (bmp2state)
258
  {
32 mjames 259
  // at the end of the period, take action
260
  case BMP_RESET_ACTIVE:
261
    power_I2C1(1);
262
    bmp2nextTime = HAL_GetTick();
263
    bmp2offset = 200; // power up and wait for 200 milliseconds
264
    bmp2state = BMP_PAUSE;
265
    break;
266
  case BMP_PAUSE:
267
    init_bmp(&conf2, &bmp2);
268
    bmp2state = BMP_NORMAL;
269
    return 1; // now in normal operation
270
  // can not get here in normal operation
271
  case BMP_NORMAL:
272
    return 1;
31 mjames 273
  }
274
  return 0;
275
}
276
 
2 mjames 277
/* USER CODE END 0 */
278
 
279
/**
32 mjames 280
 * @brief  The application entry point.
281
 * @retval int
282
 */
26 mjames 283
int main(void)
2 mjames 284
{
285
  /* USER CODE BEGIN 1 */
286
 
287
  /* USER CODE END 1 */
288
 
289
  /* MCU Configuration--------------------------------------------------------*/
290
 
291
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
26 mjames 292
  HAL_Init();
2 mjames 293
 
294
  /* USER CODE BEGIN Init */
295
 
296
  /* USER CODE END Init */
297
 
298
  /* Configure the system clock */
26 mjames 299
  SystemClock_Config();
2 mjames 300
 
301
  /* USER CODE BEGIN SysInit */
302
 
303
  /* USER CODE END SysInit */
304
 
305
  /* Initialize all configured peripherals */
26 mjames 306
  MX_GPIO_Init();
307
  MX_SPI1_Init();
308
  MX_TIM4_Init();
309
  MX_USART1_UART_Init();
310
  MX_TIM3_Init();
311
  MX_I2C2_Init();
312
  MX_RTC_Init();
313
  MX_USB_DEVICE_Init();
27 mjames 314
  MX_IWDG_Init();
28 mjames 315
  MX_I2C1_Init();
2 mjames 316
  /* USER CODE BEGIN 2 */
14 mjames 317
 
32 mjames 318
  HAL_GPIO_WritePin(USB_PULLUP_GPIO_Port, USB_PULLUP_Pin, GPIO_PIN_RESET);
319
  HAL_Delay(1000);
320
  HAL_GPIO_WritePin(USB_PULLUP_GPIO_Port, USB_PULLUP_Pin, GPIO_PIN_SET);
24 mjames 321
  /* setup the USART control blocks */
2 mjames 322
 
22 mjames 323
#if defined SERIAL_UART1
32 mjames 324
  init_usart_ctl(&uc1, &huart1);
22 mjames 325
 
32 mjames 326
  EnableSerialRxInterrupt(&uc1);
25 mjames 327
 
22 mjames 328
#endif
2 mjames 329
 
32 mjames 330
  init_bmp(&conf, &bmp);
31 mjames 331
  initBmp2();
11 mjames 332
 
32 mjames 333
  cc_init();
11 mjames 334
 
32 mjames 335
  uint32_t timeNext = HAL_GetTick() + TICKS_LOOP;
11 mjames 336
 
2 mjames 337
  /* USER CODE END 2 */
338
 
339
  /* Infinite loop */
340
  /* USER CODE BEGIN WHILE */
341
  while (1)
32 mjames 342
  {
343
    uint32_t timeNow = HAL_GetTick();
22 mjames 344
 
32 mjames 345
    if (timeNow < timeNext)
346
      HAL_Delay(timeNext - timeNow);
347
    timeNext += TICKS_LOOP;
348
    cc_run(&bmp, &bmp2);
2 mjames 349
 
32 mjames 350
    // refresh watchdog timer
351
    HAL_IWDG_Refresh(&hiwdg);
27 mjames 352
 
26 mjames 353
    /* USER CODE END WHILE */
14 mjames 354
 
26 mjames 355
    /* USER CODE BEGIN 3 */
32 mjames 356
  }
2 mjames 357
  /* USER CODE END 3 */
358
}
359
 
360
/**
32 mjames 361
 * @brief System Clock Configuration
362
 * @retval None
363
 */
26 mjames 364
void SystemClock_Config(void)
2 mjames 365
{
26 mjames 366
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
367
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
368
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
2 mjames 369
 
370
  /** Initializes the RCC Oscillators according to the specified parameters
32 mjames 371
   * in the RCC_OscInitTypeDef structure.
372
   */
373
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_LSE;
2 mjames 374
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
375
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
13 mjames 376
  RCC_OscInitStruct.LSEState = RCC_LSE_ON;
2 mjames 377
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
27 mjames 378
  RCC_OscInitStruct.LSIState = RCC_LSI_ON;
2 mjames 379
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
380
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
13 mjames 381
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
26 mjames 382
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
383
  {
384
    Error_Handler();
385
  }
2 mjames 386
  /** Initializes the CPU, AHB and APB buses clocks
32 mjames 387
   */
388
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
2 mjames 389
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
390
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
391
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
392
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
393
 
26 mjames 394
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
395
  {
396
    Error_Handler();
397
  }
32 mjames 398
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC | RCC_PERIPHCLK_USB;
13 mjames 399
  PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
400
  PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;
26 mjames 401
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
402
  {
403
    Error_Handler();
404
  }
2 mjames 405
}
406
 
407
/**
32 mjames 408
 * @brief I2C1 Initialization Function
409
 * @param None
410
 * @retval None
411
 */
28 mjames 412
static void MX_I2C1_Init(void)
413
{
414
 
415
  /* USER CODE BEGIN I2C1_Init 0 */
416
 
417
  /* USER CODE END I2C1_Init 0 */
418
 
419
  /* USER CODE BEGIN I2C1_Init 1 */
420
 
421
  /* USER CODE END I2C1_Init 1 */
422
  hi2c1.Instance = I2C1;
423
  hi2c1.Init.ClockSpeed = 100000;
424
  hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
425
  hi2c1.Init.OwnAddress1 = 0;
426
  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
427
  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
428
  hi2c1.Init.OwnAddress2 = 0;
429
  hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
430
  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
431
  if (HAL_I2C_Init(&hi2c1) != HAL_OK)
432
  {
433
    Error_Handler();
434
  }
435
  /* USER CODE BEGIN I2C1_Init 2 */
436
 
437
  /* USER CODE END I2C1_Init 2 */
438
}
439
 
440
/**
32 mjames 441
 * @brief I2C2 Initialization Function
442
 * @param None
443
 * @retval None
444
 */
26 mjames 445
static void MX_I2C2_Init(void)
11 mjames 446
{
447
 
448
  /* USER CODE BEGIN I2C2_Init 0 */
449
 
450
  /* USER CODE END I2C2_Init 0 */
451
 
452
  /* USER CODE BEGIN I2C2_Init 1 */
453
 
454
  /* USER CODE END I2C2_Init 1 */
455
  hi2c2.Instance = I2C2;
456
  hi2c2.Init.ClockSpeed = 100000;
457
  hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2;
458
  hi2c2.Init.OwnAddress1 = 0;
459
  hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
460
  hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
461
  hi2c2.Init.OwnAddress2 = 0;
462
  hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
463
  hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
26 mjames 464
  if (HAL_I2C_Init(&hi2c2) != HAL_OK)
465
  {
466
    Error_Handler();
467
  }
11 mjames 468
  /* USER CODE BEGIN I2C2_Init 2 */
469
 
470
  /* USER CODE END I2C2_Init 2 */
471
}
472
 
473
/**
32 mjames 474
 * @brief IWDG Initialization Function
475
 * @param None
476
 * @retval None
477
 */
27 mjames 478
static void MX_IWDG_Init(void)
479
{
480
 
481
  /* USER CODE BEGIN IWDG_Init 0 */
482
 
483
  /* USER CODE END IWDG_Init 0 */
484
 
485
  /* USER CODE BEGIN IWDG_Init 1 */
486
 
487
  /* USER CODE END IWDG_Init 1 */
488
  hiwdg.Instance = IWDG;
489
  hiwdg.Init.Prescaler = IWDG_PRESCALER_16;
490
  hiwdg.Init.Reload = 4095;
491
  if (HAL_IWDG_Init(&hiwdg) != HAL_OK)
492
  {
493
    Error_Handler();
494
  }
495
  /* USER CODE BEGIN IWDG_Init 2 */
496
 
497
  /* USER CODE END IWDG_Init 2 */
498
}
499
 
500
/**
32 mjames 501
 * @brief RTC Initialization Function
502
 * @param None
503
 * @retval None
504
 */
26 mjames 505
static void MX_RTC_Init(void)
13 mjames 506
{
507
 
508
  /* USER CODE BEGIN RTC_Init 0 */
509
 
510
  /* USER CODE END RTC_Init 0 */
511
 
26 mjames 512
  RTC_TimeTypeDef sTime = {0};
513
  RTC_DateTypeDef DateToUpdate = {0};
13 mjames 514
 
515
  /* USER CODE BEGIN RTC_Init 1 */
516
 
517
  /* USER CODE END RTC_Init 1 */
518
  /** Initialize RTC Only
32 mjames 519
   */
13 mjames 520
  hrtc.Instance = RTC;
521
  hrtc.Init.AsynchPrediv = RTC_AUTO_1_SECOND;
522
  hrtc.Init.OutPut = RTC_OUTPUTSOURCE_ALARM;
26 mjames 523
  if (HAL_RTC_Init(&hrtc) != HAL_OK)
524
  {
525
    Error_Handler();
526
  }
13 mjames 527
 
528
  /* USER CODE BEGIN Check_RTC_BKUP */
529
 
530
  /* USER CODE END Check_RTC_BKUP */
531
 
532
  /** Initialize RTC and set the Time and Date
32 mjames 533
   */
13 mjames 534
  sTime.Hours = 0x0;
535
  sTime.Minutes = 0x0;
536
  sTime.Seconds = 0x0;
537
 
26 mjames 538
  if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
539
  {
540
    Error_Handler();
541
  }
13 mjames 542
  DateToUpdate.WeekDay = RTC_WEEKDAY_MONDAY;
543
  DateToUpdate.Month = RTC_MONTH_JANUARY;
544
  DateToUpdate.Date = 0x1;
545
  DateToUpdate.Year = 0x0;
546
 
26 mjames 547
  if (HAL_RTC_SetDate(&hrtc, &DateToUpdate, RTC_FORMAT_BCD) != HAL_OK)
548
  {
549
    Error_Handler();
550
  }
13 mjames 551
  /* USER CODE BEGIN RTC_Init 2 */
552
 
553
  /* USER CODE END RTC_Init 2 */
554
}
555
 
556
/**
32 mjames 557
 * @brief SPI1 Initialization Function
558
 * @param None
559
 * @retval None
560
 */
26 mjames 561
static void MX_SPI1_Init(void)
2 mjames 562
{
563
 
564
  /* USER CODE BEGIN SPI1_Init 0 */
565
 
566
  /* USER CODE END SPI1_Init 0 */
567
 
568
  /* USER CODE BEGIN SPI1_Init 1 */
569
 
570
  /* USER CODE END SPI1_Init 1 */
571
  /* SPI1 parameter configuration*/
572
  hspi1.Instance = SPI1;
573
  hspi1.Init.Mode = SPI_MODE_MASTER;
574
  hspi1.Init.Direction = SPI_DIRECTION_1LINE;
575
  hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
5 mjames 576
  hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
577
  hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
2 mjames 578
  hspi1.Init.NSS = SPI_NSS_SOFT;
579
  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
580
  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
581
  hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
582
  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
583
  hspi1.Init.CRCPolynomial = 10;
26 mjames 584
  if (HAL_SPI_Init(&hspi1) != HAL_OK)
585
  {
586
    Error_Handler();
587
  }
2 mjames 588
  /* USER CODE BEGIN SPI1_Init 2 */
589
 
590
  /* USER CODE END SPI1_Init 2 */
591
}
592
 
593
/**
32 mjames 594
 * @brief TIM3 Initialization Function
595
 * @param None
596
 * @retval None
597
 */
26 mjames 598
static void MX_TIM3_Init(void)
2 mjames 599
{
600
 
9 mjames 601
  /* USER CODE BEGIN TIM3_Init 0 */
602
 
603
  /* USER CODE END TIM3_Init 0 */
604
 
26 mjames 605
  TIM_MasterConfigTypeDef sMasterConfig = {0};
606
  TIM_OC_InitTypeDef sConfigOC = {0};
9 mjames 607
 
608
  /* USER CODE BEGIN TIM3_Init 1 */
609
 
610
  /* USER CODE END TIM3_Init 1 */
611
  htim3.Instance = TIM3;
21 mjames 612
  htim3.Init.Prescaler = 719;
9 mjames 613
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
614
  htim3.Init.Period = 10000;
615
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
616
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
26 mjames 617
  if (HAL_TIM_OC_Init(&htim3) != HAL_OK)
618
  {
619
    Error_Handler();
620
  }
621
  if (HAL_TIM_OnePulse_Init(&htim3, TIM_OPMODE_SINGLE) != HAL_OK)
622
  {
623
    Error_Handler();
624
  }
9 mjames 625
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_ENABLE;
626
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
26 mjames 627
  if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
628
  {
629
    Error_Handler();
630
  }
9 mjames 631
  sConfigOC.OCMode = TIM_OCMODE_TIMING;
632
  sConfigOC.Pulse = 9999;
633
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
634
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
26 mjames 635
  if (HAL_TIM_OC_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
636
  {
637
    Error_Handler();
638
  }
9 mjames 639
  /* USER CODE BEGIN TIM3_Init 2 */
640
 
641
  /* USER CODE END TIM3_Init 2 */
642
}
643
 
644
/**
32 mjames 645
 * @brief TIM4 Initialization Function
646
 * @param None
647
 * @retval None
648
 */
26 mjames 649
static void MX_TIM4_Init(void)
9 mjames 650
{
651
 
2 mjames 652
  /* USER CODE BEGIN TIM4_Init 0 */
653
 
654
  /* USER CODE END TIM4_Init 0 */
655
 
26 mjames 656
  TIM_Encoder_InitTypeDef sConfig = {0};
657
  TIM_MasterConfigTypeDef sMasterConfig = {0};
2 mjames 658
 
659
  /* USER CODE BEGIN TIM4_Init 1 */
660
 
661
  /* USER CODE END TIM4_Init 1 */
662
  htim4.Instance = TIM4;
663
  htim4.Init.Prescaler = 0;
664
  htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
665
  htim4.Init.Period = 65535;
666
  htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
667
  htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
668
  sConfig.EncoderMode = TIM_ENCODERMODE_TI12;
669
  sConfig.IC1Polarity = TIM_ICPOLARITY_RISING;
670
  sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;
671
  sConfig.IC1Prescaler = TIM_ICPSC_DIV1;
21 mjames 672
  sConfig.IC1Filter = 15;
2 mjames 673
  sConfig.IC2Polarity = TIM_ICPOLARITY_RISING;
674
  sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI;
675
  sConfig.IC2Prescaler = TIM_ICPSC_DIV1;
21 mjames 676
  sConfig.IC2Filter = 15;
26 mjames 677
  if (HAL_TIM_Encoder_Init(&htim4, &sConfig) != HAL_OK)
678
  {
679
    Error_Handler();
680
  }
2 mjames 681
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
682
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
26 mjames 683
  if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK)
684
  {
685
    Error_Handler();
686
  }
2 mjames 687
  /* USER CODE BEGIN TIM4_Init 2 */
688
 
689
  /* USER CODE END TIM4_Init 2 */
690
}
691
 
692
/**
32 mjames 693
 * @brief USART1 Initialization Function
694
 * @param None
695
 * @retval None
696
 */
26 mjames 697
static void MX_USART1_UART_Init(void)
2 mjames 698
{
699
 
700
  /* USER CODE BEGIN USART1_Init 0 */
701
 
702
  /* USER CODE END USART1_Init 0 */
703
 
704
  /* USER CODE BEGIN USART1_Init 1 */
705
 
706
  /* USER CODE END USART1_Init 1 */
707
  huart1.Instance = USART1;
27 mjames 708
  huart1.Init.BaudRate = 9600;
2 mjames 709
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
710
  huart1.Init.StopBits = UART_STOPBITS_1;
711
  huart1.Init.Parity = UART_PARITY_NONE;
712
  huart1.Init.Mode = UART_MODE_TX_RX;
713
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
714
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
26 mjames 715
  if (HAL_UART_Init(&huart1) != HAL_OK)
716
  {
717
    Error_Handler();
718
  }
2 mjames 719
  /* USER CODE BEGIN USART1_Init 2 */
720
 
721
  /* USER CODE END USART1_Init 2 */
722
}
723
 
724
/**
32 mjames 725
 * @brief GPIO Initialization Function
726
 * @param None
727
 * @retval None
728
 */
26 mjames 729
static void MX_GPIO_Init(void)
2 mjames 730
{
26 mjames 731
  GPIO_InitTypeDef GPIO_InitStruct = {0};
2 mjames 732
 
733
  /* GPIO Ports Clock Enable */
5 mjames 734
  __HAL_RCC_GPIOC_CLK_ENABLE();
2 mjames 735
  __HAL_RCC_GPIOD_CLK_ENABLE();
736
  __HAL_RCC_GPIOA_CLK_ENABLE();
737
  __HAL_RCC_GPIOB_CLK_ENABLE();
738
 
739
  /*Configure GPIO pin Output Level */
27 mjames 740
  HAL_GPIO_WritePin(Green_LED_GPIO_Port, Green_LED_Pin, GPIO_PIN_RESET);
741
 
742
  /*Configure GPIO pin Output Level */
32 mjames 743
  HAL_GPIO_WritePin(GPIOA, SPI_CD_Pin | SPI_RESET_Pin | SPI_NSS1_Pin, GPIO_PIN_RESET);
2 mjames 744
 
14 mjames 745
  /*Configure GPIO pin Output Level */
29 mjames 746
  HAL_GPIO_WritePin(I2C1_BusPower_GPIO_Port, I2C1_BusPower_Pin, GPIO_PIN_SET);
747
 
748
  /*Configure GPIO pin Output Level */
26 mjames 749
  HAL_GPIO_WritePin(USB_PULLUP_GPIO_Port, USB_PULLUP_Pin, GPIO_PIN_RESET);
14 mjames 750
 
27 mjames 751
  /*Configure GPIO pin : Green_LED_Pin */
752
  GPIO_InitStruct.Pin = Green_LED_Pin;
753
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
754
  GPIO_InitStruct.Pull = GPIO_NOPULL;
755
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
756
  HAL_GPIO_Init(Green_LED_GPIO_Port, &GPIO_InitStruct);
757
 
13 mjames 758
  /*Configure GPIO pins : SPI_CD_Pin SPI_RESET_Pin SPI_NSS1_Pin */
32 mjames 759
  GPIO_InitStruct.Pin = SPI_CD_Pin | SPI_RESET_Pin | SPI_NSS1_Pin;
2 mjames 760
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
761
  GPIO_InitStruct.Pull = GPIO_NOPULL;
762
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
26 mjames 763
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
2 mjames 764
 
29 mjames 765
  /*Configure GPIO pin : I2C1_BusPower_Pin */
766
  GPIO_InitStruct.Pin = I2C1_BusPower_Pin;
767
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
768
  GPIO_InitStruct.Pull = GPIO_NOPULL;
769
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
770
  HAL_GPIO_Init(I2C1_BusPower_GPIO_Port, &GPIO_InitStruct);
771
 
14 mjames 772
  /*Configure GPIO pin : USB_PULLUP_Pin */
773
  GPIO_InitStruct.Pin = USB_PULLUP_Pin;
774
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
775
  GPIO_InitStruct.Pull = GPIO_NOPULL;
776
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
26 mjames 777
  HAL_GPIO_Init(USB_PULLUP_GPIO_Port, &GPIO_InitStruct);
14 mjames 778
 
21 mjames 779
  /*Configure GPIO pin : encoder_push_Pin */
780
  GPIO_InitStruct.Pin = encoder_push_Pin;
781
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
782
  GPIO_InitStruct.Pull = GPIO_PULLUP;
26 mjames 783
  HAL_GPIO_Init(encoder_push_GPIO_Port, &GPIO_InitStruct);
2 mjames 784
}
785
 
786
/* USER CODE BEGIN 4 */
787
 
788
/* USER CODE END 4 */
789
 
790
/**
32 mjames 791
 * @brief  This function is executed in case of error occurrence.
792
 * @retval None
793
 */
26 mjames 794
void Error_Handler(void)
2 mjames 795
{
796
  /* USER CODE BEGIN Error_Handler_Debug */
797
  /* User can add his own implementation to report the HAL error return state */
798
 
799
  /* USER CODE END Error_Handler_Debug */
800
}
801
 
32 mjames 802
#ifdef USE_FULL_ASSERT
2 mjames 803
/**
32 mjames 804
 * @brief  Reports the name of the source file and the source line number
805
 *         where the assert_param error has occurred.
806
 * @param  file: pointer to the source file name
807
 * @param  line: assert_param error line source number
808
 * @retval None
809
 */
2 mjames 810
void assert_failed(uint8_t *file, uint32_t line)
811
{
812
  /* USER CODE BEGIN 6 */
813
  /* User can add his own implementation to report the file name and line number,
814
     tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
815
  /* USER CODE END 6 */
816
}
817
#endif /* USE_FULL_ASSERT */
818
 
819
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/