Subversion Repositories EngineBay2

Rev

Rev 21 | Rev 23 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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