I'm trying to trigger an ADC channel with a Timer, but it isn't working. Could you tell me where is the mistake? I'm using an LPC4337.
This is the code :
#include "board.h"
/************ ADC setup ************/
void ADC_setup(void) {
Chip_SCU_ADC_Channel_Config(0, ADC_CH1);
static ADC_CLOCK_SETUP_T ADCSetup;
Chip_ADC_Init(LPC_ADC0, &ADCSetup);
Chip_ADC_EnableChannel(LPC_ADC0, ADC_CH1, ENABLE);
Chip_ADC_SetSampleRate(LPC_ADC0, &ADCSetup, ADC_MAX_SAMPLE_RATE);
Chip_ADC_SetResolution(LPC_ADC0, &ADCSetup, ADC_10BITS);
Chip_ADC_SetStartMode(LPC_ADC0, ADC_START_ON_CTOUT15, ADC_TRIGGERMODE_RISING);
Chip_ADC_SetBurstCmd(LPC_ADC0, DISABLE);
Chip_ADC_Int_SetChannelCmd(LPC_ADC0, ADC_CH1, ENABLE);
}
uint16_t data = 0;
void ADC0_IRQHandler(void) {
if(Chip_ADC_ReadStatus(LPC_ADC0, ADC_CH1, ADC_DR_OVERRUN_STAT) == SET){
printf("ADC OVERRUN\n");
}
Chip_ADC_ReadValue(LPC_ADC0, ADC_CH1, &data);
printf("Enter ADC0_IRQHandler()\n");
}
/************ TIMER setup ************/
#define TICKRATE_HZ 50000
#define LPC_TIMER LPC_TIMER3
#define NVIC_TIMER_IRQ TIMER3_IRQn
#define TIMER_IRQHandler void TIMER3_IRQHandler(void)
#define CHIP_RGU_TIMER_RST RGU_TIMER3_RST
#define CHIP_CLK_TIMER CLK_MX_TIMER3
#define LPC_TIMER_MATCH_INT_NUM 3
void TIMER_setup(){
Chip_TIMER_Init(LPC_TIMER);
Chip_RGU_TriggerReset(CHIP_RGU_TIMER_RST);
while (Chip_RGU_InReset(CHIP_RGU_TIMER_RST)) {}
uint32_t timerFreq = Chip_Clock_GetRate(CHIP_CLK_TIMER);
Chip_TIMER_Reset(LPC_TIMER);
Chip_TIMER_MatchEnableInt(LPC_TIMER, LPC_TIMER_MATCH_INT_NUM);
Chip_TIMER_SetMatch(LPC_TIMER, LPC_TIMER_MATCH_INT_NUM, (timerFreq / TICKRATE_HZ));
Chip_TIMER_ResetOnMatchEnable(LPC_TIMER, LPC_TIMER_MATCH_INT_NUM);
Chip_TIMER_ExtMatchControlSet(LPC_TIMER, 1, TIMER_EXTMATCH_TOGGLE, LPC_TIMER_MATCH_INT_NUM);
Chip_TIMER_Enable(LPC_TIMER);
}
TIMER_IRQHandler {
if (Chip_TIMER_MatchPending(LPC_TIMER, LPC_TIMER_MATCH_INT_NUM)) {
Chip_TIMER_ClearMatch(LPC_TIMER, LPC_TIMER_MATCH_INT_NUM);
}
//printf("Enter TIMER_IRQHandler()\n");
}
/************ main() ************/
int main(void)
{
SystemCoreClockUpdate();
Board_Init();
ADC_setup();
TIMER_setup();
NVIC_EnableIRQ(ADC0_IRQn);
NVIC_ClearPendingIRQ(ADC0_IRQn);
NVIC_EnableIRQ(NVIC_TIMER_IRQ);
NVIC_ClearPendingIRQ(NVIC_TIMER_IRQ);
while(1) {
__WFI();
}
return 1;
}
Just in case anyone else runs into this, the EDGE bit (0x2) also needs to be set in order to the trigger the ADC.
LPC_GIMA->ADCSTART0_IN = 0x6;
I found the solution reading the chapter about GIMA:
So, what worked for me was to add the following line:
LPC_GIMA->ADCSTART0_IN = 0x4;