Reading and writing pins

Informative

How to read, write, and toggle pins and ports. Binding rules: CHI-PORT-MUST-03, CHI-PORT-MUST-04, CHI-PORT-MUST-05, and CHI-PORT-MUST-06 in Port — Requirements.

Single pins

Port_SetPinValue(PORT_PIN_LED, PORT_PIN_VALUE_HIGH);   /* write */
Port_PinValueType v = Port_GetPinValue(PORT_PIN_BUTTON);  /* read (LOW/HIGH) */
Port_PinValueType now = Port_TogglePinValue(PORT_PIN_LED); /* flip, returns new value */

Reads work for both input and output pins. Where the hardware can read the physical pin directly, the read reflects the pin; otherwise it reflects the latched output-register value. On error a read returns PORT_PIN_VALUE_LOW.

Whole ports

Read every pin of a port in one call — bit n is pin index n; undefined pins read LOW:

uint32 pins = Port_GetPortValue(PORT_PORT_SENSOR);

Write selected pins of a port atomically with a bitmask (only the masked pins change):

/* set pins 0 and 3 high, clear pin 1, leave the rest untouched */
Port_SetMaskedPortValue(PORT_PORT_SENSOR, 0x09u, 0x0Bu);   /* writes value & mask */

/* update every pin in the port at once */
Port_SetMaskedPortValue(PORT_PORT_SENSOR, value, PORT_PIN_MASK_ALL);

Where the hardware allows, all masked pins update simultaneously — including a mix of LOW→HIGH and HIGH→LOW in the same call.

Note

Set APIs are thread-safe. Writing an input pin, however, is implementation-defined (some MCUs use it to steer internal pull-ups/downs, others forbid it) — don’t rely on a particular behavior (CHI-PORT-MUST-04).