Subversion Repositories EngineBay2

Rev

Rev 17 | Rev 19 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 mjames 1
/**
2
  ******************************************************************************
3
  * File Name          : main.c
4
  * Description        : Main program body
5
  ******************************************************************************
6
  *
18 mjames 7
  * COPYRIGHT(c) 2017 STMicroelectronics
2 mjames 8
  *
9
  * Redistribution and use in source and binary forms, with or without modification,
10
  * are permitted provided that the following conditions are met:
11
  *   1. Redistributions of source code must retain the above copyright notice,
12
  *      this list of conditions and the following disclaimer.
13
  *   2. Redistributions in binary form must reproduce the above copyright notice,
14
  *      this list of conditions and the following disclaimer in the documentation
15
  *      and/or other materials provided with the distribution.
16
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
17
  *      may be used to endorse or promote products derived from this software
18
  *      without specific prior written permission.
19
  *
20
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
  *
31
  ******************************************************************************
32
  */
33
/* Includes ------------------------------------------------------------------*/
34
#include "stm32l1xx_hal.h"
35
 
36
/* USER CODE BEGIN Includes */
7 mjames 37
#include "serial.h"
9 mjames 38
#include "plx.h"
39
#include "misc.h"
2 mjames 40
/* USER CODE END Includes */
41
 
42
/* Private variables ---------------------------------------------------------*/
43
ADC_HandleTypeDef hadc;
6 mjames 44
DMA_HandleTypeDef hdma_adc;
2 mjames 45
 
46
SPI_HandleTypeDef hspi1;
47
 
48
TIM_HandleTypeDef htim2;
49
TIM_HandleTypeDef htim6;
50
 
51
UART_HandleTypeDef huart1;
6 mjames 52
UART_HandleTypeDef huart2;
2 mjames 53
 
54
/* USER CODE BEGIN PV */
55
/* Private variables ---------------------------------------------------------*/
56
 
8 mjames 57
 
9 mjames 58
// with a dwell angle of 45 degrees , 4 cylinders and a maximum RPM of 5000
59
// freq = 5000/60 * 2 = 166Hz. Because the breaker might bounce , we accept the first pulse longer than 1/300 of a second as being a proper closure .
60
// the TIM2 counter counts in 10uS increments,
61
 
62
#define BREAKER_MIN (RPM_COUNT_RATE/300)
63
 
64
 
65
volatile char TimerFlag = 0;
66
 
67
volatile char NoSerialInCTR = 0; // Missing characters coming in on USART1
68
volatile char NoSerialIn = 0;
69
 
8 mjames 70
// storage for ADC
14 mjames 71
uint16_t  ADC_Samples[6];
8 mjames 72
 
17 mjames 73
#define Scale 1024.0
74
const float ADC_Scale = 3.3 / (Scale * 4096.0); // convert to a voltage
75
 
76
uint32_t  FILT_Samples[6]; // filtered ADC samples * 1024
9 mjames 77
// Rev counter processing from original RevCounter Project
78
unsigned int RPM_Diff = 0;
79
unsigned int RPM_Count_Latch = 0;
80
// accumulators
81
unsigned int RPM_Pulsecount = 0;
82
unsigned int RPM_FilteredWidth = 0;
83
 
84
unsigned int Coded_RPM = 0;
85
unsigned int Coded_CHT = 0;
86
 
18 mjames 87
uint32_t Power_CHT_Timer;
88
 
2 mjames 89
/* USER CODE END PV */
90
 
91
/* Private function prototypes -----------------------------------------------*/
92
void SystemClock_Config(void);
93
void Error_Handler(void);
94
static void MX_GPIO_Init(void);
6 mjames 95
static void MX_DMA_Init(void);
2 mjames 96
static void MX_ADC_Init(void);
97
static void MX_SPI1_Init(void);
98
static void MX_TIM2_Init(void);
99
static void MX_TIM6_Init(void);
13 mjames 100
static void MX_USART2_UART_Init(void);
2 mjames 101
static void MX_USART1_UART_Init(void);
102
 
103
/* USER CODE BEGIN PFP */
104
/* Private function prototypes -----------------------------------------------*/
105
 
9 mjames 106
/* USER CODE END PFP */
7 mjames 107
 
9 mjames 108
/* USER CODE BEGIN 0 */
7 mjames 109
 
9 mjames 110
void plx_sendword(int x) {
111
        PutCharSerial(&uc1, ((x) >> 6) & 0x3F);
112
        PutCharSerial(&uc1, (x) & 0x3F);
113
}
2 mjames 114
 
