mss_gpio.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*******************************************************************************
  2. * (c) Copyright 2008-2015 Microsemi SoC Products Group. All rights reserved.
  3. *
  4. * SmartFusion2 Microcontroller Subsystem GPIO bare metal software driver public
  5. * API.
  6. *
  7. * SVN $Revision: 7748 $
  8. * SVN $Date: 2015-09-04 11:36:30 +0530 (Fri, 04 Sep 2015) $
  9. */
  10. /*=========================================================================*//**
  11. @mainpage SmartFusion2 MSS GPIO Bare Metal Driver.
  12. @section intro_sec Introduction
  13. The SmartFusion2 Microcontroller Subsystem (MSS) includes a block of 32 general
  14. purpose input/outputs (GPIO).
  15. This software driver provides a set of functions for controlling the MSS GPIO
  16. block as part of a bare metal system where no operating system is available.
  17. This driver can be adapted for use as part of an operating system but the
  18. implementation of the adaptation layer between this driver and the operating
  19. system's driver model is outside the scope of this driver.
  20. @section hw_dependencies Hardware Flow Dependencies
  21. The configuration of all features of the MSS GPIOs is covered by this driver
  22. with the exception of the SmartFusion2 IOMUX configuration. SmartFusion2
  23. allows multiple non-concurrent uses of some external pins through IOMUX
  24. configuration. This feature allows optimization of external pin usage by
  25. assigning external pins for use by either the microcontroller subsystem or the
  26. FPGA fabric. The MSS GPIOs share SmartFusion2 device external pins with the
  27. FPGA fabric and with other MSS peripherals via an IOMUX. The MSS GPIO ports
  28. can alternatively be routed to the FPGA fabric through an IOMUX.
  29. The IOMUXs are configured using the SmartFusion2 MSS configurator tool. You
  30. must ensure that the MSS GPIOs are enabled and configured in the SmartFusion2
  31. MSS configurator if you wish to use them. For more information on IOMUXs,
  32. refer to the IOMUX section of the SmartFusion2 Microcontroller Subsystem (MSS)
  33. User’s Guide.
  34. The base address, register addresses and interrupt number assignment for the
  35. MSS GPIO block are defined as constants in the SmartFusion2 CMSIS HAL. You
  36. must ensure that the latest SmartFusion2 CMSIS HAL is included in the project
  37. settings of the software tool chain used to build your project and that it is
  38. generated into your project.
  39. @section theory_op Theory of Operation
  40. The MSS GPIO driver functions are grouped into the following categories:
  41. - Initialization
  42. - Configuration
  43. - Reading and setting GPIO state
  44. - Interrupt control
  45. Initialization
  46. The MSS GPIO driver is initialized through a call to the MSS_GPIO_init()
  47. function. The MSS_GPIO_init() function must be called before any other MSS
  48. GPIO driver functions can be called.
  49. Configuration
  50. Each GPIO port is individually configured through a call to the
  51. MSS_GPIO_config() function. Configuration includes deciding if a GPIO port
  52. will be used as an input, an output or both. GPIO ports configured as inputs
  53. can be further configured to generate interrupts based on the input's state.
  54. Interrupts can be level or edge sensitive.
  55. Reading and Setting GPIO State
  56. The state of the GPIO ports can be read and set using the following functions:
  57. - MSS_GPIO_get_inputs()
  58. - MSS_GPIO_get_outputs()
  59. - MSS_GPIO_set_outputs()
  60. - MSS_GPIO_set_output()
  61. - MSS_GPIO_drive_inout()
  62. Interrupt Control
  63. Interrupts generated by GPIO ports configured as inputs are controlled using
  64. the following functions:
  65. - MSS_GPIO_enable_irq()
  66. - MSS_GPIO_disable_irq()
  67. - MSS_GPIO_clear_irq()
  68. *//*=========================================================================*/
  69. #ifndef MSS_GPIO_H_
  70. #define MSS_GPIO_H_
  71. #ifdef __cplusplus
  72. extern "C" {
  73. #endif
  74. #include "../../CMSIS/m2sxxx.h"
  75. /*-------------------------------------------------------------------------*//**
  76. The mss_gpio_id_t enumeration is used to identify individual GPIO ports as an
  77. argument to functions:
  78. - MSS_GPIO_config()
  79. - MSS_GPIO_set_output() and MSS_GPIO_drive_inout()
  80. - MSS_GPIO_enable_irq(), MSS_GPIO_disable_irq() and MSS_GPIO_clear_irq()
  81. */
  82. typedef enum __mss_gpio_id_t
  83. {
  84. MSS_GPIO_0 = 0,
  85. MSS_GPIO_1 = 1,
  86. MSS_GPIO_2 = 2,
  87. MSS_GPIO_3 = 3,
  88. MSS_GPIO_4 = 4,
  89. MSS_GPIO_5 = 5,
  90. MSS_GPIO_6 = 6,
  91. MSS_GPIO_7 = 7,
  92. MSS_GPIO_8 = 8,
  93. MSS_GPIO_9 = 9,
  94. MSS_GPIO_10 = 10,
  95. MSS_GPIO_11 = 11,
  96. MSS_GPIO_12 = 12,
  97. MSS_GPIO_13 = 13,
  98. MSS_GPIO_14 = 14,
  99. MSS_GPIO_15 = 15,
  100. MSS_GPIO_16 = 16,
  101. MSS_GPIO_17 = 17,
  102. MSS_GPIO_18 = 18,
  103. MSS_GPIO_19 = 19,
  104. MSS_GPIO_20 = 20,
  105. MSS_GPIO_21 = 21,
  106. MSS_GPIO_22 = 22,
  107. MSS_GPIO_23 = 23,
  108. MSS_GPIO_24 = 24,
  109. MSS_GPIO_25 = 25,
  110. MSS_GPIO_26 = 26,
  111. MSS_GPIO_27 = 27,
  112. MSS_GPIO_28 = 28,
  113. MSS_GPIO_29 = 29,
  114. MSS_GPIO_30 = 30,
  115. MSS_GPIO_31 = 31
  116. } mss_gpio_id_t;
  117. /*-------------------------------------------------------------------------*//**
  118. These constant definitions are used as an argument to the
  119. MSS_GPIO_set_outputs() function to identify GPIO ports. A logical OR of these
  120. constants can be used to specify multiple GPIO ports.
  121. These definitions can also be used to identify GPIO ports through logical
  122. operations on the return value of the MSS_GPIO_get_inputs() function.
  123. */
  124. #define MSS_GPIO_0_MASK 0x00000001uL
  125. #define MSS_GPIO_1_MASK 0x00000002uL
  126. #define MSS_GPIO_2_MASK 0x00000004uL
  127. #define MSS_GPIO_3_MASK 0x00000008uL
  128. #define MSS_GPIO_4_MASK 0x00000010uL
  129. #define MSS_GPIO_5_MASK 0x00000020uL
  130. #define MSS_GPIO_6_MASK 0x00000040uL
  131. #define MSS_GPIO_7_MASK 0x00000080uL
  132. #define MSS_GPIO_8_MASK 0x00000100uL
  133. #define MSS_GPIO_9_MASK 0x00000200uL
  134. #define MSS_GPIO_10_MASK 0x00000400uL
  135. #define MSS_GPIO_11_MASK 0x00000800uL
  136. #define MSS_GPIO_12_MASK 0x00001000uL
  137. #define MSS_GPIO_13_MASK 0x00002000uL
  138. #define MSS_GPIO_14_MASK 0x00004000uL
  139. #define MSS_GPIO_15_MASK 0x00008000uL
  140. #define MSS_GPIO_16_MASK 0x00010000uL
  141. #define MSS_GPIO_17_MASK 0x00020000uL
  142. #define MSS_GPIO_18_MASK 0x00040000uL
  143. #define MSS_GPIO_19_MASK 0x00080000uL
  144. #define MSS_GPIO_20_MASK 0x00100000uL
  145. #define MSS_GPIO_21_MASK 0x00200000uL
  146. #define MSS_GPIO_22_MASK 0x00400000uL
  147. #define MSS_GPIO_23_MASK 0x00800000uL
  148. #define MSS_GPIO_24_MASK 0x01000000uL
  149. #define MSS_GPIO_25_MASK 0x02000000uL
  150. #define MSS_GPIO_26_MASK 0x04000000uL
  151. #define MSS_GPIO_27_MASK 0x08000000uL
  152. #define MSS_GPIO_28_MASK 0x10000000uL
  153. #define MSS_GPIO_29_MASK 0x20000000uL
  154. #define MSS_GPIO_30_MASK 0x40000000uL
  155. #define MSS_GPIO_31_MASK 0x80000000uL
  156. /*-------------------------------------------------------------------------*//**
  157. These constant definitions are used as an argument to the MSS_GPIO_config()
  158. function to specify the I/O mode of each GPIO port.
  159. */
  160. #define MSS_GPIO_INPUT_MODE 0x0000000002uL
  161. #define MSS_GPIO_OUTPUT_MODE 0x0000000005uL
  162. #define MSS_GPIO_INOUT_MODE 0x0000000003uL
  163. /*-------------------------------------------------------------------------*//**
  164. These constant definitions are used as an argument to the MSS_GPIO_config()
  165. function to specify the interrupt mode of each GPIO port.
  166. */
  167. #define MSS_GPIO_IRQ_LEVEL_HIGH 0x0000000000uL
  168. #define MSS_GPIO_IRQ_LEVEL_LOW 0x0000000020uL
  169. #define MSS_GPIO_IRQ_EDGE_POSITIVE 0x0000000040uL
  170. #define MSS_GPIO_IRQ_EDGE_NEGATIVE 0x0000000060uL
  171. #define MSS_GPIO_IRQ_EDGE_BOTH 0x0000000080uL
  172. /*-------------------------------------------------------------------------*//**
  173. The mss_gpio_inout_state_t enumeration is used to specify the output state of
  174. an INOUT GPIO port as an argument to the MSS_GPIO_drive_inout() function.
  175. */
  176. typedef enum mss_gpio_inout_state
  177. {
  178. MSS_GPIO_DRIVE_LOW = 0,
  179. MSS_GPIO_DRIVE_HIGH,
  180. MSS_GPIO_HIGH_Z
  181. } mss_gpio_inout_state_t;
  182. /*-------------------------------------------------------------------------*//**
  183. The MSS_GPIO_init() function initializes the SmartFusion2 MSS GPIO block. It
  184. resets the MSS GPIO hardware block and it also clears any pending MSS GPIO
  185. interrupts in the ARM Cortex-M3 interrupt controller. When the function exits,
  186. it takes the MSS GPIO block out of reset.
  187. @param
  188. This function has no parameters.
  189. @return
  190. This function does not return a value.
  191. */
  192. void MSS_GPIO_init( void );
  193. /*-------------------------------------------------------------------------*//**
  194. The MSS_GPIO_config() function is used to configure an individual GPIO port.
  195. @param port_id
  196. The port_id parameter identifies the GPIO port to be configured. An
  197. enumeration item of the form MSS_GPIO_n, where n is the number of the GPIO
  198. port, is used to identify the GPIO port. For example, MSS_GPIO_0 identifies
  199. the first GPIO port and MSS_GPIO_31 is the last one.
  200. @param config
  201. The config parameter specifies the configuration to be applied to the GPIO
  202. port identified by the port_id parameter. It is a logical OR of the required
  203. I/O mode and the required interrupt mode. The interrupt mode is not relevant
  204. if the GPIO is configured as an output only.
  205. These I/O mode constants are allowed:
  206. - MSS_GPIO_INPUT_MODE
  207. - MSS_GPIO_OUTPUT_MODE
  208. - MSS_GPIO_INOUT_MODE
  209. These interrupt mode constants are allowed:
  210. - MSS_GPIO_IRQ_LEVEL_HIGH
  211. - MSS_GPIO_IRQ_LEVEL_LOW
  212. - MSS_GPIO_IRQ_EDGE_POSITIVE
  213. - MSS_GPIO_IRQ_EDGE_NEGATIVE
  214. - MSS_GPIO_IRQ_EDGE_BOTH
  215. @return
  216. none.
  217. Example:
  218. The following call will configure GPIO 4 as an input generating interrupts on
  219. a Low to High transition of the input:
  220. @code
  221. MSS_GPIO_config( MSS_GPIO_4, MSS_GPIO_INPUT_MODE | MSS_GPIO_IRQ_EDGE_POSITIVE );
  222. @endcode
  223. */
  224. void MSS_GPIO_config
  225. (
  226. mss_gpio_id_t port_id,
  227. uint32_t config
  228. );
  229. /*-------------------------------------------------------------------------*//**
  230. The MSS_GPIO_set_outputs() function is used to set the state of all GPIO ports
  231. configured as outputs.
  232. @param value
  233. The value parameter specifies the state of the GPIO ports configured as
  234. outputs. It is a bit mask of the form (MSS_GPIO_n_MASK | MSS_GPIO_m_MASK)
  235. where n and m are numbers identifying GPIOs. For example, (MSS_GPIO_0_MASK |
  236. MSS_GPIO_1_MASK | MSS_GPIO_2_MASK ) specifies that the first, second and
  237. third GPIO outputs must be set High and all other GPIO outputs set Low. The
  238. driver provides 32 mask constants, MSS_GPIO_0_MASK to MSS_GPIO_31_MASK
  239. inclusive, for this purpose.
  240. @return
  241. none.
  242. Example 1:
  243. Set GPIOs outputs 0 and 8 high and all other GPIO outputs low.
  244. @code
  245. MSS_GPIO_set_outputs( MSS_GPIO_0_MASK | MSS_GPIO_8_MASK );
  246. @endcode
  247. Example 2:
  248. Set GPIOs outputs 2 and 4 low without affecting other GPIO outputs.
  249. @code
  250. uint32_t gpio_outputs;
  251. gpio_outputs = MSS_GPIO_get_outputs();
  252. gpio_outputs &= ~( MSS_GPIO_2_MASK | MSS_GPIO_4_MASK );
  253. MSS_GPIO_set_outputs( gpio_outputs );
  254. @endcode
  255. @see MSS_GPIO_get_outputs()
  256. */
  257. static __INLINE void
  258. MSS_GPIO_set_outputs
  259. (
  260. uint32_t value
  261. )
  262. {
  263. GPIO->GPIO_OUT = value;
  264. }
  265. /*-------------------------------------------------------------------------*//**
  266. The MSS_GPIO_set_output() function is used to set the state of a single GPIO
  267. port configured as an output.
  268. Note: Using bit-band writes might be a better option than this function for
  269. performance critical applications where the application code is not
  270. intended to be ported to a processor other than the ARM Cortex-M3 in
  271. SmartFusion2. The bit-band write equivalent to this function would be:
  272. GPIO_BITBAND->GPIO_OUT[port_id] = (uint32_t)value;
  273. @param port_id
  274. The port_id parameter identifies the GPIO port that is to have its output
  275. set. An enumeration item of the form MSS_GPIO_n, where n is the number of
  276. the GPIO port, is used to identify the GPIO port. For example, MSS_GPIO_0
  277. identifies the first GPIO port and MSS_GPIO_31 is the last one.
  278. @param value
  279. The value parameter specifies the desired state for the GPIO output. A value
  280. of 0 will set the output Low and a value of 1 will set the output High.
  281. @return
  282. This function does not return a value.
  283. Example:
  284. The following call will set GPIO output 12 High, leaving all other GPIO
  285. outputs unaffected:
  286. @code
  287. _GPIO_set_output(MSS_GPIO_12, 1);
  288. @endcode
  289. */
  290. void MSS_GPIO_set_output
  291. (
  292. mss_gpio_id_t port_id,
  293. uint8_t value
  294. );
  295. /*-------------------------------------------------------------------------*//**
  296. The MSS_GPIO_get_inputs() function is used to read the current state all GPIO
  297. ports configured as inputs.
  298. @return
  299. This function returns a 32-bit unsigned integer where each bit represents
  300. the state of a GPIO input. The least significant bit represents the state of
  301. GPIO input 0 and the most significant bit the state of GPIO input 31.
  302. Example:
  303. Read and assign the current state of the GPIO outputs to a variable.
  304. @code
  305. uint32_t gpio_inputs;
  306. gpio_inputs = MSS_GPIO_get_inputs();
  307. @endcode
  308. */
  309. static __INLINE uint32_t
  310. MSS_GPIO_get_inputs( void )
  311. {
  312. return GPIO->GPIO_IN;
  313. }
  314. /*-------------------------------------------------------------------------*//**
  315. The MSS_GPIO_get_outputs() function is used to read the current state all GPIO
  316. ports configured as outputs.
  317. @return
  318. This function returns a 32-bit unsigned integer where each bit represents
  319. the state of a GPIO output. The least significant bit represents the state
  320. of GPIO output 0 and the most significant bit the state of GPIO output 31.
  321. Example:
  322. Read and assign the current state of the GPIO outputs to a variable.
  323. @code
  324. uint32_t gpio_outputs;
  325. gpio_outputs = MSS_GPIO_get_outputs();
  326. @endcode
  327. */
  328. static __INLINE uint32_t
  329. MSS_GPIO_get_outputs( void )
  330. {
  331. return GPIO->GPIO_OUT;
  332. }
  333. /*-------------------------------------------------------------------------*//**
  334. The MSS_GPIO_drive_inout() function is used to set the output state of a
  335. single GPIO port configured as an INOUT. An INOUT GPIO can be in one of three
  336. states:
  337. - High
  338. - Low
  339. - High impedance
  340. An INOUT output would typically be used where several devices can drive the
  341. state of a shared signal line. The High and Low states are equivalent to the
  342. High and Low states of a GPIO configured as an output. The High impedance
  343. state is used to prevent the GPIO from driving its output state onto the
  344. signal line, while at the same time allowing the input state of the GPIO to
  345. be read.
  346. @param port_id
  347. The port_id parameter identifies the GPIO port for which you want to change
  348. the output state. An enumeration item of the form MSS_GPIO_n, where n is the
  349. number of the GPIO port, is used to identify the GPIO port. For example,
  350. MSS_GPIO_0 identifies the first GPIO port and MSS_GPIO_31 is the last one.
  351. @param inout_state
  352. The inout_state parameter specifies the state of the GPIO port identified by
  353. the port_id parameter. Allowed values of type mss_gpio_inout_state_t are as
  354. follows:
  355. - MSS_GPIO_DRIVE_HIGH
  356. - MSS_GPIO_DRIVE_LOW
  357. - MSS_GPIO_HIGH_Z (High impedance)
  358. @return
  359. This function does not return a value.
  360. Example:
  361. The call to MSS_GPIO_drive_inout() below will set the GPIO 7 output to the
  362. high impedance state.
  363. @code
  364. MSS_GPIO_drive_inout( MSS_GPIO_7, MSS_GPIO_HIGH_Z );
  365. @endcode
  366. */
  367. void MSS_GPIO_drive_inout
  368. (
  369. mss_gpio_id_t port_id,
  370. mss_gpio_inout_state_t inout_state
  371. );
  372. /*-------------------------------------------------------------------------*//**
  373. The MSS_GPIO_enable_irq() function is used to enable interrupt generation for
  374. the specified GPIO input. Interrupts are generated based on the state of the
  375. GPIO input and the interrupt mode configured for it by MSS_GPIO_config().
  376. @param port_id
  377. The port_id parameter identifies the GPIO port for which you want to enable
  378. interrupt generation. An enumeration item of the form MSS_GPIO_n, where n is
  379. the number of the GPIO port, is used to identify the GPIO port. For example,
  380. MSS_GPIO_0 identifies the first GPIO port and MSS_GPIO_31 is the last one.
  381. @return
  382. This function does not return a value.
  383. Example:
  384. The call to MSS_GPIO_enable_irq() below will allow GPIO 8 to generate
  385. interrupts.
  386. @code
  387. MSS_GPIO_enable_irq( MSS_GPIO_8 );
  388. @endcode
  389. */
  390. void MSS_GPIO_enable_irq
  391. (
  392. mss_gpio_id_t port_id
  393. );
  394. /*-------------------------------------------------------------------------*//**
  395. The MSS_GPIO_disable_irq() function is used to disable interrupt generation
  396. for the specified GPIO input.
  397. @param port_id
  398. The port_id parameter identifies the GPIO port for which you want to disable
  399. interrupt generation. An enumeration item of the form MSS_GPIO_n, where n is
  400. the number of the GPIO port, is used to identify the GPIO port. For example,
  401. MSS_GPIO_0 identifies the first GPIO port and MSS_GPIO_31 is the last one.
  402. @return
  403. This function does not return a value.
  404. Example:
  405. The call to MSS_GPIO_disable_irq() below will prevent GPIO 8 from generating
  406. interrupts.
  407. @code
  408. MSS_GPIO_disable_irq( MSS_GPIO_8 );
  409. @endcode
  410. */
  411. void MSS_GPIO_disable_irq
  412. (
  413. mss_gpio_id_t port_id
  414. );
  415. /*-------------------------------------------------------------------------*//**
  416. The MSS_GPIO_clear_irq() function is used to clear a pending interrupt from
  417. the specified GPIO input.
  418. Note: The MSS_GPIO_clear_irq() function must be called as part of any GPIO
  419. interrupt service routine (ISR) in order to prevent the same interrupt
  420. event retriggering a call to the GPIO ISR.
  421. @param port_id
  422. The port_id parameter identifies the GPIO port for which you want to clear
  423. the interrupt. An enumeration item of the form MSS_GPIO_n, where n is the
  424. number of the GPIO port, is used to identify the GPIO port. For example,
  425. MSS_GPIO_0 identifies the first GPIO port and MSS_GPIO_31 is the last one.
  426. @return
  427. none.
  428. Example:
  429. The example below demonstrates the use of the MSS_GPIO_clear_irq() function
  430. as part of the GPIO 9 interrupt service routine.
  431. @code
  432. void GPIO9_IRQHandler( void )
  433. {
  434. do_interrupt_processing();
  435. MSS_GPIO_clear_irq( MSS_GPIO_9 );
  436. }
  437. @endcode
  438. */
  439. void MSS_GPIO_clear_irq
  440. (
  441. mss_gpio_id_t port_id
  442. );
  443. #ifdef __cplusplus
  444. }
  445. #endif
  446. #endif /* MSS_GPIO_H_ */