Skip to content

Commit

Permalink
Replaced ADC polling with DMAs and made changes to .ioc
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanK952 committed Nov 24, 2024
1 parent d638ee9 commit 6233621
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .mxproject

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion Core/Inc/stm32f4xx_it.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ void BusFault_Handler(void);
void UsageFault_Handler(void);
void DebugMon_Handler(void);
void SysTick_Handler(void);
void CAN1_RX0_IRQHandler(void);
void DMA1_Stream4_IRQHandler(void);
void CAN1_RX0_IRQHandler(void);
void UART4_IRQHandler(void);
void DMA2_Stream0_IRQHandler(void);
void DMA2_Stream2_IRQHandler(void);
void CAN2_RX0_IRQHandler(void);
void CAN2_RX1_IRQHandler(void);
/* USER CODE BEGIN EFP */

/* USER CODE END EFP */
Expand Down
37 changes: 23 additions & 14 deletions Core/Src/compute.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
uint8_t fan_speed;
bool is_charging_enabled;
enum { CHARGE_ENABLED, CHARGE_DISABLED };
uint32_t channel_1_buf[2];
uint32_t raw_high_current_buf;

extern TIM_HandleTypeDef htim1;
extern TIM_HandleTypeDef htim8;
Expand Down Expand Up @@ -61,7 +63,14 @@ uint8_t compute_init(acc_data_t *bmsdata)
// HAL_TIM_PWM_Start(&htim8, fan_channels[FAN6]);
bmsdata->is_charger_connected = false;

HAL_ADC_Start(&hadc2);
//DMA for first ADC channel -- raw_low_current and ref_5V
assert(!HAL_ADC_Start_DMA(&hadc1, channel_1_buf,
sizeof(channel_1_buf) / sizeof(uint32_t)));

//DMA for second ADC channel -- raw_high_current
assert(!HAL_ADC_Start_DMA(&hadc2, &raw_high_current_buf,
sizeof(raw_high_current_buf) /
sizeof(uint32_t)));

return 0;
}
Expand Down Expand Up @@ -196,20 +205,19 @@ int16_t compute_get_pack_current()
1 / 0.0041; // Calibrated with current = 5A, 10A, 20A
static const float LOWCHANNEL_GAIN = 1 / 0.0267;
*/
// Change ADC channel to read the high current sensor
change_adc1_channel(VOUT_CHANNEL);
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);
int raw_low_current = HAL_ADC_GetValue(&hadc1);

HAL_ADC_Start(&hadc2);
HAL_ADC_PollForConversion(&hadc2, HAL_MAX_DELAY);
int raw_high_current = HAL_ADC_GetValue(&hadc2);
uint32_t raw_high_current;
uint32_t raw_low_current;
uint32_t ref_5V;

memcpy(&raw_high_current, &raw_high_current_buf,
sizeof(raw_high_current_buf));

memcpy(&raw_low_current, &channel_1_buf[0],
sizeof(channel_1_buf[0])); //From the rank of ADC_CHANNEL_15

change_adc1_channel(REF_CHANNEL);
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);
int ref_5V = HAL_ADC_GetValue(&hadc1);
memcpy(&ref_5V, &channel_1_buf[1],
sizeof(channel_1_buf[1])); //From the rank of ADC_CHANNEL_9

int16_t ref_voltage_raw =
(int16_t)(1000.0f * ((float)ref_5V * CURRENT_ADC_RESOLUTION));
Expand Down Expand Up @@ -245,7 +253,8 @@ int16_t compute_get_pack_current()
return -low_current;
}

// printf("\rHigh Current: %d\n", -high_current);
// printf("Low Current: %d\n", -low_current);
// printf("High Current: %d\n", -high_current);
return -high_current;
}

Expand Down
17 changes: 15 additions & 2 deletions Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
ADC_HandleTypeDef hadc1;
ADC_HandleTypeDef hadc2;
DMA_HandleTypeDef hdma_adc1;
DMA_HandleTypeDef hdma_adc2;