17 mjames 115
void init_ADC_filter()
116
{
117
        int i;
118
         for(i=0;i<6;i++)
119
         {
120
                FILT_Samples[i] = 0;
121
         }
122
}
123
 
124
void filter_ADC_samples()
125
{
126
 int i;
127
 for(i=0;i<6;i++)
128
 {
129
        FILT_Samples[i] += (ADC_Samples[i] * Scale - FILT_Samples[i]) / 2;
130
 }
131
}
132
 
133
 
9 mjames 134
void ProcessRPM(int instance) {
135
// compute the timer values
136
// snapshot timers
137
        unsigned long RPM_Pulsewidth;
138
        unsigned long RPM_Count_Val;
139
        __disable_irq(); // copy the counter value
140
        RPM_Count_Val = RPM_Count;
141
        __enable_irq();
142
// do calculations
143
// if there is only one entry, cannot get difference
144
        if (RPM_Count_Latch != RPM_Count_Val) {
145
                while (1) {
146
                        unsigned int base_time;
147
                        unsigned int new_time;
148
                        // if we are at N-1, stop.
149
                        unsigned int next_count = RPM_Count_Latch + 1;
150
                        if (next_count == RPM_SAMPLES) {
151
                                next_count = 0;
152
                        }
153
                        if (next_count == RPM_Count_Val) {
154
                                break;
155
                        }
156
                        base_time = RPM_Time[RPM_Count_Latch];
157
                        new_time = RPM_Time[next_count];
158
                        RPM_Count_Latch = next_count;
159
                        if (new_time > base_time) {
160
                                RPM_Pulsewidth = new_time - base_time; // not wrapped
161
                        } else {
13 mjames 162
                                RPM_Pulsewidth = new_time - base_time + 65536; // deal with wrapping
9 mjames 163
                        }
2 mjames 164
 
9 mjames 165
                        RPM_Diff += RPM_Pulsewidth;
166
                        // need to check if this is a long pulse. If it is, keep the answer
167
                        if (RPM_Pulsewidth > BREAKER_MIN) {
168
                                RPM_Pulsecount++; // count one pulse
169
                                RPM_FilteredWidth += RPM_Diff; // add its width to the accumulator
170
                                RPM_Diff = 0; // reset accumulator of all the narrow widths
171
                        }
172
                }
173
 
174
        }
175
 
176
        if (RPM_Pulsecount > 0) {
177
                // now have time for N pulses in clocks
178
                // need to scale by 19.55: one unit is 19.55 RPM
179
                // 1Hz is 60 RPM
17 mjames 180
                float new_RPM = (30.0 / 19.55 * RPM_Pulsecount * RPM_COUNT_RATE)
181
                                                / (RPM_FilteredWidth) + 0.5;
182
 
183
                Coded_RPM += (new_RPM * Scale - Coded_RPM)/4;
184
 
9 mjames 185
#if !defined MY_DEBUG
186
                // reset here unless we want to debug
187
                RPM_Pulsecount = 0;
188
                RPM_FilteredWidth = 0;
189
#endif
190
        }
191
 
17 mjames 192
// send the current RPM *calculation
9 mjames 193
        plx_sendword(PLX_RPM);
194
        PutCharSerial(&uc1, instance);
17 mjames 195
        plx_sendword(Coded_RPM/Scale);
9 mjames 196
}
197
 
198
 
199
// this uses a MAX6675 which is a simple 16 bit read
200
// SPI is configured for 8 bits so I can use an OLED display if I need it
11 mjames 201
// must wait > 0.22 seconds between conversion attempts as this is the measurement time
202
//
18 mjames 203
 
204
uint8_t CHT_Timer[2] = { 0, 0 }; // two temperature readings
205
 
