How to use RTC module with no SDK?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to use RTC module with no SDK?

Jump to solution
1,947 Views
pengchenbing
Contributor II

Could I use RTC with no SDK ?what's the step , is there any examples?

tks.

0 Kudos
1 Solution
1,623 Views
danielmartynek
NXP TechSupport
NXP TechSupport

Hi,

I tested the below simple code on S32K144 EVB.

The RTC second interrupt toggles Blue LED every second while the Alarm interrupt toggles Red LED every 4 second.

It uses 32KHz LPO_CLK.  

void RTC_init(void)
{
 // Power-on cycle the MCU before writing to this register
 SIM->LPOCLKS |= (1 << 3) | (1 << 4);
 // [5-4] RTCCLKSEL = 0b01 (32 kHz LPO_CLK)
 
 // Peripheral Clock Control for RTC
 PCC->PCCn[PCC_RTC_INDEX] = PCC_PCCn_CGC_MASK; // Enable clock to RTC module - BUS_CLK
 
 // RTC_TSR is writable provided the time counter is disabled
 RTC->SR &= ~(1 << 4);
 // [4] TCE = 0 (Time Counter Disabled)

 // RTC_CR Control Registers
 RTC->CR &= ~(1 << 7);
 // [7] LPOS = 0 (RTC prescaler increments using 32kHz)
 
 RTC->IER &= ~(0b111 << 16);
 // [16] TSIC = 0b000 (1 Hz second interrupt)
 
 RTC->IER |= (1 << 4) | (1 << 2);
 // [4] TSIE = 1 (Time Seconds Interrupt Enable)
 // [2] TAIE = 1 (Time alarm flag does generate an interrupt)
 // [1] TOIE = 0 (Time overflow flag does not generate an interrupt)

 // Writing to RTC_TSR when the time counter is disabled will clear Time Invalid Flag
 RTC->TSR = 0x00000000;
 
 // RTC_TAR Alarm Register
 RTC->TAR = 4;    // Alarm in 5s

 // The prescaler output clock is output on RTC_CLKOUT pin
 RTC->CR |= (1 << 24);
 // [25-24] CPE = 0b01 RTC_CLKOUT is enabled

 // RTC_Interrupt
 S32_NVIC->ICPR[1] = (1 << (46 % 32));
 S32_NVIC->ISER[1] = (1 << (46 % 32));
 S32_NVIC->IP[46] = 0x00;  // Priority level 0

 // RTC_Seconds_Interrupt
 S32_NVIC->ICPR[1] = (1 << (47 % 32));
 S32_NVIC->ISER[1] = (1 << (47 % 32));
 S32_NVIC->IP[47] = 0x10;  // Priority level 1

 RTC->SR |= (1 << 4);
 // [4] TCE = 1 (Time Counter Enable)
}

void port_init(void)
{
 PCC-> PCCn[PCC_PORTD_INDEX] = PCC_PCCn_CGC_MASK; // Enable clock to PortD - BUS_CLK
 
 // Configure port D15 as GPIO output (Blue LED on EVB)
 PTD->PDDR |= (1 << 0);             // Port D0: Data Direction (output)
 PORTD->PCR[0] = 0x00000100;        // Port D0: MUX = GPIO
 PTD-> PSOR |= (1 << 0);            // Clear Output on port D0 (LED off)
 
 // Configure port D15 as GPIO output (Red LED on EVB)
 PTD->PDDR |= (1 << 15);            // Port D15: Data Direction (output)
 PORTD->PCR[15] = 0x00000100;       // Port D15: MUX = GPIO
 PTD-> PSOR |= (1 << 15);           // Clear Output on port D15 (LED off)
 
 PORTD->PCR[13] |= PORT_PCR_MUX(7); // Port D13: MUX = ALT7, RTC_CLKOUT
}

void RTC_Seconds_IRQHandler(void)
{
 PTD->PTOR |= (1 << 0); // toggle the Blue LED
}

void RTC_IRQHandler(void)
{
 PTD->PTOR |= (1 << 15); // toggle the Red LED
 uint32_t tar = RTC->TAR;
 RTC->TAR = tar + 4;     // Alarm in 4s
}

