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

📄 lib_at91.c

📁 ARM入门的好帮手.包含了从简单到相对较复杂的程序.
💻 C
📖 第 1 页 / 共 2 页
字号:
//*----------------------------------------------------------------------------
//*      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           : lib_at91.c
//* Object              : bench for the AT91EB01, use PIO in input and
//*                       output libraries definition
//*
//* 1.0 03/09/01 JPP    : Creation
//*----------------------------------------------------------------------------


#include    "periph/pio/lib_pio.h"
#include    "periph/power_saving/lib_power_save.h"
#include    "periph/power_saving/ps40800.h"
#include    "parts/m40800/lib_m40800.h"
#include    "periph/usart/lib_usart.h"
#include    "periph/power_saving/lib_power_save.h"
#include    "periph/timer_counter/tc.h"
#include    "periph/timer_counter/lib_tc.h"
#include    "periph/power_saving/lib_power_save.h"
#include    "periph/stdc/lib_err.h"


/*-----------------*/
/* PIO Controllers */
/*-----------------*/

/* Instantiate PIO Controllers Pointers */
const StructPIO *Pio   = PIO_BASE ;

/* PIO Controller Descriptor */
const PioCtrlDesc PIO_DESC =
{
    PIO_BASE,
    PIO_ID,
    NB_PIO
} ;


/*-------*/
/* USART */
/*-------*/

/* Usart 0 Descriptor */
const UsartDesc USART0_DESC =
{
    USART0_BASE,
    &PIO_DESC,
    PIORXD0,
    PIOTXD0,
    PIOSCK0,
    US0_ID ,
} ;

/* Usart 1 Descriptor */
const UsartDesc USART1_DESC =
{
    USART1_BASE ,
    &PIO_DESC,
    PIORXD1,
    PIOTXD1,
    PIOSCK1,
    US1_ID ,
} ;


/*------------------------*/
/* Timer Counter Channels */
/*------------------------*/

/* Timer Counter Channel 0 Descriptor */
const TCDesc TC0_DESC =
{
    TC0_BASE,
    &PIO_DESC,
    TC0_ID,
    PIOTIOA0,
    PIOTIOB0,
    PIOTCLK0
} ;

/* Timer Counter Channel 1 Descriptor */
const TCDesc TC1_DESC =
{
    TC1_BASE,
    &PIO_DESC,
    TC1_ID,
    PIOTIOA1,
    PIOTIOB1,
    PIOTCLK1
} ;

/* Timer Counter Channel 2 Descriptor */
const TCDesc TC2_DESC =
{
    TC2_BASE,
    &PIO_DESC,
    TC2_ID,
    PIOTIOA2,
    PIOTIOB2,
    PIOTCLK2
} ;

/* Timer Counter Block Descriptor */
const TCBlockDesc TCB_DESC =
{
    &TC0_DESC,
    &TC1_DESC,
    &TC2_DESC,
} ;