206
void ProcessCHT(int instance) {
9 mjames 207
        uint8_t buffer[2];
18 mjames 208
        if (instance > 2)
209
                return;
210
        CHT_Timer[instance]++;
211
        if (CHT_Timer[instance] >= 3) // every 300 milliseconds
11 mjames 212
 
18 mjames 213
                        {
214
                CHT_Timer[instance] = 0;
11 mjames 215
 
18 mjames 216
                uint16_t Pin = (instance == 0) ? SPI_NS_Temp_Pin : SPI_NS_Temp2_Pin;
9 mjames 217
 
18 mjames 218
                HAL_GPIO_WritePin(SPI_NS_Temp_GPIO_Port, Pin, GPIO_PIN_RESET);
9 mjames 219
 
18 mjames 220
                HAL_SPI_Receive(&hspi1, buffer, 2, 2);
9 mjames 221
 
18 mjames 222
                HAL_GPIO_WritePin(SPI_NS_Temp_GPIO_Port, Pin, GPIO_PIN_SET);
9 mjames 223
 
18 mjames 224
                uint16_t obs = (buffer[0] << 8) | buffer[1];
9 mjames 225
 
18 mjames 226
                uint8_t good = (obs & 4) == 0;
227
                if (good) {
228
                        Coded_CHT = obs >> 5;
229
                } else {
230
                        Coded_CHT = 1024; // signal fail
231
                }
11 mjames 232
        }
233
 
16 mjames 234
        plx_sendword(PLX_X_CHT);
9 mjames 235
        PutCharSerial(&uc1, instance);
236
        plx_sendword(Coded_CHT);
237
 
238
}
239
 
17 mjames 240
// 1023 is 20.00 volts.
18 mjames 241
void ProcessBatteryVoltage(int instance) {
242
        float reading = FILT_Samples[instance] * ADC_Scale;
243
        reading = reading * 7.8125; // real voltage
244
        reading = reading * 51.15; // 1023/20
17 mjames 245
 
12 mjames 246
        plx_sendword(PLX_Volts);
247
        PutCharSerial(&uc1, instance);
18 mjames 248
        plx_sendword((uint16_t) reading);
12 mjames 249
 
18 mjames 250
}
12 mjames 251
 
18 mjames 252
/****!
253
 * @brief this reads the reference voltage within the STM32L151
254
 * Powers up reference voltage and temperature sensor, waits 3mS  and takes reading
255
 * Requires that the ADC be powered up
256
 */
12 mjames 257
 
18 mjames 258
uint32_t ADC_VREF_MV = 3300;           // 3.300V typical
259
const uint16_t STM32REF_MV = 1224;           // 1.224V typical
260
 
261
void CalibrateADC(void) {
262
        uint32_t adc_val = FILT_Samples[6];       // as set up in device config
263
        ADC_VREF_MV = (STM32REF_MV * 4096) / adc_val;
12 mjames 264
}
265
 
18 mjames 266
void ProcessCPUTemperature(int instance) {
267
        int32_t temp_val;
268
        uint16_t TS_CAL30 = *(uint16_t *) 0x1FF8007A; /* ADC reading for temperature sensor at 30 degrees C with Vref = 3000mV */
269
        uint16_t TS_CAL110 = *(uint16_t *) 0x1FF8007E; /* ADC reading for temperature sensor at 110 degrees C with Vref = 3000mV */
270
        /* get the ADC reading corresponding to ADC channel 16 after turning on the ADC */
271
 
272
        temp_val = FILT_Samples[5];
273
 
274
        /* renormalise temperature value to account for different ADC Vref  : normalise to that which we would get for a 3000mV reference */
275
        temp_val = temp_val * ADC_VREF_MV / 3000UL;
276
 
277
        int32_t result = 800 * ((int32_t) temp_val - TS_CAL30);
278
        result = result / (TS_CAL110 - TS_CAL30) + 300;
279
 
280
    if(result < 0)
281
    {
282
        result = 0;
283
    }
284
        plx_sendword(PLX_FluidTemp);
285
        PutCharSerial(&uc1, instance);
286
        plx_sendword(result/10);
287
 
288
}
289
 
17 mjames 290
// the MAP sensor is giving us a reading of
291
// 4.6 volts for 1019mB or 2.27 volts at the ADC input (resistive divider by 2.016)
292
// I believe the sensor reads  4.5V at 1000kPa and 0.5V at  0kPa
12 mjames 293
 
17 mjames 294
void ProcessMAP(int instance)
295
{
296
// Using ADC_Samples[3] as the MAP input
297
    float reading = FILT_Samples[3] * ADC_Scale;
18 mjames 298
    reading = reading * 2.016;      // real voltage
299
    reading = (reading) * 1000/ 4.5; // do not assume 0.5 volt offset : reading from 0 to 4.5 instead of 0.5 to 4.5
17 mjames 300
        plx_sendword(PLX_MAP);
301
        PutCharSerial(&uc1, instance);
302
        plx_sendword((uint16_t)reading);
303
 
304
}
305
 
