mirror of
https://github.com/FreeRTOS/FreeRTOS-Kernel.git
synced 2025-08-19 09:38:32 -04:00
Atmel provided hardware specifics.
This commit is contained in:
parent
5e6d50864c
commit
efc8243397
73 changed files with 24720 additions and 0 deletions
336
Demo/Common/drivers/Atmel/at91lib/peripherals/pio/pio.c
Normal file
336
Demo/Common/drivers/Atmel/at91lib/peripherals/pio/pio.c
Normal file
|
@ -0,0 +1,336 @@
|
|||
/* ----------------------------------------------------------------------------
|
||||
* ATMEL Microcontroller Software Support
|
||||
* ----------------------------------------------------------------------------
|
||||
* Copyright (c) 2008, Atmel Corporation
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the disclaimer below.
|
||||
*
|
||||
* Atmel's name may not be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
*
|
||||
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
* ----------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Headers
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
#include "pio.h"
|
||||
#include <board.h>
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Internal definitions
|
||||
//------------------------------------------------------------------------------
|
||||
/// \internal Returns the current value of a register.
|
||||
#define READ(peripheral, register) (peripheral->register)
|
||||
/// \internal Modifies the current value of a register.
|
||||
#define WRITE(peripheral, register, value) (peripheral->register = value)
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Internal functions
|
||||
//------------------------------------------------------------------------------
|
||||
//------------------------------------------------------------------------------
|
||||
/// Configures one or more pin(s) of a PIO controller as being controlled by
|
||||
/// peripheral A. Optionally, the corresponding internal pull-up(s) can be
|
||||
/// enabled.
|
||||
/// \param pio Pointer to a PIO controller.
|
||||
/// \param mask Bitmask of one or more pin(s) to configure.
|
||||
/// \param enablePullUp Indicates if the pin(s) internal pull-up shall be
|
||||
/// configured.
|
||||
//------------------------------------------------------------------------------
|
||||
static void PIO_SetPeripheralA(AT91S_PIO *pio,
|
||||
unsigned int mask,
|
||||
unsigned char enablePullUp)
|
||||
{
|
||||
// Disable interrupts on the pin(s)
|
||||
WRITE(pio, PIO_IDR, mask);
|
||||
|
||||
// Enable the pull-up(s) if necessary
|
||||
if (enablePullUp) {
|
||||
|
||||
WRITE(pio, PIO_PPUER, mask);
|
||||
}
|
||||
else {
|
||||
|
||||
WRITE(pio, PIO_PPUDR, mask);
|
||||
}
|
||||
|
||||
// Configure pin
|
||||
WRITE(pio, PIO_ASR, mask);
|
||||
WRITE(pio, PIO_PDR, mask);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
/// Configures one or more pin(s) of a PIO controller as being controlled by
|
||||
/// peripheral A. Optionally, the corresponding internal pull-up(s) can be
|
||||
/// enabled.
|
||||
/// \param pio Pointer to a PIO controller.
|
||||
/// \param mask Bitmask of one or more pin(s) to configure.
|
||||
/// \param enablePullUp Indicates if the pin(s) internal pull-up shall be
|
||||
/// configured.
|
||||
//------------------------------------------------------------------------------
|
||||
static void PIO_SetPeripheralB(AT91S_PIO *pio,
|
||||
unsigned int mask,
|
||||
unsigned char enablePullUp)
|
||||
{
|
||||
// Disable interrupts on the pin(s)
|
||||
WRITE(pio, PIO_IDR, mask);
|
||||
|
||||
// Enable the pull-up(s) if necessary
|
||||
if (enablePullUp) {
|
||||
|
||||
WRITE(pio, PIO_PPUER, mask);
|
||||
}
|
||||
else {
|
||||
|
||||
WRITE(pio, PIO_PPUDR, mask);
|
||||
}
|
||||
|
||||
// Configure pin
|
||||
WRITE(pio, PIO_BSR, mask);
|
||||
WRITE(pio, PIO_PDR, mask);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
/// Configures one or more pin(s) or a PIO controller as inputs. Optionally,
|
||||
/// the corresponding internal pull-up(s) and glitch filter(s) can be
|
||||
/// enabled.
|
||||
/// \param pio Pointer to a PIO controller.
|
||||
/// \param mask Bitmask indicating which pin(s) to configure as input(s).
|
||||
/// \param enablePullUp Indicates if the internal pull-up(s) must be enabled.
|
||||
/// \param enableFilter Indicates if the glitch filter(s) must be enabled.
|
||||
//------------------------------------------------------------------------------
|
||||
static void PIO_SetInput(AT91S_PIO *pio,
|
||||
unsigned int mask,
|
||||
unsigned char enablePullUp,
|
||||
unsigned char enableFilter)
|
||||
{
|
||||
// Disable interrupts
|
||||
WRITE(pio, PIO_IDR, mask);
|
||||
|
||||
// Enable pull-up(s) if necessary
|
||||
if (enablePullUp) {
|
||||
|
||||
WRITE(pio, PIO_PPUER, mask);
|
||||
}
|
||||
else {
|
||||
|
||||
WRITE(pio, PIO_PPUDR, mask);
|
||||
}
|
||||
|
||||
// Enable filter(s) if necessary
|
||||
if (enableFilter) {
|
||||
|
||||
WRITE(pio, PIO_IFER, mask);
|
||||
}
|
||||
else {
|
||||
|
||||
WRITE(pio, PIO_IFDR, mask);
|
||||
}
|
||||
|
||||
// Configure pin as input
|
||||
WRITE(pio, PIO_ODR, mask);
|
||||
WRITE(pio, PIO_PER, mask);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
/// Configures one or more pin(s) of a PIO controller as outputs, with the
|
||||
/// given default value. Optionally, the multi-drive feature can be enabled
|
||||
/// on the pin(s).
|
||||
/// \param pio Pointer to a PIO controller.
|
||||
/// \param mask Bitmask indicating which pin(s) to configure.
|
||||
/// \param defaultValue Default level on the pin(s).
|
||||
/// \param enableMultiDrive Indicates if the pin(s) shall be configured as
|
||||
/// open-drain.
|
||||
/// \param enablePullUp Indicates if the pin shall have its pull-up activated.
|
||||
//------------------------------------------------------------------------------
|
||||
static void PIO_SetOutput(AT91S_PIO *pio,
|
||||
unsigned int mask,
|
||||
unsigned char defaultValue,
|
||||
unsigned char enableMultiDrive,
|
||||
unsigned char enablePullUp)
|
||||
{
|
||||
// Disable interrupts
|
||||
WRITE(pio, PIO_IDR, mask);
|
||||
|
||||
// Enable pull-up(s) if necessary
|
||||
if (enablePullUp) {
|
||||
|
||||
WRITE(pio, PIO_PPUER, mask);
|
||||
}
|
||||
else {
|
||||
|
||||
WRITE(pio, PIO_PPUDR, mask);
|
||||
}
|
||||
|
||||
// Enable multi-drive if necessary
|
||||
if (enableMultiDrive) {
|
||||
|
||||
WRITE(pio, PIO_MDER, mask);
|
||||
}
|
||||
else {
|
||||
|
||||
WRITE(pio, PIO_MDDR, mask);
|
||||
}
|
||||
|
||||
// Set default value
|
||||
if (defaultValue) {
|
||||
|
||||
WRITE(pio, PIO_SODR, mask);
|
||||
}
|
||||
else {
|
||||
|
||||
WRITE(pio, PIO_CODR, mask);
|
||||
}
|
||||
|
||||
// Configure pin(s) as output(s)
|
||||
WRITE(pio, PIO_OER, mask);
|
||||
WRITE(pio, PIO_PER, mask);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Exported functions
|
||||
//------------------------------------------------------------------------------
|
||||
//------------------------------------------------------------------------------
|
||||
/// Configures a list of Pin instances, which can either hold a single pin or a
|
||||
/// group of pins, depending on the mask value; all pins are configured by this
|
||||
/// function.
|
||||
/// Returns 1 if the configuration has been performed successfully; otherwise 0.
|
||||
/// \param list Pointer to a list of Pin instances.
|
||||
/// \param size Size of the Pin list (see <PIO_LISTSIZE>).
|
||||
//------------------------------------------------------------------------------
|
||||
unsigned char PIO_Configure(const Pin *list, unsigned int size)
|
||||
{
|
||||
// Configure pins
|
||||
while (size > 0) {
|
||||
|
||||
switch (list->type) {
|
||||
|
||||
case PIO_PERIPH_A:
|
||||
PIO_SetPeripheralA(list->pio,
|
||||
list->mask,
|
||||
(list->attribute & PIO_PULLUP) ? 1 : 0);
|
||||
break;
|
||||
|
||||
case PIO_PERIPH_B:
|
||||
PIO_SetPeripheralB(list->pio,
|
||||
list->mask,
|
||||
(list->attribute & PIO_PULLUP) ? 1 : 0);
|
||||
break;
|
||||
|
||||
case PIO_INPUT:
|
||||
AT91C_BASE_PMC->PMC_PCER = 1 << list->id;
|
||||
PIO_SetInput(list->pio,
|
||||
list->mask,
|
||||
(list->attribute & PIO_PULLUP) ? 1 : 0,
|
||||
(list->attribute & PIO_DEGLITCH)? 1 : 0);
|
||||
break;
|
||||
|
||||
case PIO_OUTPUT_0:
|
||||
case PIO_OUTPUT_1:
|
||||
PIO_SetOutput(list->pio,
|
||||
list->mask,
|
||||
(list->type == PIO_OUTPUT_1),
|
||||
(list->attribute & PIO_OPENDRAIN) ? 1 : 0,
|
||||
(list->attribute & PIO_PULLUP) ? 1 : 0);
|
||||
break;
|
||||
|
||||
default: return 0;
|
||||
}
|
||||
|
||||
list++;
|
||||
size--;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
/// Sets a high output level on one or more pin(s) (if configured as output(s)).
|
||||
/// \param pin Pointer to a Pin instance describing one or more pins.
|
||||
//------------------------------------------------------------------------------
|
||||
void PIO_Set(const Pin *pin)
|
||||
{
|
||||
WRITE(pin->pio, PIO_SODR, pin->mask);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
/// Sets a low output level on one or more pin(s) (if configured as output(s)).
|
||||
/// \param pin Pointer to a Pin instance describing one or more pins.
|
||||
//------------------------------------------------------------------------------
|
||||
void PIO_Clear(const Pin *pin)
|
||||
{
|
||||
WRITE(pin->pio, PIO_CODR, pin->mask);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
/// Returns 1 if one or more PIO of the given Pin instance currently have a high
|
||||
/// level; otherwise returns 0.
|
||||
/// \param pin Pointer to a Pin instance describing one or more pins.
|
||||
//------------------------------------------------------------------------------
|
||||
unsigned char PIO_Get(const Pin *pin)
|
||||
{
|
||||
unsigned int reg;
|
||||
if ((pin->type == PIO_OUTPUT_0) || (pin->type == PIO_OUTPUT_1)) {
|
||||
|
||||
reg = READ(pin->pio, PIO_ODSR);
|
||||
}
|
||||
else {
|
||||
|
||||
reg = READ(pin->pio, PIO_PDSR);
|
||||
}
|
||||
|
||||
if ((reg & pin->mask) == 0) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
/// Returns 1 if one or more PIO of the given Pin data to be driven on the I/O line
|
||||
/// level; otherwise returns 0.
|
||||
/// \param pin Pointer to a Pin instance describing one or more pins.
|
||||
//------------------------------------------------------------------------------
|
||||
unsigned char PIO_GetOutputDataStatus(const Pin *pin)
|
||||
{
|
||||
if ((READ(pin->pio, PIO_ODSR) & pin->mask) == 0) {
|
||||
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
/// Returns the value of ISR for the PIO controller of the pin.
|
||||
/// Reading this register acknoledges all the ITs.
|
||||
/// \param pin Pointer to a Pin instance describing one or more pins.
|
||||
//------------------------------------------------------------------------------
|
||||
unsigned int PIO_GetISR(const Pin *pin)
|
||||
{
|
||||
return (READ(pin->pio, PIO_ISR));
|
||||
}
|
||||
|
169
Demo/Common/drivers/Atmel/at91lib/peripherals/pio/pio.h
Normal file
169
Demo/Common/drivers/Atmel/at91lib/peripherals/pio/pio.h
Normal file
|
@ -0,0 +1,169 @@
|
|||
/* ----------------------------------------------------------------------------
|
||||
* ATMEL Microcontroller Software Support
|
||||
* ----------------------------------------------------------------------------
|
||||
* Copyright (c) 2008, Atmel Corporation
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the disclaimer below.
|
||||
*
|
||||
* Atmel's name may not be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
*
|
||||
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
* ----------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
/// \dir
|
||||
/// !Purpose
|
||||
///
|
||||
/// Definition of methods and structures for using PIOs in a transparent
|
||||
/// way. The main purpose is to allow portability between several boards.
|
||||
///
|
||||
/// !Usage
|
||||
///
|
||||
/// -# To configure and use pins, see pio.h.
|
||||
/// -# To enable and use interrupt generation on PIO status change, see
|
||||
/// pio_it.h.
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
/// \unit
|
||||
/// !Purpose
|
||||
///
|
||||
/// Simple & portable usage of PIO pins.
|
||||
///
|
||||
/// !Usage
|
||||
///
|
||||
/// -# Define a constant pin description array such as the following one:
|
||||
/// \code
|
||||
/// const Pin at91board_dbgu[] = {
|
||||
/// {AT91C_BASE_PIOA, (1 << 30), PIO_PERIPH_A, PIO_DEFAULT},
|
||||
/// {AT91C_BASE_PIOA, (1 << 31), PIO_PERIPH_A, PIO_DEFAULT},
|
||||
/// };
|
||||
/// \endcode
|
||||
/// Alternatively, constants defined in the piodefs.h header file of the
|
||||
/// board module can be used:
|
||||
/// \code
|
||||
/// const Pin at91board_dbgu[] = {PINS_DBGU};
|
||||
/// const Pin at91board_usart[] = {PIN_USART0_RXD, PIN_USART0_TXD};
|
||||
/// \endcode
|
||||
/// It is possible to group multiple pins if they share the same
|
||||
/// attributes, to save memory. Here is the previous DBGU example
|
||||
/// rewritten in such a way:
|
||||
/// \code
|
||||
/// const Pin at91board_dbgu[] = {
|
||||
/// {AT91C_BASE_PIOA, 0xC0000000, PIO_PERIPH_A, PIO_DEFAULT}
|
||||
/// };
|
||||
/// \endcode
|
||||
/// -# For pins configured as inputs, the PIO controller must be enabled
|
||||
/// in the PMC (*enabled by PIO_Configure at the moment*).
|
||||
/// -# Configure a pin array by calling PIO_Configure, using
|
||||
/// the PIO_LISTSIZE macro to calculate the array size if needed. Do not
|
||||
/// forget to check the return value for any error.
|
||||
/// -# Set and get the value of a pin using the PIO_Set, PIO_Clear and
|
||||
/// PIO_Get methods.
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
#ifndef PIO_H
|
||||
#define PIO_H
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Headers
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
#include <board.h>
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Definitions
|
||||
//------------------------------------------------------------------------------
|
||||
//------------------------------------------------------------------------------
|
||||
/// \page "Pin types"
|
||||
/// This page lists the available types for a Pin instance (in its type field).
|
||||
/// !Types
|
||||
/// - PIO_PERIPH_A
|
||||
/// - PIO_PERIPH_B
|
||||
/// - PIO_INPUT
|
||||
/// - PIO_OUTPUT_0
|
||||
/// - PIO_OUTPUT_1
|
||||
|
||||
/// The pin is controlled by the associated signal of peripheral A.
|
||||
#define PIO_PERIPH_A 0
|
||||
/// The pin is controlled by the associated signal of peripheral B.
|
||||
#define PIO_PERIPH_B 1
|
||||
/// The pin is an input.
|
||||
#define PIO_INPUT 2
|
||||
/// The pin is an output and has a default level of 0.
|
||||
#define PIO_OUTPUT_0 3
|
||||
/// The pin is an output and has a default level of 1.
|
||||
#define PIO_OUTPUT_1 4
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
/// \page "Pin attributes"
|
||||
/// This page lists the valid values for the attribute field of a Pin instance.
|
||||
/// !Attributes
|
||||
/// - PIO_DEFAULT
|
||||
/// - PIO_PULLUP
|
||||
/// - PIO_DEGLITCH
|
||||
/// - PIO_OPENDRAIN
|
||||
|
||||
/// Default pin configuration (no attribute).
|
||||
#define PIO_DEFAULT (0 << 0)
|
||||
/// The internal pin pull-up is active.
|
||||
#define PIO_PULLUP (1 << 0)
|
||||
/// The internal glitch filter is active.
|
||||
#define PIO_DEGLITCH (1 << 1)
|
||||
/// The pin is open-drain.
|
||||
#define PIO_OPENDRAIN (1 << 2)
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
/// Calculates the size of a Pin instances array. The array must be local (i.e.
|
||||
/// not a pointer), otherwise the computation will not be correct.
|
||||
#define PIO_LISTSIZE(list) (sizeof(list) / sizeof(Pin))
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Types
|
||||
//------------------------------------------------------------------------------
|
||||
//------------------------------------------------------------------------------
|
||||
/// Describes the type and attribute of one PIO pin or a group of similar pins.
|
||||
typedef struct {
|
||||
/// Bitmask indicating which pin(s) to configure.
|
||||
unsigned int mask;
|
||||
/// Pointer to the PIO controller which has the pin(s).
|
||||
AT91S_PIO *pio;
|
||||
/// Peripheral ID of the PIO controller which has the pin(s).
|
||||
unsigned char id;
|
||||
/// Pin type (see "Pin types").
|
||||
unsigned char type;
|
||||
/// Pin attribute (see "Pin attributes").
|
||||
unsigned char attribute;
|
||||
} Pin;
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Exported functions
|
||||
//------------------------------------------------------------------------------
|
||||
extern unsigned char PIO_Configure(const Pin *list, unsigned int size);
|
||||
extern void PIO_Set(const Pin *pin );
|
||||
extern void PIO_Clear(const Pin *pin);
|
||||
extern unsigned char PIO_Get(const Pin *pin);
|
||||
extern unsigned int PIO_GetISR(const Pin *pin);
|
||||
extern unsigned char PIO_GetOutputDataStatus(const Pin *pin);
|
||||
|
||||
#endif //#ifndef PIO_H
|
||||
|
387
Demo/Common/drivers/Atmel/at91lib/peripherals/pio/pio_it.c
Normal file
387
Demo/Common/drivers/Atmel/at91lib/peripherals/pio/pio_it.c
Normal file
|
@ -0,0 +1,387 @@
|
|||
/* ----------------------------------------------------------------------------
|
||||
* ATMEL Microcontroller Software Support
|
||||
* ----------------------------------------------------------------------------
|
||||
* Copyright (c) 2008, Atmel Corporation
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the disclaimer below.
|
||||
*
|
||||
* Atmel's name may not be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
*
|
||||
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
* ----------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/// Disable traces for this file
|
||||
#ifndef NOTRACE
|
||||
#define NOTRACE
|
||||
#endif
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Headers
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
#include "pio_it.h"
|
||||
#include "pio.h"
|
||||
#include <aic/aic.h>
|
||||
#include <board.h>
|
||||
#include <utility/assert.h>
|
||||
#include <utility/trace.h>
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Local definitions
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
/// Returns the current value of a register.
|
||||
#define READ(peripheral, register) (peripheral->register)
|
||||
/// Modifies the current value of a register.
|
||||
#define WRITE(peripheral, register, value) (peripheral->register = value)
|
||||
|
||||
/// Maximum number of interrupt sources that can be defined.
|
||||
#define MAX_INTERRUPT_SOURCES 7
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Local types
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
/// Describes a PIO interrupt source, including the PIO instance triggering the
|
||||
/// interrupt and the associated interrupt handler.
|
||||
typedef struct _InterruptSource {
|
||||
|
||||
/// Interrupt source pin.
|
||||
const Pin *pPin;
|
||||
|
||||
/// Interrupt handler.
|
||||
void (*handler)(const Pin *);
|
||||
|
||||
} InterruptSource;
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Local variables
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
/// List of interrupt sources.
|
||||
static InterruptSource pSources[MAX_INTERRUPT_SOURCES];
|
||||
|
||||
/// Number of currently defined interrupt sources.
|
||||
static unsigned int numSources;
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Local functions
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
/// Handles all interrupts on the given PIO controller.
|
||||
/// \param id PIO controller ID.
|
||||
/// \param pBase PIO controller base address.
|
||||
//------------------------------------------------------------------------------
|
||||
void PioInterruptHandler(unsigned int id, AT91S_PIO *pBase)
|
||||
{
|
||||
unsigned int status;
|
||||
unsigned int i;
|
||||
|
||||
// Check PIO controller status
|
||||
status = pBase->PIO_ISR;
|
||||
status &= pBase->PIO_IMR;
|
||||
if (status != 0) {
|
||||
|
||||
trace_LOG(trace_DEBUG, "-D- PIO interrupt on PIO controller #%d\n\r", id);
|
||||
|
||||
// Check all sources
|
||||
i = 0;
|
||||
while (status != 0) {
|
||||
|
||||
// There cannot be an unconfigured source enabled.
|
||||
SANITY_CHECK(i < numSources);
|
||||
|
||||
// Source if configured on PIOA
|
||||
if (pSources[i].pPin->id == id) {
|
||||
|
||||
// Source has PIOs which have changed
|
||||
if ((status & pSources[i].pPin->mask) != 0) {
|
||||
|
||||
trace_LOG(trace_DEBUG, "-D- Interrupt source #%d triggered\n\r", i);
|
||||
|
||||
pSources[i].handler(pSources[i].pPin);
|
||||
status &= ~(pSources[i].pPin->mask);
|
||||
}
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
/// Generic PIO interrupt handler. Single entry point for interrupts coming
|
||||
/// from any PIO controller (PIO A, B, C ...). Dispatches the interrupt to
|
||||
/// the user-configured handlers.
|
||||
//------------------------------------------------------------------------------
|
||||
void InterruptHandler()
|
||||
{
|
||||
#if defined(AT91C_ID_PIOA)
|
||||
// Treat PIOA interrupts
|
||||
PioInterruptHandler(AT91C_ID_PIOA, AT91C_BASE_PIOA);
|
||||
#endif
|
||||
|
||||
#if defined(AT91C_ID_PIOB)
|
||||
// Treat PIOB interrupts
|
||||
PioInterruptHandler(AT91C_ID_PIOB, AT91C_BASE_PIOB);
|
||||
#endif
|
||||
|
||||
#if defined(AT91C_ID_PIOC)
|
||||
// Treat PIOC interrupts
|
||||
PioInterruptHandler(AT91C_ID_PIOC, AT91C_BASE_PIOC);
|
||||
#endif
|
||||
|
||||
#if defined(AT91C_ID_PIOD)
|
||||
// Treat PIOD interrupts
|
||||
PioInterruptHandler(AT91C_ID_PIOD, AT91C_BASE_PIOD);
|
||||
#endif
|
||||
|
||||
#if defined(AT91C_ID_PIOE)
|
||||
// Treat PIOE interrupts
|
||||
PioInterruptHandler(AT91C_ID_PIOE, AT91C_BASE_PIOE);
|
||||
#endif
|
||||
|
||||
#if defined(AT91C_ID_PIOABCD)
|
||||
// Treat PIOABCD interrupts
|
||||
#if !defined(AT91C_ID_PIOA)
|
||||
PioInterruptHandler(AT91C_ID_PIOABCD, AT91C_BASE_PIOA);
|
||||
#endif
|
||||
#if !defined(AT91C_ID_PIOB)
|
||||
PioInterruptHandler(AT91C_ID_PIOABCD, AT91C_BASE_PIOB);
|
||||
#endif
|
||||
#if !defined(AT91C_ID_PIOC)
|
||||
PioInterruptHandler(AT91C_ID_PIOABCD, AT91C_BASE_PIOC);
|
||||
#endif
|
||||
#if !defined(AT91C_ID_PIOD)
|
||||
PioInterruptHandler(AT91C_ID_PIOABCD, AT91C_BASE_PIOD);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(AT91C_ID_PIOABCDE)
|
||||
// Treat PIOABCDE interrupts
|
||||
#if !defined(AT91C_ID_PIOA)
|
||||
PioInterruptHandler(AT91C_ID_PIOABCDE, AT91C_BASE_PIOA);
|
||||
#endif
|
||||
#if !defined(AT91C_ID_PIOB)
|
||||
PioInterruptHandler(AT91C_ID_PIOABCDE, AT91C_BASE_PIOB);
|
||||
#endif
|
||||
#if !defined(AT91C_ID_PIOC)
|
||||
PioInterruptHandler(AT91C_ID_PIOABCDE, AT91C_BASE_PIOC);
|
||||
#endif
|
||||
#if !defined(AT91C_ID_PIOD)
|
||||
PioInterruptHandler(AT91C_ID_PIOABCDE, AT91C_BASE_PIOD);
|
||||
#endif
|
||||
#if !defined(AT91C_ID_PIOE)
|
||||
PioInterruptHandler(AT91C_ID_PIOABCDE, AT91C_BASE_PIOE);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(AT91C_ID_PIOCDE)
|
||||
// Treat PIOCDE interrupts
|
||||
#if !defined(AT91C_ID_PIOC)
|
||||
PioInterruptHandler(AT91C_ID_PIOCDE, AT91C_BASE_PIOC);
|
||||
#endif
|
||||
#if !defined(AT91C_ID_PIOD)
|
||||
PioInterruptHandler(AT91C_ID_PIOCDE, AT91C_BASE_PIOD);
|
||||
#endif
|
||||
#if !defined(AT91C_ID_PIOE)
|
||||
PioInterruptHandler(AT91C_ID_PIOCDE, AT91C_BASE_PIOE);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Global functions
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
/// Initializes the PIO interrupt management logic.
|
||||
/// \param priority PIO controller interrupts priority.
|
||||
//------------------------------------------------------------------------------
|
||||
void PIO_InitializeInterrupts(unsigned int priority)
|
||||
{
|
||||
trace_LOG(trace_DEBUG, "-D- PIO_Initialize()\n\r");
|
||||
|
||||
SANITY_CHECK((priority & ~AT91C_AIC_PRIOR) == 0);
|
||||
|
||||
// Reset sources
|
||||
numSources = 0;
|
||||
|
||||
#ifdef AT91C_ID_PIOA
|
||||
// Configure PIO interrupt sources
|
||||
trace_LOG(trace_DEBUG, "-D- PIO_Initialize: Configuring PIOA\n\r");
|
||||
AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOA;
|
||||
AT91C_BASE_PIOA->PIO_ISR;
|
||||
AT91C_BASE_PIOA->PIO_IDR = 0xFFFFFFFF;
|
||||
AIC_ConfigureIT(AT91C_ID_PIOA, priority, InterruptHandler);
|
||||
AIC_EnableIT(AT91C_ID_PIOA);
|
||||
#endif
|
||||
|
||||
#ifdef AT91C_ID_PIOB
|
||||
trace_LOG(trace_DEBUG, "-D- PIO_Initialize: Configuring PIOB\n\r");
|
||||
AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOB;
|
||||
AT91C_BASE_PIOB->PIO_ISR;
|
||||
AT91C_BASE_PIOB->PIO_IDR = 0xFFFFFFFF;
|
||||
AIC_ConfigureIT(AT91C_ID_PIOB, priority, InterruptHandler);
|
||||
AIC_EnableIT(AT91C_ID_PIOB);
|
||||
#endif
|
||||
|
||||
#ifdef AT91C_ID_PIOC
|
||||
trace_LOG(trace_DEBUG, "-D- PIO_Initialize: Configuring PIOC\n\r");
|
||||
AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOC;
|
||||
AT91C_BASE_PIOC->PIO_ISR;
|
||||
AT91C_BASE_PIOC->PIO_IDR = 0xFFFFFFFF;
|
||||
AIC_ConfigureIT(AT91C_ID_PIOC, priority, InterruptHandler);
|
||||
AIC_EnableIT(AT91C_ID_PIOC);
|
||||
#endif
|
||||
|
||||
#ifdef AT91C_ID_PIOD
|
||||
trace_LOG(trace_DEBUG, "-D- PIO_Initialize: Configuring PIOD\n\r");
|
||||
AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOD;
|
||||
AT91C_BASE_PIOC->PIO_ISR;
|
||||
AT91C_BASE_PIOC->PIO_IDR = 0xFFFFFFFF;
|
||||
AIC_ConfigureIT(AT91C_ID_PIOD, priority, InterruptHandler);
|
||||
AIC_EnableIT(AT91C_ID_PIOD);
|
||||
#endif
|
||||
|
||||
#ifdef AT91C_ID_PIOE
|
||||
trace_LOG(trace_DEBUG, "-D- PIO_Initialize: Configuring PIOE\n\r");
|
||||
AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOE;
|
||||
AT91C_BASE_PIOC->PIO_ISR;
|
||||
AT91C_BASE_PIOC->PIO_IDR = 0xFFFFFFFF;
|
||||
AIC_ConfigureIT(AT91C_ID_PIOE, priority, InterruptHandler);
|
||||
AIC_EnableIT(AT91C_ID_PIOE);
|
||||
#endif
|
||||
|
||||
#if defined(AT91C_ID_PIOABCD)
|
||||
// Treat PIOABCD interrupts
|
||||
#if !defined(AT91C_ID_PIOA) \
|
||||
&& !defined(AT91C_ID_PIOB) \
|
||||
&& !defined(AT91C_ID_PIOC) \
|
||||
&& !defined(AT91C_ID_PIOD)
|
||||
|
||||
trace_LOG(trace_DEBUG, "-D- PIO_Initialize: Configuring PIOABCD\n\r");
|
||||
AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOABCD;
|
||||
AT91C_BASE_PIOA->PIO_ISR;
|
||||
AT91C_BASE_PIOA->PIO_IDR = 0xFFFFFFFF;
|
||||
AIC_ConfigureIT(AT91C_ID_PIOABCD, priority, InterruptHandler);
|
||||
AIC_EnableIT(AT91C_ID_PIOABCD);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(AT91C_ID_PIOABCDE)
|
||||
// Treat PIOABCDE interrupts
|
||||
#if !defined(AT91C_ID_PIOA) \
|
||||
&& !defined(AT91C_ID_PIOB) \
|
||||
&& !defined(AT91C_ID_PIOC) \
|
||||
&& !defined(AT91C_ID_PIOD) \
|
||||
&& !defined(AT91C_ID_PIOE)
|
||||
|
||||
trace_LOG(trace_DEBUG, "-D- PIO_Initialize: Configuring PIOABCDE\n\r");
|
||||
AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOABCDE;
|
||||
AT91C_BASE_PIOA->PIO_ISR;
|
||||
AT91C_BASE_PIOA->PIO_IDR = 0xFFFFFFFF;
|
||||
AIC_ConfigureIT(AT91C_ID_PIOABCDE, priority, InterruptHandler);
|
||||
AIC_EnableIT(AT91C_ID_PIOABCDE);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(AT91C_ID_PIOCDE)
|
||||
// Treat PIOCDE interrupts
|
||||
#if !defined(AT91C_ID_PIOC) \
|
||||
&& !defined(AT91C_ID_PIOD) \
|
||||
&& !defined(AT91C_ID_PIOE)
|
||||
|
||||
trace_LOG(trace_DEBUG, "-D- PIO_Initialize: Configuring PIOC\n\r");
|
||||
AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOCDE;
|
||||
AT91C_BASE_PIOC->PIO_ISR;
|
||||
AT91C_BASE_PIOC->PIO_IDR = 0xFFFFFFFF;
|
||||
AIC_ConfigureIT(AT91C_ID_PIOCDE, priority, InterruptHandler);
|
||||
AIC_EnableIT(AT91C_ID_PIOCDE);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
/// Configures an interrupt source.
|
||||
/// \param pPin Interrupt source.
|
||||
/// \param handler Desired interrupt handler for the source.
|
||||
//------------------------------------------------------------------------------
|
||||
void PIO_ConfigureIt(const Pin *pPin, void (*handler)(const Pin *))
|
||||
{
|
||||
InterruptSource *pSource;
|
||||
|
||||
trace_LOG(trace_DEBUG, "-D- PIO_ConfigureIt()\n\r");
|
||||
|
||||
SANITY_CHECK(pPin);
|
||||
ASSERT(numSources < MAX_INTERRUPT_SOURCES,
|
||||
"-F- PIO_ConfigureIt: Increase MAX_INTERRUPT_SOURCES\n\r");
|
||||
|
||||
// Define new source
|
||||
trace_LOG(trace_DEBUG, "-D- PIO_ConfigureIt: Defining new source #%d.\n\r", numSources);
|
||||
|
||||
pSource = &(pSources[numSources]);
|
||||
pSource->pPin = pPin;
|
||||
pSource->handler = handler;
|
||||
numSources++;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
/// Enables the given interrupt source if it has been configured.
|
||||
/// \param pPin Interrupt source to enable.
|
||||
//------------------------------------------------------------------------------
|
||||
void PIO_EnableIt(const Pin *pPin)
|
||||
{
|
||||
trace_LOG(trace_DEBUG, "-D- PIO_EnableIt()\n\r");
|
||||
|
||||
SANITY_CHECK(pPin);
|
||||
|
||||
#ifndef NOASSERT
|
||||
unsigned int i = 0;
|
||||
unsigned char found = 0;
|
||||
while ((i < numSources) && !found) {
|
||||
|
||||
if (pSources[i].pPin == pPin) {
|
||||
|
||||
found = 1;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
ASSERT(found, "-F- PIO_EnableIt: Interrupt source has not been configured\n\r");
|
||||
#endif
|
||||
|
||||
pPin->pio->PIO_ISR;
|
||||
pPin->pio->PIO_IER = pPin->mask;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
/// Disables a given interrupt source.
|
||||
/// \param pPin Interrupt source to disable.
|
||||
//------------------------------------------------------------------------------
|
||||
void PIO_DisableIt(const Pin *pPin)
|
||||
{
|
||||
SANITY_CHECK(pPin);
|
||||
|
||||
trace_LOG(trace_DEBUG, "-D- PIO_DisableIt()\n\r");
|
||||
|
||||
pPin->pio->PIO_IDR = pPin->mask;
|
||||
}
|
||||
|
61
Demo/Common/drivers/Atmel/at91lib/peripherals/pio/pio_it.h
Normal file
61
Demo/Common/drivers/Atmel/at91lib/peripherals/pio/pio_it.h
Normal file
|
@ -0,0 +1,61 @@
|
|||
/* ----------------------------------------------------------------------------
|
||||
* ATMEL Microcontroller Software Support
|
||||
* ----------------------------------------------------------------------------
|
||||
* Copyright (c) 2008, Atmel Corporation
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* - Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the disclaimer below.
|
||||
*
|
||||
* Atmel's name may not be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
*
|
||||
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
||||
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
||||
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
* ----------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
/// \unit
|
||||
/// !Purpose
|
||||
///
|
||||
/// Configuration and handling of interrupts on PIO status changes.
|
||||
///
|
||||
/// !Usage
|
||||
///
|
||||
/// -# Configure an status change interrupt on one or more pin(s) with
|
||||
/// PIO_ConfigureIt.
|
||||
/// -# Enable & disable interrupts on pins using PIO_EnableIt and
|
||||
/// PIO_DisableIt.
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Headers
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
#include "pio.h"
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Global functions
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
extern void PIO_InitializeInterrupts(unsigned int priority);
|
||||
|
||||
extern void PIO_ConfigureIt(const Pin *pPin, void (*handler)(const Pin *));
|
||||
|
||||
extern void PIO_EnableIt(const Pin *pPin);
|
||||
|
||||
extern void PIO_DisableIt(const Pin *pPin);
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue