Skip to content

portMode

gicking edited this page Feb 6, 2018 · 5 revisions

back to Command Reference / Digital-IO

Description

Individually configure each of the specified 8 port pins of the specified I/O port as input or output. Inputs can optionally be configured with pull-up and/or external (port) interrupt. Outputs can optionally be configured as open-drain and/or slow mode e.g. for better EMC behavior.

Notes:

  • after reset all I/O pins are configured as INPUT
  • for external port interrupts, also set edge sensitivity via configEdgeExint()
  • for top level pin interrupt, also set edge sensitivity via configEdgeTLI()

Inclusion

  • defined in gpio.h
  • auto-loaded in main_general.h
  • no #define required

Syntax

portMode(pPort,dir,cr1, cr2)

Parameters

  • input:

    • pPort: address of port to read, e.g. &PORT_A
    • dir: direction bitmask: input(=0) or output(=1)
    • cr1: control 1 bitmask: input: 0=float, 1=pull-up; output: 0=open-drain, 1=push-pull
    • cr2: control 2 bitmask: input: 0=no exint, 1=exint; output: 0=slow, 1=fast
  • output:

    • none

Returns

  • nothing

Example Code

The below code makes pins PD0..3 to output, push-pull, fast, and pins PD4..7 to input, pull-up, no interrupt. Then toggle all output port pins HIGH and LOW with 1s delay.

#include "main_general.h"   // board-independent main

void setup()
{
  // set PD0..3 to output/push-pull/fast, and  
  portMode(&PORT_D, 0b00001111, 0b11111111, 0b00001111);
}

void loop()
{
  portOutputReg(&PORT_D) ^= 0b1111111;  // toggle PD0..3, others are inputs -> don't care
  delay(1000);                          // wait 1s
}

Relevant Tutorial

  • tbd

Notes and Warnings

  • portMode() does for a complete 8-pin port, what pinMode() does for a single port pin. Advantage is that all 8 port pinscan be configured with one command. Disadvantage is a more complex API with three bitmasks instead of one parameter.

EXINT specific:

  • after reset the edge sensitivity of all EXINT ports is LOW. If a pin is static low when configured as input with interrupt, this will stall the device. Therefore set the edge sensitivity first via configEdgeExint()
  • a floating pin configured as input with interrupt enabled can generate excessive interrupt load and effectively stall the device. Therefore configure EXINT pins with pull-up or assert stable levels via the external circuit

To avoid damage

  • do not expose I/Os to voltages outside [-0.3V; Vdd+0.3V], else limit injection currents to the specificied limits
  • for OUTPUT pins assert that sink and source currents are below the specificied limits
  • do not directly connect two OUTPUT pins. If e.g. half-duplex is required, use a pull-up and OUTPUT_OPENDRAIN, instead

See also

Clone this wiki locally