Peripherals

Real-Time Counter (RTC)

The RTC is configured to generate periodic events used to trigger ADC conversions. Periodic events are generated at periodic intervals with frequency given by:
fPER(n)=fCLK_RTC_OSC2n+3
where fCLK_RTC_OSC is the frequency of the internal prescaler clock CLK_RTC_OSC and n=07 is the position of the Periodic Interval n Event Output Enable bitgroup in the Event Control register (EVCTRL.PEROn). In the example we choose a periodic event frequency of 32Hz to start 32 sequential conversions per second. To achieve the desired frequency, the 32.768kHz RTC clock source OSCULP32K must be prescaled by 1024, giving n=7 from the equation 2n+3=1024 . The 32Hz periodic events are routed via an event channel to the ADC. The following configurations are made to enable generation of the seventh periodic event:
conf_rtc_events.generate_event_on_periodic[0] = false;
...
conf_rtc_events.generate_event_on_periodic[6] = false;
conf_rtc_events.generate_event_on_periodic[7] = true;

Event System (EVENT)

The event system must be configured with a channel to route events from the RTC periodic event generator to the ADC start conversion user. The following configurations allocates a channel which runs asynchronously in standby with RTC periodic event 7 set as generator and ADC start conversion set as user:

config_evsys.generator      = EVSYS_ID_GEN_RTC_PER_7;
config_evsys.edge_detect    = EVENTS_EDGE_DETECT_NONE;
config_evsys.path           = EVENTS_PATH_ASYNCHRONOUS;
config_evsys.clock_source   = GCLK_GENERATOR_0;
config_evsys.on_demand      = true;
config_evsys.run_in_standby = true;
events_allocate(&event_channel, &config_evsys);
events_attach_user(&event_channel, EVSYS_ID_USER_ADC_START);

Analog-to-Digital Converter (ADC)

The ADC is configured to start converting a sequence of inputs when incomming event occurs. To minimize the power consumption, sleepwalking is enabled to ensure the ADC only runs in sleep mode when requested. The following configurations set the incoming event action to start new conversion and enables the ADC to run as a sleepwalking task:

config_adc.event_action    = ADC_EVENT_ACTION_START_CONV;
config_adc.run_in_standby  = true;
config_adc.on_demand       = true;

The sequential sampling is configured with the Sequence Control (SEQCTRL) register and the Enable Positive Input n in the Sequence (SEQENn) bitgroup. The bits are set according to the Positive Mux Input Selection. The three inputs selected in the example application are SCALEDCOREVCC, SCALEDIOVCC, and SCALEDVBAT.

ADC->SEQCTRL.reg = 0x2C000000;

Direct Memory Access Controller (DMAC)

The DMA is configured to transfer the ADC results from the result buffer to a user defined array in RAM. Since the ADC is configured to sample a sequence of inputs, the individual results must be transfered after each conversion. The following configurations defines the DMA descriptor source and destination addresses for transfering data from ADC result register to RAM.

config_descriptor.source_address      =
        (uint32_t)(&adc_instance.hw->RESULT.reg);
config_descriptor.destination_address =
        (uint32_t)&adc_result_store[NO_OF_INPUT_SCAN];

The transfer is initiated when the ADC signals that a conversion has ended, using a dedicated event channel. This is configured using the following code line in the DMA configuration function:

config_dma.peripheral_trigger = ADC_DMAC_ID_RESRDY;

External Interrupt Controller (EIC)

The SW0 button on the Xplained Pro board is in the application used to switch between active mode and sleep mode. To identify a button push, EIC is configured to detect a falling slope on the SW0 button interrupt line. A callback function is registered and called when a button push is detected. The interrupt wakes the device if sleeping and the callback function switches a power mode variable, which keeps track of the current mode.

Serial Communication Interface (SERCOM USART)

The USART is configured for serial communication between the application and a computer. The communication is used to print out the latest ADC samples when the device is in active mode. Communication to and from the USART is relayed via the EDBG, which enumerates as a USB communication device to the PC using the same port and cable as for programming.

Revision A Workarounds

Some of the SAM L21 revision A erratas are fixed in the example project using workarounds. These workarounds are applied if the SAM_L21_REV_A define is present. Fix for errata reference 13901 is added to the extint_detection_callback() function. A workaround is also implemented for errata reference 14268 regarding the ADC voltage reference. For this purpose the DAC is enabled to ensure the analog reference is available when requested by the ADC.