S32K144 EVB之RTC

开发环境

IAR7.8 + S32K144-EVB

这次依旧采用了裸机编程,本次例程使用了RTC的秒中断来点亮LED灯:
S32K144 EVB之RTC_第1张图片
step1:使能中断

/* clr any pending IRQ*/
FSL_NVIC->ICPR[1] = 1 << (RTC_Seconds_IRQn % 32);
/* enable IRQ */
FSL_NVIC->ISER[1] = 1 << (RTC_Seconds_IRQn % 32);  
/* priority 10 of 0-15*/
FSL_NVIC->IP[RTC_Seconds_IRQn] = 0xA;              

step2:RTC初始化

//config clock source
PCC->PCCn[PCC_RTC_INDEX] = 0x40000000;

//Init RTC
RTC->SR = 0x00000000;
RTC->IER = 0x00000000;

//config RTC
RTC->TSR = 0x00000001;
RTC->CR = 0x00000004;
RTC->IER = 0x00000010;
RTC->SR = 0x00000010;

step3:编写中断函数

void RTC_Seconds_IRQHandler(void)
{
        LED_light(color ++ % 8);
}

在使用时遇到一个问题:
SIM->LPOCLKS = 0x1 << 4;
上句代码本来是用来切换RTC的时钟源的,但是看datasheet发现SIM是核心寄存器,必须使用特权模式才能访问,目前正在想办法解决ing

全部示例代码如下:

#include "S32K144.h"
#include "S32K144_features.h"

#define RED     15
#define GREEN   16
#define BLUE    0

#define BTN0    12
#define BTN1    13
#define BIT(n)  (1 << (n))

typedef unsigned char   u8;
typedef unsigned short  u16;
typedef unsigned long   u32;

static void Clock_Config(void);
static void Gpio_Config(void);
static void LED_light(u8 color);
static void IRQ_Config(void);
static void RTC_init(void);

static u8 color = 0;

int main(void)
{
    u32 cnt;

    cnt = 0;

        Clock_Config();
        Gpio_Config();
        IRQ_Config();
        RTC_init();

        while (1) {
                cnt ++;
        }
}

void PORTC_IRQHandler(void)
{
        if (PORTC->ISFR & BIT(BTN0)) {
                LED_light(color ++ % 8);
                PORTC->ISFR |= BIT(BTN0);
        } else if (PORTC->ISFR & BIT(BTN1)) {
                LED_light(color -- % 8);
                PORTC->ISFR |= BIT(BTN1);
        } else {
                /* do_nothing(); */
        }
}

void RTC_Seconds_IRQHandler(void)
{
        LED_light(color ++ % 8);
}

