00001
00034 #ifndef __SILICON_LABS_EM_GPIO_H_
00035 #define __SILICON_LABS_EM_GPIO_H_
00036
00037 #include "em_device.h"
00038 #if defined(GPIO_COUNT) && (GPIO_COUNT > 0)
00039
00040 #include <stdbool.h>
00041 #include "em_bitband.h"
00042 #include "em_assert.h"
00043
00044 #ifdef __cplusplus
00045 extern "C" {
00046 #endif
00047
00048
00053
00058
00059
00060
00061
00063 typedef enum
00064 {
00065 gpioPortA = 0,
00066 gpioPortB = 1,
00067 gpioPortC = 2,
00068 gpioPortD = 3,
00069 gpioPortE = 4,
00070 gpioPortF = 5
00071 } GPIO_Port_TypeDef;
00072
00074 typedef enum
00075 {
00077 gpioDriveModeStandard = GPIO_P_CTRL_DRIVEMODE_STANDARD,
00079 gpioDriveModeLowest = GPIO_P_CTRL_DRIVEMODE_LOWEST,
00081 gpioDriveModeHigh = GPIO_P_CTRL_DRIVEMODE_HIGH,
00083 gpioDriveModeLow = GPIO_P_CTRL_DRIVEMODE_LOW
00084 } GPIO_DriveMode_TypeDef;
00085
00088 typedef enum
00089 {
00091 gpioModeDisabled = _GPIO_P_MODEL_MODE0_DISABLED,
00093 gpioModeInput = _GPIO_P_MODEL_MODE0_INPUT,
00095 gpioModeInputPull = _GPIO_P_MODEL_MODE0_INPUTPULL,
00097 gpioModeInputPullFilter = _GPIO_P_MODEL_MODE0_INPUTPULLFILTER,
00099 gpioModePushPull = _GPIO_P_MODEL_MODE0_PUSHPULL,
00101 gpioModePushPullDrive = _GPIO_P_MODEL_MODE0_PUSHPULLDRIVE,
00103 gpioModeWiredOr = _GPIO_P_MODEL_MODE0_WIREDOR,
00105 gpioModeWiredOrPullDown = _GPIO_P_MODEL_MODE0_WIREDORPULLDOWN,
00107 gpioModeWiredAnd = _GPIO_P_MODEL_MODE0_WIREDAND,
00109 gpioModeWiredAndFilter = _GPIO_P_MODEL_MODE0_WIREDANDFILTER,
00111 gpioModeWiredAndPullUp = _GPIO_P_MODEL_MODE0_WIREDANDPULLUP,
00113 gpioModeWiredAndPullUpFilter = _GPIO_P_MODEL_MODE0_WIREDANDPULLUPFILTER,
00115 gpioModeWiredAndDrive = _GPIO_P_MODEL_MODE0_WIREDANDDRIVE,
00117 gpioModeWiredAndDriveFilter = _GPIO_P_MODEL_MODE0_WIREDANDDRIVEFILTER,
00119 gpioModeWiredAndDrivePullUp = _GPIO_P_MODEL_MODE0_WIREDANDDRIVEPULLUP,
00121 gpioModeWiredAndDrivePullUpFilter = _GPIO_P_MODEL_MODE0_WIREDANDDRIVEPULLUPFILTER
00122 } GPIO_Mode_TypeDef;
00123
00124
00125
00126
00127
00128
00132 #define GPIO_PIN_VALID(pin) ((pin) < 16)
00133
00135 #define GPIO_PORT_VALID(port) ((port) <= gpioPortF)
00136
00140
00141
00142
00143
00144 void GPIO_DbgLocationSet(unsigned int location);
00145
00146 void GPIO_IntConfig(GPIO_Port_TypeDef port,
00147 unsigned int pin,
00148 bool risingEdge,
00149 bool fallingEdge,
00150 bool enable);
00151
00152 void GPIO_PinModeSet(GPIO_Port_TypeDef port,
00153 unsigned int pin,
00154 GPIO_Mode_TypeDef mode,
00155 unsigned int out);
00156
00157 # if defined( GPIO_CTRL_EM4RET )
00158 __STATIC_INLINE void GPIO_EM4SetPinRetention(bool enable);
00159 #endif
00160
00161
00173 __STATIC_INLINE void GPIO_DbgSWDClkEnable(bool enable)
00174 {
00175 BITBAND_Peripheral(&(GPIO->ROUTE), _GPIO_ROUTE_SWCLKPEN_SHIFT, (unsigned int)enable);
00176 }
00177
00178
00179
00191 __STATIC_INLINE void GPIO_DbgSWDIOEnable(bool enable)
00192 {
00193 BITBAND_Peripheral(&(GPIO->ROUTE), _GPIO_ROUTE_SWDIOPEN_SHIFT, (unsigned int)enable);
00194 }
00195
00196
00197 #if defined( GPIO_ROUTE_SWOPEN )
00198
00211 __STATIC_INLINE void GPIO_DbgSWOEnable(bool enable)
00212 {
00213 BITBAND_Peripheral(&(GPIO->ROUTE), _GPIO_ROUTE_SWOPEN_SHIFT, (unsigned int)enable);
00214 }
00215 #endif
00216
00217 void GPIO_DriveModeSet(GPIO_Port_TypeDef port, GPIO_DriveMode_TypeDef mode);
00218
00219
00220 # if defined( _GPIO_EM4WUEN_MASK )
00221
00229 __STATIC_INLINE void GPIO_EM4DisablePinWakeup(uint32_t pinmask)
00230 {
00231 EFM_ASSERT((pinmask & ~_GPIO_EM4WUEN_MASK) == 0);
00232
00233 GPIO->EM4WUEN &= ~pinmask;
00234 }
00235 #endif
00236
00237
00238 # if defined( _GPIO_EM4WUEN_MASK )
00239
00255 __STATIC_INLINE void GPIO_EM4EnablePinWakeup(uint32_t pinmask,
00256 uint32_t polaritymask)
00257 {
00258 EFM_ASSERT((pinmask & ~_GPIO_EM4WUEN_MASK) == 0);
00259 EFM_ASSERT((polaritymask & ~_GPIO_EM4WUPOL_MASK) == 0);
00260
00261 GPIO->EM4WUPOL &= ~pinmask;
00262 GPIO->EM4WUPOL |= pinmask & polaritymask;
00263 GPIO->EM4WUEN |= pinmask;
00264
00265 GPIO_EM4SetPinRetention(true);
00266
00267 GPIO->CMD = GPIO_CMD_EM4WUCLR;
00268 }
00269 #endif
00270
00271 #if defined( _GPIO_EM4WUCAUSE_MASK )
00272
00280 __STATIC_INLINE uint32_t GPIO_EM4GetPinWakeupCause(void)
00281 {
00282 return GPIO->EM4WUCAUSE & _GPIO_EM4WUCAUSE_MASK;
00283 }
00284 #endif
00285
00286
00287 # if defined( GPIO_CTRL_EM4RET )
00288
00297 __STATIC_INLINE void GPIO_EM4SetPinRetention(bool enable)
00298 {
00299 if (enable)
00300 {
00301 GPIO->CTRL |= GPIO_CTRL_EM4RET;
00302 }
00303 else
00304 {
00305 GPIO->CTRL &= ~GPIO_CTRL_EM4RET;
00306 }
00307 }
00308 #endif
00309
00310
00311
00327 __STATIC_INLINE void GPIO_InputSenseSet(uint32_t val, uint32_t mask)
00328 {
00329 GPIO->INSENSE = (GPIO->INSENSE & ~mask) | (val & mask);
00330 }
00331
00332
00333
00340 __STATIC_INLINE void GPIO_IntClear(uint32_t flags)
00341 {
00342 GPIO->IFC = flags;
00343 }
00344
00345
00346
00353 __STATIC_INLINE void GPIO_IntDisable(uint32_t flags)
00354 {
00355 GPIO->IEN &= ~flags;
00356 }
00357
00358
00359
00371 __STATIC_INLINE void GPIO_IntEnable(uint32_t flags)
00372 {
00373 GPIO->IEN |= flags;
00374 }
00375
00376
00377
00384 __STATIC_INLINE uint32_t GPIO_IntGet(void)
00385 {
00386 return(GPIO->IF);
00387 }
00388
00389
00390
00405 __STATIC_INLINE uint32_t GPIO_IntGetEnabled(void)
00406 {
00407 uint32_t tmp;
00408
00409
00410
00411 tmp = GPIO->IEN;
00412
00413
00414 return GPIO->IF & tmp;
00415 }
00416
00417
00418
00425 __STATIC_INLINE void GPIO_IntSet(uint32_t flags)
00426 {
00427 GPIO->IFS = flags;
00428 }
00429
00430
00431
00435 __STATIC_INLINE void GPIO_Lock(void)
00436 {
00437 GPIO->LOCK = GPIO_LOCK_LOCKKEY_LOCK;
00438 }
00439
00440
00441
00454 __STATIC_INLINE unsigned int GPIO_PinInGet(GPIO_Port_TypeDef port, unsigned int pin)
00455 {
00456 EFM_ASSERT(GPIO_PORT_VALID(port) && GPIO_PIN_VALID(pin));
00457
00458 return((unsigned int)((GPIO->P[port].DIN >> pin) & 0x1));
00459 }
00460
00461
00462
00477 __STATIC_INLINE void GPIO_PinOutClear(GPIO_Port_TypeDef port, unsigned int pin)
00478 {
00479 EFM_ASSERT(GPIO_PORT_VALID(port) && GPIO_PIN_VALID(pin));
00480
00481 GPIO->P[port].DOUTCLR = 1 << pin;
00482 }
00483
00484
00485
00498 __STATIC_INLINE unsigned int GPIO_PinOutGet(GPIO_Port_TypeDef port, unsigned int pin)
00499 {
00500 EFM_ASSERT(GPIO_PORT_VALID(port) && GPIO_PIN_VALID(pin));
00501
00502 return((unsigned int)((GPIO->P[port].DOUT >> pin) & 0x1));
00503 }
00504
00505
00506
00521 __STATIC_INLINE void GPIO_PinOutSet(GPIO_Port_TypeDef port, unsigned int pin)
00522 {
00523 EFM_ASSERT(GPIO_PORT_VALID(port) && GPIO_PIN_VALID(pin));
00524
00525 GPIO->P[port].DOUTSET = 1 << pin;
00526 }
00527
00528
00529
00544 __STATIC_INLINE void GPIO_PinOutToggle(GPIO_Port_TypeDef port, unsigned int pin)
00545 {
00546 EFM_ASSERT(GPIO_PORT_VALID(port) && GPIO_PIN_VALID(pin));
00547
00548 GPIO->P[port].DOUTTGL = 1 << pin;
00549 }
00550
00551
00552
00559 __STATIC_INLINE uint32_t GPIO_PortInGet(GPIO_Port_TypeDef port)
00560 {
00561 EFM_ASSERT(GPIO_PORT_VALID(port));
00562
00563 return(GPIO->P[port].DIN & _GPIO_P_DIN_DIN_MASK);
00564 }
00565
00566
00567
00582 __STATIC_INLINE void GPIO_PortOutClear(GPIO_Port_TypeDef port, uint32_t pins)
00583 {
00584 EFM_ASSERT(GPIO_PORT_VALID(port));
00585
00586 GPIO->P[port].DOUTCLR = pins & _GPIO_P_DOUTCLR_DOUTCLR_MASK;
00587 }
00588
00589
00590
00600 __STATIC_INLINE uint32_t GPIO_PortOutGet(GPIO_Port_TypeDef port)
00601 {
00602 EFM_ASSERT(GPIO_PORT_VALID(port));
00603
00604 return(GPIO->P[port].DOUT & _GPIO_P_DOUT_DOUT_MASK);
00605 }
00606
00607
00608
00623 __STATIC_INLINE void GPIO_PortOutSet(GPIO_Port_TypeDef port, uint32_t pins)
00624 {
00625 EFM_ASSERT(GPIO_PORT_VALID(port));
00626
00627 GPIO->P[port].DOUTSET = pins & _GPIO_P_DOUTSET_DOUTSET_MASK;
00628 }
00629
00630
00631
00649 __STATIC_INLINE void GPIO_PortOutSetVal(GPIO_Port_TypeDef port, uint32_t val, uint32_t mask)
00650 {
00651 EFM_ASSERT(GPIO_PORT_VALID(port));
00652
00653 GPIO->P[port].DOUT = (GPIO->P[port].DOUT & ~mask) | (val & mask);
00654 }
00655
00656
00657
00672 __STATIC_INLINE void GPIO_PortOutToggle(GPIO_Port_TypeDef port, uint32_t pins)
00673 {
00674 EFM_ASSERT(GPIO_PORT_VALID(port));
00675
00676 GPIO->P[port].DOUTTGL = pins & _GPIO_P_DOUTTGL_DOUTTGL_MASK;
00677 }
00678
00679
00680
00684 __STATIC_INLINE void GPIO_Unlock(void)
00685 {
00686 GPIO->LOCK = GPIO_LOCK_LOCKKEY_UNLOCK;
00687 }
00688
00689
00693 #ifdef __cplusplus
00694 }
00695 #endif
00696
00697 #endif
00698 #endif