//*----------------------------------------------------------------------------
//* Function Name       : at91_pio_open
//* Object              : Setup pins to be Parallel IOs, as managed by the PIO
//* Input Parameters    : <pio_pt> = PIO Controller Descriptor
//*                     : <mask>   = bit mask identifying the PIOs
//*                     : <config> = mask identifying the PIOs configuration
//* Output Parameters   : none
//* Functions called    : none
//*----------------------------------------------------------------------------
void at91_pio_open ( const PioCtrlDesc *pio_pt, u_int mask, u_int config )
//* Begin
{
    u_int x ;
    //* If PIOs required to be output
    if ((config & PIO_SENSE_BIT) != 0 )
    {
        //* Defines the PIOs as output
        pio_pt->pio_base->PIO_OER = mask ;
    }
    //* Else
    else
    {
        //* Defines the PIOs as input
        pio_pt->pio_base->PIO_ODR = mask ;
    }

    //* If PIOs required to be filtered
    if ((config & PIO_FILTER_BIT) != 0 )
    {
        //* Enable the filter on PIOs
        pio_pt->pio_base->PIO_IFER = mask ;
    }
    else
    {
        //* Disable the filter on PIOs
        pio_pt->pio_base->PIO_IFDR = mask ;
    }

    //* If PIOs required to be open-drain
    if ((config & PIO_OPENDRAIN_BIT) != 0 )
    {
        //* Enable the filter on PIOs
        pio_pt->pio_base->PIO_MDER = mask ;
    }
    else
    {
        //* Disable the filter on PIOs
        pio_pt->pio_base->PIO_MDSR = mask ;
    }

    //* If PIOs required for an input change interrupt
    if ((config & PIO_INPUT_IRQ_BIT) != 0 )
    {
        //* Remove any interrupt */
        x = pio_pt->pio_base->PIO_ISR ;
        //* Enable the Input Change Interrupt on PIOs
        pio_pt->pio_base->PIO_IER = mask ;
    }
    else
    {
        //* Disable the Input Change Interrupt on PIOs
        pio_pt->pio_base->PIO_IDR = mask ;
    }

    //* Defines the pins to be controlled by PIO Controller
    pio_pt->pio_base->PIO_PER = mask ;

//* End
}
//*----------------------------------------------------------------------------
//* Function Name       : at91_pio_close
//* Object              : Cancel PIO Controller handling from pins managed by
//*                       a peripheral
//* Input Parameters    : <pio_pt> = PIO Descriptor pointer
//*                     : <mask>   = defines the pins to managed by peripheral
//* Output Parameters   : none
//* Functions called    : none
//*----------------------------------------------------------------------------
void at91_pio_close ( const PioCtrlDesc *pio_pt, u_int mask )
//* Begin
{
    //* Define PIOs to be controlled by peripherals
    pio_pt->pio_base->PIO_PDR = mask ;

//* End
}

//*----------------------------------------------------------------------------
//* Function Name       : at91_pio_write
//* Object              : Write a data on required PIOs
//* Input Parameters    : <pio_pt> = PIO Controller Descriptor Address
//*                     : <mask>   = defines work pins
//*                     : <state>  = defines set ( =0) or clear ( #0)
//* Output Parameters   : none
//* Functions called    : none
//*----------------------------------------------------------------------------
void at91_pio_write ( const PioCtrlDesc *pio_pt, u_int mask, u_int state )
//* Begin
{
    if (state == PIO_CLEAR_OUT )
    {
        //* Clear PIOs with data at 0 in CODR (Clear Output Data Register)
        pio_pt->pio_base->PIO_CODR = mask ;
    }
    else
    {
        //* Set PIOs with data at 1 in SODR (Set Output Data Register)
        pio_pt->pio_base->PIO_SODR = mask ;
    }

//* End
}
//*----------------------------------------------------------------------------
//* Function Name       : at91_pio_read
//* Object              : Read the state of the PIO pins
//* Input Parameters    : <pio_pt> = PIO Controller Descriptor Address
//* Output Parameters   : defines the pins value
//* Functions called    : at91_clock_get_status, at91_clock_open,
//*                       at91_clock_close
//*----------------------------------------------------------------------------
u_int at91_pio_read ( const PioCtrlDesc *pio_pt)

//* Begin
{

    u_int   return_val ;
    u_int   save_clock ;

    //* Get clock Status
    save_clock = at91_clock_get_status ( pio_pt->periph_id ) ;

    //* Enable the PIO Clock
    at91_clock_open ( pio_pt->periph_id ) ;

    //* Read the Data in input of the PIO Controller
    return_val = pio_pt->pio_base->PIO_PDSR ;

    //* If PIO controller clock was disabled
    if (( save_clock & (1 << pio_pt->periph_id)) == 0 )
    {
        //* Disable the PIO Clock
        at91_clock_close ( pio_pt->periph_id ) ;
    }

    return (return_val);

//* End
}

