power_main.c

来自「Freescale MCF5445evb 参考测试代码」· C语言 代码 · 共 559 行

C
559
字号
/* * File:        main.c * Purpose:     Main process * * Notes:        */#include "common.h"#include "uif.h"#include "power.h"#include "clock.h"#include "uart.h"/********************************************************************//* * Setup user interface */const char PROMPT[] = "PWR> ";UIF_CMD UIF_CMDTAB[] ={    UIF_CMDS_ALL    {"stop",    0,1,0,cmd_stop,     "STOP low-power mode", ""},    {"run",     0,0,0,cmd_run,      "RUN low-power mode", ""},    {"wait",    0,0,0,cmd_wait,     "WAIT low-power mode", ""},    {"doze",    0,0,0,cmd_doze,     "DOZE low-power mode", ""},    {"loop",    0,1,0,cmd_loop,     "Enter loop", "<one|nop|tpf|poll>"},    {"psd",     0,1,0,cmd_psd,      "Peripheral shutdown <periph #>", ""},    {"ppu",     0,1,0,cmd_ppu,      "Peripheral power-up <periph #>", ""},    {"ddr",     0,0,0,cmd_ddr,      "Exercise DDR2", ""},    {"rd",      0,0,2,cmd_rd,       "Register Disply",""},    {"appmax",  0,0,2,cmd_appmax,   "Application: max",""},};const int UIF_NUM_CMD    = UIF_CMDTAB_SIZE;UIF_SETCMD UIF_SETCMDTAB[] = {    {"sysclk", 0,1,setshow_sysclk,""},    {"icache", 0,1,setshow_icache,""},    {"bcache", 0,1,setshow_bcache,""},    {"dcache", 0,1,setshow_dcache,""},};const int UIF_NUM_SETCMD = UIF_SETCMDTAB_SIZE;/* * Set/show and global variables */int icache      = OFF;int bcache      = OFF;int dcache      = OFF;UART_INFO uart_info = {    .chan       = TERM_PORT,    .baud       = TERM_BAUD,    .dbits      = UART_DATA_BITS_8,    .sbits      = UART_STOP_BITS_1,    .parity     = UART_PARTIY_NONE,    .dma        = FALSE,    .ffull      = FALSE,    .flow       = FALSE,    .clkfreq    = FBUS,    .clksrc     = NULL,};/********************************************************************/voidmain (void){    extern char __DATA_ROM[];    extern char __DATA_RAM[];    /* Determine if execution is from Flash or SRAM */    //running_in_sram = (__DATA_ROM == __DATA_RAM) ? TRUE : FALSE;    /* Enable platform interrupts */    platform_enable_interrupts();    mcf5xxx_set_handler(64 + 4, (ADDRESS)sw6_handler);    mcf5xxx_set_handler(64 + 7, (ADDRESS)sw7_handler);    printf("\n");    printf("**************************************************\n");    printf("*                                                *\n");    printf("*           Power Measurement Utility            *\n");    printf("*                                                *\n");    printf("**************************************************\n");    printf(HELPMSG);    printf("\n");    mainloop();}/********************************************************************/voidmainloop (void){    /* Enable interrupts to the core */    mcf5xxx_irq_enable();    while (TRUE)    {        printf(PROMPT);        run_cmd();    }}/********************************************************************/__interrupt__ voidsw6_handler (void){    printf("\nSW6 Handler\n");        /* Clear the interrupt event */    MCF_EPORT_EPFR = MCF_EPORT_EPFR_EPF4;}/********************************************************************/__interrupt__ voidsw7_handler (void){    /* Wait for the switch to deassert */    while (!(MCF_EPORT_EPPDR & MCF_EPORT_EPPDR_EPPD7)) {};    /* Clear the interrupt event */    MCF_EPORT_EPFR = MCF_EPORT_EPFR_EPF7;        mainloop();}/********************************************************************/voidcmd_rd (int argc, char **argv){    printf("PMM Registers:\n");    printf("WCR          = %02X\n", MCF_PMM_WCR);    printf("PPMSR        = %02X\n", MCF_PMM_PPMSR);    printf("PPMCR        = %02X\n", MCF_PMM_PPMCR);    printf("PPMHR0       = %08X\n", MCF_PMM_PPMHR);    printf("PPMLR0       = %08X\n", MCF_PMM_PPMLR);    printf("LPCR         = %02X\n", MCF_PMM_LPCR);}/********************************************************************/voidcmd_stop(int argc, char **argv){    int stpmd, success;        if (argc == 1)        stpmd = 0;    else    {        stpmd = get_value(argv[1],&success,10);        if (success == 0)        {            printf(INVALUE,argv[1]);            return;        }    }    if (stpmd > 3 || stpmd < 0)        stpmd = 0;        printf("Entering STOP mode %d\n", stpmd);    /* Setup the low-power control registers */    MCF_PMM_LPCR = 0    //    | MCF_PMM_LPCR_FWKUP        | MCF_PMM_LPCR_STPMD(stpmd);    MCF_PMM_WCR = 0        | MCF_PMM_WCR_PRILVL(0)        | MCF_PMM_WCR_LPMD_STOP        | MCF_PMM_WCR_ENBWCR;    /* Wait for the UART to finish transmitting */    while(!(MCF_UART_UCSR(TERM_PORT) & MCF_UART_USR_TXEMP));        /* Execute the stop instruction */    stop_2000();}/********************************************************************/voidcmd_run(int argc, char **argv){    printf("Entering RUN mode \n");    /* Setup the low-power control registers */    MCF_PMM_LPCR = 0    //    | MCF_PMM_LPCR_FWKUP        | MCF_PMM_LPCR_STPMD(0);    MCF_PMM_WCR = 0        | MCF_PMM_WCR_PRILVL(0)        | MCF_PMM_WCR_LPMD_RUN        | MCF_PMM_WCR_ENBWCR;    /* Wait for the UART to finish transmitting */    while(!(MCF_UART_UCSR(TERM_PORT) & MCF_UART_USR_TXEMP));        /* Execute the stop instruction */    stop_2000();}/********************************************************************/voidcmd_wait(int argc, char **argv){    printf("Entering WAIT mode \n");    /* Setup the low-power control registers */    MCF_PMM_LPCR = 0    //    | MCF_PMM_LPCR_FWKUP        | MCF_PMM_LPCR_STPMD(0);    MCF_PMM_WCR = 0        | MCF_PMM_WCR_PRILVL(0)        | MCF_PMM_WCR_LPMD_WAIT        | MCF_PMM_WCR_ENBWCR;    /* Wait for the UART to finish transmitting */    while(!(MCF_UART_UCSR(TERM_PORT) & MCF_UART_USR_TXEMP));        /* Execute the stop instruction */    stop_2000();}/********************************************************************/voidcmd_doze(int argc, char **argv){    printf("Entering DOZE mode \n");    /* Setup the low-power control registers */    MCF_PMM_LPCR = 0    //    | MCF_PMM_LPCR_FWKUP        | MCF_PMM_LPCR_STPMD(0);    MCF_PMM_WCR = 0        | MCF_PMM_WCR_PRILVL(0)        | MCF_PMM_WCR_LPMD_DOZE        | MCF_PMM_WCR_ENBWCR;    /* Wait for the UART to finish transmitting */    while(!(MCF_UART_UCSR(TERM_PORT) & MCF_UART_USR_TXEMP));        /* Execute the stop instruction */    stop_2000();}/********************************************************************/voidcmd_psd (int argc, char **argv){    int success, i, ppmhr;    if (argc == 1)    {        printf("Enter a peripheral number to shutdown (99 for all)\n");        return;    }        if (argc == 2)    {        i = get_value(argv[1],&success,10);        if (success == 0 || (i >= 64 && i != 99))        {            printf(INVALUE,argv[1]);            return;        }    }        if (i == 99)    {        /*          * Shutdown everything except UART0, INTC0, IACK, Edge Port,         * Reset Controller/CCM/PM, PLL, and Flexbus         */        MCF_PMM_PPMHR = 0            | MCF_PMM_PPMHR_CD32                | MCF_PMM_PPMHR_CD33            | MCF_PMM_PPMHR_CD34            | MCF_PMM_PPMHR_CD35            | MCF_PMM_PPMHR_CD36        //    | MCF_PMM_PPMHR_CD37        //    | MCF_PMM_PPMHR_CD40            | MCF_PMM_PPMHR_CD41            | MCF_PMM_PPMHR_CD42            | MCF_PMM_PPMHR_CD43            | MCF_PMM_PPMHR_CD44            | MCF_PMM_PPMHR_CD45            | MCF_PMM_PPMHR_CD46            | MCF_PMM_PPMHR_CD47            | MCF_PMM_PPMHR_CD48;        //    | MCF_PMM_PPMHR_CD49                MCF_PMM_PPMLR = 0        //    | MCF_PMM_PPMLR_CD2            | MCF_PMM_PPMLR_CD12            | MCF_PMM_PPMLR_CD13            | MCF_PMM_PPMLR_CD15            | MCF_PMM_PPMLR_CD17        //    | MCF_PMM_PPMLR_CD18            | MCF_PMM_PPMLR_CD19        //    | MCF_PMM_PPMLR_CD21            | MCF_PMM_PPMLR_CD22            | MCF_PMM_PPMLR_CD23        //    | MCF_PMM_PPMLR_CD24            | MCF_PMM_PPMLR_CD25            | MCF_PMM_PPMLR_CD26            | MCF_PMM_PPMLR_CD28            | MCF_PMM_PPMLR_CD29            | MCF_PMM_PPMLR_CD30            | MCF_PMM_PPMLR_CD31;    }    else     {        MCF_PMM_PPMSR = MCF_PMM_PPMSR_SMCD(i);    }}/********************************************************************/voidcmd_ppu (int argc, char **argv){    int success, i, ppmhr;    if (argc == 1)    {        printf("Enter a peripheral number to power up (99 for all)\n");        return;    }        if (argc == 2)    {        i = get_value(argv[1],&success,10);        if (success == 0 || (i >= 64 && i != 99))        {            printf(INVALUE,argv[1]);            return;        }    }        if (i == 99)    {        /* Power-up everything */        MCF_PMM_PPMCR = MCF_PMM_PPMCR_CAMCD;    }    else     {        MCF_PMM_PPMCR = MCF_PMM_PPMCR_CMCD(i);    }}/********************************************************************/voidcmd_ddr (int argc, char **argv){    while (!char_present())    {        power_app_ddr();    }    in_char();}/********************************************************************/voidcmd_appmax (int argc, char **argv){    power_app_max();}/********************************************************************/voidcmd_loop (int argc, char **argv){    if (argc == 1)    {        printf("Enter an argument:\n");        printf("   \"one\" = while(1) loop\n");        printf("   \"nop\"  = nop loop\n");        printf("   \"tpf\"  = tpf loop\n");        printf("   \"poll\" = poll uart\n");    }    else    {        /* Wait for the UART to finish transmitting */        while(!(MCF_UART_UCSR(TERM_PORT) & MCF_UART_USR_TXEMP));        /* Enter the desired loop */        if (strcasecmp(argv[1], "one") == 0)        {            while (1)            {                /* null */            }        }        else if (strcasecmp(argv[1], "nop") == 0)        {            while (1)            {                nop();                nop();                nop();                nop();            }        }        else if (strcasecmp(argv[1], "tpf") == 0)        {            while (1)            {                tpf();                tpf();                tpf();                tpf();            }        }        else if (strcasecmp(argv[1], "poll") == 0)        {            while (!(MCF_UART_USR(TERM_PORT) & MCF_UART_USR_RXRDY))            {                /* null */            }            /* Clear the UART FIFO */            uart_getchar(TERM_PORT);        }        else        {            printf(INVALUE,argv[1]);        }    }}/********************************************************************/voidsetshow_sysclk (int argc, char **argv){    int success;    int pll_flags;        /* Set */    if (argv[2] != NULL)    {        pll_flags = 0        //    | CLOCK_PLL_LOLDIS            | CLOCK_PLL_LOLIRQ;                    if (FREF == 33333333)            pll_flags |= CLOCK_PLL_FBCLK_DIV8;                if (strcasecmp(argv[2], "266") == 0)        {            clock_pll_init(FREF, 266666666, pll_flags, NULL);        }        else if (strcasecmp(argv[2], "233") == 0)        {            clock_pll_init(FREF, 233333333, pll_flags, NULL);        }        else if (strcasecmp(argv[2], "200") == 0)        {            clock_pll_init(FREF, 200000000, pll_flags, NULL);        }        else if (strcasecmp(argv[2], "166") == 0)        {            clock_pll_init(FREF, 166666666, pll_flags, NULL);        }        else        {            printf("Try 266, 233, 200, or 166\n");        }                uart_info.clkfreq = clock_get_fbus();        uart_init(&uart_info);    }    /* Show */    else    {        printf("%d Hz (Flexbus: %d Hz)", clock_get_fsys(), clock_get_ffb());    }}/********************************************************************/voidsetshow_icache (int argc, char **argv){    int success;        /* Set */    if (argv[2] != NULL)    {        if (strcasecmp(argv[2], "on") == 0)        {            icache = ON;            cpu_icache_enable(0, SIZE_2G);        }        else if (strcasecmp(argv[2], "off") == 0)        {            icache = OFF;            cpu_icache_disable();        }        else            printf(INVALUE,argv[2]);    }    /* Show */    else    {        if (icache == ON)            printf("ON");        else            printf("OFF");    }}/********************************************************************/voidsetshow_bcache (int argc, char **argv){    int success;        /* Set */    if (argv[2] != NULL)    {        if (strcasecmp(argv[2], "on") == 0)        {            bcache = ON;            cpu_bcache_enable();        }        else if (strcasecmp(argv[2], "off") == 0)        {            bcache = OFF;            cpu_bcache_disable();        }        else            printf(INVALUE,argv[2]);    }    /* Show */    else    {        if (bcache == ON)            printf("ON");        else            printf("OFF");    }}/********************************************************************/voidsetshow_dcache (int argc, char **argv){    int success;        /* Set */    if (argv[2] != NULL)    {        if (strcasecmp(argv[2], "on") == 0)        {            dcache = ON;            cpu_dcache_enable(0, SIZE_2G, MCF5XXX_CACR_DDCM_CB);        }        else if (strcasecmp(argv[2], "off") == 0)        {            dcache = OFF;            cpu_dcache_disable();        }        else            printf(INVALUE,argv[2]);    }    /* Show */    else    {        if (dcache == ON)            printf("ON");        else            printf("OFF");    }}/********************************************************************/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?