⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eb42_consume.c

📁 ARM入门的好帮手.包含了从简单到相对较复杂的程序.
💻 C
字号:
//*-----------------------------------------------------------------------------
//*      ATMEL Microcontroller Software Support  -  ROUSSET  -
//*-----------------------------------------------------------------------------
//* The software is delivered "AS IS" without warranty or condition of any
//* kind, either express, implied or statutory. This includes without
//* limitation any warranty or condition with respect to merchantability or
//* fitness for any particular purpose, or against the infringements of
//* intellectual property rights of others.
//*-----------------------------------------------------------------------------
//* File Name           : eb42_consume.c
//* Object              : AT91M42800 - Consumption measurement
//* Exported resources  : main
//* Imported resources  : ExecuteTest
//*
//* 1.0 29/07/01  PF    : Creation
//*-----------------------------------------------------------------------------

/*----- Files to be included Definition -----*/

#include <stdio.h>
#include <stdlib.h>
#include "parts\m42800\lib_m42800.h"
#include "targets\eb42\eb42.h"
#include "clock_switching_driver.h"


/*----- Types and Constants Definition -----*/

#define CHIP_ID            "AT91M42800"
#define TARGET_ID          "EB42"

#define ARM_MODE        0
#define THUMB_MODE      1

#define MOSC_CONFIG     ( APMC_MOSC_EN | (34<<B_OSCOUNT)|(0x1<<B_PLLCOUNT) )

#define PLL         0x2     //* low frequency oscillator output => MCK
#define SLOW_OSC    0x0     //* PLL output =MCK

#define MUL_x3      0x3
#define MUL_x2      0x2
#define NO_MUL      0x1

/* Global Variables */


/*----- Imported Resources Definition -----*/
extern void ExecuteArmTest ( void ) ;
extern void ExecuteThumbTest ( void ) ;
extern void ExecuteTest ( u_int *address, u_int arm ) ;

//* Assembler Interrupt
extern void ext_irq_asm_handler ( void ) ;

//*-----------------------------------------------------------------------------
//* Function Name       : freq_choice
//* Object              : Choice of the AT91M42800
//* Input Parameters    : none
//* Output Parameters   : none
//* Functions called    : none
//*-----------------------------------------------------------------------------
void freq_choice ( void )
//* Begin
{
        u_int freq ;
        u_int mck_select ;
        u_int pmc_status ;

        //* Frequency choice
        printf ( "\n Select the desired Frequency :\n" ) ;
        printf ( "\n  1 - 48MHz (with PLL)\n" ) ;
        printf ( "  2 - 32,768MHz (with PLL)\n\n" ) ;
        printf ( "  3 - 16MHz (with PLL)\n" ) ;
        printf ( "  4 - 8MHz (with PLL)\n" ) ;
        printf ( "  5 - 4MHz (with PLL)\n\n" ) ;
        printf ( "  6 - 1MHz (with PLL)\n" ) ;
        printf ( "  7 - 250KHz (with PLL)\n" ) ;
        printf ( "  8 - 125KHz (with PLL)\n" ) ;
        printf ( "  9 - 32KHz (Low frequency oscillator)\n" ) ;

        printf ( "\n   Enter your choice : " ) ;
        scanf ("%d", &freq ) ;

        switch ( freq )
        {
            case 1 :    //* 48MHz (with PLL)

                mck_clock_speed ( PLL, 1464, PMC_PRES_NONE ) ;

                break ;

            case 2 :    //* 32MHz (with PLL)

                mck_clock_speed ( PLL, 999, PMC_PRES_NONE) ;

                break ;

            case 3 :    //* 16MHz (with PLL)

                mck_clock_speed ( PLL, 499, PMC_PRES_NONE ) ;

                break ;

            case 4 :    //* 8MHz (with PLL)

                mck_clock_speed ( PLL, 249, PMC_PRES_NONE ) ;

                break ;

            case 5 :    //* 4MHz (with PLL)

                mck_clock_speed ( PLL, 124, PMC_PRES_NONE ) ;

                break ;

            case 6 :    //* 1MHz (PLL : 8MHz with Prescaler at 8)

                mck_clock_speed ( PLL, 249, PMC_PRES_DIV8 ) ;

                break ;

            case 7 :    //* 250KHz (PLL : 8MHz with Prescaler at 32)

                mck_clock_speed ( PLL, 249, PMC_PRES_DIV32 ) ;

                break ;

            case 8 :    //* 125KHz (PLL : 8MHz with Prescaler at 64)

                mck_clock_speed ( PLL, 249, PMC_PRES_DIV64 ) ;

                break ;

            case 9 :    //* 32KHz ( Slow Clock )

                mck_clock_speed ( SLOW_OSC, NO_MUL, PMC_PRES_NONE ) ;

                break ;

            default :

                break ;
        }

        printf ( "\n Switch to Slow Clock :\n" ) ;
        printf ( "1 - No (with PLL Activeted)\n" ) ;
        printf ( "2 - Yes (with PLL Activeted)\n\n" ) ;

        scanf ("%d", &mck_select ) ;

        switch ( mck_select )
        {
            case 1 : /* No (with PLL Activeted) */

                pmc_status = at91_clock_generator_state () ;  //* PMC_CGMR reading NEW

                break ;

            case 2 : /* Yes (with PLL Activeted) */

                pmc_status = ((at91_clock_generator_state () & 0xFFFFFF0F ) | PMC_CSS_LF ) ;  //* PMC_CGMR reading NEW
                at91_clock_generator_mode ( pmc_status | PMC_CSS_LF ) ;
                printf("PMC_CGMR : 0x%x \n", at91_clock_generator_state ()) ;

                break ;

        }

}

