2016-07-15 01:13 PM
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...
2016-07-18 07:10 AM
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 intiteled ''ADC_TriggerMode'' located at this path: stm32cubef4_1.11\STM32Cube_FW_F4_V1.12.0\Projects\STM324x9I_EVAL\Examples\ADC\ADC_TriggerMode -Hannibal-2016-07-18 07:29 PM
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)...
2016-07-19 03:03 AM
HI johnson.thor,
In that case, you can get help form the ''ADC_RegularConversion_DMA'' located in the same library and subfolder. -Hannibal-2016-07-19 06:55 PM
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...
2016-07-20 10:32 AM
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?
2016-07-20 04:46 PM
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.