S32K144 EVB之UART

开发环境

IAR7.8 + S32K144-EVB

本次使用到的是UART1,可以使用OpenSDA的mini USB口直接接到电脑上即可看到串口信息:
S32K144 EVB之UART_第1张图片
S32K144 EVB之UART_第2张图片
使用到的端口引脚为:
UART_TX -> PTC6
UART_RX -> PTC7

step1:配置GPIO

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

PORTC->PCR[6] |= PORT_PCR_MUX(2); /* Port C6: MUX = ALT2,UART1 TX */
PORTC->PCR[7] |= PORT_PCR_MUX(2); /* Port C7: MUX = ALT2,UART1 RX */

step2:初始化uart

void LPUART1_init(void)
{
        //config clock source
        PCC->PCCn[PCC_LPUART1_INDEX] &= ~PCC_PCCn_CGC_MASK;
        PCC->PCCn[PCC_LPUART1_INDEX] |= PCC_PCCn_PCS(1) | PCC_PCCn_CGC_MASK;
        //set baud rate:9600 8N1
        LPUART1->BAUD = 0x0F000034;
        LPUART1->CTRL = 0x000C0000;
        //enable TIE and RIE irq
        LPUART1->CTRL|= 0x00A00000;
}

step3:配置中断及编写中断函数

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

void LPUART1_RxTx_IRQHandler(void)
{
        unsigned char rev = 0;

        //TDRE
        if (LPUART1->STAT & 0x00800000) {
                LPUART1->CTRL &=~0x00800000;
        }
        //TC
        if (LPUART1->STAT & 0x00400000) {
                LPUART1->CTRL &=~0x00400000;
        }
        //RDRF
        if (LPUART1->STAT & 0x00200000) {
                rev = LPUART1->DATA;
                LED_light(rev % 8);
        }
}

本次只是做了一个验证UART基本功能的Demo,上电之后会uart会输出一个’>’,然后等待输入,输入%8即为LED灯的颜色,具体见”S32K144 EVB之GPIO”

完整版Code如下:

#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);
void LPUART1_init(void);
void LPUART1_transmit_char(char send);

static u8 color = 0;

int main(void)
{
    u32 cnt;

    cnt = 0;

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

        LPUART1_init(); /* Initialize LPUART @ 9600*/
        LPUART1_transmit_char('>'); /* Transmit prompt character*/

        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 LPUART1_RxTx_IRQHandler(void)
{
        unsigned char rev = 0;

        //TDRE
        if (LPUART1->STAT & 0x00800000) {
                LPUART1->CTRL &=~0x00800000;
        }
        //TC
        if (LPUART1->STAT & 0x00400000) {
                LPUART1->CTRL &=~0x00400000;
        }
        //RDRF
        if (LPUART1->STAT & 0x00200000) {
                rev = LPUART1->DATA;
                LED_light(rev % 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));

        /**
         * UART_TX -> PTC6
         * UART_RX -> PTC7
         */
        PORTC->PCR[6] |= PORT_PCR_MUX(2); /* Port C6: MUX = ALT2,UART1 TX */
        PORTC->PCR[7] |= PORT_PCR_MUX(2); /* Port C7: MUX = ALT2,UART1 RX */
}

/**
 * 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)
{       
        //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*/

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

void LPUART1_init(void)
{
        //config clock source
        PCC->PCCn[PCC_LPUART1_INDEX] &= ~PCC_PCCn_CGC_MASK;
        PCC->PCCn[PCC_LPUART1_INDEX] |= PCC_PCCn_PCS(1) | PCC_PCCn_CGC_MASK;
        //set baud rate:9600 8N1
        LPUART1->BAUD = 0x0F000034;
        LPUART1->CTRL = 0x000C0000;
        //enable TIE and RIE irq
        LPUART1->CTRL|= 0x00A00000;
}

void LPUART1_transmit_char(char send) 
{
        LPUART1->DATA=send;
        //enable TIE irq
        LPUART1->CTRL |= 0x00800000;
}

你可能感兴趣的:(S32K144)