Hello everyone,
I am currently trying to read two different analogue inputs at the same time using the M4 of a Toradex imx8dx system.
Right now i have one ADC channel in use reading on an interrupt.
The ideal solution would be to use the same interrupt to read a second channel (as far as I understand it the ADC chip reads all inputs at the same time, therefore they should already be ready at the same time).
Can someone help me to set the settings correctly?
Here are the useful bits of my code:
/*
* ADC IN 1 PIN: SODIMM 4 -> ADMA_ADC0_IN4
* ADC IN 2 PIN: SODIMM 2 -> ADMA_ADC0_IN5
*/
#define SENSOR_LPADC_BASE ADMA__ADC0
#define SENSOR_1_LPADC_USER_CHANNEL 4U
#define SENSOR_1_LPADC_USER_CMDID 1U
#define SENSOR_2_LPADC_USER_CHANNEL 5U
#define SENSOR_2_LPADC_USER_CMDID 2U
#define SENSOR_LPADC_IRQn ADMA_ADC0_INT_IRQn
#define SENSOR_LPADC_IRQ_HANDLER_FUNC ADMA_ADC0_INT_IRQHandler
/*
* ADC VARs
*/
volatile bool g_LpadcConversionCompletedFlag = false;
volatile uint32_t g_LpadcInterruptCounter = 0U;
lpadc_conv_result_t g_LpadcResultConfigStruct;
#if (defined(DEMO_LPADC_USE_HIGH_RESOLUTION) && DEMO_LPADC_USE_HIGH_RESOLUTION)
const uint32_t g_LpadcFullRange = 65536U;
const uint32_t g_LpadcResultShift = 0U;
#else
const uint32_t g_LpadcFullRange = 4096U;
const uint32_t g_LpadcResultShift = 3U;
#endif /* DEMO_LPADC_USE_HIGH_RESOLUTION */
/*
* IRQ_HANDLER OF ADC
*
*/
void SENSOR_LPADC_IRQ_HANDLER_FUNC(void)
{
g_LpadcInterruptCounter++;
if (LPADC_GetConvResult(SENSOR_LPADC_BASE, &g_LpadcResultConfigStruct))
{
//read value and write to picture
picture_1[irq_count-1] = ((g_LpadcResultConfigStruct.convValue) >> g_LpadcResultShift);
//read second value
picture_2[irq_count-1] = 0; //get second value from ADC channel 5U
}
SDK_ISR_EXIT_BARRIER;
}
Solved! Go to Solution.
Hi @Cillex ,
I hope you are doing well.
One can use separate command for multiple ADC channels.
Please refer to example given at boards/mekmimx8qm/driver_examples/lpadc/interrupt/cm4_core0 in SDK
e.g.
LPADC_GetDefaultConvCommandConfig(&mLpadcCommandConfigStruct1);
mLpadcCommandConfigStruct1.channelNumber = DEMO_LPADC_USER_CHANNEL1;
mLpadcCommandConfigStruct1.conversionResolutionMode = kLPADC_ConversionResolutionHigh;
LPADC_SetConvCommandConfig(DEMO_LPADC_BASE, DEMO_LPADC_USER_CMDID1, &mLpadcCommandConfigStruct1);
LPADC_GetDefaultConvCommandConfig(&mLpadcCommandConfigStruct2);
mLpadcCommandConfigStruct2.channelNumber = DEMO_LPADC_USER_CHANNEL2;
mLpadcCommandConfigStruct2.conversionResolutionMode = kLPADC_ConversionResolutionHigh;
LPADC_SetConvCommandConfig(DEMO_LPADC_BASE, DEMO_LPADC_USER_CMDID2, &mLpadcCommandConfigStruct2);
/* Set trigger configuration. */
LPADC_GetDefaultConvTriggerConfig(&mLpadcTriggerConfigStruct1);
mLpadcTriggerConfigStruct1.targetCommandId = DEMO_LPADC_USER_CMDID1;
mLpadcTriggerConfigStruct1.enableHardwareTrigger = false;
LPADC_SetConvTriggerConfig(DEMO_LPADC_BASE, 0U, &mLpadcTriggerConfigStruct1); /* Configurate the trigger0. */
LPADC_GetDefaultConvTriggerConfig(&mLpadcTriggerConfigStruct2);
mLpadcTriggerConfigStruct2.targetCommandId = DEMO_LPADC_USER_CMDID2;
mLpadcTriggerConfigStruct2.enableHardwareTrigger = false;
LPADC_SetConvTriggerConfig(DEMO_LPADC_BASE, 1U, &mLpadcTriggerConfigStruct2);
-----------------------------------------------------------------------------------------------------------------------------
Then one can use LPADC_DoSoftwareTrigger to trigger software.
LPADC_DoSoftwareTrigger(DEMO_LPADC_BASE, 1U<<0); // trigger 0
LPADC_DoSoftwareTrigger(DEMO_LPADC_BASE, 1U<<1)); // trigger 1.
One can determine which channel reading are available using g_LpadcResultConfigStruct in ISR.
g_LpadcResultConfigStruct.commandIdSource // One can determine the channel using command ID.
Please refer to 17.2 Analog-to-Digital Converter (ADC) in RM for more information.
I hope it helps!
Thanks & Regards,
Sanket Parekh
Dear Sanket Parekh,
Thank you very much for the quick response.
It worked perfectly and exactly as intended.
Best Regards,
Cillex
Hi @Cillex ,
I hope you are doing well.
One can use separate command for multiple ADC channels.
Please refer to example given at boards/mekmimx8qm/driver_examples/lpadc/interrupt/cm4_core0 in SDK
e.g.
LPADC_GetDefaultConvCommandConfig(&mLpadcCommandConfigStruct1);
mLpadcCommandConfigStruct1.channelNumber = DEMO_LPADC_USER_CHANNEL1;
mLpadcCommandConfigStruct1.conversionResolutionMode = kLPADC_ConversionResolutionHigh;
LPADC_SetConvCommandConfig(DEMO_LPADC_BASE, DEMO_LPADC_USER_CMDID1, &mLpadcCommandConfigStruct1);
LPADC_GetDefaultConvCommandConfig(&mLpadcCommandConfigStruct2);
mLpadcCommandConfigStruct2.channelNumber = DEMO_LPADC_USER_CHANNEL2;
mLpadcCommandConfigStruct2.conversionResolutionMode = kLPADC_ConversionResolutionHigh;
LPADC_SetConvCommandConfig(DEMO_LPADC_BASE, DEMO_LPADC_USER_CMDID2, &mLpadcCommandConfigStruct2);
/* Set trigger configuration. */
LPADC_GetDefaultConvTriggerConfig(&mLpadcTriggerConfigStruct1);
mLpadcTriggerConfigStruct1.targetCommandId = DEMO_LPADC_USER_CMDID1;
mLpadcTriggerConfigStruct1.enableHardwareTrigger = false;
LPADC_SetConvTriggerConfig(DEMO_LPADC_BASE, 0U, &mLpadcTriggerConfigStruct1); /* Configurate the trigger0. */
LPADC_GetDefaultConvTriggerConfig(&mLpadcTriggerConfigStruct2);
mLpadcTriggerConfigStruct2.targetCommandId = DEMO_LPADC_USER_CMDID2;
mLpadcTriggerConfigStruct2.enableHardwareTrigger = false;
LPADC_SetConvTriggerConfig(DEMO_LPADC_BASE, 1U, &mLpadcTriggerConfigStruct2);
-----------------------------------------------------------------------------------------------------------------------------
Then one can use LPADC_DoSoftwareTrigger to trigger software.
LPADC_DoSoftwareTrigger(DEMO_LPADC_BASE, 1U<<0); // trigger 0
LPADC_DoSoftwareTrigger(DEMO_LPADC_BASE, 1U<<1)); // trigger 1.
One can determine which channel reading are available using g_LpadcResultConfigStruct in ISR.
g_LpadcResultConfigStruct.commandIdSource // One can determine the channel using command ID.
Please refer to 17.2 Analog-to-Digital Converter (ADC) in RM for more information.
I hope it helps!
Thanks & Regards,
Sanket Parekh