1 /*******************************************************************************
\r
2 * (c) Copyright 2008-2015 Microsemi SoC Products Group. All rights reserved.
\r
4 * CoreGPIO bare metal driver public API.
\r
6 * SVN $Revision: 7964 $
\r
7 * SVN $Date: 2015-10-09 18:26:53 +0530 (Fri, 09 Oct 2015) $
\r
10 /*=========================================================================*//**
\r
11 @mainpage CoreGPIO Bare Metal Driver.
\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
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
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
33 The CoreGPIO driver functions are logically grouped into the following groups:
\r
36 - Reading and writing GPIO state
\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
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
53 The state of the GPIO ports can be read and written using the following
\r
56 - GPIO_get_outputs()
\r
57 - GPIO_set_outputs()
\r
58 - GPIO_drive_inout()
\r
60 Interrupts generated by GPIO ports configured as inputs are controlled using
\r
61 the following functions:
\r
63 - GPIO_disable_irq()
\r
66 *//*=========================================================================*/
\r
67 #ifndef CORE_GPIO_H_
\r
68 #define CORE_GPIO_H_
\r
71 #include "cpu_types.h"
\r
73 /*-------------------------------------------------------------------------*//**
\r
74 The gpio_id_t enumeration is used to identify GPIOs as part of the
\r
75 parameter to functions:
\r
77 - GPIO_drive_inout(),
\r
78 - GPIO_enable_int(),
\r
79 - GPIO_disable_int(),
\r
82 typedef enum __gpio_id_t
\r
118 typedef enum __gpio_apb_width_t
\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
126 /*-------------------------------------------------------------------------*//**
\r
128 typedef struct __gpio_instance_t
\r
131 gpio_apb_width_t apb_bus_width;
\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
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
173 /*-------------------------------------------------------------------------*//**
\r
176 #define GPIO_INPUT_MODE 0x0000000002UL
\r
177 #define GPIO_OUTPUT_MODE 0x0000000005UL
\r
178 #define GPIO_INOUT_MODE 0x0000000003UL
\r
180 /*-------------------------------------------------------------------------*//**
\r
181 * Possible GPIO inputs interrupt configurations.
\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
189 /*-------------------------------------------------------------------------*//**
\r
190 * Possible states for GPIO configured as INOUT.
\r
192 typedef enum gpio_inout_state
\r
194 GPIO_DRIVE_LOW = 0,
\r
197 } gpio_inout_state_t;
\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
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
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
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
232 #define COREGPIO_BASE_ADDR 0xC2000000
\r
234 gpio_instance_t g_gpio;
\r
236 void system_init( void )
\r
238 GPIO_init( &g_gpio, COREGPIO_BASE_ADDR, GPIO_APB_32_BITS_BUS );
\r
244 gpio_instance_t * this_gpio,
\r
246 gpio_apb_width_t bus_width
\r
249 /*-------------------------------------------------------------------------*//**
\r
250 The GPIO_config() function is used to configure an individual GPIO port.
\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
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
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
269 - GPIO_OUTPUT_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
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
284 GPIO_config( &g_gpio, GPIO_4, GPIO_INPUT_MODE | GPIO_IRQ_EDGE_POSITIVE );
\r
289 gpio_instance_t * this_gpio,
\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
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
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
314 Set GPIOs outputs 0 and 8 high and all other GPIO outputs low.
\r
316 GPIO_set_outputs( &g_gpio, GPIO_0_MASK | GPIO_8_MASK );
\r
320 Set GPIOs outputs 2 and 4 low without affecting other GPIO outputs.
\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
328 @see GPIO_get_outputs()
\r
330 void GPIO_set_outputs
\r
332 gpio_instance_t * this_gpio,
\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
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
345 The port_id parameter specifies the GPIO port that will have its output set
\r
346 by a call to this function.
\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
355 void GPIO_set_output
\r
357 gpio_instance_t * this_gpio,
\r
362 /*-------------------------------------------------------------------------*//**
\r
363 The GPIO_get_inputs() function is used to read the state of all GPIOs
\r
364 confgured as inputs.
\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
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
375 uint32_t GPIO_get_inputs
\r
377 gpio_instance_t * this_gpio
\r
380 /*-------------------------------------------------------------------------*//**
\r
381 The GPIO_get_outputs() function is used to read the current state of all
\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
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
393 uint32_t GPIO_get_outputs
\r
395 gpio_instance_t * this_gpio
\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
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
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
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
425 The inout_state parameter specifies the state of the I/O identified by the
\r
426 first parameter. Possible states are:
\r
429 - GPIO_HIGH_Z (high impedance)
\r
435 The call to GPIO_drive_inout() below will set the GPIO 7 output to
\r
436 high impedance state.
\r
438 GPIO_drive_inout( &g_gpio, GPIO_7, GPIO_HIGH_Z );
\r
441 void GPIO_drive_inout
\r
443 gpio_instance_t * this_gpio,
\r
445 gpio_inout_state_t inout_state
\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
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
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
467 The call to GPIO_enable_irq() below will allow GPIO 8 to generate
\r
470 GPIO_enable_irq( &g_gpio, GPIO_8 );
\r
473 void GPIO_enable_irq
\r
475 gpio_instance_t * this_gpio,
\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
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
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
498 The call to GPIO_disable_irq() below will prevent GPIO 8 from generating
\r
501 GPIO_disable_irq( &g_gpio, GPIO_8 );
\r
504 void GPIO_disable_irq
\r
506 gpio_instance_t * this_gpio,
\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
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
523 The port_id parameter identifies the GPIO input for which to clear the
\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
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
536 void GPIO9_IRQHandler( void )
\r
538 do_interrupt_processing();
\r
540 GPIO_clear_irq( &g_gpio, GPIO_9 );
\r
542 NVIC_ClearPendingIRQ( GPIO9_IRQn );
\r
546 void GPIO_clear_irq
\r
548 gpio_instance_t * this_gpio,
\r
552 #endif /* CORE_GPIO_H_ */
\r