Updated for the next revision of the motor board

Revision:
7:e3a2ade56b79
Parent:
6:417655779dc5
--- a/HardwareSetup.cpp	Wed Aug 26 19:35:02 2020 +0000
+++ b/HardwareSetup.cpp	Wed Aug 26 23:42:07 2020 +0000
@@ -22,15 +22,13 @@
 static void MX_ADC1_Init(void);
 static void MX_ADC2_Init(void); 
 
-uint16_t Current_A;
-uint16_t Current_B;
-uint16_t Current_C;
-uint16_t Current_D;
+void config_adc1_channel(int val);
+void config_adc2_channel(int val);
 
-AnalogIn currentA(PF_12); //Enable ADC pin
-AnalogIn currentB(PF_11); //Enable ADC pin
-AnalogIn currentC(PF_13); //Enable ADC pin
-AnalogIn currentD(PA_4);  //Enable ADC pin
+AnalogIn currentA(PF_12); //Enable ADC hardware pin, don't use AnalogIn reads
+AnalogIn currentB(PF_11); //Enable ADC hardware pin, don't use AnalogIn reads
+AnalogIn currentC(PF_13); //Enable ADC hardware pin, don't use AnalogIn reads
+AnalogIn currentD(PA_4);  //Enable ADC hardware pin, don't use AnalogIn reads
 
 void initHardware(int periodTicks){
     PWM_PERIOD = periodTicks;
@@ -46,7 +44,7 @@
     MX_TIM17_Init();
     MX_ADC1_Init();
     MX_ADC2_Init();
-    //HAL_TIM_Base_Start(&htim15);
+
 
     HAL_TIM_PWM_Start(&htim12, TIM_CHANNEL_1); // start pwm generation
     HAL_TIM_PWM_Start(&htim12, TIM_CHANNEL_2); // start pwm generation
@@ -62,37 +60,85 @@
 
 
 uint16_t readADC1(int channel){
-    
+    config_adc1_channel(channel);
     HAL_ADC_Start(&hadc1);
     HAL_ADC_PollForConversion(&hadc1,20);
-    Current_A = HAL_ADC_GetValue(&hadc1);
-    HAL_ADC_PollForConversion(&hadc1,20);
-    Current_B = HAL_ADC_GetValue(&hadc1);
+    uint32_t result = HAL_ADC_GetValue(&hadc1);
     HAL_ADC_Stop (&hadc1);    
-    if (channel){
+    return result;
+
+}
 
-        return Current_A;
-    }
-    else 
-        return Current_B;
+void config_adc1_channel(int val)
+{
+  ADC_ChannelConfTypeDef sConfig;
+  if (val == 0){
+      sConfig.Channel = ADC_CHANNEL_6;
+      sConfig.Rank = ADC_REGULAR_RANK_1;
+      sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
+      sConfig.SingleDiff = ADC_SINGLE_ENDED;
+      sConfig.OffsetNumber = ADC_OFFSET_NONE;
+      sConfig.Offset = 0;
+      if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
+      {
+        //Error_Handler();
+      }
+  }
+  else{
+      sConfig.Channel = ADC_CHANNEL_2;
+      sConfig.Rank = ADC_REGULAR_RANK_1;
+      sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
+      sConfig.SingleDiff = ADC_SINGLE_ENDED;
+      sConfig.OffsetNumber = ADC_OFFSET_NONE;
+      sConfig.Offset = 0;
+      if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
+      {
+        //Error_Handler();
+      }
+  }
+
 }
 
+
 uint16_t readADC2(int channel){  
     
+    config_adc2_channel(channel);
     HAL_ADC_Start(&hadc2);
     HAL_ADC_PollForConversion(&hadc2,20);
-    Current_C = HAL_ADC_GetValue(&hadc2);
-    HAL_ADC_PollForConversion(&hadc2,20);
-    Current_D = HAL_ADC_GetValue(&hadc2);
+    uint32_t result = HAL_ADC_GetValue(&hadc2);
     HAL_ADC_Stop (&hadc2);    
-    if (channel){
-
-        return Current_C;
-    }
-    else 
-        return Current_D;
+    return result;
 }
 
+void config_adc2_channel(int val)
+{
+  ADC_ChannelConfTypeDef sConfig;
+  if (val == 0){
+      sConfig.Channel = ADC_CHANNEL_18;
+      sConfig.Rank = ADC_REGULAR_RANK_1;
+      sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
+      sConfig.SingleDiff = ADC_SINGLE_ENDED;
+      sConfig.OffsetNumber = ADC_OFFSET_NONE;
+      sConfig.Offset = 0;
+      if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
+      {
+        //Error_Handler();
+      }
+  }
+  else{
+      sConfig.Channel = ADC_CHANNEL_2;
+      sConfig.Rank = ADC_REGULAR_RANK_1;
+      sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
+      sConfig.SingleDiff = ADC_SINGLE_ENDED;
+      sConfig.OffsetNumber = ADC_OFFSET_NONE;
+      sConfig.Offset = 0;
+      if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
+      {
+        //Error_Handler();
+      }
+  }
+
+}
 
 static void MX_ADC1_Init(void)
 {
@@ -103,11 +149,11 @@
   hadc1.Instance = ADC1;
   hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
   hadc1.Init.Resolution = ADC_RESOLUTION_16B;
-  hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;
+  hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
   hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
   hadc1.Init.LowPowerAutoWait = DISABLE;
   hadc1.Init.ContinuousConvMode = ENABLE;
-  hadc1.Init.NbrOfConversion = 2;
+  hadc1.Init.NbrOfConversion = 1;
   hadc1.Init.DiscontinuousConvMode = DISABLE;
   hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
   hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
@@ -119,13 +165,6 @@
   {
     //Error_Handler();
   }
-  /** Configure the ADC multi-mode 
-  */
-  multimode.Mode = ADC_MODE_INDEPENDENT;
-  if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK)
-  {
-    //Error_Handler();
-  }
   /** Configure Regular Channel 
   */
   sConfig.Channel = ADC_CHANNEL_2;
@@ -138,17 +177,6 @@
   {
     //Error_Handler();
   }
-
-  sConfig.Channel = ADC_CHANNEL_6;
-  sConfig.Rank = ADC_REGULAR_RANK_2;
-  sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
-  sConfig.SingleDiff = ADC_SINGLE_ENDED;
-  sConfig.OffsetNumber = ADC_OFFSET_NONE;
-  sConfig.Offset = 0;
-  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
-  {
-    //Error_Handler();
-  }
 }
 
 /**
@@ -158,18 +186,17 @@
   */
 static void MX_ADC2_Init(void)
 {
-  ADC_MultiModeTypeDef multimode = {0};
   ADC_ChannelConfTypeDef sConfig = {0};
   /** Common config 
   */
   hadc2.Instance = ADC2;
   hadc2.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
   hadc2.Init.Resolution = ADC_RESOLUTION_16B;
-  hadc2.Init.ScanConvMode = ADC_SCAN_ENABLE;
+  hadc2.Init.ScanConvMode = ADC_SCAN_DISABLE;
   hadc2.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
   hadc2.Init.LowPowerAutoWait = DISABLE;
   hadc2.Init.ContinuousConvMode = ENABLE;
-  hadc2.Init.NbrOfConversion = 2;
+  hadc2.Init.NbrOfConversion = 1;
   hadc2.Init.DiscontinuousConvMode = DISABLE;
   hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START;
   hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
@@ -181,13 +208,7 @@
   {
     //Error_Handler();
   }
-  /** Configure the ADC multi-mode 
-  */
-  multimode.Mode = ADC_MODE_INDEPENDENT;
-  if (HAL_ADCEx_MultiModeConfigChannel(&hadc2, &multimode) != HAL_OK)
-  {
-    //Error_Handler();
-  }
+
   /** Configure Regular Channel 
   */
   sConfig.Channel = ADC_CHANNEL_2;
@@ -201,17 +222,6 @@
     //Error_Handler();
   }
 
-  sConfig.Channel = ADC_CHANNEL_18;
-  sConfig.Rank = ADC_REGULAR_RANK_2;
-  sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
-  sConfig.SingleDiff = ADC_SINGLE_ENDED;
-  sConfig.OffsetNumber = ADC_OFFSET_NONE;
-  sConfig.Offset = 0;
-  if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
-  {
-    //Error_Handler();
-  }
-
 }