efm32_gpio.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /***************************************************************************//**
  2. * @file
  3. * @brief General Purpose IO (GPIO) peripheral API for EFM32.
  4. * @author Energy Micro AS
  5. * @version 1.3.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. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /***************************************************************************//**
  37. * @addtogroup EFM32_Library
  38. * @{
  39. ******************************************************************************/
  40. /***************************************************************************//**
  41. * @addtogroup GPIO
  42. * @{
  43. ******************************************************************************/
  44. /*******************************************************************************
  45. ******************************** ENUMS ************************************
  46. ******************************************************************************/
  47. /** GPIO ports identificator. */
  48. typedef enum
  49. {
  50. gpioPortA = 0, /**< Port A */
  51. gpioPortB = 1, /**< Port B */
  52. gpioPortC = 2, /**< Port C */
  53. gpioPortD = 3, /**< Port D */
  54. gpioPortE = 4, /**< Port E */
  55. gpioPortF = 5 /**< Port F */
  56. } GPIO_Port_TypeDef;
  57. /** GPIO drive mode. */
  58. typedef enum
  59. {
  60. /** Default 6mA */
  61. gpioDriveModeStandard = GPIO_P_CTRL_DRIVEMODE_STANDARD,
  62. /** 0.5 mA */
  63. gpioDriveModeLowest = GPIO_P_CTRL_DRIVEMODE_LOWEST,
  64. /** 20 mA */
  65. gpioDriveModeHigh = GPIO_P_CTRL_DRIVEMODE_HIGH,
  66. /** 2 mA */
  67. gpioDriveModeLow = GPIO_P_CTRL_DRIVEMODE_LOW
  68. } GPIO_DriveMode_TypeDef;
  69. /** Pin mode. For more details on each mode, please refer to the EFM32
  70. * reference manual. */
  71. typedef enum
  72. {
  73. /** Input disabled. Pullup if DOUT is set. */
  74. gpioModeDisabled = _GPIO_P_MODEL_MODE0_DISABLED,
  75. /** Input enabled. Filter if DOUT is set */
  76. gpioModeInput = _GPIO_P_MODEL_MODE0_INPUT,
  77. /** Input enabled. DOUT determines pull direction */
  78. gpioModeInputPull = _GPIO_P_MODEL_MODE0_INPUTPULL,
  79. /** Input enabled with filter. DOUT determines pull direction */
  80. gpioModeInputPullFilter = _GPIO_P_MODEL_MODE0_INPUTPULLFILTER,
  81. /** Push-pull output */
  82. gpioModePushPull = _GPIO_P_MODEL_MODE0_PUSHPULL,
  83. /** Push-pull output with drive-strength set by DRIVEMODE */
  84. gpioModePushPullDrive = _GPIO_P_MODEL_MODE0_PUSHPULLDRIVE,
  85. /** Wired-or output */
  86. gpioModeWiredOr = _GPIO_P_MODEL_MODE0_WIREDOR,
  87. /** Wired-or output with pull-down */
  88. gpioModeWiredOrPullDown = _GPIO_P_MODEL_MODE0_WIREDORPULLDOWN,
  89. /** Open-drain output */
  90. gpioModeWiredAnd = _GPIO_P_MODEL_MODE0_WIREDAND,
  91. /** Open-drain output with filter */
  92. gpioModeWiredAndFilter = _GPIO_P_MODEL_MODE0_WIREDANDFILTER,
  93. /** Open-drain output with pullup */
  94. gpioModeWiredAndPullUp = _GPIO_P_MODEL_MODE0_WIREDANDPULLUP,
  95. /** Open-drain output with filter and pullup */
  96. gpioModeWiredAndPullUpFilter = _GPIO_P_MODEL_MODE0_WIREDANDPULLUPFILTER,
  97. /** Open-drain output with drive-strength set by DRIVEMODE */
  98. gpioModeWiredAndDrive = _GPIO_P_MODEL_MODE0_WIREDANDDRIVE,
  99. /** Open-drain output with filter and drive-strength set by DRIVEMODE */
  100. gpioModeWiredAndDriveFilter = _GPIO_P_MODEL_MODE0_WIREDANDDRIVEFILTER,
  101. /** Open-drain output with pullup and drive-strength set by DRIVEMODE */
  102. gpioModeWiredAndDrivePullUp = _GPIO_P_MODEL_MODE0_WIREDANDDRIVEPULLUP,
  103. /** Open-drain output with filter, pullup and drive-strength set by DRIVEMODE */
  104. gpioModeWiredAndDrivePullUpFilter = _GPIO_P_MODEL_MODE0_WIREDANDDRIVEPULLUPFILTER
  105. } GPIO_Mode_TypeDef;
  106. /*******************************************************************************
  107. ***************************** PROTOTYPES **********************************
  108. ******************************************************************************/
  109. void GPIO_DbgLocationSet(unsigned int location);
  110. /***************************************************************************//**
  111. * @brief
  112. * Enable/disable serial wire clock pin.
  113. *
  114. * @note
  115. * Disabling SWDClk will disable the debug interface, which may result in
  116. * a lockout if done early in startup (before debugger is able to halt core).
  117. *
  118. * @param[in] enable
  119. * @li false - disable serial wire clock.
  120. * @li true - enable serial wire clock (default after reset).
  121. ******************************************************************************/
  122. static __INLINE void GPIO_DbgSWDClkEnable(bool enable)
  123. {
  124. BITBAND_Peripheral(&(GPIO->ROUTE), _GPIO_ROUTE_SWCLKPEN_SHIFT, (unsigned int) enable);
  125. }
  126. /***************************************************************************//**
  127. * @brief
  128. * Enable/disable serial wire data pin.
  129. *
  130. * @note
  131. * Disabling SWDClk will disable the debug interface, which may result in
  132. * a lockout if done early in startup (before debugger is able to halt core).
  133. *
  134. * @param[in] enable
  135. * @li false - disable serial wire data pin.
  136. * @li true - enable serial wire data pin (default after reset).
  137. ******************************************************************************/
  138. static __INLINE void GPIO_DbgSWDIOEnable(bool enable)
  139. {
  140. BITBAND_Peripheral(&(GPIO->ROUTE), _GPIO_ROUTE_SWDIOPEN_SHIFT, (unsigned int) enable);
  141. }
  142. /***************************************************************************//**
  143. * @brief
  144. * Enable/Disable serial wire output pin.
  145. *
  146. * @note
  147. * Enabling this pin is not sufficient to fully enable serial wire output
  148. * which is also dependent on issues outside the GPIO module. Please refer to
  149. * DBG_SWOEnable().
  150. *
  151. * @param[in] enable
  152. * @li false - disable serial wire viewer pin (default after reset).
  153. * @li true - enable serial wire viewer pin.
  154. ******************************************************************************/
  155. static __INLINE void GPIO_DbgSWOEnable(bool enable)
  156. {
  157. BITBAND_Peripheral(&(GPIO->ROUTE), _GPIO_ROUTE_SWOPEN_SHIFT, (unsigned int) enable);
  158. }
  159. void GPIO_DriveModeSet(GPIO_Port_TypeDef port, GPIO_DriveMode_TypeDef mode);
  160. /***************************************************************************//**
  161. * @brief
  162. * Enable/disable input sensing.
  163. *
  164. * @details
  165. * Disabling input sensing if not used, can save some energy consumption.
  166. *
  167. * @param[in] val
  168. * Logical OR of one or more of:
  169. * @li GPIO_INSENSE_INTSENSE - interrupt input sensing.
  170. * @li GPIO_INSENSE_PRSSENSE - peripheral reflex system input sensing.
  171. *
  172. * @param[in] mask
  173. * Mask containing logical OR of bits similar as for @p val used to indicate
  174. * which input sense options to disable/enable.
  175. ******************************************************************************/
  176. static __INLINE void GPIO_InputSenseSet(uint32_t val, uint32_t mask)
  177. {
  178. GPIO->INSENSE = (GPIO->INSENSE & ~mask) | (val & mask);
  179. }
  180. /***************************************************************************//**
  181. * @brief
  182. * Clear one or more pending GPIO interrupts.
  183. *
  184. * @param[in] flags
  185. * GPIO interrupt sources to clear.
  186. ******************************************************************************/
  187. static __INLINE void GPIO_IntClear(uint32_t flags)
  188. {
  189. GPIO->IFC = flags;
  190. }
  191. void GPIO_IntConfig(GPIO_Port_TypeDef port,
  192. unsigned int pin,
  193. bool risingEdge,
  194. bool fallingEdge,
  195. bool enable);
  196. /***************************************************************************//**
  197. * @brief
  198. * Disable one or more GPIO interrupts.
  199. *
  200. * @param[in] flags
  201. * GPIO interrupt sources to disable.
  202. ******************************************************************************/
  203. static __INLINE void GPIO_IntDisable(uint32_t flags)
  204. {
  205. GPIO->IEN &= ~flags;
  206. }
  207. /***************************************************************************//**
  208. * @brief
  209. * Enable one or more GPIO interrupts.
  210. *
  211. * @note
  212. * Depending on the use, a pending interrupt may already be set prior to
  213. * enabling the interrupt. Consider using GPIO_IntClear() prior to enabling
  214. * if such a pending interrupt should be ignored.
  215. *
  216. * @param[in] flags
  217. * GPIO interrupt sources to enable.
  218. ******************************************************************************/
  219. static __INLINE void GPIO_IntEnable(uint32_t flags)
  220. {
  221. GPIO->IEN |= flags;
  222. }
  223. /***************************************************************************//**
  224. * @brief
  225. * Get pending GPIO interrupts.
  226. *
  227. * @return
  228. * GPIO interrupt sources pending.
  229. ******************************************************************************/
  230. static __INLINE uint32_t GPIO_IntGet(void)
  231. {
  232. return(GPIO->IF);
  233. }
  234. /**************************************************************************//**
  235. * @brief
  236. * Set one or more pending GPIO interrupts from SW.
  237. *
  238. * @param[in] flags
  239. * GPIO interrupt sources to set to pending.
  240. *****************************************************************************/
  241. static __INLINE void GPIO_IntSet(uint32_t flags)
  242. {
  243. GPIO->IFS = flags;
  244. }
  245. /***************************************************************************//**
  246. * @brief
  247. * Locks the GPIO configuration.
  248. ******************************************************************************/
  249. static __INLINE void GPIO_Lock(void)
  250. {
  251. GPIO->LOCK = GPIO_LOCK_LOCKKEY_LOCK;
  252. }
  253. unsigned int GPIO_PinInGet(GPIO_Port_TypeDef port, unsigned int pin);
  254. void GPIO_PinModeSet(GPIO_Port_TypeDef port,
  255. unsigned int pin,
  256. GPIO_Mode_TypeDef mode,
  257. unsigned int out);
  258. void GPIO_PinOutClear(GPIO_Port_TypeDef port, unsigned int pin);
  259. unsigned int GPIO_PinOutGet(GPIO_Port_TypeDef port, unsigned int pin);
  260. void GPIO_PinOutSet(GPIO_Port_TypeDef port, unsigned int pin);
  261. void GPIO_PinOutToggle(GPIO_Port_TypeDef port, unsigned int pin);
  262. uint32_t GPIO_PortInGet(GPIO_Port_TypeDef port);
  263. void GPIO_PortOutClear(GPIO_Port_TypeDef port, uint32_t pins);
  264. uint32_t GPIO_PortOutGet(GPIO_Port_TypeDef port);
  265. void GPIO_PortOutSet(GPIO_Port_TypeDef port, uint32_t pins);
  266. void GPIO_PortOutSetVal(GPIO_Port_TypeDef port, uint32_t val, uint32_t mask);
  267. void GPIO_PortOutToggle(GPIO_Port_TypeDef port, uint32_t pins);
  268. /***************************************************************************//**
  269. * @brief
  270. * Unlocks the GPIO configuration.
  271. ******************************************************************************/
  272. static __INLINE void GPIO_Unlock(void)
  273. {
  274. GPIO->LOCK = GPIO_LOCK_LOCKKEY_UNLOCK;
  275. }
  276. /** @} (end addtogroup GPIO) */
  277. /** @} (end addtogroup EFM32_Library) */
  278. #ifdef __cplusplus
  279. }
  280. #endif
  281. #endif /* __EFM32_GPIO_H */