123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377 |
- /******************************************************************************
- * Filename: gpio.c
- * Revised: $Date: 2013-04-29 09:36:44 +0200 (Mon, 29 Apr 2013) $
- * Revision: $Revision: 9922 $
- *
- * Description: Driver for the GPIO controller.
- *
- * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
- *
- *
- * 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 following disclaimer.
- *
- * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * Neither the name of Texas Instruments Incorporated nor the names of
- * its contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS 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.
- *
- ******************************************************************************/
- //*****************************************************************************
- //
- //! \addtogroup gpio_api
- //! @{
- //
- //*****************************************************************************
- #include "hw_gpio.h"
- #include "hw_ints.h"
- #include "hw_sys_ctrl.h"
- #include "hw_memmap.h"
- #include "debug.h"
- #include "gpio.h"
- #include "interrupt.h"
- #include "ioc.h"
- //*****************************************************************************
- //
- //! \internal
- //! Checks a GPIO base address
- //!
- //! \param ui32Port is the base address of the GPIO port.
- //!
- //! This function determines if a GPIO port base address is valid.
- //!
- //! \return Returns \b true if the base address is valid and \b false
- //! otherwise.
- //
- //*****************************************************************************
- #ifdef ENABLE_ASSERT
- static bool
- GPIOBaseValid(uint32_t ui32Port)
- {
- return((ui32Port == GPIO_A_BASE) || (ui32Port == GPIO_B_BASE) ||
- (ui32Port == GPIO_C_BASE) || (ui32Port == GPIO_D_BASE));
- }
- #endif
- //*****************************************************************************
- //
- //! \internal
- //! Gets the GPIO interrupt number
- //!
- //! \param ui32Port is the base address of the GPIO port.
- //!
- //! Given a GPIO base address, returns the corresponding interrupt number.
- //!
- //! \return Returns a GPIO interrupt number, or 0 if \e ui32Port is invalid.
- //
- //*****************************************************************************
- uint32_t
- GPIOGetIntNumber(uint32_t ui32Port)
- {
- uint32_t ui32Int;
- //
- // Check the arguments.
- //
- ASSERT(GPIOBaseValid(ui32Port));
- //
- // Determine the GPIO interrupt number for the given module.
- //
- switch(ui32Port)
- {
- case GPIO_A_BASE:
- {
- ui32Int = INT_GPIOA;
- break;
- }
- case GPIO_B_BASE:
- {
- ui32Int = INT_GPIOB;
- break;
- }
- case GPIO_C_BASE:
- {
- ui32Int = INT_GPIOC;
- break;
- }
- case GPIO_D_BASE:
- {
- ui32Int = INT_GPIOD;
- break;
- }
- default:
- {
- return(0);
- }
- }
- //
- // Return GPIO interrupt number.
- //
- return(ui32Int);
- }
- //*****************************************************************************
- //
- //! Sets the direction and mode of the specified pin(s)
- //!
- //! \param ui32Port is the base address of the GPIO port.
- //! \param ui8Pins is the bit-packed representation of the pin(s).
- //! \param ui32PinIO is the pin direction and/or mode.
- //!
- //! This function sets the specified pin(s) on the selected GPIO port
- //! as either an input or output under software control or sets the
- //! pin to be under hardware control.
- //!
- //! The parameter \e ui32PinIO is an enumerated data type that can be one of
- //! the following values:
- //!
- //! - \b GPIO_DIR_MODE_IN
- //! - \b GPIO_DIR_MODE_OUT
- //! - \b GPIO_DIR_MODE_HW
- //!
- //! where \b GPIO_DIR_MODE_IN specifies that the pin will be programmed as
- //! a software controlled input, \b GPIO_DIR_MODE_OUT specifies that the pin
- //! will be programmed as a software controlled output, and
- //! \b GPIO_DIR_MODE_HW specifies that the pin will be placed under
- //! hardware control.
- //!
- //! The pin(s) are specified using a bit-packed byte, where each bit that is
- //! set identifies the pin to be accessed, and where bit 0 of the byte
- //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
- //!
- //! \return None
- //
- //*****************************************************************************
- void
- GPIODirModeSet(uint32_t ui32Port, uint8_t ui8Pins,
- uint32_t ui32PinIO)
- {
- //
- // Check the arguments.
- //
- ASSERT(GPIOBaseValid(ui32Port));
- ASSERT((ui32PinIO == GPIO_DIR_MODE_IN) || (ui32PinIO == GPIO_DIR_MODE_OUT) ||
- (ui32PinIO == GPIO_DIR_MODE_HW));
- //
- // Set the pin direction and mode.
- //
- HWREG(ui32Port + GPIO_O_DIR) = ((ui32PinIO & GPIO_DIR_MODE_OUT) ?
- (HWREG(ui32Port + GPIO_O_DIR) | ui8Pins) :
- (HWREG(ui32Port + GPIO_O_DIR) & ~(ui8Pins)));
- HWREG(ui32Port + GPIO_O_AFSEL) = ((ui32PinIO & GPIO_DIR_MODE_HW) ?
- (HWREG(ui32Port + GPIO_O_AFSEL) | ui8Pins) :
- (HWREG(ui32Port + GPIO_O_AFSEL) & ~(ui8Pins)));
- }
- //*****************************************************************************
- //
- //! Gets the direction and mode of a pin
- //!
- //! \param ui32Port is the base address of the GPIO port.
- //! \param ui8Pin is the pin number.
- //!
- //! This function gets the direction and control mode for a specified pin on
- //! the selected GPIO port. The pin can be configured as either an input or
- //! output under software control, or it can be under hardware control. The
- //! type of control and direction are returned as an enumerated data type.
- //!
- //! \return Returns one of the enumerated data types described for
- //! GPIODirModeSet().
- //
- //*****************************************************************************
- uint32_t
- GPIODirModeGet(uint32_t ui32Port, uint8_t ui8Pin)
- {
- uint32_t ui32Dir;
- uint32_t ui32AFSEL;
- //
- // Check the arguments.
- //
- ASSERT(GPIOBaseValid(ui32Port));
- ASSERT(ui8Pin < 8);
- //
- // Convert from a pin number to a bit position.
- //
- ui8Pin = 1 << ui8Pin;
- //
- // Return the pin direction and mode.
- //
- ui32Dir = HWREG(ui32Port + GPIO_O_DIR);
- ui32AFSEL = HWREG(ui32Port + GPIO_O_AFSEL);
- return(((ui32Dir & ui8Pin) ? GPIO_DIR_MODE_OUT : GPIO_DIR_MODE_IN) |
- ((ui32AFSEL & ui8Pin) ? GPIO_DIR_MODE_HW : GPIO_DIR_MODE_IN));
- }
- //*****************************************************************************
- //
- //! Sets the interrupt type for the specified pin(s)
- //!
- //! \param ui32Port is the base address of the GPIO port.
- //! \param ui8Pins is the bit-packed representation of the pin(s).
- //! \param ui32IntType specifies the type of interrupt trigger mechanism.
- //!
- //! This function sets up the various interrupt trigger mechanisms for the
- //! specified pin(s) on the selected GPIO port.
- //!
- //! The parameter \e ui32IntType is an enumerated data type that can be one of
- //! the following values:
- //!
- //! - \b GPIO_FALLING_EDGE
- //! - \b GPIO_RISING_EDGE
- //! - \b GPIO_BOTH_EDGES
- //! - \b GPIO_LOW_LEVEL
- //! - \b GPIO_HIGH_LEVEL
- //!
- //! where the different values describe the interrupt detection mechanism
- //! (edge or level) and the particular triggering event (falling, rising,
- //! or both edges for edge detect, low or high for level detect).
- //!
- //! The pin(s) are specified using a bit-packed byte, where each bit that is
- //! set identifies the pin to be accessed, and where bit 0 of the byte
- //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
- //!
- //! \note To avoid any spurious interrupts, the user must
- //! ensure that the GPIO inputs remain stable for the duration of
- //! this function.
- //!
- //! \return None
- //
- //*****************************************************************************
- void
- GPIOIntTypeSet(uint32_t ui32Port, uint8_t ui8Pins,
- uint32_t ui32IntType)
- {
- //
- // Check the arguments.
- //
- ASSERT(GPIOBaseValid(ui32Port));
- ASSERT((ui32IntType == GPIO_FALLING_EDGE) ||
- (ui32IntType == GPIO_RISING_EDGE) || (ui32IntType == GPIO_BOTH_EDGES) ||
- (ui32IntType == GPIO_LOW_LEVEL) || (ui32IntType == GPIO_HIGH_LEVEL));
- //
- // Set the pin interrupt type.
- //
- HWREG(ui32Port + GPIO_O_IBE) = ((ui32IntType & 1) ?
- (HWREG(ui32Port + GPIO_O_IBE) | ui8Pins) :
- (HWREG(ui32Port + GPIO_O_IBE) & ~(ui8Pins)));
- HWREG(ui32Port + GPIO_O_IS) = ((ui32IntType & 2) ?
- (HWREG(ui32Port + GPIO_O_IS) | ui8Pins) :
- (HWREG(ui32Port + GPIO_O_IS) & ~(ui8Pins)));
- HWREG(ui32Port + GPIO_O_IEV) = ((ui32IntType & 4) ?
- (HWREG(ui32Port + GPIO_O_IEV) | ui8Pins) :
- (HWREG(ui32Port + GPIO_O_IEV) & ~(ui8Pins)));
- }
- //*****************************************************************************
- //
- //! Gets the interrupt type for a pin
- //!
- //! \param ui32Port is the base address of the GPIO port.
- //! \param ui8Pin is the pin number.
- //!
- //! This function gets the interrupt type for a specified pin on the selected
- //! GPIO port. The pin can be configured as a falling edge, rising edge, or
- //! both edge detected interrupt, or can be configured as a low level or
- //! high level detected interrupt. The type of interrupt detection mechanism
- //! is returned as an enumerated data type.
- //!
- //! \return Returns one of the enumerated data types described for
- //! GPIOIntTypeSet().
- //
- //*****************************************************************************
- uint32_t
- GPIOIntTypeGet(uint32_t ui32Port, uint8_t ui8Pin)
- {
- uint32_t ui32IBE, ui32IS, ui32IEV;
- //
- // Check the arguments.
- //
- ASSERT(GPIOBaseValid(ui32Port));
- ASSERT(ui8Pin < 8);
- //
- // Convert from a pin number to a bit position.
- //
- ui8Pin = 1 << ui8Pin;
- //
- // Return the pin interrupt type.
- //
- ui32IBE = HWREG(ui32Port + GPIO_O_IBE);
- ui32IS = HWREG(ui32Port + GPIO_O_IS);
- ui32IEV = HWREG(ui32Port + GPIO_O_IEV);
- return(((ui32IBE & ui8Pin) ? 1 : 0) | ((ui32IS & ui8Pin) ? 2 : 0) |
- ((ui32IEV & ui8Pin) ? 4 : 0));
- }
- //*****************************************************************************
- //
- //! Enables interrupts for the specified pin(s)
- //!
- //! \param ui32Port is the base address of the GPIO port.
- //! \param ui8Pins is the bit-packed representation of the pin(s).
- //!
- //! Unmasks the interrupt for the specified pin(s).
- //!
- //! The pin(s) are specified using a bit-packed byte, where each bit that is
- //! set identifies the pin to be accessed, and where bit 0 of the byte
- //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
- //!
- //! \return None
- //
- //*****************************************************************************
- void
- GPIOPinIntEnable(uint32_t ui32Port, uint8_t ui8Pins)
- {
- //
- // Check the arguments.
- //
- ASSERT(GPIOBaseValid(ui32Port));
- //
- // Enable the interrupts.
- //
- HWREG(ui32Port + GPIO_O_IE) |= ui8Pins;
- }
- //*****************************************************************************
- //
- //! Disables interrupts for the specified pin(s)
- //!
- //! \param ui32Port is the base address of the GPIO port.
- //! \param ui8Pins is the bit-packed representation of the pin(s).
- //!
- //! Masks the interrupt for the specified pin(s)
- //!
- //! The pin(s) are specified using a bit-packed byte, where each bit that is
- //! set identifies the pin to be accessed, and where bit 0 of the byte
- //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
- //!
- //! \return None
- //
- //*****************************************************************************
- void
- GPIOPinIntDisable(uint32_t ui32Port, uint8_t ui8Pins)
- {
- //
- // Check the arguments.
- //
- ASSERT(GPIOBaseValid(ui32Port));
- //
- // Disable the interrupts.
- //
- HWREG(ui32Port + GPIO_O_IE) &= ~(ui8Pins);
- }
- //*****************************************************************************
- //
- //! Gets interrupt status for the specified GPIO port
- //!
- //! \param ui32Port is the base address of the GPIO port.
- //! \param bMasked specifies whether masked or raw interrupt status is
- //! returned.
- //!
- //! If \e bMasked is set as \b true, then the masked interrupt status is
- //! returned; otherwise, the raw interrupt status is returned.
- //!
- //! \return Returns a bit-packed byte, where each bit that is set identifies
- //! an active masked or raw interrupt, and where bit 0 of the byte
- //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
- //! Bits 31:8 should be ignored.
- //
- //*****************************************************************************
- uint32_t
- GPIOPinIntStatus(uint32_t ui32Port, bool bMasked)
- {
- //
- // Check the arguments.
- //
- ASSERT(GPIOBaseValid(ui32Port));
- //
- // Return the interrupt status.
- //
- if(bMasked)
- {
- return(HWREG(ui32Port + GPIO_O_MIS));
- }
- else
- {
- return(HWREG(ui32Port + GPIO_O_RIS));
- }
- }
- //*****************************************************************************
- //
- //! Clears the interrupt for the specified pin(s)
- //!
- //! \param ui32Port is the base address of the GPIO port.
- //! \param ui8Pins is the bit-packed representation of the pin(s).
- //!
- //! Clears the interrupt for the specified pin(s).
- //!
- //! The pin(s) are specified using a bit-packed byte, where each bit that is
- //! set identifies the pin to be accessed, and where bit 0 of the byte
- //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
- //!
- //! \note The write buffer in the Cortex-M3 processor can cause the interrupt
- //! source to take several clock cycles before clearing.
- //! Therefore, TI recommends clearing the interrupt source early in the
- //! interrupt handler (as opposed to the very last action) to avoid
- //! returning from the interrupt handler before the interrupt source is
- //! actually cleared. Failure to clear the interrupt source early can result in
- //! the interrupt handler being immediately reentered (because NVIC still sees
- //! the interrupt source asserted).
- //!
- //! \return None
- //
- //*****************************************************************************
- void
- GPIOPinIntClear(uint32_t ui32Port, uint8_t ui8Pins)
- {
- //
- // Check the arguments.
- //
- ASSERT(GPIOBaseValid(ui32Port));
- //
- // Clear the interrupts.
- //
- HWREG(ui32Port + GPIO_O_IC) = ui8Pins;
- }
- //*****************************************************************************
- //
- //! Registers an interrupt handler for a GPIO port
- //!
- //! \param ui32Port is the base address of the GPIO port.
- //! \param pfnHandler is a pointer to the GPIO port interrupt handling
- //! function.
- //!
- //! This function ensures that the interrupt handler specified by
- //! \e pfnHandler is called when an interrupt is detected from the selected
- //! GPIO port. This function also enables the corresponding GPIO interrupt
- //! in the interrupt controller; individual pin interrupts and interrupt
- //! sources must be enabled with GPIOPinIntEnable().
- //!
- //! \sa IntRegister() for important information about registering interrupt
- //! handlers.
- //!
- //! \return None
- //
- //*****************************************************************************
- void
- GPIOPortIntRegister(uint32_t ui32Port, void (*pfnHandler)(void))
- {
- //
- // Check the arguments.
- //
- ASSERT(GPIOBaseValid(ui32Port));
- //
- // Get the interrupt number associated with the specified GPIO.
- //
- ui32Port = GPIOGetIntNumber(ui32Port);
- //
- // Register the interrupt handler.
- //
- IntRegister(ui32Port, pfnHandler);
- //
- // Enable the GPIO interrupt.
- //
- IntEnable(ui32Port);
- }
- //*****************************************************************************
- //
- //! Removes an interrupt handler for a GPIO port
- //!
- //! \param ui32Port is the base address of the GPIO port.
- //!
- //! This function unregisters the interrupt handler for the specified
- //! GPIO port. This function also disables the corresponding
- //! GPIO port interrupt in the interrupt controller; individual GPIO interrupts
- //! and interrupt sources must be disabled with GPIOPinIntDisable().
- //!
- //! \sa IntRegister() for important information about registering interrupt
- //! handlers.
- //!
- //! \return None
- //
- //*****************************************************************************
- void
- GPIOPortIntUnregister(uint32_t ui32Port)
- {
- //
- // Check the arguments.
- //
- ASSERT(GPIOBaseValid(ui32Port));
- //
- // Get the interrupt number associated with the specified GPIO.
- //
- ui32Port = GPIOGetIntNumber(ui32Port);
- //
- // Disable the GPIO interrupt.
- //
- IntDisable(ui32Port);
- //
- // Unregister the interrupt handler.
- //
- IntUnregister(ui32Port);
- }
- //*****************************************************************************
- //
- //! Reads the values present of the specified pin(s)
- //!
- //! \param ui32Port is the base address of the GPIO port.
- //! \param ui8Pins is the bit-packed representation of the pin(s).
- //!
- //! The values at the specified pin(s) are read, as specified by \e ui8Pins.
- //! Values are returned for both input and output pin(s), and the value
- //! for pin(s) that are not specified by \e ui8Pins are set to 0.
- //!
- //! The pin(s) are specified using a bit-packed byte, where each bit that is
- //! set identifies the pin to be accessed, and where bit 0 of the byte
- //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
- //!
- //! \return Returns a bit-packed byte providing the state of the specified
- //! pin, where bit 0 of the byte represents GPIO port pin 0, bit 1 represents
- //! GPIO port pin 1, and so on. Any bit that is not specified by \e ui8Pins
- //! is returned as a 0. Bits 31:8 should be ignored.
- //
- //*****************************************************************************
- uint32_t
- GPIOPinRead(uint32_t ui32Port, uint8_t ui8Pins)
- {
- //
- // Check the arguments.
- //
- ASSERT(GPIOBaseValid(ui32Port));
- //
- // Return the pin value(s).
- //
- return(HWREG(ui32Port + (GPIO_O_DATA + (ui8Pins << 2))));
- }
- //*****************************************************************************
- //
- //! Writes a value to the specified pin(s)
- //!
- //! \param ui32Port is the base address of the GPIO port.
- //! \param ui8Pins is the bit-packed representation of the pin(s).
- //! \param ui8Val is the value to write to the pin(s).
- //!
- //! Writes the corresponding bit values to the output pin(s) specified by
- //! \e ui8Pins. Writing to a pin configured as an input pin has no effect.
- //!
- //! The pin(s) are specified using a bit-packed byte, where each bit that is
- //! set identifies the pin to be accessed, and where bit 0 of the byte
- //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
- //!
- //! \return None
- //
- //*****************************************************************************
- void
- GPIOPinWrite(uint32_t ui32Port, uint8_t ui8Pins, uint8_t ui8Val)
- {
- //
- // Check the arguments.
- //
- ASSERT(GPIOBaseValid(ui32Port));
- //
- // Write the pins.
- //
- HWREG(ui32Port + (GPIO_O_DATA + (ui8Pins << 2))) = ui8Val;
- }
- //*****************************************************************************
- //
- //! Configures pin(s) for use as GPIO inputs
- //!
- //! \param ui32Port is the base address of the GPIO port.
- //! \param ui8Pins is the bit-packed representation of the pin(s).
- //!
- //! The GPIO pins must be properly configured in order to function correctly as
- //! GPIO inputs. This function provides the proper configuration for those
- //! pin(s).
- //!
- //! The pin(s) are specified using a bit-packed byte, where each bit that is
- //! set identifies the pin to be accessed, and where bit 0 of the byte
- //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
- //!
- //! \return None
- //
- //*****************************************************************************
- void
- GPIOPinTypeGPIOInput(uint32_t ui32Port, uint8_t ui8Pins)
- {
- //
- // Check the arguments.
- //
- ASSERT(GPIOBaseValid(ui32Port));
- //
- // Make the pin(s) be inputs.
- //
- GPIODirModeSet(ui32Port, ui8Pins, GPIO_DIR_MODE_IN);
- //
- // Set the pad(s) to no override of the drive type.
- //
- IOCPadConfigSet(ui32Port, ui8Pins, IOC_OVERRIDE_DIS);
- }
- //*****************************************************************************
- //
- //! Configures pin(s) for use as GPIO outputs
- //!
- //! \param ui32Port is the base address of the GPIO port.
- //! \param ui8Pins is the bit-packed representation of the pin(s).
- //!
- //! The GPIO pins must be properly configured to function correctly as
- //! GPIO outputs. This function provides the proper configuration for those
- //! pin(s).
- //!
- //! The pin(s) are specified using a bit-packed byte, where each bit that is
- //! set identifies the pin to be accessed, and where bit 0 of the byte
- //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
- //!
- //! \return None
- //
- //*****************************************************************************
- void
- GPIOPinTypeGPIOOutput(uint32_t ui32Port, uint8_t ui8Pins)
- {
- //
- // Check the arguments.
- //
- ASSERT(GPIOBaseValid(ui32Port));
- //
- // Make the pin(s) be outputs.
- //
- GPIODirModeSet(ui32Port, ui8Pins, GPIO_DIR_MODE_OUT);
- //
- // Set the pad(s) no override of the drive type.
- //
- IOCPadConfigSet(ui32Port, ui8Pins, IOC_OVERRIDE_DIS);
- }
- //*****************************************************************************
- //
- //! Configures pin(s) for use by the I2C peripheral
- //!
- //! \param ui32Port is the base address of the GPIO port.
- //! \param ui8Pins is the bit-packed representation of the pin(s).
- //!
- //! The I2C pins must be properly configured for the I2C peripheral to function
- //! correctly. This function provides the proper configuration for those
- //! pin(s).
- //!
- //! The pin(s) are specified using a bit-packed byte, where each bit that is
- //! set identifies the pin to be accessed, and where bit 0 of the byte
- //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
- //!
- //! \note This function cannot be used to turn any pin into an I2C pin; it only
- //! configures an I2C pin for proper operation.
- //!
- //! \return None
- //
- //*****************************************************************************
- void
- GPIOPinTypeI2C(uint32_t ui32Port, uint8_t ui8Pins)
- {
- //
- // Check the arguments.
- //
- ASSERT(GPIOBaseValid(ui32Port));
- //
- // Make the pin(s) be peripheral controlled.
- //
- GPIODirModeSet(ui32Port, ui8Pins, GPIO_DIR_MODE_HW);
- //
- // Set the pad(s) to no drive type.
- //
- IOCPadConfigSet(ui32Port, ui8Pins, IOC_OVERRIDE_DIS);
- }
- //*****************************************************************************
- //
- //! Configures pin(s) for use by the SSI peripheral
- //!
- //! \param ui32Port is the base address of the GPIO port.
- //! \param ui8Pins is the bit-packed representation of the pin(s).
- //!
- //! The SSI pins must be properly configured for the SSI peripheral to function
- //! correctly. This function provides a typical configuration for those
- //! pin(s); other configurations might work as well depending upon the board
- //! setup (for example, using the on-chip pull-ups).
- //!
- //! The pin(s) are specified using a bit-packed byte, where each bit that is
- //! set identifies the pin to be accessed, and where bit 0 of the byte
- //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
- //!
- //! \note This function cannot be used to turn any pin into a SSI pin; but only
- //! configures an SSI pin for proper operation.
- //!
- //! \return None
- //
- //*****************************************************************************
- void
- GPIOPinTypeSSI(uint32_t ui32Port, uint8_t ui8Pins)
- {
- //
- // Check the arguments.
- //
- ASSERT(GPIOBaseValid(ui32Port));
- //
- // Make the pin(s) be peripheral controlled.
- //
- GPIODirModeSet(ui32Port, ui8Pins, GPIO_DIR_MODE_HW);
- //
- // Set the pad(s) to no drive type.
- //
- IOCPadConfigSet(ui32Port, ui8Pins, IOC_OVERRIDE_DIS);
- }
- //*****************************************************************************
- //
- //! Configures pin(s) for use by the Timer peripheral
- //!
- //! \param ui32Port is the base address of the GPIO port.
- //! \param ui8Pins is the bit-packed representation of the pin(s).
- //!
- //! The CCP pins must be properly configured for the timer peripheral to
- //! function correctly. This function provides a typical configuration for
- //! those pin(s); other configurations might work as well depending upon the
- //! board setup (for example, using the on-chip pull-ups).
- //!
- //! The pin(s) are specified using a bit-packed byte, where each bit that is
- //! set identifies the pin to be accessed, and where bit 0 of the byte
- //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
- //!
- //! \note This function cannot be used to turn any pin into a timer pin but only
- //! configures a timer pin for proper operation.
- //!
- //! \return None
- //
- //*****************************************************************************
- void
- GPIOPinTypeTimer(uint32_t ui32Port, uint8_t ui8Pins)
- {
- //
- // Check the arguments.
- //
- ASSERT(GPIOBaseValid(ui32Port));
- //
- // Make the pin(s) be peripheral controlled.
- //
- GPIODirModeSet(ui32Port, ui8Pins, GPIO_DIR_MODE_HW);
- //
- // Set the pad(s) to no drive type.
- //
- IOCPadConfigSet(ui32Port, ui8Pins, IOC_OVERRIDE_DIS);
- }
- //*****************************************************************************
- //
- //! Configures input pin(s) for use by the UART peripheral
- //!
- //! \param ui32Port is the base address of the GPIO port.
- //! \param ui8Pins is the bit-packed representation of the pin(s).
- //!
- //! The UART input pins must be properly configured for the UART peripheral to
- //! function correctly. This function provides a typical configuration for
- //! those pin(s); other configurations might work as well depending upon the
- //! board setup (for example, using the on-chip pull-ups).
- //!
- //! \note For PC0 through PC3 the function GPIOPinTypeUARTHiDrive() should
- //! be used to configure these high drive pins.
- //!
- //! The pin(s) are specified using a bit-packed byte, where each bit that is
- //! set identifies the pin to be accessed, and where bit 0 of the byte
- //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
- //!
- //! \note This function cannot be used to turn any pin into a UART pin; but only
- //! configures a UART pin for proper operation.
- //!
- //! \return None
- //
- //*****************************************************************************
- void
- GPIOPinTypeUARTInput(uint32_t ui32Port, uint8_t ui8Pins)
- {
- //
- // Check the arguments.
- //
- ASSERT(GPIOBaseValid(ui32Port));
- ASSERT(!((ui32Port == GPIO_C_BASE) && ((ui8Pins & 0xf) > 0)));
- //
- // Make the pin(s) be peripheral controlled.
- //
- GPIODirModeSet(ui32Port, ui8Pins, GPIO_DIR_MODE_HW);
- //
- // Set the pad(s) to override disable.
- //
- IOCPadConfigSet(ui32Port, ui8Pins, IOC_OVERRIDE_DIS);
- }
- //*****************************************************************************
- //
- //! Configures output pin(s) for use by the UART peripheral
- //!
- //! \param ui32Port is the base address of the GPIO port.
- //! \param ui8Pins is the bit-packed representation of the pin(s).
- //!
- //! The UART output pins must be properly configured for the UART peripheral to
- //! function correctly. This function provides a typical configuration for
- //! those pin(s); other configurations might work as well depending upon the
- //! board setup (for example, using the on-chip pull-ups).
- //!
- //! The pin(s) are specified using a bit-packed byte, where each bit that is
- //! set identifies the pin to be accessed, and where bit 0 of the byte
- //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
- //!
- //! \note This function cannot be used to turn any pin into a UART pin; but only
- //! configures a UART pin for proper operation.
- //!
- //! \return None
- //
- //*****************************************************************************
- void
- GPIOPinTypeUARTOutput(uint32_t ui32Port, uint8_t ui8Pins)
- {
- //
- // Check the arguments.
- //
- ASSERT(GPIOBaseValid(ui32Port));
- ASSERT(!((ui32Port == GPIO_C_BASE) && ((ui8Pins & 0xf) > 0)));
- //
- // Make the pin(s) be peripheral controlled.
- //
- GPIODirModeSet(ui32Port, ui8Pins, GPIO_DIR_MODE_HW);
- //
- // Set the pad(s) to output enable.
- //
- IOCPadConfigSet(ui32Port, ui8Pins, IOC_OVERRIDE_OE);
- }
- //*****************************************************************************
- //
- //! Sets the power-up interrupt type for the specified pin(s)
- //!
- //! \param ui32Port is the base address of the GPIO port.
- //! \param ui8Pins is the bit-packed representation of the pin(s).
- //! \param ui32IntType specifies type of power-up interrupt trigger mechanism.
- //!
- //! This function sets up the various interrupt trigger mechanisms for the
- //! specified pin(s) on the selected GPIO port.
- //!
- //! The parameter \e ui32IntType is an enumerated data type that can be one of
- //! the following values:
- //!
- //! - \b GPIO_POW_FALLING_EDGE
- //! - \b GPIO_POW_RISING_EDGE
- //!
- //! The pin(s) are specified using a bit-packed byte, where each bit that is
- //! set identifies the pin to be accessed, and where bit 0 of the byte
- //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
- //!
- //! \note To avoid any spurious interrupts, the user must
- //! ensure that the GPIO inputs remain stable for the duration of
- //! this function.
- //!
- //! \return None
- //
- //*****************************************************************************
- void
- GPIOPowIntTypeSet(uint32_t ui32Port, uint8_t ui8Pins,
- uint32_t ui32IntType)
- {
- uint32_t ui32PortOffset;
- uint32_t ui32IntPins;
- //
- // Initialize value
- //
- ui32PortOffset = 0;
- //
- // Check the arguments.
- //
- ASSERT(GPIOBaseValid(ui32Port));
- ASSERT((ui32IntType == GPIO_POW_FALLING_EDGE) ||
- (ui32IntType == GPIO_POW_RISING_EDGE));
- //
- // Find bit mask for wanted pin(s)
- //
- if(ui32Port == GPIO_A_BASE)
- {
- ui32PortOffset = 0;
- }
- if(ui32Port == GPIO_B_BASE)
- {
- ui32PortOffset = 8;
- }
- if(ui32Port == GPIO_C_BASE)
- {
- ui32PortOffset = 16;
- }
- if(ui32Port == GPIO_D_BASE)
- {
- ui32PortOffset = 24;
- }
- ui32IntPins = ui8Pins << ui32PortOffset;
- //
- // Set the pin interrupt type.
- //
- if(ui32IntType == GPIO_POW_FALLING_EDGE)
- {
- HWREG(ui32Port + GPIO_O_P_EDGE_CTRL) |= ui32IntPins;
- }
- else // GPIO_POW_RAISING_EDGE
- {
- HWREG(ui32Port + GPIO_O_P_EDGE_CTRL) &= ~(ui32IntPins);
- }
- }
- //*****************************************************************************
- //
- //! Gets the power-up interrupt type for a pin
- //!
- //! \param ui32Port is the base address of the GPIO port.
- //! \param ui8Pin is the pin number.
- //!
- //! This function gets the interrupt type for a specified pin on the selected
- //! GPIO port. The pin can be configured as a falling edge, rising edge, or
- //! both edge detected interrupt, or it can be configured as a low level or
- //! high level detected interrupt. The type of interrupt detection mechanism
- //! is returned as an enumerated data type.
- //!
- //! \return Returns one of the enumerated data types described for
- //! GPIOIntTypeSet().
- //
- //*****************************************************************************
- uint32_t
- GPIOPowIntTypeGet(uint32_t ui32Port, uint8_t ui8Pin)
- {
- uint32_t ui32PortOffset;
- uint32_t ui32IntPin;
- //
- // Initialize value
- //
- ui32PortOffset = 0;
- //
- // Check the arguments.
- //
- ASSERT(GPIOBaseValid(ui32Port));
- ASSERT(ui8Pin < 8);
- //
- // Convert from a port- pin number to a bit position.
- //
- if(ui32Port == GPIO_A_BASE)
- {
- ui32PortOffset = 0;
- }
- if(ui32Port == GPIO_B_BASE)
- {
- ui32PortOffset = 8;
- }
- if(ui32Port == GPIO_C_BASE)
- {
- ui32PortOffset = 16;
- }
- if(ui32Port == GPIO_D_BASE)
- {
- ui32PortOffset = 24;
- }
- ui32IntPin = 1 << (ui8Pin + ui32PortOffset);
- //
- // Return the pin interrupt type.
- //
- if(HWREG(ui32Port + GPIO_O_P_EDGE_CTRL) & ui32IntPin)
- {
- return(GPIO_POW_FALLING_EDGE);
- }
- else
- {
- return(GPIO_POW_RISING_EDGE);
- }
- }
- //*****************************************************************************
- //
- //! Enables power-up interrupts for the specified pin(s)
- //!
- //! \param ui32Port is the base address of the GPIO port.
- //! \param ui8Pins is the bit-packed representation of the pin(s).
- //!
- //! Unmasks the interrupt for the specified pin(s).
- //!
- //! The pin(s) are specified using a bit-packed byte, where each bit that is
- //! set identifies the pin to be accessed, and where bit 0 of the byte
- //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
- //!
- //! \return None
- //
- //*****************************************************************************
- void
- GPIOPowIntEnable(uint32_t ui32Port, uint8_t ui8Pins)
- {
- uint32_t ui32PortOffset;
- uint32_t ui32IntPins;
- //
- // Initialize value
- //
- ui32PortOffset = 0;
- //
- // Check the arguments.
- //
- ASSERT(GPIOBaseValid(ui32Port));
- //
- // Find bit mask for wanted pin(s)
- //
- if(ui32Port == GPIO_A_BASE)
- {
- ui32PortOffset = 0;
- }
- if(ui32Port == GPIO_B_BASE)
- {
- ui32PortOffset = 8;
- }
- if(ui32Port == GPIO_C_BASE)
- {
- ui32PortOffset = 16;
- }
- if(ui32Port == GPIO_D_BASE)
- {
- ui32PortOffset = 24;
- }
- ui32IntPins = ui8Pins << ui32PortOffset;
- //
- // Enable the interrupts.
- //
- HWREG(ui32Port + GPIO_O_PI_IEN) |= ui32IntPins;
- }
- //*****************************************************************************
- //
- //! Disables power-up interrupts for the specified pin(s)
- //!
- //! \param ui32Port is the base address of the GPIO port.
- //! \param ui8Pins is the bit-packed representation of the pin(s).
- //!
- //! Masks the interrupt for the specified pin(s).
- //!
- //! The pin(s) are specified using a bit-packed byte, where each bit that is
- //! set identifies the pin to be accessed, and where bit 0 of the byte
- //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
- //!
- //! \return None
- //
- //*****************************************************************************
- void
- GPIOPowIntDisable(uint32_t ui32Port, uint8_t ui8Pins)
- {
- uint32_t ui32PortOffset;
- uint32_t ui32IntPins;
- //
- // Initialize value
- //
- ui32PortOffset = 0;
- //
- // Check the arguments.
- //
- ASSERT(GPIOBaseValid(ui32Port));
- //
- // Find bit mask for wanted pin(s)
- //
- if(ui32Port == GPIO_A_BASE)
- {
- ui32PortOffset = 0;
- }
- if(ui32Port == GPIO_B_BASE)
- {
- ui32PortOffset = 8;
- }
- if(ui32Port == GPIO_C_BASE)
- {
- ui32PortOffset = 16;
- }
- if(ui32Port == GPIO_D_BASE)
- {
- ui32PortOffset = 24;
- }
- ui32IntPins = ui8Pins << ui32PortOffset;
- //
- // Disable the interrupts.
- //
- HWREG(ui32Port + GPIO_O_PI_IEN) &= ~(ui32IntPins);
- }
- //*****************************************************************************
- //
- //! Gets power-up interrupt status for the specified GPIO port
- //!
- //! \param ui32Port is the base address of the GPIO port.
- //! \param bMasked specifies whether masked or raw interrupt status is
- //! returned.
- //!
- //! If \e bMasked is set as \b true, then the masked interrupt status is
- //! returned; otherwise, the raw interrupt status is returned.
- //!
- //! \return Returns a bit-packed byte, where each bit that is set identifies
- //! an active masked or raw interrupt, and where bit 0 of the byte
- //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
- //! Bits 31:8 should be ignored.
- //
- //*****************************************************************************
- uint32_t
- GPIOPowIntStatus(uint32_t ui32Port, bool bMasked)
- {
- uint32_t ui32PortOffset;
- //
- // Initialize value
- //
- ui32PortOffset = 0;
- // Check the arguments.
- ASSERT(GPIOBaseValid(ui32Port));
- //
- // Find bit mask for wanted pin(s)
- //
- if(ui32Port == GPIO_A_BASE)
- {
- ui32PortOffset = 0;
- }
- if(ui32Port == GPIO_B_BASE)
- {
- ui32PortOffset = 8;
- }
- if(ui32Port == GPIO_C_BASE)
- {
- ui32PortOffset = 16;
- }
- if(ui32Port == GPIO_D_BASE)
- {
- ui32PortOffset = 24;
- }
- // Return the interrupt status.
- if(bMasked)
- {
- return((HWREG(ui32Port + GPIO_O_IRQ_DETECT_ACK) >> ui32PortOffset) &
- 0xFF);
- }
- else
- {
- return((HWREG(ui32Port + GPIO_O_IRQ_DETECT_UNMASK) >> ui32PortOffset) &
- 0xFF);
- }
- }
- //*****************************************************************************
- //
- //! Clears the power-up interrupt for the specified pin(s)
- //!
- //! \param ui32Port is the base address of the GPIO port.
- //! \param ui8Pins is the bit-packed representation of the pin(s).
- //!
- //! Clears the interrupt for the specified pin(s).
- //!
- //! The pin(s) are specified using a bit-packed byte, where each bit that is
- //! set identifies the pin to be accessed, and where bit 0 of the byte
- //! represents GPIO port pin 0, bit 1 represents GPIO port pin 1, and so on.
- //!
- //! \return None
- //
- //*****************************************************************************
- void
- GPIOPowIntClear(uint32_t ui32Port, uint8_t ui8Pins)
- {
- uint32_t ui32PortOffset;
- uint32_t ui32IntPins;
- //
- // Initialize value
- //
- ui32PortOffset = 0;
- //
- // Check the arguments.
- //
- ASSERT(GPIOBaseValid(ui32Port));
- //
- // Find bit mask for wanted pin(s)
- //
- if(ui32Port == GPIO_A_BASE)
- {
- ui32PortOffset = 0;
- }
- if(ui32Port == GPIO_B_BASE)
- {
- ui32PortOffset = 8;
- }
- if(ui32Port == GPIO_C_BASE)
- {
- ui32PortOffset = 16;
- }
- if(ui32Port == GPIO_D_BASE)
- {
- ui32PortOffset = 24;
- }
- ui32IntPins = ui8Pins << ui32PortOffset;
- //
- // Clear the interrupts.
- //
- HWREG(ui32Port + GPIO_O_IRQ_DETECT_ACK) |= ui32IntPins;
- }
- //*****************************************************************************
- //
- //! Enable Wake Up Interrupt
- //!
- //! \param ui32Config is the source to enable wake up on interrupt.
- //!
- //! This function enables wake up on interrupt from the selected sources.
- //!
- //! The \e ui32Config argument must be one or the logical or of several of
- //! the following values:
- //!
- //! \b GPIO_IWE_PORT_A, \b GPIO_IWE_PORT_B, \b GPIO_IWE_PORT_C,
- //! \b GPIO_IWE_PORT_D, \b GPIO_IWE_USB,
- //! \b GPIO_IWE_SM_TIMER.
- //!
- //! \return None
- //
- //*****************************************************************************
- void
- GPIOIntWakeupEnable(uint32_t ui32Config)
- {
- ASSERT((ui32Config &
- (GPIO_IWE_PORT_A |
- GPIO_IWE_PORT_B |
- GPIO_IWE_PORT_C |
- GPIO_IWE_PORT_D |
- GPIO_IWE_USB |
- GPIO_IWE_SM_TIMER)) != 0);
- //
- // Enable Wakeup from selected Interrupt sources
- //
- HWREG(SYS_CTRL_IWE) |= ui32Config;
- }
- //*****************************************************************************
- //
- //! Disable Wake Up Interrupt
- //!
- //! \param ui32Config is the source to disable wake on interrupt from.
- //!
- //! This function disables Wake up on interrupt from the selected sources.
- //!
- //! The \e ui32Config argument must be one or the logical or of several of
- //! the following values:
- //!
- //! \b GPIO_IWE_PORT_A, \b GPIO_IWE_PORT_B, \b GPIO_IWE_PORT_C,
- //! \b GPIO_IWE_PORT_D, \b GPIO_IWE_USB,
- //! \b GPIO_IWE_SM_TIMER,
- //!
- //! \return None
- //
- //*****************************************************************************
- void
- GPIOIntWakeupDisable(uint32_t ui32Config)
- {
- ASSERT((ui32Config &
- (GPIO_IWE_PORT_A |
- GPIO_IWE_PORT_B |
- GPIO_IWE_PORT_C |
- GPIO_IWE_PORT_D |
- GPIO_IWE_USB |
- GPIO_IWE_SM_TIMER)) != 0);
- //
- // Disable Wakeup from selected Interrupt sources
- //
- HWREG(SYS_CTRL_IWE) &= ~ui32Config;
- }
- //*****************************************************************************
- //! Close the Doxygen group.
- //! @}
- //*****************************************************************************
|