cancel
Showing results for 
Search instead for 
Did you mean: 

Basic help on ISR's and the HAL

stthormj9
Associate II
Posted on July 15, 2016 at 22:13

I'm trying to kick off the ADC via Timer1 at 9600 sps for 4 channels.

I have it all working -- once, but I think I'm (ab)using the HAL wrong;

I tried to put the kick off code in a function in main called

HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)

, but that never got called. I then relocated it to the TIM1_BRK_UP_TRG_COM_IRQHandler like this:

void TIM1_BRK_UP_TRG_COM_IRQHandler(void)
{
/* USER CODE BEGIN TIM1_BRK_UP_TRG_COM_IRQn 0 */
//--This only gets called once?
//Trigger a LED Pin so I can see the frequency (should be 9600)
HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_9);
//Tell the ADC to start a scan and populate it.
HAL_ADC_Start_DMA(&hadc, (uint32_t *)&adc[1], 4);
//We catch the end of AD Conversion in the ADCScanCompleted interrupt.
/* USER CODE END TIM1_BRK_UP_TRG_COM_IRQn 0 */
HAL_TIM_IRQHandler(&htim1);
/* USER CODE BEGIN TIM1_BRK_UP_TRG_COM_IRQn 1 */
/* USER CODE END TIM1_BRK_UP_TRG_COM_IRQn 1 */
}

But that only gets called once (when I execute HAL_TIM_Base_Start_IT(&htim1) in main), not every 5000 counts.

What am I missing (Keil uVision 5.3)?

Update: I got it repeating (I had Prescalar and Period mixed up from the cube). But I still don't know why I have to put the code in the ISR and not in a Callback...

6 REPLIES 6
Walid FTITI_O
Senior II
Posted on July 18, 2016 at 16:10

Hi,

Normally, you d'ont need to put Timer IRQ_Handler. You just need to put the ADC_IRQHandler in _STM32Fxx_it.c . Ensure that you have correctly configure both ADC and Timer to make the timer triger the ADC each programmed period (use a timer that can trigger the ADC via TRGO signal).

I recommend to check an example in

http://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32cube-embedded-software/stm32cubef4.html

intiteled ''ADC_TriggerMode'' located at this path:

stm32cubef4_1.11\STM32Cube_FW_F4_V1.12.0\Projects\STM324x9I_EVAL\Examples\ADC\ADC_TriggerMode

-Hannibal-

stthormj9
Associate II
Posted on July 19, 2016 at 04:29

I was thinking I could do it that way, but I couldn't figure out the linkage to

HAL_ADC_Start_DMA(&hadc, (uint32_t *)&adc[1], 4);

without explicitly calling that in the ISR (or callback)...

Walid FTITI_O
Senior II
Posted on July 19, 2016 at 12:03

HI johnson.thor,

In that case, you can get help form the ''ADC_RegularConversion_DMA'' located in the same library and subfolder.

-Hannibal-

stthormj9
Associate II
Posted on July 20, 2016 at 03:55

So, looking at the example provided, it does continuous DMA on one channel (and if I run that, it works), butI am having trouble getting mine to work right (Manually start [for now],DMA Sample channels 1,2,temp,vref, and store those into adc[1..4]) -- my ADC init:


/* ADC init function */

static
void
MX_ADC_Init(
void
)

{


ADC_ChannelConfTypeDef sConfig;


/**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) 

*/

hadc.Instance = ADC1;

hadc.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;

hadc.Init.Resolution = ADC_RESOLUTION_12B;

hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;

hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;

hadc.Init.EOCSelection = ADC_EOC_SEQ_CONV;

hadc.Init.LowPowerAutoWait = DISABLE;

hadc.Init.LowPowerAutoPowerOff = DISABLE;

hadc.Init.ContinuousConvMode = DISABLE;

hadc.Init.DiscontinuousConvMode = DISABLE;

hadc.Init.ExternalTrigConvEdge = 0;
//ADC_EXTERNALTRIGCONVEDGE_RISING;

hadc.Init.ExternalTrigConv=0;
//ToDo: ADC_EXTERNALTRIGCONV_T1_TRGO;

hadc.Init.DMAContinuousRequests = ENABLE;

hadc.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;

if
(HAL_ADC_Init(&hadc) != HAL_OK)

{

Error_Handler();

}


/**Configure for the selected ADC regular channel to be converted. 

*/

sConfig.Channel = ADC_CHANNEL_1;

sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;

sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;

if
(HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)

{

Error_Handler();

}


/**Configure for the selected ADC regular channel to be converted. 

*/

sConfig.Channel = ADC_CHANNEL_2;

if
(HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)

{

Error_Handler();

}


/**Configure for the selected ADC regular channel to be converted. 

*/

sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;

if
(HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)

{

Error_Handler();

}


/**Configure for the selected ADC regular channel to be converted. 

*/

sConfig.Channel = ADC_CHANNEL_VREFINT;

if
(HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)

{

Error_Handler();

}


}

The HAL_ADC_Start_DMA(&hadc, (uint32_t *)&adc[1], 4); executes (from the TIM1 ISR -- I see a signal on the PC9 GPIO that I toggle on the line before the ADC_Start), but I don't see any populations of the values in the adc array, nor do I get an ISR callback on DMA1_Channel1_IRQHandler(void). I also don't see any activity in the ADC ISR... it just sits toggling PC9...

stthormj9
Associate II
Posted on July 20, 2016 at 19:32

Update:

I copies the  HAL_ADC_Start_DMA(&hadc, (uint32_t *)&adc[1], 4);

to main(), and it worked.  But it doesn't seem to work in the TIM1_BRK_UP_TRG_COM_IRQHandler ISR.

Does the CPU have to be in User mode to start the ADC/DMA with HAL_ADC_Start_DMA?

stthormj9
Associate II
Posted on July 21, 2016 at 01:46

More a-ha moments:

It appears that my issue with ADC_Start_DMA is part of the __HAL_LOCK issue; I was having the same issue trying to read TIM14.CCR1 inside the ISR using HAL_TIM_ReadCapturedValue(&htim14,1) and just getting 0.

Reading the value directly  tim14.Instance->CCR1 made it work.