CAN_HandleTypeDef hcan1;
CAN_HandleTypeDef hcan2;
Expand Down Expand Up @@ -438,13 +439,13 @@ static void MX_ADC1_Init(void)
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.ScanConvMode = DISABLE;
hadc1.Init.ScanConvMode = ENABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 1;
hadc1.Init.NbrOfConversion = 2;
hadc1.Init.DMAContinuousRequests = ENABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
Expand All @@ -461,6 +462,15 @@ static void MX_ADC1_Init(void)
{
Error_Handler();
}

/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_9;
sConfig.Rank = 2;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC1_Init 2 */

/* USER CODE END ADC1_Init 2 */
Expand Down Expand Up @@ -1065,6 +1075,9 @@ static void MX_DMA_Init(void)
/* DMA2_Stream0_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA2_Stream0_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(DMA2_Stream0_IRQn);
/* DMA2_Stream2_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA2_Stream2_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(DMA2_Stream2_IRQn);

}

Expand Down
28 changes: 27 additions & 1 deletion Core/Src/stm32f4xx_hal_msp.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
/* USER CODE END Includes */
extern DMA_HandleTypeDef hdma_adc1;

extern DMA_HandleTypeDef hdma_adc2;

extern DMA_HandleTypeDef hdma_uart4_tx;

/* Private typedef -----------------------------------------------------------*/
Expand Down Expand Up @@ -126,7 +128,7 @@ void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
hdma_adc1.Init.MemInc = DMA_MINC_ENABLE;
hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
hdma_adc1.Init.Mode = DMA_NORMAL;
hdma_adc1.Init.Mode = DMA_CIRCULAR;
hdma_adc1.Init.Priority = DMA_PRIORITY_LOW;
hdma_adc1.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
if (HAL_DMA_Init(&hdma_adc1) != HAL_OK)
Expand Down Expand Up @@ -157,6 +159,25 @@ void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(I_SenseB0_GPIO_Port, &GPIO_InitStruct);

/* ADC2 DMA Init */
/* ADC2 Init */
hdma_adc2.Instance = DMA2_Stream2;
hdma_adc2.Init.Channel = DMA_CHANNEL_1;
hdma_adc2.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_adc2.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_adc2.Init.MemInc = DMA_MINC_ENABLE;
hdma_adc2.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
hdma_adc2.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
hdma_adc2.Init.Mode = DMA_CIRCULAR;
hdma_adc2.Init.Priority = DMA_PRIORITY_LOW;
hdma_adc2.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
if (HAL_DMA_Init(&hdma_adc2) != HAL_OK)
{
Error_Handler();
}

__HAL_LINKDMA(hadc,DMA_Handle,hdma_adc2);

/* USER CODE BEGIN ADC2_MspInit 1 */

/* USER CODE END ADC2_MspInit 1 */
Expand Down Expand Up @@ -207,6 +228,8 @@ void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc)
*/
HAL_GPIO_DeInit(I_SenseB0_GPIO_Port, I_SenseB0_Pin);

/* ADC2 DMA DeInit */
HAL_DMA_DeInit(hadc->DMA_Handle);
/* USER CODE BEGIN ADC2_MspDeInit 1 */