//*-----------------------------------------------------------------------------
//* Function Name       : PCA_choice
//* Object              : Peripheral clocks activated or not choice
//* Input Parameters    : none
//* Output Parameters   : none
//* Functions called    : none
//*-----------------------------------------------------------------------------
void PCA_choice ( void )
//* Begin
{
    int     choice ;

        printf ( "\n  1 - All peripheral clocks activated\n" ) ;
        printf ( "  2 - All peripheral clocks deactivated \n" ) ;
        printf ( "  3 - USART 0 clock activated\n" ) ;
        printf ( "  4 - USART 0 and 1 clock activated\n" ) ;
        printf ( "  5 - SPIA clock activated\n" ) ;
        printf ( "  6 - SPIA and SPIB clock activated\n" ) ;
        printf ( "  7 - Timer Counter Channel 0 clock activated\n" ) ;
        printf ( "  8 - Timer Counter 0, 1 and 3 clock activated\n" ) ;
        printf ( "  9 - PIOA clock activated\n" ) ;
        printf ( " 10 - PIOA and PIOB clock activated\n" ) ;
        printf ( "\n   Enter your choice : By default all peripheral clocks are stopped\n" ) ;
        scanf ("%d", &choice ) ;

        switch ( choice )
        {
        case 1 : /* All peripheral clocks activated */

           at91_clock_set_mode ( PS_ALL_PERIPH_ACTIVE ) ;

       break ;

        case 2 : /* All peripheral clocks deactivated */

           at91_clock_set_mode ( PS_ALL_PERIPH_INACTIVE ) ;

       break ;


        case 3 : /* USART 0 clock activated */

            at91_clock_set_mode ( PS_ALL_PERIPH_INACTIVE ) ;
            at91_clock_open  ( US0_ID  ) ;

       break ;


        case 4 : /* USART 0 and 1 clock activated */

            at91_clock_set_mode ( PS_ALL_PERIPH_INACTIVE ) ;
            at91_clock_open ( US0_ID ) ;
            at91_clock_open ( US1_ID ) ;

        break ;

        case 5 : /* SPIA clock activated */

        at91_clock_set_mode ( PS_ALL_PERIPH_INACTIVE ) ;
        at91_clock_open  ( SPIA_ID  ) ;

        break ;


        case 6 : /* SPIA and SPIB clock activated */

             at91_clock_set_mode ( PS_ALL_PERIPH_INACTIVE ) ;
             at91_clock_open  ( SPIA_ID ) ;
             at91_clock_open  ( SPIB_ID ) ;

        break ;

        case 7 : /* Timer Counter Channel 0 clock activated */

             at91_clock_set_mode ( PS_ALL_PERIPH_INACTIVE ) ;
             at91_clock_open  ( TC0_ID ) ;

        break ;


        case 8 : /* Timer Counter 0, 1 and 3 clock activated */

             at91_clock_set_mode ( PS_ALL_PERIPH_INACTIVE ) ;
             at91_clock_open  ( TC0_ID ) ;
             at91_clock_open  ( TC1_ID ) ;
             at91_clock_open  ( TC2_ID ) ;

        break ;


        case 9 : /* PIOA clock activated */

             at91_clock_set_mode ( PS_ALL_PERIPH_INACTIVE ) ;
             at91_clock_open  ( PIOA_ID ) ;

        break ;


        case 10 : /* PIOA and PIOB clock activated */

             at91_clock_set_mode ( PS_ALL_PERIPH_INACTIVE ) ;
             at91_clock_open  ( PIOA_ID ) ;
             at91_clock_open  ( PIOB_ID ) ;

        break ;


        default : /* Default Mode */

             at91_clock_set_mode ( PS_ALL_PERIPH_INACTIVE ) ;

        break ;

    }
}


