View Source grisp_gpio (grisp v2.8.0)

GRiSP General Purpose Input/Output (GPIO) API.

General Purpose Input / Output (GPIO) is used to control digital signals on a pin. The digital values 0 and 1 correspond to a low or high voltage respectively. On GRiSP the voltage for GPIO pins is 3.3V.

A pin can be controlled either as an output pin or an input pin. For an output pin, it is possible to set or get the current value. For an input pin, it is possible to get the current value.

Pin Mappings

For PMOD connectors, the number column (#) maps to the respective PMOD pin (see Figure 1).

GRiSP 2 Pin Mappings

IDSlotType#SchematicDirectionDescription
gpio1_1GPIO1PMOD 1A1X1404.1In/Out
gpio1_2GPIO1PMOD 1A2X1404.2In/Out
gpio1_3GPIO1PMOD 1A3X1404.3In/Out
gpio1_4GPIO1PMOD 1A4X1404.4In/Out
gpio1_7GPIO1PMOD 1A7X1404.7In/Out
gpio1_8GPIO1PMOD 1A8X1404.8In/Out
gpio1_9GPIO1PMOD 1A9X1404.9In/Out
gpio1_10GPIO1PMOD 1A10X1404.10In/Out
gpio_1_3GPIO_1GenericX1300.3In/Out
gpio_1_4GPIO_1GenericX1300.4In/Out
gpio_1_5GPIO_1GenericX1300.5In/Out
gpio_1_6GPIO_1GenericX1300.6In/Out
gpio_2_3GPIO_2/4GenericX1301.3In/Out
gpio_2_4GPIO_2/4GenericX1301.4In/Out
gpio_2_5GPIO_2/4GenericX1301.5In/Out
gpio_2_6GPIO_2/4GenericX1301.6In/Out
gpio_2_7GPIO_2/4GenericX1301.7In/Out
gpio_2_8GPIO_2/4GenericX1301.8In/Out
gpio_2_9GPIO_2/4GenericX1301.9In/Out
gpio_2_10GPIO_2/4GenericX1301.10In/Out
gpio_2_11GPIO_2/4GenericX1301.11In/Out
gpio_2_12GPIO_2/4GenericX1301.12In/Out
gpio_2_13GPIO_2/4GenericX1301.13In/Out
gpio_2_14GPIO_2/4GenericX1301.14In/Out
led1_rLED 1LEDRGB1redOutReserved by LED driver
led1_gLED 1LEDRGB1greenOutReserved by LED driver
led1_bLED 1LEDRGB1blueOutReserved by LED driver
led2_rLED 2LEDRGB2redOutReserved by LED driver
led2_gLED 2LEDRGB2greenOutReserved by LED driver
led2_bLED 2LEDRGB3blueOutReserved by LED driver
jumper_1ModeJumperJUMPER1InMode Switch Jumper State
jumper_2ModeJumperJUMPER2InMode Switch Jumper State
jumper_3ModeJumperJUMPER3InMode Switch Jumper State
jumper_4ModeJumperJUMPER4InMode Switch Jumper State
jumper_5ModeJumperJUMPER5InMode Switch Jumper State

Warning

Reserved pins should only be carefully used if their drivers are not in use

GRiSP 1 Pin Mappings

IDSlotType#SchematicDirectionDescription
gpio1_1GPIO1PMOD 11X502.1In/Out
gpio1_2GPIO1PMOD 12X502.2In/Out
gpio1_3GPIO1PMOD 13X502.3In/Out
gpio1_4GPIO1PMOD 14X502.4In/Out
gpio2_1GPIO2PMOD 11X503.1In/Out
gpio2_2GPIO2PMOD 12X503.2In/Out
gpio2_3GPIO2PMOD 13X503.3In/Out
gpio2_4GPIO2PMOD 14X503.4In/Out
led1_rLED 1LEDRGB1redOutReserved by LED driver
led1_gLED 1LEDRGB1greenOutReserved by LED driver
led1_bLED 1LEDRGB1blueOutReserved by LED driver
led2_rLED 2LEDRGB2redOutReserved by LED driver
led2_gLED 2LEDRGB2greenOutReserved by LED driver
led2_bLED 2LEDRGB3blueOutReserved by LED driver
jumper_1ModeJumperJUMPER1InMode Switch Jumper State
jumper_2ModeJumperJUMPER2InMode Switch Jumper State
jumper_3ModeJumperJUMPER3InMode Switch Jumper State
jumper_4ModeJumperJUMPER4InMode Switch Jumper State
jumper_5ModeJumperJUMPER5InMode Switch Jumper State
spi1_pin7SPI1PMOD 2A7X501.7In/Out
spi1_pin8SPI1PMOD 2A8X501.8In/Out
spi1_pin9SPI1PMOD 2A9X501.9In/OutReserved by SPI driver
spi1_pin10SPI1PMOD 2A10X501.10In/OutReserved by SPI driver
spi1_pin1SPI1PMOD 2A1X501.1In/OutReserved by SPI driver
spi2_pin1SPI2PMOD 21X509.1In/OutReserved by SPI driver
uart_1_ctsUARTPMOD 31X508.1In/Out
uart_2_txdUARTPMOD 32X508.2In/Out
uart_3_rxdUARTPMOD 33X508.3In/Out
uart_4_rtsUARTPMOD 34X508.4In/Out

Warning

Reserved pins should only be carefully used if their drivers are not in use

PMOD Pin Numbers

Figure 1. PMOD connectors as seen from the side of a GRiSP board with number mappings

PMOD Type A consists of:

  • 4 × data, pins #1-4</li>
  • 1 × ground, pin #5</li>
  • 1 × 3.3V power, pin #6</li>

PMOD Type B consists of:

  • 8 × data, pins #1-4 and #7-10</li>
  • 1 × ground, pins #5 and #11</li>
  • 1 × 3.3V power, pins #6 and #12</li>

Summary

Functions

Returns the current value of a pin.

Equivalent to open(Pin, #{}).

Creates a reference to a GPIO pin. If no mode is given in the options, it defaults to {output, 0}.

Sets the current value of an output pin.

Types

mode()

-type mode() :: input | {output, value()}.

opts()

-type opts() :: #{mode => mode(), _ => _}.

pin()

-type pin() :: atom().

ref()

-opaque ref()

value()

-type value() :: 0 | 1.

Functions

get(Pin)

-spec get(ref()) -> value().

Returns the current value of a pin.

Returns the actual value for input pins or the currently set value for output pins.

Examples

To see whether the red component of LED 1 is enabled:

1> LED1R = grisp_gpio:open(led1_r, #{mode => {output, 0}}).
#Ref<0.2691682867.116916226.176944>
2> grisp_gpio:get(LED1R).
0
3> grisp_gpio:set(LED1R, 1).
ok
2> grisp_gpio:get(LED1R).
1

To see whether Mode Jumper 1 is on or off:

1> Jumper1 = grisp_gpio:open(jumper_1, #{mode => input}).
#Ref<0.2691682867.116916226.176944>
2> grisp_gpio:get(Jumper1).
0

Flip the jumper

3> grisp_gpio:get(Jumper1).
1

open(Pin)

-spec open(pin()) -> ref().

Equivalent to open(Pin, #{}).

open(Pin, UserOpts)

-spec open(pin(), opts()) -> ref().

Creates a reference to a GPIO pin. If no mode is given in the options, it defaults to {output, 0}.

Open the GPIO pin of the red component of LED 1 as an output pin with initial value of 0:

1> grisp_gpio:open(led1_r, #{mode => {output, 0}}).
#Ref<0.2691682867.116916226.176944>

set(Pin, Value)

-spec set(ref(), value()) -> ok.

Sets the current value of an output pin.

Example

Turn off the red component of LED 1:

1> LED1R = grisp_gpio:open(led1_r, #{mode => {output, 0}}).
#Ref<0.2691682867.116916226.176944>
2> grisp_gpio:set(LED1R, 0).
ok

Turn on the red component of LED 1:

3> grisp_gpio:set(LED1R, 1).
ok