/* USER CODE END ADC2_MspDeInit 1 */
Expand Down Expand Up @@ -282,6 +305,8 @@ void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan)
/* CAN2 interrupt Init */
HAL_NVIC_SetPriority(CAN2_RX0_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(CAN2_RX0_IRQn);
HAL_NVIC_SetPriority(CAN2_RX1_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(CAN2_RX1_IRQn);
/* USER CODE BEGIN CAN2_MspInit 1 */

/* USER CODE END CAN2_MspInit 1 */
Expand Down Expand Up @@ -340,6 +365,7 @@ void HAL_CAN_MspDeInit(CAN_HandleTypeDef* hcan)

/* CAN2 interrupt DeInit */
HAL_NVIC_DisableIRQ(CAN2_RX0_IRQn);
HAL_NVIC_DisableIRQ(CAN2_RX1_IRQn);
/* USER CODE BEGIN CAN2_MspDeInit 1 */

/* USER CODE END CAN2_MspDeInit 1 */
Expand Down
57 changes: 43 additions & 14 deletions Core/Src/stm32f4xx_it.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@

/* External variables --------------------------------------------------------*/
extern DMA_HandleTypeDef hdma_adc1;
extern DMA_HandleTypeDef hdma_adc2;
extern CAN_HandleTypeDef hcan1;
extern CAN_HandleTypeDef hcan2;
extern DMA_HandleTypeDef hdma_uart4_tx;
Expand Down Expand Up @@ -201,20 +202,6 @@ void DMA1_Stream4_IRQHandler(void)
/* USER CODE END DMA1_Stream4_IRQn 1 */
}

/**
* @brief This function handles UART4 global interrupt.
*/
void UART4_IRQHandler(void)
{
/* USER CODE BEGIN UART4_IRQn 0 */

/* USER CODE END UART4_IRQn 0 */
HAL_UART_IRQHandler(&huart4);
/* USER CODE BEGIN UART4_IRQn 1 */

/* USER CODE END UART4_IRQn 1 */
}

/**
* @brief This function handles CAN1 RX0 interrupts.
*/
Expand All @@ -229,6 +216,20 @@ void CAN1_RX0_IRQHandler(void)
/* USER CODE END CAN1_RX0_IRQn 1 */
}

/**
* @brief This function handles UART4 global interrupt.
*/
void UART4_IRQHandler(void)
{
/* USER CODE BEGIN UART4_IRQn 0 */

/* USER CODE END UART4_IRQn 0 */
HAL_UART_IRQHandler(&huart4);
/* USER CODE BEGIN UART4_IRQn 1 */

/* USER CODE END UART4_IRQn 1 */
}

/**
* @brief This function handles DMA2 stream0 global interrupt.
*/
Expand All @@ -243,6 +244,20 @@ void DMA2_Stream0_IRQHandler(void)
/* USER CODE END DMA2_Stream0_IRQn 1 */
}

/**
* @brief This function handles DMA2 stream2 global interrupt.
*/
void DMA2_Stream2_IRQHandler(void)
{
/* USER CODE BEGIN DMA2_Stream2_IRQn 0 */

/* USER CODE END DMA2_Stream2_IRQn 0 */
HAL_DMA_IRQHandler(&hdma_adc2);
/* USER CODE BEGIN DMA2_Stream2_IRQn 1 */

/* USER CODE END DMA2_Stream2_IRQn 1 */
}

/**
* @brief This function handles CAN2 RX0 interrupts.
*/
Expand All @@ -257,6 +272,20 @@ void CAN2_RX0_IRQHandler(void)
/* USER CODE END CAN2_RX0_IRQn 1 */
}

/**
* @brief This function handles CAN2 RX1 interrupt.
*/
void CAN2_RX1_IRQHandler(void)
{
/* USER CODE BEGIN CAN2_RX1_IRQn 0 */

/* USER CODE END CAN2_RX1_IRQn 0 */
HAL_CAN_IRQHandler(&hcan2);
/* USER CODE BEGIN CAN2_RX1_IRQn 1 */

/* USER CODE END CAN2_RX1_IRQn 1 */
}

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
##########################################################################################################################
# File automatically-generated by tool: [projectgenerator] version: [4.2.0-B44] date: [Fri Nov 08 16:32:06 EST 2024]
# File automatically-generated by tool: [projectgenerator] version: [4.2.0-B44] date: [Sun Nov 24 16:29:33 EST 2024]
##########################################################################################################################