//*-----------------------------------------------------------------------------
//* Function Name       : ext_irq_c_handler
//* Object              : External Interrupt C handler
//* Input Parameters    : none
//* Output Parameters   : none
//* Functions called    : None
//*-----------------------------------------------------------------------------
void ext_irq_c_handler ( void )
//* Begin
{
    at91_clock_open ( PIOA_ID ) ;
    *(u_int*)0xFFE00130 = 0 ;
}


//*-----------------------------------------------------------------------------
//* Function Name       : main
//* Object              : AT91M42800 Consume Test
//* Input Parameters    : none
//* Output Parameters   : none
//* Functions called    : None
//*-----------------------------------------------------------------------------
int main( void )
//* Begin
{
    int         nws = 0 ;
    int         resp = 0 ;

    StructPIO   *pioa_pt = PIOA_BASE ;
    StructPIO   *piob_pt = PIOB_BASE ;
    StructEBI   *ebi_pt = EBI_BASE ;



    at91_clock_open ( PIOA_ID ) ;
    at91_clock_open ( PIOB_ID ) ;


    pioa_pt -> PIO_ODR = 0x00004488 ;
    pioa_pt -> PIO_OER = 0xFFFFBB77 ;

    piob_pt -> PIO_ODR = 0x002300C0 ;
    piob_pt -> PIO_OER = 0xFFDCFF3F ;

    //* Enable the IRQ0 in interrupt controller
    at91_extirq_open (&IRQ0_DESC , 7 , AIC_SRCTYPE_EXT_POSITIVE_EDGE , ext_irq_asm_handler ) ;

    //* Repeat for ever
    for (;;)
    {
        //* Display Utility Header
        printf ( "\n**** %s Consumption Measurement Utility on %s board ****\n", CHIP_ID, TARGET_ID ) ;

        //* Choice menu
        printf ( "\n  1 - Idle mode\n" ) ;
        printf ( "  2 - Internal memory working mode in ARM code\n" ) ;
        printf ( "  3 - Internal memory working mode in Thumb code\n" ) ;
        printf ( "  4 - External memory working mode in ARM code\n" ) ;
        printf ( "  5 - External memory working mode in Thumb code\n" ) ;

        printf ( "\n   Enter your choice : " ) ;
        scanf ("%d", &resp ) ;

        switch ( resp )
        {
            case 1 :   //* Idle mode

                printf ( "\n\n\n*** Idle mode ***\n" ) ;
                PCA_choice ( ) ;
                printf ( "\n\n*** To return to the main menu, press the SW2/IRQ0 push button  ***\n" ) ;
                freq_choice () ;
                at91_clock_set_mode ( PS_MODE_IDLE ) ;
                break ;

            case 2 :    //* Internal memory working mode in ARM code

                printf ( "\n\n\n*** Internal memory working mode in ARM code ***\n" ) ;
                PCA_choice ( ) ;
                printf ( "\n\n*** To return to the main menu, press the SW2/IRQ0 push button ***\n" ) ;
                freq_choice () ;
                ExecuteTest ( (u_int *)0x100, ARM_MODE ) ;

                break ;

            case 3 :    //* Internal memory working mode in Thumb code

                printf ( "\n\n\n*** Internal memory working mode in Thumb code ***\n" ) ;
                PCA_choice ( ) ;
                printf ( "\n\n*** To return to the main menu, press the SW2/IRQ0 push button  ***\n" ) ;
                freq_choice () ;
                ExecuteTest ( (u_int *)0x100, THUMB_MODE ) ;

                break ;

            case 4 :    //* External memory working mode in ARM code

                do
                {
                    printf ( "\n\n   Enter the wait states number [0..8] : " ) ;
                    scanf ( "%d", &nws ) ;
                }
                while ( ( nws < 0 ) || ( nws > 8 ) ) ;

                if ( nws == 0 )
                {
                    ebi_pt->EBI_MCR |= EBI_DRP_EARLY ;
                    ebi_pt->EBI_CSR[1] &= ~EBI_WSE ;
                }
                else
                {
                    ebi_pt->EBI_CSR[1] &= ~EBI_NWS ;
                    ebi_pt->EBI_CSR[1] |= ( EBI_WSE | (( nws - 1 ) << 2 ) ) ;
                }

                printf ( "\n\n\n*** External memory %d wait states working mode in ARM code ***\n", nws ) ;
                PCA_choice ( ) ;
                printf ( "\n\n*** To return to the main menu, press the SW2/IRQ0 push button  ***\n" ) ;
                freq_choice () ;
                ExecuteTest ( (u_int *)0x2020000, ARM_MODE ) ;
                if ( nws == 0 )
                {
                    ebi_pt->EBI_CSR[1] |= EBI_WSE ;
                    ebi_pt->EBI_MCR &= ~EBI_DRP_EARLY ;
                }

                break ;

            case 5 :    //* External memory working mode in Thumb code

                do
                {
                    printf ( "\n\n   Enter the wait states number [0..8] : " ) ;
                    scanf ( "%d", &nws ) ;
                }
                while ( ( nws < 0 ) || ( nws > 8 ) ) ;

                if ( nws == 0 )
                {
                    ebi_pt->EBI_MCR |= EBI_DRP_EARLY ;
                    ebi_pt->EBI_CSR[1] &= ~EBI_WSE ;
                }
                else
                {
                    ebi_pt->EBI_CSR[1] &= ~nws ;
                    ebi_pt->EBI_CSR[1] |= ( EBI_WSE | (( nws - 1 ) << 2 ) ) ;
                }

                printf ( "\n\n\n*** External memory %d wait states working mode in Thumb code ***\n", nws ) ;
                PCA_choice ( ) ;
                printf ( "\n\n*** To return to the main menu, press the SW2/IRQ0 push button  ***\n" ) ;
                freq_choice () ;
                ExecuteTest ( (u_int *)0x2020000, THUMB_MODE ) ;
                if ( nws == 0 )
                {
                    ebi_pt->EBI_CSR[1] |= EBI_WSE ;
                    ebi_pt->EBI_MCR &= ~EBI_DRP_EARLY ;
                }

                break ;

            default :

                break ;
        }
    }
    return(0);
}

⌨️ 快捷键说明

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