General Purpose Input Output (GPIO)

Note: The code snippets in this article are intended for Mita version 1a1ed7881e2220895ec60ce4f657972538c43195 and XDK-Workbench versions 3.4.0 and higher.

This article introduces the configuration and use of the GPIO Input (read) and Output (write) in Mita.

  1. General Information
  2. Pull Up Pull Down Resistor and Electrical Current Limitation
  3. GPIO API Overview
  4. Configuring the GPIO pin
  5. Reading data from a GPIO pin
  6. Writing data to a GPIO pin

General Information

The General Purpose Input Output (GPIO) module is used for pin configuration, direct pin manipulation, sensing and routing for peripheral pin connection. General Purpose input/output pins minimize the software control overhead and are able to fit many communication protocols, are highly configurable, and therefore easy to use.

Each pin on the extension bus can be individually configured as either input or an output with different modes, such as pull up or pull down for inputs and push pull drive for outputs and so on. Every configuration is controlled by the GPIO module. The I/O pins are organized in ports with up to 16 pins each and are named in the scheme PXN, where X indicates the port (A,B,C,…) and N indicates the pin number (0,1,2…15). For example, PB6 refers to pin 6 on port B. The XDKs MCU (EFM32GG390), for example, has up to 90 general use I/O pins and up to 19 I/O pins on the extension bus, which can be used as general purpose pins without the limitations mentioned in the next chapter.

The article’s focus is on the software and API provided by the platform. For more specific information about the hardware itself, specifically the GPIO pins used by the XDK, please read the corresponding datasheet.

Pull Up Pull Down Resistor and Electrical Current Limitation

This chapter gives a short overview and explanation on the three possible modes for the pins on the extension bus, and which mode has to be set, when external devices are connected to input or output pins on the extension bus.

In digital circuits, it is important that the signal lines are never allowed to float. A floating (undetermined) state is usually causing issues regarding its interpretation. This mainly occurs if no external devices are connected or due to high- impedance. Therefore the signal has to always be either high or low.

To prevent this undetermined state, input pins can be configured to either operate with a pull up or pull down resistor.

A pull-up resistor weakly pulls the voltage level of the wire to the voltage source level, in case of the XDK to 2.5 V, when the other components on the input pin are inactive.

When no other component on the input pin is active (i.e. changing the pin state) or no component is connected to the input, it is in a state of high impedance. When another component on the input pin is connected or actively changing the pin state, it will override the high logic level set by the pull-up resistor. The pull-up resistor assures that the wire is at a defined logic level even if no active devices are connected to it.

Image

The pull down resistor has a similar effect, but pulls the input signal weakly to the ground level instead. It holds the logic signal near zero volt when no other device is actively changing the pin state or no other device is connected to the input pin.

Image

Furthermore, it depends on the external device whether the logic level should either be pulled high or low by a Pull up/Pull down resistor. Nevertheless a floating state of the input pin should be prevented.

Since this configuration is only preventing failures on the input pin, it is also necessary to ensure that the pin is working properly, when configured as output. For that, the electrical current draining from the pin must be controlled. As mentioned in chapter 1.3, this is ensured by an internal programmable current limitation. Please note that the output current also differs from the combined impedance of the internal series resistor of the respective output pin and the impedance of the connected component. The configuration of the electrical current limitation will be explained in detail in the following chapter.

GPIO API Overview

Mita provides the possibility to use the GPIO resources without setting them up beforehand. If the configuration and implementation provided by the Mita implementation are insufficient for a certain purpose, it is recommended to make adapting changes in the generated C code.

The GPIO implementation in Mita is based on the Platform Essentials, which is included in the XDK SDK. It contains the implementation for the pin corresponding modules on the extension board.

Configuring the GPIO pin

This section provides a brief overview of the possible configuration for the GPIO pins.

In general is the GPIO setup code defined as shown below:

setup GPIOPin : GPIO {
    var variable = signal(GPIOPin,GPIOMode);
}

Depending on the application of the GPIO pins as either input or output, different setups need to be applied.

The GPIO pin can be configured as input as shown below:

setup PA1 : GPIO {
    var input = digitalIn(PA1,PullUp);
}

The configuration-item GPIOMode can be set to one of the following values in case the pin is configured as input:

  • NoPull, Input enabled
  • PullUp, Input enabled with internal pull-up resistor
  • PullDown, Input enabled with internal pull-down resistor
  • PullUpFilter, Input enabled with internal pull-up and glitch filter enabled

On the other hand, the GPIO pin can also be configured as output as shown below:

setup PA1 : GPIO {
    var out = digitalOut(PA1,false);
}

While in case of an input the GPIO pin additional configuration could be applied, in case of an output only the initial output value could be configured with the secondary parameter.

The configuration-item GPIOPin can be configured for one of the following GPIO pins on the Extension Bus:

  • PA0, Alternate function shall be Timer0 channel 0 compare operations
  • PA1, General purpose I/O
  • PB2, General purpose I/O
  • PB3, General purpose I/O, SPI MISO operations
  • PB4, General purpose I/O, SPI MISO operations
  • PB5, General purpose I/O, SPI clock operations
  • PB9, General purpose I/O, UART1_TX operations
  • PB10, General purpose I/O, UART1_RX operations
  • PC0, General purpose I/O, Timer0 channel 1 compare operations
  • PC1, General purpose I/O, Timer0 channel 2 compare operations
  • PC2, General purpose I/O, Timer0 Channel 0 Complementary Dead-Time Insertion operations
  • PC3, General purpose I/O, Timer0 Channel 1 Complementary Dead-Time Insertion operations
  • PC4, General purpose I/O, Timer0 Channel 2 Complementary Dead-Time Insertion operations
  • PC8, General purpose I/O, Timer2 channel 0 capture operations
  • PC9, General purpose I/O, Timer2 channel 1 capture operations
  • PC10, General purpose I/O, Timer2 channel 2 capture operations
  • PD8, General purpose I/O
  • PE2, General purpose I/O
  • PD5, General purpose I/O

Reading data from a GPIO pin

Reading data from a GPIO pin is fairly simple, since only the previously declared variable configured as input in the GPIO setup is required.

The following code showcases how to read the incoming data on the GPIO pin PA1 and prints the values to the console every second.

every 1 second {
    println(`state PA1: ${PA1.input.read()}`);
}

Writing data to a GPIO pin

Writing data to a GPIO pin is fairly simple as well, since only the previously declared variable configured as output in the GPIO setup is required.

The following code showcases how a logical one or logical zero can be written to the GPIO pin PA1, depending on the current value of the variable toogle.

var toogle = true;

every 1 second {

    if(toogle){
        PA1.out.write(true);
        toogle = false;
    }

    else{
        PA1.out.write(false);
        toogle = true;
    }
}