306
// the Oil pressi sensor is giving us a reading of
307
// 4.5 volts for 100 PSI or  2.25 volts at the ADC input (resistive divider by 2.016)
308
// I believe the sensor reads  4.5V at 100PSI and 0.5V at  0PSI
309
// an observation of 1024 is 200PSI, so observation of 512 is 100 PSI.
310
 
311
void ProcessOilPress(int instance)
312
{
313
// Using ADC_Samples[2] as the MAP input
314
    float reading = FILT_Samples[2] *  ADC_Scale ;
315
    reading = reading * 2.00 ; // real voltage
316
    reading = (reading-0.5) * 512 / 4;  // this is 1023 * 100/200
317
 
318
        plx_sendword(PLX_FluidPressure);
319
        PutCharSerial(&uc1, instance);
320
        plx_sendword((uint16_t)reading);
321
 
322
}
323
 
324
 
325
 
16 mjames 326
void ProcessTiming(int instance)
327
{
328
        plx_sendword(PLX_Timing);
329
        PutCharSerial(&uc1, instance);
330
        plx_sendword(64-15); // make it negative
331
}
332
 
17 mjames 333
 
334
 
2 mjames 335
/* USER CODE END 0 */
336
 
18 mjames 337
int main(void) {
2 mjames 338
 
18 mjames 339
        /* USER CODE BEGIN 1 */
2 mjames 340
 
18 mjames 341
        /* USER CODE END 1 */
2 mjames 342
 
18 mjames 343
        /* MCU Configuration----------------------------------------------------------*/
2 mjames 344
 
18 mjames 345
        /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
346
        HAL_Init();
2 mjames 347
 
18 mjames 348
        /* Configure the system clock */
349
        SystemClock_Config();
2 mjames 350
 
18 mjames 351
        /* Initialize all configured peripherals */
352
        MX_GPIO_Init();
353
        MX_DMA_Init();
354
        MX_ADC_Init();
355
        MX_SPI1_Init();
356
        MX_TIM2_Init();
357
        MX_TIM6_Init();
358
        MX_USART2_UART_Init();
359
        MX_USART1_UART_Init();
2 mjames 360
 
18 mjames 361
        /* USER CODE BEGIN 2 */
13 mjames 362
        HAL_MspInit();
2 mjames 363
 
13 mjames 364
// Not using HAL USART code
9 mjames 365
        __HAL_RCC_USART1_CLK_ENABLE()
366
        ; // PLX comms port
367
        __HAL_RCC_USART2_CLK_ENABLE()
368
        ;  // Debug comms port
7 mjames 369
        /* setup the USART control blocks */
370
        init_usart_ctl(&uc1, huart1.Instance);
371
        init_usart_ctl(&uc2, huart2.Instance);
372
 
373
        EnableSerialRxInterrupt(&uc1);
374
        EnableSerialRxInterrupt(&uc2);
375
 
13 mjames 376
        HAL_SPI_MspInit(&hspi1);
377
 
378
        HAL_ADC_MspInit(&hadc);
14 mjames 379
 
13 mjames 380
        HAL_ADC_Start_DMA(&hadc, ADC_Samples, 6);
381
 
18 mjames 382
        HAL_ADC_Start_IT(&hadc);
13 mjames 383
 
384
        HAL_TIM_Base_MspInit(&htim6);
9 mjames 385
        HAL_TIM_Base_Start_IT(&htim6);
13 mjames 386
 
387
// initialise all the STMCubeMX stuff
388
        HAL_TIM_Base_MspInit(&htim2);
389
// Start the counter
12 mjames 390
        HAL_TIM_Base_Start(&htim2);
13 mjames 391
// Start the input capture and the interrupt
18 mjames 392
        HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_1);
8 mjames 393
 
17 mjames 394
        init_ADC_filter();
7 mjames 395
 
18 mjames 396
        uint32_t Ticks = HAL_GetTick() + 100;
397
        int CalCounter = 0;
2 mjames 398
 
18 mjames 399
        Power_CHT_Timer = HAL_GetTick() + 10000; /* wait 10 seconds before powering up the CHT sensor */
400
 
401
        /* USER CODE END 2 */
402
 
403
        /* Infinite loop */
404
        /* USER CODE BEGIN WHILE */
