efm32_gpio.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /***************************************************************************//**
  2. * @file
  3. * @brief General Purpose IO (GPIO) peripheral API for EFM32.
  4. * @author Energy Micro AS
  5. * @version 2.0.0
  6. *******************************************************************************
  7. * @section License
  8. * <b>(C) Copyright 2010 Energy Micro AS, http://www.energymicro.com</b>
  9. *******************************************************************************
  10. *
  11. * This source code is the property of Energy Micro AS. The source and compiled
  12. * code may only be used on Energy Micro "EFM32" microcontrollers.
  13. *
  14. * This copyright notice may not be removed from the source code nor changed.
  15. *
  16. * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Energy Micro AS has no
  17. * obligation to support this Software. Energy Micro AS is providing the
  18. * Software "AS IS", with no express or implied warranties of any kind,
  19. * including, but not limited to, any implied warranties of merchantability
  20. * or fitness for any particular purpose or warranties against infringement
  21. * of any proprietary rights of a third party.
  22. *
  23. * Energy Micro AS will not be liable for any consequential, incidental, or
  24. * special damages, or any other relief, or for any claim by any third party,
  25. * arising from your use of this Software.
  26. *
  27. ******************************************************************************/
  28. #ifndef __EFM32_GPIO_H
  29. #define __EFM32_GPIO_H
  30. #include <stdbool.h>
  31. #include "efm32.h"
  32. #include "efm32_bitband.h"
  33. #include "efm32_assert.h"
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. /***************************************************************************//**
  38. * @addtogroup EFM32_Library
  39. * @{
  40. ******************************************************************************/
  41. /***************************************************************************//**
  42. * @addtogroup GPIO
  43. * @{
  44. ******************************************************************************/
  45. /*******************************************************************************
  46. ******************************** ENUMS ************************************
  47. ******************************************************************************/
  48. /** GPIO ports identificator. */
  49. typedef enum
  50. {
  51. gpioPortA = 0, /**< Port A */
  52. gpioPortB = 1, /**< Port B */
  53. gpioPortC = 2, /**< Port C */
  54. gpioPortD = 3, /**< Port D */
  55. gpioPortE = 4, /**< Port E */
  56. gpioPortF = 5 /**< Port F */
  57. } GPIO_Port_TypeDef;
  58. /** GPIO drive mode. */
  59. typedef enum
  60. {
  61. /** Default 6mA */
  62. gpioDriveModeStandard = GPIO_P_CTRL_DRIVEMODE_STANDARD,
  63. /** 0.5 mA */
  64. gpioDriveModeLowest = GPIO_P_CTRL_DRIVEMODE_LOWEST,
  65. /** 20 mA */
  66. gpioDriveModeHigh = GPIO_P_CTRL_DRIVEMODE_HIGH,
  67. /** 2 mA */
  68. gpioDriveModeLow = GPIO_P_CTRL_DRIVEMODE_LOW
  69. } GPIO_DriveMode_TypeDef;
  70. /** Pin mode. For more details on each mode, please refer to the EFM32
  71. * reference manual. */
  72. typedef enum
  73. {
  74. /** Input disabled. Pullup if DOUT is set. */
  75. gpioModeDisabled = _GPIO_P_MODEL_MODE0_DISABLED,
  76. /** Input enabled. Filter if DOUT is set */
  77. gpioModeInput = _GPIO_P_MODEL_MODE0_INPUT,
  78. /** Input enabled. DOUT determines pull direction */
  79. gpioModeInputPull = _GPIO_P_MODEL_MODE0_INPUTPULL,
  80. /** Input enabled with filter. DOUT determines pull direction */
  81. gpioModeInputPullFilter = _GPIO_P_MODEL_MODE0_INPUTPULLFILTER,
  82. /** Push-pull output */
  83. gpioModePushPull = _GPIO_P_MODEL_MODE0_PUSHPULL,
  84. /** Push-pull output with drive-strength set by DRIVEMODE */
  85. gpioModePushPullDrive = _GPIO_P_MODEL_MODE0_PUSHPULLDRIVE,
  86. /** Wired-or output */
  87. gpioModeWiredOr = _GPIO_P_MODEL_MODE0_WIREDOR,
  88. /** Wired-or output with pull-down */
  89. gpioModeWiredOrPullDown = _GPIO_P_MODEL_MODE0_WIREDORPULLDOWN,
  90. /** Open-drain output */
  91. gpioModeWiredAnd = _GPIO_P_MODEL_MODE0_WIREDAND,
  92. /** Open-drain output with filter */
  93. gpioModeWiredAndFilter = _GPIO_P_MODEL_MODE0_WIREDANDFILTER,
  94. /** Open-drain output with pullup */
  95. gpioModeWiredAndPullUp = _GPIO_P_MODEL_MODE0_WIREDANDPULLUP,
  96. /** Open-drain output with filter and pullup */
  97. gpioModeWiredAndPullUpFilter = _GPIO_P_MODEL_MODE0_WIREDANDPULLUPFILTER,
  98. /** Open-drain output with drive-strength set by DRIVEMODE */
  99. gpioModeWiredAndDrive = _GPIO_P_MODEL_MODE0_WIREDANDDRIVE,
  100. /** Open-drain output with filter and drive-strength set by DRIVEMODE */
  101. gpioModeWiredAndDriveFilter = _GPIO_P_MODEL_MODE0_WIREDANDDRIVEFILTER,
  102. /** Open-drain output with pullup and drive-strength set by DRIVEMODE */
  103. gpioModeWiredAndDrivePullUp = _GPIO_P_MODEL_MODE0_WIREDANDDRIVEPULLUP,
  104. /** Open-drain output with filter, pullup and drive-strength set by DRIVEMODE */
  105. gpioModeWiredAndDrivePullUpFilter = _GPIO_P_MODEL_MODE0_WIREDANDDRIVEPULLUPFILTER
  106. } GPIO_Mode_TypeDef;
  107. /*******************************************************************************
  108. ***************************** PROTOTYPES **********************************
  109. ******************************************************************************/
  110. void GPIO_DbgLocationSet(unsigned int location);
  111. #if defined(_EFM32_GIANT_FAMILY) || defined(_EFM32_TINY_FAMILY)
  112. static __INLINE void GPIO_EM4SetPinRetention(bool enable);
  113. #endif
  114. /***************************************************************************//**
  115. * @brief
  116. * Enable/disable serial wire clock pin.
  117. *
  118. * @note
  119. * Disabling SWDClk will disable the debug interface, which may result in
  120. * a lockout if done early in startup (before debugger is able to halt core).
  121. *
  122. * @param[in] enable
  123. * @li false - disable serial wire clock.
  124. * @li true - enable serial wire clock (default after reset).
  125. ******************************************************************************/
  126. static __INLINE void GPIO_DbgSWDClkEnable(bool enable)
  127. {
  128. BITBAND_Peripheral(&(GPIO->ROUTE), _GPIO_ROUTE_SWCLKPEN_SHIFT, (unsigned int)enable);
  129. }
  130. /***************************************************************************//**
  131. * @brief
  132. * Enable/disable serial wire data pin.
  133. *
  134. * @note
  135. * Disabling SWDClk will disable the debug interface, which may result in
  136. * a lockout if done early in startup (before debugger is able to halt core).
  137. *
  138. * @param[in] enable
  139. * @li false - disable serial wire data pin.
  140. * @li true - enable serial wire data pin (default after reset).
  141. ******************************************************************************/
  142. static __INLINE void GPIO_DbgSWDIOEnable(bool enable)
  143. {
  144. BITBAND_Peripheral(&(GPIO->ROUTE), _GPIO_ROUTE_SWDIOPEN_SHIFT, (unsigned int)enable);
  145. }
  146. /***************************************************************************//**
  147. * @brief
  148. * Enable/Disable serial wire output pin.
  149. *
  150. * @note
  151. * Enabling this pin is not sufficient to fully enable serial wire output
  152. * which is also dependent on issues outside the GPIO module. Please refer to
  153. * DBG_SWOEnable().
  154. *
  155. * @param[in] enable
  156. * @li false - disable serial wire viewer pin (default after reset).
  157. * @li true - enable serial wire viewer pin.
  158. ******************************************************************************/
  159. static __INLINE void GPIO_DbgSWOEnable(bool enable)
  160. {
  161. BITBAND_Peripheral(&(GPIO->ROUTE), _GPIO_ROUTE_SWOPEN_SHIFT, (unsigned int)enable);
  162. }
  163. void GPIO_DriveModeSet(GPIO_Port_TypeDef port, GPIO_DriveMode_TypeDef mode);
  164. #if defined(_EFM32_GIANT_FAMILY) || defined(_EFM32_TINY_FAMILY)
  165. /**************************************************************************//**
  166. * @brief
  167. * Disable GPIO pin wake-up from EM4.
  168. *
  169. * @param[in] pinmask
  170. * Bitmask containing the bitwise logic OR of which GPIO pin(s) to disable.
  171. * Refer to Reference Manuals for pinmask to GPIO port/pin mapping.
  172. *****************************************************************************/
  173. static __INLINE void GPIO_EM4DisablePinWakeup(uint32_t pinmask)
  174. {
  175. EFM_ASSERT((pinmask & ~_GPIO_EM4WUEN_MASK) == 0);
  176. GPIO->EM4WUEN &= ~pinmask;
  177. }
  178. /**************************************************************************//**
  179. * @brief
  180. * Enable GPIO pin wake-up from EM4. When the function exits,
  181. * EM4 mode can be safely entered.
  182. *
  183. * @note
  184. * It is assumed that the GPIO pin modes are set correctly.
  185. * Valid modes are @ref gpioModeInput and @ref gpioModeInputPull.
  186. *
  187. * @param[in] pinmask
  188. * Bitmask containing the bitwise logic OR of which GPIO pin(s) to enable.
  189. * Refer to Reference Manuals for pinmask to GPIO port/pin mapping.
  190. * @param[in] polaritymask
  191. * Bitmask containing the bitwise logic OR of GPIO pin(s) wake-up polarity.
  192. * Refer to Reference Manuals for pinmask to GPIO port/pin mapping.
  193. *****************************************************************************/
  194. static __INLINE void GPIO_EM4EnablePinWakeup(uint32_t pinmask,
  195. uint32_t polaritymask)
  196. {
  197. EFM_ASSERT((pinmask & ~_GPIO_EM4WUEN_MASK) == 0);
  198. EFM_ASSERT((polaritymask & ~_GPIO_EM4WUPOL_MASK) == 0);
  199. GPIO->EM4WUPOL &= ~pinmask; /* Set wakeup polarity */
  200. GPIO->EM4WUPOL |= pinmask & polaritymask;
  201. GPIO->EM4WUEN |= pinmask; /* Enable wakeup */
  202. GPIO_EM4SetPinRetention(true); /* Enable pin retention */
  203. GPIO->CMD = GPIO_CMD_EM4WUCLR; /* Clear wake-up logic */
  204. }
  205. /**************************************************************************//**
  206. * @brief
  207. * Check which GPIO pin(s) that caused a wake-up from EM4.
  208. *
  209. * @return
  210. * Bitmask containing the bitwise logic OR of which GPIO pin(s) caused the
  211. * wake-up. Refer to Reference Manuals for pinmask to GPIO port/pin mapping.
  212. *****************************************************************************/
  213. static __INLINE uint32_t GPIO_EM4GetPinWakeupCause(void)
  214. {
  215. return GPIO->EM4WUCAUSE & _GPIO_EM4WUCAUSE_MASK;
  216. }
  217. /**************************************************************************//**
  218. * @brief
  219. * Enable GPIO pin retention of output enable, output value, pull enable and
  220. * pull direction in EM4.
  221. *
  222. * @param[in] enable
  223. * @li true - enable EM4 pin retention.
  224. * @li false - disable EM4 pin retention.
  225. *****************************************************************************/
  226. static __INLINE void GPIO_EM4SetPinRetention(bool enable)
  227. {
  228. if (enable)
  229. {
  230. GPIO->CTRL |= GPIO_CTRL_EM4RET;
  231. }
  232. else
  233. {
  234. GPIO->CTRL &= ~GPIO_CTRL_EM4RET;
  235. }
  236. }
  237. #endif
  238. /***************************************************************************//**
  239. * @brief
  240. * Enable/disable input sensing.
  241. *
  242. * @details
  243. * Disabling input sensing if not used, can save some energy consumption.
  244. *
  245. * @param[in] val
  246. * Bitwise logic OR of one or more of:
  247. * @li GPIO_INSENSE_INTSENSE - interrupt input sensing.
  248. * @li GPIO_INSENSE_PRSSENSE - peripheral reflex system input sensing.
  249. *
  250. * @param[in] mask
  251. * Mask containing bitwise logic OR of bits similar as for @p val used to indicate
  252. * which input sense options to disable/enable.
  253. ******************************************************************************/
  254. static __INLINE void GPIO_InputSenseSet(uint32_t val, uint32_t mask)
  255. {
  256. GPIO->INSENSE = (GPIO->INSENSE & ~mask) | (val & mask);
  257. }
  258. /***************************************************************************//**
  259. * @brief
  260. * Clear one or more pending GPIO interrupts.
  261. *
  262. * @param[in] flags
  263. * Bitwise logic OR of GPIO interrupt sources to clear.
  264. ******************************************************************************/
  265. static __INLINE void GPIO_IntClear(uint32_t flags)
  266. {
  267. GPIO->IFC = flags;
  268. }
  269. void GPIO_IntConfig(GPIO_Port_TypeDef port,
  270. unsigned int pin,
  271. bool risingEdge,
  272. bool fallingEdge,
  273. bool enable);
  274. /***************************************************************************//**
  275. * @brief
  276. * Disable one or more GPIO interrupts.
  277. *
  278. * @param[in] flags
  279. * GPIO interrupt sources to disable.
  280. ******************************************************************************/
  281. static __INLINE void GPIO_IntDisable(uint32_t flags)
  282. {
  283. GPIO->IEN &= ~flags;
  284. }
  285. /***************************************************************************//**
  286. * @brief
  287. * Enable one or more GPIO interrupts.
  288. *
  289. * @note
  290. * Depending on the use, a pending interrupt may already be set prior to
  291. * enabling the interrupt. Consider using GPIO_IntClear() prior to enabling
  292. * if such a pending interrupt should be ignored.
  293. *
  294. * @param[in] flags
  295. * GPIO interrupt sources to enable.
  296. ******************************************************************************/
  297. static __INLINE void GPIO_IntEnable(uint32_t flags)
  298. {
  299. GPIO->IEN |= flags;
  300. }
  301. /***************************************************************************//**
  302. * @brief
  303. * Get pending GPIO interrupts.
  304. *
  305. * @return
  306. * GPIO interrupt sources pending.
  307. ******************************************************************************/
  308. static __INLINE uint32_t GPIO_IntGet(void)
  309. {
  310. return(GPIO->IF);
  311. }
  312. /***************************************************************************//**
  313. * @brief
  314. * Get enabled and pending GPIO interrupt flags.
  315. * Useful for handling more interrupt sources in the same interrupt handler.
  316. *
  317. * @note
  318. * Interrupt flags are not cleared by the use of this function.
  319. *
  320. * @return
  321. * Pending and enabled GPIO interrupt sources.
  322. * The return value is the bitwise AND combination of
  323. * - the OR combination of enabled interrupt sources in GPIO_IEN register
  324. * and
  325. * - the OR combination of valid interrupt flags in GPIO_IF register.
  326. ******************************************************************************/
  327. static __INLINE uint32_t GPIO_IntGetEnabled(void)
  328. {
  329. uint32_t tmp;
  330. /* Store GPIO->IEN in temporary variable in order to define explicit order
  331. * of volatile accesses. */
  332. tmp = GPIO->IEN;
  333. /* Bitwise AND of pending and enabled interrupts */
  334. return GPIO->IF & tmp;
  335. }
  336. /**************************************************************************//**
  337. * @brief
  338. * Set one or more pending GPIO interrupts from SW.
  339. *
  340. * @param[in] flags
  341. * GPIO interrupt sources to set to pending.
  342. *****************************************************************************/
  343. static __INLINE void GPIO_IntSet(uint32_t flags)
  344. {
  345. GPIO->IFS = flags;
  346. }
  347. /***************************************************************************//**
  348. * @brief
  349. * Locks the GPIO configuration.
  350. ******************************************************************************/
  351. static __INLINE void GPIO_Lock(void)
  352. {
  353. GPIO->LOCK = GPIO_LOCK_LOCKKEY_LOCK;
  354. }
  355. unsigned int GPIO_PinInGet(GPIO_Port_TypeDef port, unsigned int pin);
  356. void GPIO_PinModeSet(GPIO_Port_TypeDef port,
  357. unsigned int pin,
  358. GPIO_Mode_TypeDef mode,
  359. unsigned int out);
  360. void GPIO_PinOutClear(GPIO_Port_TypeDef port, unsigned int pin);
  361. unsigned int GPIO_PinOutGet(GPIO_Port_TypeDef port, unsigned int pin);
  362. void GPIO_PinOutSet(GPIO_Port_TypeDef port, unsigned int pin);
  363. void GPIO_PinOutToggle(GPIO_Port_TypeDef port, unsigned int pin);
  364. uint32_t GPIO_PortInGet(GPIO_Port_TypeDef port);
  365. void GPIO_PortOutClear(GPIO_Port_TypeDef port, uint32_t pins);
  366. uint32_t GPIO_PortOutGet(GPIO_Port_TypeDef port);
  367. void GPIO_PortOutSet(GPIO_Port_TypeDef port, uint32_t pins);
  368. void GPIO_PortOutSetVal(GPIO_Port_TypeDef port, uint32_t val, uint32_t mask);
  369. void GPIO_PortOutToggle(GPIO_Port_TypeDef port, uint32_t pins);
  370. /***************************************************************************//**
  371. * @brief
  372. * Unlocks the GPIO configuration.
  373. ******************************************************************************/
  374. static __INLINE void GPIO_Unlock(void)
  375. {
  376. GPIO->LOCK = GPIO_LOCK_LOCKKEY_UNLOCK;
  377. }
  378. /** @} (end addtogroup GPIO) */
  379. /** @} (end addtogroup EFM32_Library) */
  380. #ifdef __cplusplus
  381. }
  382. #endif
  383. #endif /* __EFM32_GPIO_H */