GPIO

Generic interface for GPIO which requires only pin number instead of port&pin-combo used in many smaller devices like AVR and PIC. This is achieved by mapping PORTB in AVR to pin numbers 8-15, PORTC to 16-23 and so on. Same for PIC, MSP430 and others that do this kind of thing instead of having continuous GPIO pin numbers.

For convenience GPIOxy constants have been defined where x is the letter of an 8-bit port and y is the bit number. These constants cover ports from A to H.

GPIO implemented this way is more and less complex thing. On most targets, like AVR, GPIO functions are inline and will often optimize as well as directly written native code.

Configuration

Enable GPIO module

To enable GPIO, flag USE_GPIO must be defined globally.

In Makefile:

USE += GPIO

From command line when compiling:

make use=gpio

Functions

void gpio_enable(uint8_t pin, bool direction)
Parameters:
  • pin (uint8_t) – pin to be enabled
  • direction (bool) –
    • 0 is input
    • 1 is output

Enable specific pin using it as an input or output.

void gpio_set(uint8_t pin, uint8_t state)
Parameters:
  • pin (uint8_t) – pin to be set
  • state (uint8_t) –
    • 0 is low
    • any other value is high
uint8_t gpio_read(uint8_t pin)
Parameters:
  • pin (uint8_t) – pin to be read
Returns:

0 if pin is low, non-zero if high

Macros

gpio_input(pin)

Calls gpio_enable(pin, 0).

gpio_output(pin)

Calls gpio_enable(pin, 1).

gpio_low(pin)

Calls gpio_set(pin, 0).

gpio_high(pin)

Calls gpio_set(pin, 1).

Defines

enumerator GPIOA0 = 0
enumerator GPIOA1 = 1
enumerator GPIOA2 = 2
enumerator GPIOA3 = 3
enumerator GPIOA4 = 4
enumerator GPIOA5 = 5
enumerator GPIOA6 = 6
enumerator GPIOA7 = 7
enumerator GPIOB0 = 8
enumerator GPIOB1 = 9
enumerator GPIOB2 = 10
enumerator GPIOB3 = 11
enumerator GPIOB4 = 12
enumerator GPIOB5 = 13
enumerator GPIOB6 = 14
enumerator GPIOB7 = 15
enumerator GPIOC0 = 16
enumerator GPIOC1 = 17
enumerator GPIOC2 = 18
enumerator GPIOC3 = 19
enumerator GPIOC4 = 20
enumerator GPIOC5 = 21
enumerator GPIOC6 = 22
enumerator GPIOC7 = 23
enumerator GPIOD0 = 24
enumerator GPIOD1 = 25
enumerator GPIOD2 = 26
enumerator GPIOD3 = 27
enumerator GPIOD4 = 28
enumerator GPIOD5 = 29
enumerator GPIOD6 = 30
enumerator GPIOD7 = 31
enumerator GPIOE0 = 32
enumerator GPIOE1 = 33
enumerator GPIOE2 = 34
enumerator GPIOE3 = 35
enumerator GPIOE4 = 36
enumerator GPIOE5 = 37
enumerator GPIOE6 = 38
enumerator GPIOE7 = 39
enumerator GPIOF0 = 40
enumerator GPIOF1 = 41
enumerator GPIOF2 = 42
enumerator GPIOF3 = 43
enumerator GPIOF4 = 44
enumerator GPIOF5 = 45
enumerator GPIOF6 = 46
enumerator GPIOF7 = 47
enumerator GPIOG0 = 48
enumerator GPIOG1 = 49
enumerator GPIOG2 = 50
enumerator GPIOG3 = 51
enumerator GPIOG4 = 52
enumerator GPIOG5 = 53
enumerator GPIOG6 = 54
enumerator GPIOG7 = 55
enumerator GPIOH0 = 56
enumerator GPIOH1 = 57
enumerator GPIOH2 = 58
enumerator GPIOH3 = 59
enumerator GPIOH4 = 60
enumerator GPIOH5 = 61
enumerator GPIOH6 = 62
enumerator GPIOH7 = 63