9 mjames 405
        while (1) {
18 mjames 406
                /* USER CODE END WHILE */
2 mjames 407
 
18 mjames 408
                /* USER CODE BEGIN 3 */
2 mjames 409
 
18 mjames 410
                if (HAL_GetTick() > Ticks) {
411
                        Ticks += 100;
412
                        filter_ADC_samples();
413
                        // delay to calibrate ADC
414
                        if (CalCounter < 500) {
415
                                CalCounter += 100;
9 mjames 416
                        }
417
 
18 mjames 418
                        if (CalCounter == 400) {
419
                                CalibrateADC();
420
                        }
9 mjames 421
 
18 mjames 422
                        /* when the starter motor is on then power down the CHT sensors as they seem to fail */
12 mjames 423
 
18 mjames 424
                        if (HAL_GPIO_ReadPin(STARTER_ON_GPIO_Port, STARTER_ON_Pin)
425
                                        == GPIO_PIN_RESET) {
426
                                HAL_GPIO_WritePin(ENA_AUX_5V_GPIO_Port, ENA_AUX_5V_Pin,
427
                                                GPIO_PIN_RESET);
428
                                Power_CHT_Timer = HAL_GetTick() + 500;
429
                        } else
430
                        /* if the Power_CHT_Timer is set then wait for it to timeout, then power up CHT */
431
                        {
432
                                if ((Power_CHT_Timer > 0)
433
                                                && (HAL_GetTick() > Power_CHT_Timer)) {
434
                                        Power_CHT_Timer = 0;
435
                                        HAL_GPIO_WritePin(ENA_AUX_5V_GPIO_Port, ENA_AUX_5V_Pin,
436
                                                        GPIO_PIN_SET);
437
                                }
438
                        }
13 mjames 439
 
18 mjames 440
                        // check to see if we have any incoming data, copy and append if so, if no data then create our own frames.
441
                        int c;
442
                        char send = 0;
13 mjames 443
 
18 mjames 444
                        // poll the  input for a stop bit or timeout
445
                        if (PollSerial(&uc1)) {
446
                                c = GetCharSerial(&uc1);
447
                                if (c != PLX_Stop) {
448
                                        PutCharSerial(&uc1, c); // echo all but the stop bit
449
                                } else { // must be a stop character
450
                                        send = 1; // start our sending process.
451
                                }
452
                        }
16 mjames 453
 
18 mjames 454
                        // sort out auto-sending
455
                        if (TimerFlag) {
456
                                TimerFlag = 0;
457
                                if (NoSerialIn) {
458
                                        PutCharSerial(&uc1, PLX_Start);
459
                                        send = 1;
460
                                }
461
                        }
462
                        if (send) {
463
                                send = 0;
464
 
465
                                uint16_t val;
466
                                val = __HAL_TIM_GET_COMPARE(&htim2,TIM_CHANNEL_1);
467
                                PutCharSerial(&uc2, (val & 31) + 32);
468
 
469
                                ProcessRPM(0);
470
 
471
                                ProcessCHT(0);
472
                        //      ProcessCHT(1);
473
                                ProcessBatteryVoltage(0); // Batt 1
474
                                ProcessBatteryVoltage(1); // Batt 2
475
 
476
                                ProcessCPUTemperature(0); //  built in temperature sensor
477
 
478
                                ProcessMAP(0);
479
                                ProcessOilPress(0);
480
 
481
                                PutCharSerial(&uc1, PLX_Stop);
482
                        }
9 mjames 483
                }
484
        }
18 mjames 485
        /* USER CODE END 3 */
2 mjames 486
 
487
}
488
/** System Clock Configuration
489
*/
490
void SystemClock_Config(void)
491
{
492
 
493
  RCC_OscInitTypeDef RCC_OscInitStruct;
494
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
495
 
496
  __HAL_RCC_PWR_CLK_ENABLE();
497
 
498
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
499
 
500
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
501
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
502
  RCC_OscInitStruct.HSICalibrationValue = 16;
503
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
504
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
505
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6;
506
  RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV3;
507
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
508
  {
509
    Error_Handler();
510
  }
511
 
512
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
513
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
514
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
515
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
516
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
517
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
518
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
519
  {
520
    Error_Handler();
521
  }
522
 
523
  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
524
 
525
  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
526
 
527
  /* SysTick_IRQn interrupt configuration */
528
  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
529
}
530
 
