This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

ADC Interrupt not starting

I am programming with mbed, and i am trying to start the adc-task via ppi and timer cc events. another ppi channel is used to toogle the LED.

I am using the nRF51822 on the RedBearLabs BLE NANO.

After 2 seconds (start of cc[1] event) thw while loops stops executing, bit led toggling via ppi continues.

I took several examples form GitHub as a starting point.

USBRX and USBTX use Pin 9 & 11

Where is my mistake?

#include <nrf.h>
#include "nrf_delay.c"
#include "mbed.h"

#define PIN_LED     19UL

Serial pc(USBTX, USBRX);

volatile int32_t adc_sample;
volatile uint8_t adc_flag = 0;

void ADC_IRQHandler(void)
{
    NRF_ADC->EVENTS_END = 0;
    adc_sample = NRF_ADC -> RESULT;
    adc_flag = 1;
}

int main(void)
{
    pc.printf("\nSERIAL_INIT\n");

    // Configure PIN_GPIO as output
    NRF_GPIO->DIRSET = (1UL << PIN_LED);
    
    // Configure GPIOTE Task
    NRF_GPIOTE->CONFIG[0] =     (GPIOTE_CONFIG_MODE_Task << GPIOTE_CONFIG_MODE_Pos)|
                                (GPIOTE_CONFIG_POLARITY_Toggle << GPIOTE_CONFIG_POLARITY_Pos)|
                                (GPIOTE_CONFIG_OUTINIT_High << GPIOTE_CONFIG_OUTINIT_Pos)|
                                (PIN_LED << GPIOTE_CONFIG_PSEL_Pos);
    pc.printf("GPIO_INIT\n"); 
    
    // Configure ADC
    // Enable interupt at END Event
    NRF_ADC->INTENSET = ADC_INTENSET_END_Msk;
    NVIC_EnableIRQ(ADC_IRQn); 
    
    // no Prescaling  - ReferenceVoltage VBG - 8bit - Analog Input 4
    NRF_ADC->CONFIG = (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos)                               /* Bits 17..16 : ADC external reference pin selection. */
                                        | (ADC_CONFIG_PSEL_AnalogInput4 << ADC_CONFIG_PSEL_Pos)                                 /*!< Use analog input 6 as analog input (P0.05). */
                                        | (ADC_CONFIG_REFSEL_VBG << ADC_CONFIG_REFSEL_Pos)                                          /*!< Use internal 1.2V bandgap voltage as reference for conversion. */
                                        | (ADC_CONFIG_INPSEL_AnalogInputNoPrescaling << ADC_CONFIG_INPSEL_Pos)  /*!< Analog input specified by PSEL with no prescaling used as input for the conversion. */
                                        | (ADC_CONFIG_RES_8bit << ADC_CONFIG_RES_Pos);  
    // enable ADV                                    
    NRF_ADC->ENABLE =       ADC_ENABLE_ENABLE_Enabled;
    
    pc.printf("ADC_STARTED\n");                      
    
    // Configure Timer 
    NRF_TIMER0->BITMODE =    (TIMER_BITMODE_BITMODE_32Bit << TIMER_BITMODE_BITMODE_Pos);
    NRF_TIMER0->PRESCALER =  (4 << TIMER_PRESCALER_PRESCALER_Pos); // 1us periode
    
    // Configure Timer Events
    NRF_TIMER0->CC[0] = 1000000; // 1s compare value
    NRF_TIMER0->CC[1] = 2000000; // 2s compare value
    
    // Shortcut clear timer at compare1 event
    NRF_TIMER0->SHORTS = (TIMER_SHORTS_COMPARE1_CLEAR_Enabled << TIMER_SHORTS_COMPARE1_CLEAR_Pos);
    pc.printf("TIMER0_INIT\n");
    
    // Configure PPI Channel 0
    NRF_PPI->CH[0].EEP = (uint32_t)&NRF_TIMER0->EVENTS_COMPARE[0];
    NRF_PPI->CH[0].TEP = (uint32_t)&NRF_GPIOTE->TASKS_OUT[0];
    
    // Configure PPI Channel 1
    NRF_PPI->CH[1].EEP = (uint32_t)&NRF_TIMER0->EVENTS_COMPARE[1];
    NRF_PPI->CH[1].TEP = (uint32_t)&NRF_ADC->TASKS_START;
    
    // Configure PPI Channel 2
    NRF_PPI->CH[2].EEP = (uint32_t)&NRF_TIMER0->EVENTS_COMPARE[1];
    NRF_PPI->CH[2].TEP = (uint32_t)&NRF_GPIOTE->TASKS_OUT[0];
    pc.printf("PPI_INIT\n");
                            
    // Enable PPI channels 0 and 1
    NRF_PPI->CHENSET = (1UL << 0) | (1UL << 1) | (1UL << 2);
    pc.printf("PPI_ENABLED\n");
    
    // Start timer0
    NRF_TIMER0->TASKS_START = 1;
    pc.printf("TIMER0_STARTED\n");
    
    while (1)
        {
            pc.printf("l");
            if (adc_flag == 1)
            {
                pc.printf("%d/n", (int)adc_sample);
                adc_flag = 0;
            }
        }
}
Related