1
0

em_gpio.h 16 KB

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