531
/* ADC init function */
532
static void MX_ADC_Init(void)
533
{
534
 
535
  ADC_ChannelConfTypeDef sConfig;
536
 
537
    /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
538
    */
539
  hadc.Instance = ADC1;
540
  hadc.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
541
  hadc.Init.Resolution = ADC_RESOLUTION_12B;
542
  hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
543
  hadc.Init.ScanConvMode = ADC_SCAN_ENABLE;
544
  hadc.Init.EOCSelection = ADC_EOC_SEQ_CONV;
545
  hadc.Init.LowPowerAutoWait = ADC_AUTOWAIT_DISABLE;
546
  hadc.Init.LowPowerAutoPowerOff = ADC_AUTOPOWEROFF_DISABLE;
547
  hadc.Init.ChannelsBank = ADC_CHANNELS_BANK_A;
548
  hadc.Init.ContinuousConvMode = DISABLE;
549
  hadc.Init.NbrOfConversion = 6;
550
  hadc.Init.DiscontinuousConvMode = DISABLE;
551
  hadc.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T6_TRGO;
552
  hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING;
14 mjames 553
  hadc.Init.DMAContinuousRequests = ENABLE;
2 mjames 554
  if (HAL_ADC_Init(&hadc) != HAL_OK)
555
  {
556
    Error_Handler();
557
  }
558
 
559
    /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
560
    */
6 mjames 561
  sConfig.Channel = ADC_CHANNEL_10;
2 mjames 562
  sConfig.Rank = 1;
17 mjames 563
  sConfig.SamplingTime = ADC_SAMPLETIME_384CYCLES;
2 mjames 564
  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
565
  {
566
    Error_Handler();
567
  }
568
 
569
    /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
570
    */
6 mjames 571
  sConfig.Channel = ADC_CHANNEL_11;
2 mjames 572
  sConfig.Rank = 2;
573
  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
574
  {
575
    Error_Handler();
576
  }
577
 
578
    /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
579
    */
6 mjames 580
  sConfig.Channel = ADC_CHANNEL_12;
2 mjames 581
  sConfig.Rank = 3;
582
  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
583
  {
584
    Error_Handler();
585
  }
586
 
587
    /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
588
    */
6 mjames 589
  sConfig.Channel = ADC_CHANNEL_13;
2 mjames 590
  sConfig.Rank = 4;
591
  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
592
  {
593
    Error_Handler();
594
  }
595
 
596
    /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
597
    */
598
  sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
599
  sConfig.Rank = 5;
600
  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
601
  {
602
    Error_Handler();
603
  }
604
 
605
    /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
606
    */
607
  sConfig.Channel = ADC_CHANNEL_VREFINT;
608
  sConfig.Rank = 6;
609
  if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
610
  {
611
    Error_Handler();
612
  }
613
 
614
}
615
 
616
/* SPI1 init function */
617
static void MX_SPI1_Init(void)
618
{
619
 
620
  hspi1.Instance = SPI1;
621
  hspi1.Init.Mode = SPI_MODE_MASTER;
3 mjames 622
  hspi1.Init.Direction = SPI_DIRECTION_2LINES;
2 mjames 623
  hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
624
  hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
625
  hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
626
  hspi1.Init.NSS = SPI_NSS_SOFT;
10 mjames 627
  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
2 mjames 628
  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
629
  hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
630
  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
631
  hspi1.Init.CRCPolynomial = 10;
632
  if (HAL_SPI_Init(&hspi1) != HAL_OK)
633
  {
634
    Error_Handler();
635
  }
636
 
637
}
638
 
639
/* TIM2 init function */
640
static void MX_TIM2_Init(void)
641
{
642
 
12 mjames 643
  TIM_ClockConfigTypeDef sClockSourceConfig;
2 mjames 644
  TIM_MasterConfigTypeDef sMasterConfig;
645
  TIM_IC_InitTypeDef sConfigIC;
646
 
647
  htim2.Instance = TIM2;
648
  htim2.Init.Prescaler = 320;
649
  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
13 mjames 650
  htim2.Init.Period = 65535;
2 mjames 651
  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
12 mjames 652
  if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
653
  {
654
    Error_Handler();
655
  }
656
 
657
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
658
  if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
659
  {
660
    Error_Handler();
661
  }
662
 
2 mjames 663
  if (HAL_TIM_IC_Init(&htim2) != HAL_OK)
664
  {
665
    Error_Handler();
666
  }
667
 
13 mjames 668
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
2 mjames 669
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
670
  if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
671
  {
672
    Error_Handler();
673
  }
674
 
675
  sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
676
  sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
677
  sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;
678
  sConfigIC.ICFilter = 0;
679
  if (HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_1) != HAL_OK)
680
  {
681
    Error_Handler();
682
  }
683
 
684
}
685
 
