Main Function

The application is implemented in the main function. Initially, the power saving features are applied before all the drivers are configured and enabled. In the while-loop the application switches between normal mode and sleep mode when a button push is detected. When in normal mode, the device is ready to recieve and send data over USART to print out the latest ADC results. By sending an 'S' over USART, the latest sequence of results are transmitted and printed in the open serial COM port terminal window. In sleep mode the conversion and transferring of data runs as sleepwalking tasks.

/** Main function */
int main(void)
{
	system_init();
	
	/** Enable power saving features */
	select_buck_regulator_as_main_vreg();
	enable_power_domain_gating_pd01();
	enable_vref_and_vreg_runstdby();

	/** Configure RTC, ADC, EVSYS, EXTINT and USART */
	configure_rtc();
	configure_adc();
	configure_evsys();
	configure_extint_channel();
	configure_usart();
	
	/** Configure DAC workaround if Rev. A */
	#ifdef SAM_L21_REV_A
	configure_dac();
	#endif
	
	/** Configure DMA channel and descriptor */
	configure_dma_channel(&dma_channel);
	configure_dma_descriptor(&descriptor_0);
	
	/** Add descriptor and start transfer job */
	dma_add_descriptor(&dma_channel, &descriptor_0);
	dma_start_transfer_job(&dma_channel);
	
	/** Enable interrupts */
	system_interrupt_enable_global();
	
	/** Set STANDBY as sleep mode */
	system_set_sleepmode(SYSTEM_SLEEPMODE_STANDBY);
	
	/** Set default power mode to normal */
	powermode = POWER_MODE_NORMAL;
	
	while (1) {
		/** Device awake */
		if (powermode == POWER_MODE_NORMAL) {
			usart_read_buffer_job(&usart_instance, 
					(uint8_t *)rx_buffer, 
					MAX_RX_BUFFER_LENGTH);
			if (send_data) {
				send_data = false;
				char buffer[100];
				sprintf(buffer,"ADC1: %u ADC2: %u ADC3: %u\r\n",
						adc_result_store[0],
						adc_result_store[1],
						adc_result_store[2]);
				usart_write_buffer_job(&usart_instance,
						(uint8_t*)buffer,
						strlen(buffer));
			}
		}
		
		/** Device asleep*/
		if (powermode == POWER_MODE_SLEEP) {
			system_sleep();
		}
	}
}
Note: When SERCOM is configured and enabled via EDBG, the serial COM port on the PC must be enabled to avoid current leakage increasing the power consumption. In the example project, opening the COM port reduces the average current consumption by ~90μA.