void SOSC_init_8MHz(void)
{
        SCG->SOSCDIV=0x00000101; /* SOSCDIV1 & SOSCDIV2 =1: divide by 1 */
        SCG->SOSCCFG=0x00000024; 
        /* Range=2: Medium freq (SOSC between 1MHz-8MHz)*/
        /* HGO=0: Config xtal osc for low power */
        /* EREFS=1: Input is external XTAL */
        while(SCG->SOSCCSR & SCG_SOSCCSR_LK_MASK); /* Ensure SOSCCSR unlocked */
        SCG->SOSCCSR=0x00000001; 
        /* LK=0: SOSCCSR can be written */
        /* SOSCCMRE=0: OSC CLK monitor IRQ if enabled */
        /* SOSCCM=0: OSC CLK monitor disabled */
        /* SOSCERCLKEN=0: Sys OSC 3V ERCLK output clk disabled */
        /* SOSCLPEN=0: Sys OSC disabled in VLP modes */
        /* SOSCSTEN=0: Sys OSC disabled in Stop modes */
        /* SOSCEN=1: Enable oscillator */
        while(!(SCG->SOSCCSR & SCG_SOSCCSR_SOSCVLD_MASK)); /* Wait for sys OSC clk valid */
}
void SPLL_init_160MHz(void)
{
        while(SCG->SPLLCSR & SCG_SPLLCSR_LK_MASK); /* Ensure SPLLCSR unlocked */
        SCG->SPLLCSR = 0x00000000; /* SPLLEN=0: SPLL is disabled (default) */
        SCG->SPLLDIV = 0x00000302; /* SPLLDIV1 divide by 2; SPLLDIV2 divide by 4 */
        SCG->SPLLCFG = 0x00180000; /* PREDIV=0: Divide SOSC_CLK by 0+1=1 */
        /* MULT=24: Multiply sys pll by 4+24=40 */
        /* SPLL_CLK = 8MHz / 1 * 40 / 2 = 160 MHz */
        while(SCG->SPLLCSR & SCG_SPLLCSR_LK_MASK); /* Ensure SPLLCSR unlocked */
        SCG->SPLLCSR = 0x00000001; 
        /* LK=0: SPLLCSR can be written */
        /* SPLLCMRE=0: SPLL CLK monitor IRQ if enabled */
        /* SPLLCM=0: SPLL CLK monitor disabled */
        /* SPLLSTEN=0: SPLL disabled in Stop modes */
        /* SPLLEN=1: Enable SPLL */
        while(!(SCG->SPLLCSR & SCG_SPLLCSR_SPLLVLD_MASK)); /* Wait for SPLL valid */
}
void NormalRUNmode_80MHz (void)
{
        /* Change to normal RUN mode with 8MHz SOSC, 80 MHz PLL*/
        SCG->RCCR=SCG_RCCR_SCS(6) | SCG_RCCR_DIVCORE(1) | SCG_RCCR_DIVBUS(1) | SCG_RCCR_DIVSLOW(2);
        while (((SCG->CSR & SCG_CSR_SCS_MASK) >> SCG_CSR_SCS_SHIFT ) != 6) {}
        /* Wait for sys clk src = SPLL */
}

static void Clock_Config(void)
{
        SOSC_init_8MHz(); /* Initialize system oscillator for 8 MHz xtal */
        SPLL_init_160MHz(); /* Initialize sysclk to 160 MHz with 8 MHz SOSC */
        NormalRUNmode_80MHz(); /* Init clocks: 80 MHz sysclk & core, 40 MHz bus, 20 MHz flash */
}
/**
 * PTD15-> RGB_RED
 * PTD16-> RGB_GREEN
 * PTD0 -> RGB_BLUE
 * BTN0 -> PTC12
 * BTN1 -> PTC13
 */
static void Gpio_Config(void)
{
        //config clock source
        PCC->PCCn[PCC_PORTD_INDEX] = 0x40000000;
        PCC->PCCn[PCC_PORTC_INDEX] = 0x40000000;

        /*
         * ISF  Interrupt Status Flag
         * IRQC Interrupt Configuration
         * LK   Lock Register
         * MUX  Pin Mux Control
         * DSE  Drive Strength Enable
         * PFE  Passive Filter Enable
         * PE   Pull Enable
         * PS   Pull Select
         */
         /*
         * ISF  0
         * IRQC 0000
         * LK   0
         * MUX  001
         * DSE  0
         * PFE  0
         * PE   0
         * PS   0
         */
        PORTD->PCR[RED] = 0x00000100;
        PORTD->PCR[GREEN] = 0x00000100;
        PORTD->PCR[BLUE] = 0x00000100;

        PTD->PDDR |= BIT(RED) | BIT(GREEN) | BIT(BLUE);

        PTD->PDOR |= BIT(RED) | BIT(GREEN) | BIT(BLUE);

        /*
         * ISF  0
         * IRQC 1001
         * LK   0
         * MUX  001
         * DSE  0
         * PFE  0
         * PE   0
         * PS   0
         */
        PORTC->PCR[BTN0] = 0x00090100;
        PORTC->PCR[BTN1] = 0x00090100;

        PTC->PDDR &=~(BIT(BTN0) | BIT(BTN1));
}

