Skip to main content

gpio-button-led

The canonical "first example" for the ALP SDK. It opens one GPIO as an output (LED) and one as an input (button), toggles the LED, and reads the button state. Works on every E1M-conformant SoM.

Source: examples/gpio-button-led/.

board.yaml

schema_version: 2

som:
sku: E1M-AEN701 # change to your MPN

carrier:
name: E1M-EVK # or your custom carrier

cores:
m55_hp:
os: zephyr # or yocto on A-class cores; baremetal where appropriate
app: ./src
peripherals: [gpio]

diagnostics:
log_level: info

Source

#include <alp/peripheral.h>
#include <alp/e1m_pinout.h>
#include <zephyr/kernel.h>

int main(void) {
// Open LED as output
alp_gpio_t *led = alp_gpio_open(&(alp_gpio_config_t){
.pin_id = E1M_GPIO_IO1,
.direction = ALP_GPIO_DIR_OUTPUT,
});
if (led == NULL) {
printk("[gpio] led open failed: err=%d\n", (int)alp_last_error());
return -1;
}

// Open button as input with pull-up
alp_gpio_t *button = alp_gpio_open(&(alp_gpio_config_t){
.pin_id = E1M_GPIO_IO0,
.direction = ALP_GPIO_DIR_INPUT,
.pull = ALP_GPIO_PULL_UP,
});
if (button == NULL) {
printk("[gpio] button open failed: err=%d\n", (int)alp_last_error());
alp_gpio_close(led);
return -1;
}

printk("[gpio] init button=E1M_GPIO_IO0, led=E1M_GPIO_IO1\n");

// Toggle the LED four times
for (int i = 0; i < 4; i++) {
int status = alp_gpio_write(led, i & 1);
printk("[gpio] led=%d status=%d\n", i & 1, status);
k_msleep(500);
}

// Read the button state
bool pressed;
int status = alp_gpio_read(button, &pressed);
printk("[gpio] is_pressed -> status=%d pressed=%d\n", status, pressed);

alp_gpio_close(button);
alp_gpio_close(led);
printk("[gpio] done\n");
return 0;
}

Build + run (native_sim)

west alp-build -b native_sim/native/64 alp-sdk/examples/gpio-button-led
west build -d build -t run

Expected output:

*** Booting Zephyr OS build v4.4.0 ***
[gpio] init button=E1M_GPIO_IO0, led=E1M_GPIO_IO1
[gpio] led=0 status=0
[gpio] led=1 status=0
[gpio] led=0 status=0
[gpio] led=1 status=0
[gpio] is_pressed -> status=0 pressed=1
[gpio] done

status=0 means ALP_OK. pressed=1 is gpio_emul's default "input is low" report; on real hardware it depends on the button state.

Build + run (real silicon)

west alp-build -b alp_e1m_evk_aen alp-sdk/examples/gpio-button-led
west flash

The example's boards/alp_e1m_evk_aen.overlay maps E1M_GPIO_IO0 / IO1 to the EVK's button + LED. Other boards work the same way once their overlay is in place.

Key teaching points

  • alp_gpio_open returns a handle or NULL. Always check.
  • alp_last_error() is the only diagnostic for NULL — it's a thread-local; check it immediately after the open call.
  • Instance IDs (E1M_GPIO_IO0) come from <alp/e1m_pinout.h> and are portable across every conformant SoM.
  • alp_gpio_close is observable — apps that leak handles will hit a runtime check eventually.

Variants

The same example pattern extends to:

See also

Questions about this page? Discuss in Community Forum