I'm trying to configure the RTC in count mode in the SAM L21 Xplained Pro B with the ultra low power clock OSCULP32K, usign ASF. In order to test it, I followed the Quick start RTC count polled example, which toggles LED0 every 2000 ms by comparing the RTC count with a given period.
In the example, RTC_CLOCK_SOURCE uses RTC_CLOCK_SELECTION_ULP1K. When I change it to RTC_CLOCK_SELECTION_ULP32K, the LED toggles 24 times every 2000ms instead of once every 2000 ms.
I have the following configuration at conf_rtc.h:
# define RTC_CLOCK_SOURCE RTC_CLOCK_SELECTION_ULP32K
Then, in conf_clocks.h I configured the main CPU clock at 4Mhz.
/* SYSTEM_CLOCK_SOURCE_OSC16M configuration - Internal 16MHz oscillator */
# define CONF_CLOCK_OSC16M_FREQ_SEL SYSTEM_OSC16M_4M
# define CONF_CLOCK_OSC16M_ON_DEMAND true
# define CONF_CLOCK_OSC16M_RUN_IN_STANDBY false
/* Configure GCLK generator 0 (Main Clock) */
# define CONF_CLOCK_GCLK_0_ENABLE true
# define CONF_CLOCK_GCLK_0_RUN_IN_STANDBY false
# define CONF_CLOCK_GCLK_0_CLOCK_SOURCE SYSTEM_CLOCK_SOURCE_OSC16M
# define CONF_CLOCK_GCLK_0_PRESCALER 1
# define CONF_CLOCK_GCLK_0_OUTPUT_ENABLE false
And in main.c, I configure and initialize the rtc as follows:
/* RTC configuration*/
void configure_rtc_count(void);
struct rtc_module rtc_instance;
// [initiate rtc]
void configure_rtc_count(void)
{
//! [set_conf]
struct rtc_count_config config_rtc_count;
//! [set_conf]
//! [get_default]
rtc_count_get_config_defaults(&config_rtc_count);
//! [get_default]
//! [set_config]
config_rtc_count.prescaler = RTC_COUNT_PRESCALER_DIV_1;
config_rtc_count.mode = RTC_COUNT_MODE_16BIT;
#ifdef FEATURE_RTC_CONTINUOUSLY_UPDATED
config_rtc_count.continuously_update = true;
#endif
config_rtc_count.compare_values[0] = 1000;
//! [set_config]
//! [init_rtc]
rtc_count_init(&rtc_instance, RTC, &config_rtc_count);
//! [init_rtc]
//! [enable]
rtc_count_enable(&rtc_instance);
//! [enable]
}
// [initiate rtc]
/* End RTC configuration*/
One it's initialized, the code at main(void) reads:
int main (void)
{
system_init();
configure_rtc_count();
/*test RTC - toggle LED0 each 2000 ms*/
rtc_count_set_period(&rtc_instance, 2000);
for (;;)
{
/*test RTC - if count match, toggle LED0 each 2000 ms*/
if (rtc_count_is_compare_match(&rtc_instance, RTC_COUNT_COMPARE_0))
{
/* Do something on RTC count match here */
port_pin_toggle_output_level(LED_0_PIN);
rtc_count_clear_compare_match(&rtc_instance, RTC_COUNT_COMPARE_0);
}
/*end test RTC*/
}
}
I cannot find the explanation of what is the meaning of config_rtc_count.compare_values[0] = 1000; I changed it to 32000, but the LED doesn't blink, and if I lower it under 1000, it keeps the same toggle rate...What does this mean?
In the examples like http://www.atmel.com/Images/Atmel-42471-SAM-L21-ADC-Sampling-using-Low-Power-Features_ApplicationNote_AT12705.pdf it seems the RTC is configured only with the #define RTC_CLOCK_SOURCE at conf_rtc.h.
However in others like http://www.atmel.com/Images/Atmel-42111-SAM-RTC-Count-Driver-RTC-Count_ApplicationNote_AT03249.pdf it defines GLCK 2 as:
/* Configure GCLK generator 2 (RTC) */
# define CONF_CLOCK_GCLK_2_ENABLE true
# define CONF_CLOCK_GCLK_2_RUN_IN_STANDBY false
# define CONF_CLOCK_GCLK_2_CLOCK_SOURCE SYSTEM_CLOCK_SOURCE_OSC32K
# define CONF_CLOCK_GCLK_2_PRESCALER 32
# define CONF_CLOCK_GCLK_2_OUTPUT_ENABLE false
Does anyone know how to configure the RTC with the ULP32k keeping the correct count rate?