SAM4SD32 (SAM4S-EK2)
Loading...
Searching...
No Matches
Quick start guide for the common IOPORT service

This is the quick start guide for the Common IOPORT API, with step-by-step instructions on how to configure and use the service in a selection of use cases.

The use cases contain several code fragments. The code fragments in the steps for setup can be copied into a custom initialization function, while the steps for usage can be copied into, e.g., the main application function.

Basic use case

In this use case we will configure one IO pin for button input and one for LED control. Then it will read the button state and output it on the LED.

Setup steps

Example code

#define MY_LED IOPORT_CREATE_PIN(PORTA, 5)
#define MY_BUTTON IOPORT_CREATE_PIN(PORTA, 6)
static void ioport_set_pin_mode(ioport_pin_t pin, ioport_mode_t mode)
Set pin mode for one single IOPORT pin.
Definition ioport.h:217
static void ioport_set_pin_dir(ioport_pin_t pin, enum ioport_direction dir)
Set direction for a single IOPORT pin.
Definition ioport.h:263
#define IOPORT_MODE_PULLUP
Definition ioport_pio.h:78
static void ioport_init(void)
Initializes the IOPORT service, ready for use.
Definition ioport.h:145
@ IOPORT_DIR_OUTPUT
Definition ioport.h:78
@ IOPORT_DIR_INPUT
Definition ioport.h:77

Workflow

  1. It's useful to give the GPIOs symbolic names and this can be done with the IOPORT_CREATE_PIN macro. We define one for a LED and one for a button.
    • #define MY_LED IOPORT_CREATE_PIN(PORTA, 5)
      #define MY_BUTTON IOPORT_CREATE_PIN(PORTA, 6)
    • Note
      The usefulness of the IOPORT_CREATE_PIN macro and port names differ between architectures:
      • MEGA, MEGA_RF and XMEGA: Use IOPORT_CREATE_PIN macro with port definitions PORTA, PORTB ...
      • UC3: Most convenient to pick up the device header file pin definition and us it directly. E.g.: AVR32_PIN_PB06
      • SAM: Most convenient to pick up the device header file pin definition and us it directly. E.g.: PIO_PA5_IDX
        IOPORT_CREATE_PIN can also be used with port definitions PIOA, PIOB ...
  2. Initialize the ioport service. This typically enables the IO module if needed.
  3. Set the LED GPIO as output:
  4. Set the button GPIO as input:
  5. Enable pull-up for the button GPIO:

Usage steps

Example code

bool value;
value = ioport_get_pin_level(MY_BUTTON);
ioport_set_pin_level(MY_LED, value);
static bool ioport_get_pin_level(ioport_pin_t pin)
Get current value of an IOPORT pin, which has been configured as an input.
Definition ioport.h:301
static void ioport_set_pin_level(ioport_pin_t pin, bool level)
Set an IOPORT pin to a specified logical value.
Definition ioport.h:275

Workflow

  1. Define a boolean variable for state storage:
    • bool value;
  2. Read out the button level into variable value:
  3. Set the LED to read out value from the button:

Advanced use cases