686
/* TIM6 init function */
687
static void MX_TIM6_Init(void)
688
{
689
 
690
  TIM_MasterConfigTypeDef sMasterConfig;
691
 
692
  htim6.Instance = TIM6;
13 mjames 693
  htim6.Init.Prescaler = 320;
2 mjames 694
  htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
13 mjames 695
  htim6.Init.Period = 9999;
2 mjames 696
  if (HAL_TIM_Base_Init(&htim6) != HAL_OK)
697
  {
698
    Error_Handler();
699
  }
700
 
701
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
702
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
703
  if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK)
704
  {
705
    Error_Handler();
706
  }
707
 
708
}
709
 
710
/* USART1 init function */
711
static void MX_USART1_UART_Init(void)
712
{
713
 
714
  huart1.Instance = USART1;
715
  huart1.Init.BaudRate = 19200;
716
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
717
  huart1.Init.StopBits = UART_STOPBITS_1;
718
  huart1.Init.Parity = UART_PARITY_NONE;
719
  huart1.Init.Mode = UART_MODE_TX_RX;
720
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
721
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
722
  if (HAL_UART_Init(&huart1) != HAL_OK)
723
  {
724
    Error_Handler();
725
  }
726
 
727
}
728
 
6 mjames 729
/* USART2 init function */
730
static void MX_USART2_UART_Init(void)
731
{
732
 
733
  huart2.Instance = USART2;
734
  huart2.Init.BaudRate = 115200;
735
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
736
  huart2.Init.StopBits = UART_STOPBITS_1;
737
  huart2.Init.Parity = UART_PARITY_NONE;
738
  huart2.Init.Mode = UART_MODE_TX_RX;
739
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
740
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
741
  if (HAL_UART_Init(&huart2) != HAL_OK)
742
  {
743
    Error_Handler();
744
  }
745
 
746
}
747
 
748
/**
749
  * Enable DMA controller clock
750
  */
751
static void MX_DMA_Init(void)
752
{
753
  /* DMA controller clock enable */
754
  __HAL_RCC_DMA1_CLK_ENABLE();
755
 
756
  /* DMA interrupt init */
757
  /* DMA1_Channel1_IRQn interrupt configuration */
758
  HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
759
  HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
760
 
761
}
762
 