//*----------------------------------------------------------------------------
//* Function Name       : at91_pio_set_mode
//* Object              : Modify the mode of PIOs
//* Input Parameters    : <pio_pt> = PIO Controller Descriptor
//*                     : <mask>   = bit mask identifying the PIOs
//*                     : <mode> = the required PIOs configuration
//* Output Parameters   : none
//* Functions called    : none
//*----------------------------------------------------------------------------
void at91_pio_set_mode ( const PioCtrlDesc *pio_pt, u_int mask, u_int mode )
//* Begin
{
    //* If PIOs required to be filtered
    if ((mode & PIO_FILTER_BIT) != 0 )
        //* Enable the filter on PIOs
        pio_pt->pio_base->PIO_IFER = mask ;
    //* Else
    else
        //* Disable the filter on PIOs
        pio_pt->pio_base->PIO_IFDR = mask ;

    //* If PIOs required to be open-drain
    if ((mode & PIO_OPENDRAIN_BIT) != 0 )
        //* Enable the filter on PIOs
        pio_pt->pio_base->PIO_MDER = mask ;
    //* Else
    else
        //* Disable the filter on PIOs
        pio_pt->pio_base->PIO_MDSR = mask ;
//* End
}
//*----------------------------------------------------------------------------
//* Function Name       : at91_clock_set_mode
//* Object              : Set System Clock Mode.
//* Input Parameters    : <mode> = mode to define
//* Output Parameters   : none
//* Functions called    : at91_error
//*----------------------------------------------------------------------------
void at91_clock_set_mode ( u_int mode )
//* Begin
{
    //* Depending on the required mode
    switch (mode)
    {
        //* Idle mode required
        case PS_MODE_IDLE:
            //* Write the System Clock Disable Register
            PS_BASE->PS_CR = PS_ARM7DIS ;
            break ;

        //* Active all peripheral clocks
        case PS_ALL_PERIPH_ACTIVE:
            //* Enable all the peripheral clocks
            PS_BASE->PS_PCER = 0xFFFFFFFF ;
            break ;

        //* Desactive all peripheral clocks
        case PS_ALL_PERIPH_INACTIVE:
            //* Disable all the peripheral clocks
            PS_BASE->PS_PCDR = 0xFFFFFFFF ;
            break ;

    //* EndSwitch
    }
}
//* End

//*----------------------------------------------------------------------------
//* Function Name       : at91_clock_open
//* Object              : Enable the peripheral clock
//* Input Parameters    : <periph_id> = peripheral identifier
//* Output Parameters   : none
//* Functions called    : none
//*----------------------------------------------------------------------------
void at91_clock_open ( u_int periph_id )
//* Begin
{
    //* Write the Peripheral Clock Enable Register
    PS_BASE->PS_PCER = (1<<periph_id) ;
//* End
}

//*----------------------------------------------------------------------------
//* Function Name       : at91_clock_close
//* Object              : Disable the clock of a Peripheral
//* Input Parameters    : <periph_id> = peripheral identifier
//* Output Parameters   : none
//* Functions called    : none
//*----------------------------------------------------------------------------
void at91_clock_close ( u_int periph_id )
//* Begin
{
    //* Write the Peripheral Clock Disable Register
    PS_BASE->PS_PCDR = (1<<periph_id) ;
//* End
}

//*----------------------------------------------------------------------------
//* Function Name       : at91_clock_get_status
//* Object              : Return the Peripheral clock status
//* Input Parameters    : <periph_id> = peripheral identifier
//* Output Parameters   : none
//* Functions called    : none
//*----------------------------------------------------------------------------
u_int at91_clock_get_status ( u_int periph_id )
//* Begin
{
    //* Return the Peripheral Clock Status Register
    return ( PS_BASE->PS_PCSR & (1<<periph_id) ) ;
//* End
}

//*----------------------------------------------------------------------------
//* Function Name       : at91_usart_open
//* Object              : Initialize an USART.
//* Input Parameters    : <usart_pt>  = the USART to initialize
//*                     : <mode>      = the Mode Register to be programmed
//*                     : <speed>     = the BRDR to be programmed
//*                     : <timeguard> = the US_TTGR to be programmed
//* Output Parameters   : None
//* Functions called    : at91_clock_open, at91_pio_close
//*----------------------------------------------------------------------------
void at91_usart_open ( const UsartDesc *usart_pt ,
                       u_int mode ,
                       u_int speed ,
                       u_int timeguard )
//* Begin
{
    //* Enable the clock
    at91_clock_open ( usart_pt->periph_id ) ;

⌨️ 快捷键说明

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