/**
 * TSR -> RTC Time Seconds Register
 * TPR -> RTC Time Prescaler Register
 * TAR -> RTC Time Alarm Register
 * TCR -> RTC Time Compensation Register
 * CR  -> RTC Control Register
 * SR  -> RTC Status Register
 * LR  -> RTC Lock Register
 * IER -> RTC Interrupt Enable Register
 */
static void RTC_init(void)
{
        SIM->LPOCLKS = 0x1 << 4;

        //config clock source
        PCC->PCCn[PCC_RTC_INDEX] = 0x40000000;

        //Init RTC
        RTC->SR = 0x00000000;
        RTC->IER = 0x00000000;

        //RTC->TCR = 0x00000001;
        /**
         * TSR
         */
        RTC->TSR = 0x00000001;
        /**
         * TPR
         */
        //RTC->TPR = ;
        /**
         * TAR
         */
        //RTC->TAR = ;
        /**
         * CIC Compensation Interval Counter
         * TCV Time Compensation Value
         * CIR Compensation Interval Register
         * TCR Time Compensation Register
         */
        //RTC->TCR = 0x00000000;
        /**
         * CPE  Clock Pin Enable
         * LPOS LPO Select
         * CPS  Clock Pin Select
         * UM   Update Mode
         * SUP  Supervisor Access
         * SWR  Software Reset
         */
        RTC->CR = 0x00000004;
        /**
         * LRL Lock Register Lock
         * SRL Status Register Lock
         * CRL Control Register Lock
         * TCL Time Compensation Lock
         */
        //RTC->LR = 0x00000078;
        /**
         * TSIC Timer Seconds Interrupt Configuration
         * TSIE Time Seconds Interrupt Enable
         * TAIE Time Alarm Interrupt Enable
         * TOIE Time Overflow Interrupt Enable
         * TIIE Time Invalid Interrupt Enable
         */
        RTC->IER = 0x00000010;
        /**
         * TCE Time Counter Enable
         * TAF Time Alarm Flag
         * TOF Time Overflow Flag
         * TIF Time Invalid Flag
         */
        RTC->SR = 0x00000010;
}

static void LED_light(u8 color)
{
        PTD->PDOR |= BIT(RED) | BIT(GREEN) | BIT(BLUE);

        if (color & 0x01)
                PTD->PDOR &=~BIT(RED);
        if (color & 0x02)
                PTD->PDOR &=~BIT(GREEN);
        if (color & 0x04)
                PTD->PDOR &=~BIT(BLUE);
}

static void IRQ_Config(void)
{
        FSL_NVIC->ICPR[1] = 1 << (PORTC_IRQn % 32);  /* clr any pending IRQ*/
        FSL_NVIC->ISER[1] = 1 << (PORTC_IRQn % 32);  /* enable IRQ */
        FSL_NVIC->IP[PORTC_IRQn] = 0xA;              /* priority 10 of 0-15*/

        FSL_NVIC->ICPR[1] = 1 << (RTC_Seconds_IRQn % 32);  /* clr any pending IRQ*/
        FSL_NVIC->ISER[1] = 1 << (RTC_Seconds_IRQn % 32);  /* enable IRQ */
        FSL_NVIC->IP[RTC_Seconds_IRQn] = 0xA;              /* priority 10 of 0-15*/
}

编译运行,即可见LED灯的颜色以1S的时间间隔交替交替变化

针对上面遇到的问题,可以在SystemInit函数中追加:

SIM->LPOCLKS = (0x1 << 4) | 0x03;

然后再RTC_init函数中做如下修改:

RTC->CR = 0x00000004;   //RTC->CR = 0x000000084;

目前试过SIM_LPOCLKS[RTCCLKSEL]的四种配置,发现RTC_CLKIN无效:
S32K144 EVB之RTC_第2张图片

另外,如果想在应用程序中去修改在特权模式下访问的核心寄存器,则需要利用到SVC中断,查找资料学习ing

你可能感兴趣的:(S32K144)