# ------------------------------------------------
Expand Down
22 changes: 19 additions & 3 deletions shepherd2.ioc
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#MicroXplorer Configuration settings - do not modify
ADC1.Channel-0\#ChannelRegularConversion=ADC_CHANNEL_15
ADC1.Channel-1\#ChannelRegularConversion=ADC_CHANNEL_9
ADC1.DMAContinuousRequests=ENABLE
ADC1.IPParameters=Rank-0\#ChannelRegularConversion,Channel-0\#ChannelRegularConversion,SamplingTime-0\#ChannelRegularConversion,NbrOfConversionFlag,master,DMAContinuousRequests
ADC1.IPParameters=Rank-0\#ChannelRegularConversion,Channel-0\#ChannelRegularConversion,SamplingTime-0\#ChannelRegularConversion,NbrOfConversionFlag,master,DMAContinuousRequests,Rank-1\#ChannelRegularConversion,Channel-1\#ChannelRegularConversion,SamplingTime-1\#ChannelRegularConversion,NbrOfConversion
ADC1.NbrOfConversion=2
ADC1.NbrOfConversionFlag=1
ADC1.Rank-0\#ChannelRegularConversion=1
ADC1.Rank-1\#ChannelRegularConversion=2
ADC1.SamplingTime-0\#ChannelRegularConversion=ADC_SAMPLETIME_3CYCLES
ADC1.SamplingTime-1\#ChannelRegularConversion=ADC_SAMPLETIME_3CYCLES
ADC1.master=1
ADC2.Channel-1\#ChannelRegularConversion=ADC_CHANNEL_8
ADC2.IPParameters=Rank-1\#ChannelRegularConversion,Channel-1\#ChannelRegularConversion,SamplingTime-1\#ChannelRegularConversion,NbrOfConversionFlag
Expand Down Expand Up @@ -49,14 +53,25 @@ Dma.ADC1.0.FIFOMode=DMA_FIFOMODE_DISABLE
Dma.ADC1.0.Instance=DMA2_Stream0
Dma.ADC1.0.MemDataAlignment=DMA_MDATAALIGN_WORD
Dma.ADC1.0.MemInc=DMA_MINC_ENABLE
Dma.ADC1.0.Mode=DMA_NORMAL
Dma.ADC1.0.Mode=DMA_CIRCULAR
Dma.ADC1.0.PeriphDataAlignment=DMA_PDATAALIGN_WORD
Dma.ADC1.0.PeriphInc=DMA_PINC_DISABLE
Dma.ADC1.0.Priority=DMA_PRIORITY_LOW
Dma.ADC1.0.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority,FIFOMode
Dma.ADC2.2.Direction=DMA_PERIPH_TO_MEMORY
Dma.ADC2.2.FIFOMode=DMA_FIFOMODE_DISABLE
Dma.ADC2.2.Instance=DMA2_Stream2
Dma.ADC2.2.MemDataAlignment=DMA_MDATAALIGN_WORD
Dma.ADC2.2.MemInc=DMA_MINC_ENABLE
Dma.ADC2.2.Mode=DMA_CIRCULAR
Dma.ADC2.2.PeriphDataAlignment=DMA_PDATAALIGN_WORD
Dma.ADC2.2.PeriphInc=DMA_PINC_DISABLE
Dma.ADC2.2.Priority=DMA_PRIORITY_LOW
Dma.ADC2.2.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority,FIFOMode
Dma.Request0=ADC1
Dma.Request1=UART4_TX
Dma.RequestsNb=2
Dma.Request2=ADC2
Dma.RequestsNb=3
Dma.UART4_TX.1.Direction=DMA_MEMORY_TO_PERIPH
Dma.UART4_TX.1.FIFOMode=DMA_FIFOMODE_DISABLE
Dma.UART4_TX.1.Instance=DMA1_Stream4
Expand Down Expand Up @@ -170,6 +185,7 @@ NVIC.CAN2_RX0_IRQn=true\:5\:0\:false\:false\:true\:true\:true\:true\:true
NVIC.CAN2_RX1_IRQn=true\:5\:0\:false\:false\:true\:true\:true\:true\:true
NVIC.DMA1_Stream4_IRQn=true\:5\:0\:false\:false\:true\:true\:false\:true\:true
NVIC.DMA2_Stream0_IRQn=true\:5\:0\:false\:false\:true\:true\:false\:true\:true
NVIC.DMA2_Stream2_IRQn=true\:5\:0\:false\:false\:true\:true\:false\:true\:true
NVIC.DebugMonitor_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false\:false
NVIC.ForceEnableDMAVector=true
NVIC.HardFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false\:false
Expand Down

0 comments on commit 6233621

Please sign in to comment.