]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RISC-V_IGLOO2_Creative_SoftConsole/Microsemi_Code/drivers/CoreGPIO/core_gpio.h
Microsemi RISC-V project:
[freertos] / FreeRTOS / Demo / RISC-V_IGLOO2_Creative_SoftConsole / Microsemi_Code / drivers / CoreGPIO / core_gpio.h
1 /*******************************************************************************\r
2  * (c) Copyright 2008-2015 Microsemi SoC Products Group. All rights reserved.\r
3  * \r
4  *  CoreGPIO bare metal driver public API.\r
5  *\r
6  * SVN $Revision: 7964 $\r
7  * SVN $Date: 2015-10-09 18:26:53 +0530 (Fri, 09 Oct 2015) $\r
8  */\r
9 \r
10 /*=========================================================================*//**\r
11   @mainpage CoreGPIO Bare Metal Driver.\r
12 \r
13   @section intro_sec Introduction\r
14   The CoreGPIO hardware IP includes up to 32 general purpose input output GPIOs.\r
15   This driver provides a set of functions for controlling the GPIOs as part of a\r
16   bare metal system where no operating system is available. These drivers\r
17   can be adapted for use as part of an operating system but the implementation\r
18   of the adaptation layer between this driver and the operating system's driver\r
19   model is outside the scope of this driver.\r
20   \r
21   @section driver_configuration Driver Configuration\r
22   The CoreGPIO individual IOs can be configured either in the hardware flow or\r
23   as part of the software application through calls to the GPIO_config() function.\r
24   GPIOs configured as as part of the hardware is fixed and cannot be modified\r
25   using a call to the GPI_config() function.\r
26   \r
27   @section theory_op Theory of Operation\r
28   The CoreGPIO driver uses the Actel Hardware Abstraction Layer (HAL) to access\r
29   hardware registers. You must ensure that the Actel HAL is included as part of\r
30   your software project. The Actel HAL is available through the Actel Firmware\r
31   Catalog.\r
32   \r
33   The CoreGPIO driver functions are logically grouped into the following groups:\r
34     - Initiliazation\r
35     - Configuration\r
36     - Reading and writing GPIO state\r
37     - Interrupt control\r
38   \r
39   The CoreGPIO driver is initialized through a call to the GPIO_init() function.\r
40   The GPIO_init() function must be called before any other GPIO driver functions\r
41   can be called.\r
42   \r
43   Each GPIO port is individually configured through a call to the\r
44   GPIO_config() function. Configuration includes deciding if a GPIO port\r
45   will be used as input, output or both. GPIO ports configured as inputs can be\r
46   further configured to generate interrupts based on the input's state.\r
47   Interrupts can be level or edge sensitive.\r
48   Please note that a CoreGPIO hardware instance can be generated, as part of the\r
49   hardware flow, with a fixed configuration for some or all of its IOs. Attempting\r
50   to modify the configuration of such a hardware configured IO using the\r
51   GPIO_config() function has no effect.\r
52   \r
53   The state of the GPIO ports can be read and written using the following\r
54   functions:\r
55     - GPIO_get_inputs()\r
56     - GPIO_get_outputs()\r
57     - GPIO_set_outputs()\r
58     - GPIO_drive_inout()\r
59     \r
60   Interrupts generated by GPIO ports configured as inputs are controlled using\r
61   the following functions:\r
62     - GPIO_enable_irq()\r
63     - GPIO_disable_irq()\r
64     - GPIO_clear_irq()\r
65   \r
66  *//*=========================================================================*/\r
67 #ifndef CORE_GPIO_H_\r
68 #define CORE_GPIO_H_\r
69 \r
70 #include <stdint.h>\r
71 #include "cpu_types.h"\r
72 \r
73 /*-------------------------------------------------------------------------*//**\r
74   The gpio_id_t enumeration is used to identify GPIOs as part of the\r
75   parameter to functions:\r
76     - GPIO_config(),\r
77     - GPIO_drive_inout(),\r
78     - GPIO_enable_int(),\r
79     - GPIO_disable_int(),\r
80     - GPIO_clear_int()\r
81  */\r
82 typedef enum __gpio_id_t\r
83 {\r
84     GPIO_0 = 0,\r
85     GPIO_1 = 1,\r
86     GPIO_2 = 2,\r
87     GPIO_3 = 3,\r
88     GPIO_4 = 4,\r
89     GPIO_5 = 5,\r
90     GPIO_6 = 6,\r
91     GPIO_7 = 7,\r
92     GPIO_8 = 8,\r
93     GPIO_9 = 9,\r
94     GPIO_10 = 10,\r
95     GPIO_11 = 11,\r
96     GPIO_12 = 12,\r
97     GPIO_13 = 13,\r
98     GPIO_14 = 14,\r
99     GPIO_15 = 15,\r
100     GPIO_16 = 16,\r
101     GPIO_17 = 17,\r
102     GPIO_18 = 18,\r
103     GPIO_19 = 19,\r
104     GPIO_20 = 20,\r
105     GPIO_21 = 21,\r
106     GPIO_22 = 22,\r
107     GPIO_23 = 23,\r
108     GPIO_24 = 24,\r
109     GPIO_25 = 25,\r
110     GPIO_26 = 26,\r
111     GPIO_27 = 27,\r
112     GPIO_28 = 28,\r
113     GPIO_29 = 29,\r
114     GPIO_30 = 30,\r
115     GPIO_31 = 31\r
116 } gpio_id_t;\r
117 \r
118 typedef enum __gpio_apb_width_t\r
119 {\r
120     GPIO_APB_8_BITS_BUS = 0,\r
121     GPIO_APB_16_BITS_BUS = 1,\r
122     GPIO_APB_32_BITS_BUS = 2,\r
123     GPIO_APB_UNKNOWN_BUS_WIDTH = 3\r
124 } gpio_apb_width_t;\r
125 \r
126 /*-------------------------------------------------------------------------*//**\r
127  */\r
128 typedef struct __gpio_instance_t\r
129 {\r
130     addr_t              base_addr;\r
131     gpio_apb_width_t    apb_bus_width;\r
132 } gpio_instance_t;\r
133 \r
134 /*-------------------------------------------------------------------------*//**\r
135   GPIO ports definitions used to identify GPIOs as part of the parameter to\r
136   function GPIO_set_outputs().\r
137   These definitions can also be used to identity GPIO through logical\r
138   operations on the return value of function GPIO_get_inputs().\r
139  */\r
140 #define GPIO_0_MASK                 0x00000001UL\r
141 #define GPIO_1_MASK                 0x00000002UL\r
142 #define GPIO_2_MASK         0x00000004UL\r
143 #define GPIO_3_MASK             0x00000008UL\r
144 #define GPIO_4_MASK             0x00000010UL\r
145 #define GPIO_5_MASK             0x00000020UL\r
146 #define GPIO_6_MASK             0x00000040UL\r
147 #define GPIO_7_MASK             0x00000080UL\r
148 #define GPIO_8_MASK             0x00000100UL\r
149 #define GPIO_9_MASK                 0x00000200UL\r
150 #define GPIO_10_MASK            0x00000400UL\r
151 #define GPIO_11_MASK            0x00000800UL\r
152 #define GPIO_12_MASK            0x00001000UL\r
153 #define GPIO_13_MASK            0x00002000UL\r
154 #define GPIO_14_MASK            0x00004000UL\r
155 #define GPIO_15_MASK            0x00008000UL\r
156 #define GPIO_16_MASK            0x00010000UL\r
157 #define GPIO_17_MASK            0x00020000UL\r
158 #define GPIO_18_MASK            0x00040000UL\r
159 #define GPIO_19_MASK            0x00080000UL\r
160 #define GPIO_20_MASK            0x00100000UL\r
161 #define GPIO_21_MASK            0x00200000UL\r
162 #define GPIO_22_MASK            0x00400000UL\r
163 #define GPIO_23_MASK            0x00800000UL\r
164 #define GPIO_24_MASK            0x01000000UL\r
165 #define GPIO_25_MASK            0x02000000UL\r
166 #define GPIO_26_MASK            0x04000000UL\r
167 #define GPIO_27_MASK            0x08000000UL\r
168 #define GPIO_28_MASK            0x10000000UL\r
169 #define GPIO_29_MASK            0x20000000UL\r
170 #define GPIO_30_MASK            0x40000000UL\r
171 #define GPIO_31_MASK            0x80000000UL\r
172 \r
173 /*-------------------------------------------------------------------------*//**\r
174  * GPIO modes\r
175  */\r
176 #define GPIO_INPUT_MODE              0x0000000002UL\r
177 #define GPIO_OUTPUT_MODE             0x0000000005UL\r
178 #define GPIO_INOUT_MODE              0x0000000003UL\r
179 \r
180 /*-------------------------------------------------------------------------*//**\r
181  * Possible GPIO inputs interrupt configurations.\r
182  */\r
183 #define GPIO_IRQ_LEVEL_HIGH                     0x0000000000UL\r
184 #define GPIO_IRQ_LEVEL_LOW                      0x0000000020UL\r
185 #define GPIO_IRQ_EDGE_POSITIVE          0x0000000040UL\r
186 #define GPIO_IRQ_EDGE_NEGATIVE          0x0000000060UL\r
187 #define GPIO_IRQ_EDGE_BOTH                      0x0000000080UL\r
188 \r
189 /*-------------------------------------------------------------------------*//**\r
190  * Possible states for GPIO configured as INOUT.\r
191  */\r
192 typedef enum gpio_inout_state\r
193 {\r
194     GPIO_DRIVE_LOW = 0,\r
195     GPIO_DRIVE_HIGH,\r
196     GPIO_HIGH_Z\r
197 } gpio_inout_state_t;\r
198 \r
199 /*-------------------------------------------------------------------------*//**\r
200   The GPIO_init() function initialises a CoreGPIO hardware instance and the data\r
201   structure associated with the CoreGPIO hardware instance.\r
202   Please note that a CoreGPIO hardware instance can be generated with a fixed\r
203   configuration for some or all of its IOs as part of the hardware flow. Attempting\r
204   to modify the configuration of such a hardware configured IO using the\r
205   GPIO_config() function has no effect.\r
206 \r
207   @param this_gpio\r
208     Pointer to the gpio_instance_t data structure instance holding all data\r
209     regarding the CoreGPIO hardware instance being initialized. A pointer to the\r
210     same data structure will be used in subsequent calls to the CoreGPIO driver\r
211     functions in order to identify the CoreGPIO instance that should perform the\r
212     operation implemented by the called driver function.\r
213 \r
214   @param base_addr\r
215     The base_addr parameter is the base address in the processor's memory map for\r
216     the registers of the GPIO instance being initialized.\r
217     \r
218   @param bus_width\r
219     The bus_width parameter informs the driver of the APB bus width selected during\r
220     the hardware flow configuration of the CoreGPIO hardware instance. It indicates\r
221     to the driver whether the CoreGPIO hardware registers will be visible as 8, 16\r
222     or 32 bits registers. Allowed value are:\r
223         - GPIO_APB_8_BITS_BUS\r
224         - GPIO_APB_16_BITS_BUS\r
225         - GPIO_APB_32_BITS_BUS\r
226   \r
227   @return\r
228     none.\r
229     \r
230   Example:\r
231   @code\r
232     #define COREGPIO_BASE_ADDR  0xC2000000\r
233     \r
234     gpio_instance_t g_gpio;\r
235     \r
236     void system_init( void )\r
237     {\r
238         GPIO_init( &g_gpio,     COREGPIO_BASE_ADDR, GPIO_APB_32_BITS_BUS );\r
239     }\r
240   @endcode\r
241   */\r
242 void GPIO_init\r
243 (\r
244     gpio_instance_t *   this_gpio,\r
245     addr_t              base_addr,\r
246     gpio_apb_width_t    bus_width\r
247 );\r
248 \r
249 /*-------------------------------------------------------------------------*//**\r
250   The GPIO_config() function is used to configure an individual GPIO port.\r
251  \r
252   @param this_gpio\r
253     The this_gpio parameter is a pointer to the gpio_instance_t structure holding\r
254     all data regarding the CoreGPIO instance controlled through this function call.\r
255 \r
256   @param port_id\r
257     The port_id parameter identifies the GPIO port to be configured.\r
258     An enumeration item of the form GPIO_n where n is the number of the GPIO\r
259     port is used to identify the GPIO port. For example GPIO_0 identifies the\r
260     first GPIO port and GPIO_31 the last one.\r
261     \r
262   @param config\r
263     The config parameter specifies the configuration to be applied to the GPIO\r
264     port identified by the first parameter. It is a logical OR of GPIO mode and\r
265     the interrupt mode. The interrupt mode is only relevant if the GPIO is\r
266     configured as input.\r
267        Possible modes are:\r
268            - GPIO_INPUT_MODE,\r
269            - GPIO_OUTPUT_MODE,\r
270            - GPIO_INOUT_MODE.\r
271        Possible interrupt modes are:\r
272            - GPIO_IRQ_LEVEL_HIGH,\r
273            - GPIO_IRQ_LEVEL_LOW,\r
274            - GPIO_IRQ_EDGE_POSITIVE,\r
275            - GPIO_IRQ_EDGE_NEGATIVE,\r
276            - GPIO_IRQ_EDGE_BOTH\r
277  \r
278   @return\r
279     none.\r
280     \r
281   For example the following call will configure GPIO 4 as an input generating\r
282   interrupts on a low to high transition of the input:\r
283   @code\r
284   GPIO_config( &g_gpio, GPIO_4, GPIO_INPUT_MODE | GPIO_IRQ_EDGE_POSITIVE );\r
285   @endcode\r
286  */\r
287 void GPIO_config\r
288 (\r
289     gpio_instance_t *   this_gpio,\r
290     gpio_id_t           port_id,\r
291     uint32_t            config\r
292 );\r
293 \r
294 /*-------------------------------------------------------------------------*//**\r
295   The GPIO_set_outputs() function is used to set the state of the GPIO ports\r
296   configured as outputs.\r
297  \r
298   @param this_gpio\r
299     The this_gpio parameter is a pointer to the gpio_instance_t structure holding\r
300     all data regarding the CoreGPIO instance controlled through this function call.\r
301 \r
302   @param value\r
303     The value parameter specifies the state of the GPIO ports configured as\r
304     outputs. It is a bit mask of the form (GPIO_n_MASK | GPIO_m_MASK) where n\r
305     and m are numbers identifying GPIOs.\r
306     For example (GPIO_0_MASK | GPIO_1_MASK | GPIO_2_MASK ) specifies that the\r
307     first, second and third GPIOs' must be set high and all other outputs set\r
308     low.\r
309 \r
310   @return\r
311     none.\r
312     \r
313   Example 1:\r
314     Set GPIOs outputs 0 and 8 high and all other GPIO outputs low.\r
315     @code\r
316         GPIO_set_outputs( &g_gpio, GPIO_0_MASK | GPIO_8_MASK );\r
317     @endcode\r
318 \r
319   Example 2:\r
320     Set GPIOs outputs 2 and 4 low without affecting other GPIO outputs.\r
321     @code\r
322         uint32_t gpio_outputs;\r
323         gpio_outputs = GPIO_get_outputs( &g_gpio );\r
324         gpio_outputs &= ~( GPIO_2_MASK | GPIO_4_MASK );\r
325         GPIO_set_outputs( &g_gpio, gpio_outputs );\r
326     @endcode\r
327 \r
328   @see GPIO_get_outputs()\r
329  */\r
330 void GPIO_set_outputs\r
331 (\r
332     gpio_instance_t *   this_gpio,\r
333     uint32_t            value\r
334 );\r
335 \r
336 /*-------------------------------------------------------------------------*//**\r
337   The GPIO_set_output() function is used to set the state of a single GPIO\r
338   port configured as output.\r
339  \r
340   @param this_gpio\r
341     The this_gpio parameter is a pointer to the gpio_instance_t structure holding\r
342     all data regarding the CoreGPIO instance controlled through this function call.\r
343 \r
344   @param port_id\r
345     The port_id parameter specifies the GPIO port that will have its output set\r
346     by a call to this function.\r
347   \r
348   @param value\r
349     The value parameter specifies the desired state for the GPIO output. A value\r
350     of 0 will set the output low and a value of 1 will set the port high.\r
351   \r
352   @return\r
353     none.\r
354  */\r
355 void GPIO_set_output\r
356 (\r
357     gpio_instance_t *   this_gpio,\r
358     gpio_id_t           port_id,\r
359     uint8_t             value\r
360 );\r
361 \r
362 /*-------------------------------------------------------------------------*//**\r
363   The GPIO_get_inputs() function is used to read the state of all GPIOs\r
364   confgured as inputs.\r
365  \r
366   @param this_gpio\r
367     The this_gpio parameter is a pointer to the gpio_instance_t structure holding\r
368     all data regarding the CoreGPIO instance controlled through this function call.\r
369 \r
370   @return\r
371     This function returns a 32 bit unsigned integer where each bit represents\r
372     the state of an input. The least significant bit representing the state of\r
373     GPIO 0 and the most significant bit the state of GPIO 31.\r
374  */\r
375 uint32_t GPIO_get_inputs\r
376 (\r
377     gpio_instance_t *   this_gpio\r
378 );\r
379 \r
380 /*-------------------------------------------------------------------------*//**\r
381   The GPIO_get_outputs() function is used to read the current state of all\r
382   GPIO outputs.\r
383  \r
384   @param this_gpio\r
385     The this_gpio parameter is a pointer to the gpio_instance_t structure holding\r
386     all data regarding the CoreGPIO instance controlled through this function call.\r
387 \r
388   @return\r
389      This function returns a 32 bit unsigned integer where each bit represents\r
390      the state of an output. The least significant bit representing the state\r
391      of GPIO 0 and the most significant bit the state of GPIO 31.\r
392  */\r
393 uint32_t GPIO_get_outputs\r
394 (\r
395     gpio_instance_t *   this_gpio\r
396 );\r
397 \r
398 /*-------------------------------------------------------------------------*//**\r
399   The GPIO_drive_inout() function is used to set the output state of a\r
400   GPIO configured as INOUT. An INOUT GPIO can be in one of three states:\r
401     - high\r
402     - low\r
403     - high impedance\r
404   An INOUT output would typically be used where several devices can drive the\r
405   state of a signal. The high and low states are equivalent to the high and low\r
406   states of a GPIO configured as output. The high impedance state is used to\r
407   prevent the GPIO from driving the state of the output and therefore allow\r
408   reading the state of the GPIO as an input.\r
409   Please note that the GPIO port you wish to use as INOUT through this function\r
410   must be configurable through software. Therefore the GPIO ports used as INOUT\r
411   must not have a fixed configuration selected as part of the hardware flow.\r
412  \r
413   @param this_gpio\r
414     The this_gpio parameter is a pointer to the gpio_instance_t structure holding\r
415     all data regarding the CoreGPIO instance controlled through this function call.\r
416 \r
417   @param port_id\r
418     The port_id parameter identifies the GPIO for whcih this function will\r
419     change the output state.\r
420     An enumeration item of the form GPIO_n where n is the number of the GPIO\r
421     port is used to identify the GPIO port. For example GPIO_0 identifies the\r
422     first GPIO port and GPIO_31 the last one.\r
423     \r
424   @param inout_state\r
425     The inout_state parameter specifies the state of the I/O identified by the\r
426     first parameter. Possible states are:\r
427                            - GPIO_DRIVE_HIGH,\r
428                            - GPIO_DRIVE_LOW,\r
429                            - GPIO_HIGH_Z (high impedance)\r
430 \r
431   @return\r
432     none.\r
433     \r
434   Example:\r
435     The call to GPIO_drive_inout() below will set the GPIO 7 output to\r
436     high impedance state.\r
437     @code\r
438     GPIO_drive_inout( &g_gpio, GPIO_7, GPIO_HIGH_Z );\r
439     @endcode\r
440  */\r
441 void GPIO_drive_inout\r
442 (\r
443     gpio_instance_t *   this_gpio,\r
444     gpio_id_t           port_id,\r
445     gpio_inout_state_t  inout_state\r
446 );\r
447 \r
448 /*-------------------------------------------------------------------------*//**\r
449   The GPIO_enable_irq() function is used to enable an interrupt to be\r
450   generated based on the state of the input identified as parameter.\r
451  \r
452   @param this_gpio\r
453     The this_gpio parameter is a pointer to the gpio_instance_t structure holding\r
454     all data regarding the CoreGPIO instance controlled through this function call.\r
455 \r
456   @param port_id\r
457     The port_id parameter identifies the GPIO input the call to\r
458     GPIO_enable_irq() will enable to generate interrupts.\r
459     An enumeration item of the form GPIO_n where n is the number of the GPIO\r
460     port is used to identify the GPIO port. For example GPIO_0 identifies the\r
461     first GPIO port and GPIO_31 the last one.\r
462     \r
463   @return\r
464     none.\r
465     \r
466   Example:\r
467     The call to GPIO_enable_irq() below will allow GPIO 8 to generate\r
468     interrupts.\r
469     @code\r
470     GPIO_enable_irq( &g_gpio, GPIO_8 );\r
471     @endcode\r
472  */\r
473 void GPIO_enable_irq\r
474 (\r
475     gpio_instance_t *   this_gpio,\r
476     gpio_id_t           port_id\r
477 );\r
478 \r
479 /*-------------------------------------------------------------------------*//**\r
480   The GPIO_disable_irq() function is used to disable interrupt from being\r
481   generated based on the state of the input specified as parameter.\r
482  \r
483   @param this_gpio\r
484     The this_gpio parameter is a pointer to the gpio_instance_t structure holding\r
485     all data regarding the CoreGPIO instance controlled through this function call.\r
486 \r
487   @param port_id\r
488     The port_id parameter identifies the GPIO input the call to\r
489     GPIO_disable_irq() will disable from generating interrupts.\r
490     An enumeration item of the form GPIO_n where n is the number of the GPIO\r
491     port is used to identify the GPIO port. For example GPIO_0 identifies the\r
492     first GPIO port and GPIO_31 the last one.\r
493  \r
494   @return\r
495     none.\r
496     \r
497   Example:\r
498     The call to GPIO_disable_irq() below will prevent GPIO 8 from generating\r
499     interrupts.\r
500     @code\r
501     GPIO_disable_irq( &g_gpio, GPIO_8 );\r
502     @endcode\r
503  */\r
504 void GPIO_disable_irq\r
505 (\r
506     gpio_instance_t *   this_gpio,\r
507     gpio_id_t           port_id\r
508 );\r
509 \r
510 /*-------------------------------------------------------------------------*//**\r
511   The GPIO_clear_irq() function is used to clear the interrupt generated by\r
512   the GPIO specified as parameter. The GPIO_clear_irq() function  must be\r
513   called as part of a GPIO interrupt service routine (ISR) in order to prevent\r
514   the same interrupt event retriggering a call to the GPIO ISR.\r
515   Please note that interrupts may also need to be cleared in the processor's\r
516   interrupt controller.\r
517  \r
518   @param this_gpio\r
519     The this_gpio parameter is a pointer to the gpio_instance_t structure holding\r
520     all data regarding the CoreGPIO instance controlled through this function call.\r
521 \r
522   @param port_id\r
523     The port_id parameter identifies the GPIO input for which to clear the\r
524     interrupt.\r
525     An enumeration item of the form GPIO_n where n is the number of the GPIO\r
526     port is used to identify the GPIO port. For example GPIO_0 identifies the\r
527     first GPIO port and GPIO_31 the last one.\r
528     \r
529   @return\r
530     none.\r
531     \r
532   Example:\r
533     The example below demonstrates the use of the GPIO_clear_irq() function as\r
534     part of the GPIO 9 interrupt service routine on a Cortex-M processor.  \r
535     @code\r
536     void GPIO9_IRQHandler( void )\r
537     {\r
538         do_interrupt_processing();\r
539         \r
540         GPIO_clear_irq( &g_gpio, GPIO_9 );\r
541         \r
542         NVIC_ClearPendingIRQ( GPIO9_IRQn );\r
543     }\r
544     @endcode\r
545  */\r
546 void GPIO_clear_irq\r
547 (\r
548     gpio_instance_t *   this_gpio,\r
549     gpio_id_t           port_id\r
550 );\r
551 \r
552 #endif /* CORE_GPIO_H_ */\r