em_gpio.c

Go to the documentation of this file.
00001 /***************************************************************************/
00035 #include "em_gpio.h"
00036 
00037 #if defined(GPIO_COUNT) && (GPIO_COUNT > 0)
00038 /***************************************************************************/
00043 /***************************************************************************/
00049 /*******************************************************************************
00050  *******************************   DEFINES   ***********************************
00051  ******************************************************************************/
00052 
00056 #define GPIO_DRIVEMODE_VALID(mode)    ((mode) <= 3)
00057 
00061 /*******************************************************************************
00062  **************************   GLOBAL FUNCTIONS   *******************************
00063  ******************************************************************************/
00064 
00065 /***************************************************************************/
00075 void GPIO_DbgLocationSet(unsigned int location)
00076 {
00077 #if defined ( _GPIO_ROUTE_SWLOCATION_MASK )
00078   EFM_ASSERT(location < AFCHANLOC_MAX);
00079 
00080   GPIO->ROUTE = (GPIO->ROUTE & ~_GPIO_ROUTE_SWLOCATION_MASK) |
00081                 (location << _GPIO_ROUTE_SWLOCATION_SHIFT);
00082 #else
00083   (void)location;
00084 #endif
00085 }
00086 
00087 
00088 /***************************************************************************/
00098 void GPIO_DriveModeSet(GPIO_Port_TypeDef port, GPIO_DriveMode_TypeDef mode)
00099 {
00100   EFM_ASSERT(GPIO_PORT_VALID(port) && GPIO_DRIVEMODE_VALID(mode));
00101 
00102   GPIO->P[port].CTRL = (GPIO->P[port].CTRL & ~(_GPIO_P_CTRL_DRIVEMODE_MASK))
00103                        | (mode << _GPIO_P_CTRL_DRIVEMODE_SHIFT);
00104 }
00105 
00106 
00107 /***************************************************************************/
00143 void GPIO_IntConfig(GPIO_Port_TypeDef port,
00144                     unsigned int pin,
00145                     bool risingEdge,
00146                     bool fallingEdge,
00147                     bool enable)
00148 {
00149   uint32_t tmp;
00150 
00151   EFM_ASSERT(GPIO_PORT_VALID(port) && GPIO_PIN_VALID(pin));
00152 
00153   /* There are two registers controlling the interrupt configuration:
00154    * The EXTIPSELL register controls pins 0-7 and EXTIPSELH controls
00155    * pins 8-15. */
00156   if (pin < 8)
00157   {
00158     GPIO->EXTIPSELL = (GPIO->EXTIPSELL & ~(0xF << (4 * pin))) |
00159                       (port << (4 * pin));
00160   }
00161   else
00162   {
00163     tmp             = pin - 8;
00164     GPIO->EXTIPSELH = (GPIO->EXTIPSELH & ~(0xF << (4 * tmp))) |
00165                       (port << (4 * tmp));
00166   }
00167 
00168   /* Enable/disable rising edge */
00169   BITBAND_Peripheral(&(GPIO->EXTIRISE), pin, (unsigned int)risingEdge);
00170 
00171   /* Enable/disable falling edge */
00172   BITBAND_Peripheral(&(GPIO->EXTIFALL), pin, (unsigned int)fallingEdge);
00173 
00174   /* Clear any pending interrupt */
00175   GPIO->IFC = 1 << pin;
00176 
00177   /* Finally enable/disable interrupt */
00178   BITBAND_Peripheral(&(GPIO->IEN), pin, (unsigned int)enable);
00179 }
00180 
00181 
00182 /***************************************************************************/
00199 void GPIO_PinModeSet(GPIO_Port_TypeDef port,
00200                      unsigned int pin,
00201                      GPIO_Mode_TypeDef mode,
00202                      unsigned int out)
00203 {
00204   EFM_ASSERT(GPIO_PORT_VALID(port) && GPIO_PIN_VALID(pin));
00205 
00206   /* If disabling pin, do not modify DOUT in order to reduce chance for */
00207   /* glitch/spike (may not be sufficient precaution in all use cases) */
00208   if (mode != gpioModeDisabled)
00209   {
00210     if (out)
00211     {
00212       GPIO->P[port].DOUTSET = 1 << pin;
00213     }
00214     else
00215     {
00216       GPIO->P[port].DOUTCLR = 1 << pin;
00217     }
00218   }
00219 
00220   /* There are two registers controlling the pins for each port. The MODEL
00221    * register controls pins 0-7 and MODEH controls pins 8-15. */
00222   if (pin < 8)
00223   {
00224     GPIO->P[port].MODEL = (GPIO->P[port].MODEL & ~(0xF << (pin * 4))) |
00225                           (mode << (pin * 4));
00226   }
00227   else
00228   {
00229     GPIO->P[port].MODEH = (GPIO->P[port].MODEH & ~(0xF << ((pin - 8) * 4))) |
00230                           (mode << ((pin - 8) * 4));
00231   }
00232 
00233   if (mode == gpioModeDisabled)
00234   {
00235     if (out)
00236     {
00237       GPIO->P[port].DOUTSET = 1 << pin;
00238     }
00239     else
00240     {
00241       GPIO->P[port].DOUTCLR = 1 << pin;
00242     }
00243   }
00244 }
00245 
00249 #endif /* defined(GPIO_COUNT) && (GPIO_COUNT > 0) */