int main(void)
{
 port_init();
 RTC_init();
 
 while(1){
 }
}

Regards,

Daniel

View solution in original post

0 Kudos
1 Reply
1,624 Views
danielmartynek
NXP TechSupport
NXP TechSupport

Hi,

I tested the below simple code on S32K144 EVB.

The RTC second interrupt toggles Blue LED every second while the Alarm interrupt toggles Red LED every 4 second.

It uses 32KHz LPO_CLK.  

void RTC_init(void)
{
 // Power-on cycle the MCU before writing to this register
 SIM->LPOCLKS |= (1 << 3) | (1 << 4);
 // [5-4] RTCCLKSEL = 0b01 (32 kHz LPO_CLK)
 
 // Peripheral Clock Control for RTC
 PCC->PCCn[PCC_RTC_INDEX] = PCC_PCCn_CGC_MASK; // Enable clock to RTC module - BUS_CLK
 
 // RTC_TSR is writable provided the time counter is disabled
 RTC->SR &= ~(1 << 4);
 // [4] TCE = 0 (Time Counter Disabled)

 // RTC_CR Control Registers
 RTC->CR &= ~(1 << 7);
 // [7] LPOS = 0 (RTC prescaler increments using 32kHz)
 
 RTC->IER &= ~(0b111 << 16);
 // [16] TSIC = 0b000 (1 Hz second interrupt)
 
 RTC->IER |= (1 << 4) | (1 << 2);
 // [4] TSIE = 1 (Time Seconds Interrupt Enable)
 // [2] TAIE = 1 (Time alarm flag does generate an interrupt)
 // [1] TOIE = 0 (Time overflow flag does not generate an interrupt)

 // Writing to RTC_TSR when the time counter is disabled will clear Time Invalid Flag
 RTC->TSR = 0x00000000;
 
 // RTC_TAR Alarm Register
 RTC->TAR = 4;    // Alarm in 5s

 // The prescaler output clock is output on RTC_CLKOUT pin
 RTC->CR |= (1 << 24);
 // [25-24] CPE = 0b01 RTC_CLKOUT is enabled

 // RTC_Interrupt
 S32_NVIC->ICPR[1] = (1 << (46 % 32));
 S32_NVIC->ISER[1] = (1 << (46 % 32));
 S32_NVIC->IP[46] = 0x00;  // Priority level 0

 // RTC_Seconds_Interrupt
 S32_NVIC->ICPR[1] = (1 << (47 % 32));
 S32_NVIC->ISER[1] = (1 << (47 % 32));
 S32_NVIC->IP[47] = 0x10;  // Priority level 1

 RTC->SR |= (1 << 4);
 // [4] TCE = 1 (Time Counter Enable)
}

void port_init(void)
{
 PCC-> PCCn[PCC_PORTD_INDEX] = PCC_PCCn_CGC_MASK; // Enable clock to PortD - BUS_CLK
 
 // Configure port D15 as GPIO output (Blue LED on EVB)
 PTD->PDDR |= (1 << 0);             // Port D0: Data Direction (output)
 PORTD->PCR[0] = 0x00000100;        // Port D0: MUX = GPIO
 PTD-> PSOR |= (1 << 0);            // Clear Output on port D0 (LED off)
 
 // Configure port D15 as GPIO output (Red LED on EVB)
 PTD->PDDR |= (1 << 15);            // Port D15: Data Direction (output)
 PORTD->PCR[15] = 0x00000100;       // Port D15: MUX = GPIO
 PTD-> PSOR |= (1 << 15);           // Clear Output on port D15 (LED off)
 
 PORTD->PCR[13] |= PORT_PCR_MUX(7); // Port D13: MUX = ALT7, RTC_CLKOUT
}

void RTC_Seconds_IRQHandler(void)
{
 PTD->PTOR |= (1 << 0); // toggle the Blue LED
}

void RTC_IRQHandler(void)
{
 PTD->PTOR |= (1 << 15); // toggle the Red LED
 uint32_t tar = RTC->TAR;
 RTC->TAR = tar + 4;     // Alarm in 4s
}

int main(void)
{
 port_init();
 RTC_init();
 
 while(1){
 }
}

Regards,

Daniel

0 Kudos