2 mjames 763
/** Configure pins as
764
        * Analog
765
        * Input
766
        * Output
767
        * EVENT_OUT
768
        * EXTI
5 mjames 769
        * Free pins are configured automatically as Analog (this feature is enabled through
770
        * the Code Generation settings)
2 mjames 771
*/
772
static void MX_GPIO_Init(void)
773
{
774
 
775
  GPIO_InitTypeDef GPIO_InitStruct;
776
 
777
  /* GPIO Ports Clock Enable */
5 mjames 778
  __HAL_RCC_GPIOC_CLK_ENABLE();
779
  __HAL_RCC_GPIOH_CLK_ENABLE();
2 mjames 780
  __HAL_RCC_GPIOA_CLK_ENABLE();
781
  __HAL_RCC_GPIOB_CLK_ENABLE();
5 mjames 782
  __HAL_RCC_GPIOD_CLK_ENABLE();
2 mjames 783
 
6 mjames 784
  /*Configure GPIO pins : PC13 PC14 PC15 PC6
18 mjames 785
                           PC7 PC8 PC9 PC11
786
                           PC12 */
6 mjames 787
  GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_6
18 mjames 788
                          |GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_11
789
                          |GPIO_PIN_12;
5 mjames 790
  GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
791
  GPIO_InitStruct.Pull = GPIO_NOPULL;
792
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
2 mjames 793
 
5 mjames 794
  /*Configure GPIO pins : PH0 PH1 */
795
  GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
796
  GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
797
  GPIO_InitStruct.Pull = GPIO_NOPULL;
798
  HAL_GPIO_Init(GPIOH, &GPIO_InitStruct);
3 mjames 799
 
6 mjames 800
  /*Configure GPIO pins : PA0 PA1 PA8 PA11
7 mjames 801
                           PA12 */
6 mjames 802
  GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_8|GPIO_PIN_11
7 mjames 803
                          |GPIO_PIN_12;
6 mjames 804
  GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
805
  GPIO_InitStruct.Pull = GPIO_NOPULL;
806
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
807
 
7 mjames 808
  /*Configure GPIO pin : LED_Blink_Pin */
809
  GPIO_InitStruct.Pin = LED_Blink_Pin;
2 mjames 810
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
811
  GPIO_InitStruct.Pull = GPIO_NOPULL;
7 mjames 812
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
813
  HAL_GPIO_Init(LED_Blink_GPIO_Port, &GPIO_InitStruct);
2 mjames 814
 
3 mjames 815
  /*Configure GPIO pins : SPI_NSS1_Pin SPI1CD_Pin */
816
  GPIO_InitStruct.Pin = SPI_NSS1_Pin|SPI1CD_Pin;
817
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
818
  GPIO_InitStruct.Pull = GPIO_NOPULL;
819
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
820
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
821
 
18 mjames 822
  /*Configure GPIO pins : SPI_RESET_Pin SPI_NS_Temp_Pin SPI_NS_Temp2_Pin ENA_AUX_5V_Pin */
823
  GPIO_InitStruct.Pin = SPI_RESET_Pin|SPI_NS_Temp_Pin|SPI_NS_Temp2_Pin|ENA_AUX_5V_Pin;
3 mjames 824
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
825
  GPIO_InitStruct.Pull = GPIO_NOPULL;
826
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
7 mjames 827
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
3 mjames 828
 
18 mjames 829
  /*Configure GPIO pins : PB11 PB12 PB13 PB14
830
                           PB15 PB3 PB4 PB5
831
                           PB6 PB7 PB8 PB9 */
832
  GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14
833
                          |GPIO_PIN_15|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5
834
                          |GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9;
5 mjames 835
  GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
836
  GPIO_InitStruct.Pull = GPIO_NOPULL;
837
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
838
 
18 mjames 839
  /*Configure GPIO pin : STARTER_ON_Pin */
840
  GPIO_InitStruct.Pin = STARTER_ON_Pin;
841
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
842
  GPIO_InitStruct.Pull = GPIO_NOPULL;
843
  HAL_GPIO_Init(STARTER_ON_GPIO_Port, &GPIO_InitStruct);
844
 
5 mjames 845
  /*Configure GPIO pin : PD2 */
846
  GPIO_InitStruct.Pin = GPIO_PIN_2;
847
  GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
848
  GPIO_InitStruct.Pull = GPIO_NOPULL;
849
  HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
850
 
851
  /*Configure GPIO pin Output Level */
7 mjames 852
  HAL_GPIO_WritePin(LED_Blink_GPIO_Port, LED_Blink_Pin, GPIO_PIN_RESET);
5 mjames 853
 
854
  /*Configure GPIO pin Output Level */
7 mjames 855
  HAL_GPIO_WritePin(SPI_NSS1_GPIO_Port, SPI_NSS1_Pin, GPIO_PIN_SET);
5 mjames 856
 
857
  /*Configure GPIO pin Output Level */
7 mjames 858
  HAL_GPIO_WritePin(SPI1CD_GPIO_Port, SPI1CD_Pin, GPIO_PIN_RESET);
859
 
860
  /*Configure GPIO pin Output Level */
18 mjames 861
  HAL_GPIO_WritePin(GPIOB, SPI_RESET_Pin|SPI_NS_Temp2_Pin|ENA_AUX_5V_Pin, GPIO_PIN_RESET);
5 mjames 862
 
7 mjames 863
  /*Configure GPIO pin Output Level */
864
  HAL_GPIO_WritePin(SPI_NS_Temp_GPIO_Port, SPI_NS_Temp_Pin, GPIO_PIN_SET);
865
 
2 mjames 866
}
867
 
868
/* USER CODE BEGIN 4 */
869
 
870
/* USER CODE END 4 */
871
 
872
/**
873
  * @brief  This function is executed in case of error occurrence.
874
  * @param  None
875
  * @retval None
876
  */
877
void Error_Handler(void)
878
{
879
  /* USER CODE BEGIN Error_Handler */
9 mjames 880
        /* User can add his own implementation to report the HAL error return state */
881
        while (1) {
882
        }
2 mjames 883
  /* USER CODE END Error_Handler */
884
}
885
 
886
#ifdef USE_FULL_ASSERT
887
 
888
/**
889
   * @brief Reports the name of the source file and the source line number
890
   * where the assert_param error has occurred.
891
   * @param file: pointer to the source file name
892
   * @param line: assert_param error line source number
893
   * @retval None
894
   */
895
void assert_failed(uint8_t* file, uint32_t line)
896
{
897
  /* USER CODE BEGIN 6 */
9 mjames 898
        /* User can add his own implementation to report the file name and line number,
899
         ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
2 mjames 900
  /* USER CODE END 6 */
901
 
902
}
903
 
904
#endif
905
 
906
/**
907
  * @}
908
  */
909
 
910
/**
911
  * @}
912
*/
913
 
914
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/