ext-gpio · C API
Pins in C
For package authors: take pins from the board file as GPIO numbers or as names of an IO expander pin, and drive them through one API.
Some boards wire a control line (a reset, a backlight enable) to an IO expander instead of a GPIO of the chip. A
board file then names the pin as a string, "rst": "exio1", where another board has a number, "rst": 7.
bm_pin.h (from bm-common 1.2) hides the difference: a driver resolves the pin
once and sets, reads and configures it with the same calls whatever drives it. ext-gpio 1.2 implements the header
and declares the pin_providers registry, where the packages of IO expanders register.
This page is for people who write a driver or an expander package. A PHP project does not call these functions.
Require ext-gpio
A package that calls the functions of bm_pin.h requires ext-gpio, which brings bm-common:
{
"require": { "php-baremetal/ext-gpio": "^1.2" }
}
Names
| Name in the board file | Pin |
|---|---|
an integer, 7 |
GPIO 7 of the chip. -1 is no pin. |
a string of decimal digits, "7" |
GPIO 7 of the chip. "-1" is no pin. |
any other string, "exio1" |
The pin of the installed provider that owns the name. |
A GPIO number past 255 is an error (BM_ERR_INVALID_ARG). A name no installed provider owns is
BM_ERR_NOT_FOUND: the project needs the expander package of its board.
Use a pin in a driver
Read the pin in onboard() with bm_pin_board(), which takes the block and the key of the board file. An absent
key is no pin (BM_PIN_NONE, with BM_OK), so an optional line needs no special case:
#include "bm_pin.h"
#include "php.h"
static bm_pin_t s_rst;
static bm_err_t my_onboard(void)
{
bm_err_t err = bm_pin_board("touch", "rst", &s_rst);
if (err != BM_OK) {
php_error_docref(NULL, E_WARNING, "touch.rst: %s", bm_pin_err_name(err));
return err;
}
if (bm_pin_valid(s_rst)) {
bm_pin_mode(s_rst, BM_PIN_OUTPUT);
bm_pin_set(s_rst, false); /* hold the chip in reset */
/* ... wait ... */
bm_pin_set(s_rst, true);
}
return BM_OK;
}
| Function | Does |
|---|---|
bm_pin_resolve(name, &pin) |
Resolves a name of the table above. A provider is opened at the first resolve of one of its pins. |
bm_pin_board(block, key, &pin) |
Resolves block.key of the board file: an integer is a GPIO, a string is resolved. |
bm_pin_mode(pin, BM_PIN_INPUT or BM_PIN_OUTPUT) |
Sets the direction. |
bm_pin_set(pin, level) |
Drives the pin. |
bm_pin_get(pin, &level) |
Reads the pin. |
bm_pin_valid(pin), bm_pin_is_gpio(pin) |
Whether it is a pin at all, and whether it is a GPIO of the chip. |
bm_pin_err_name(err) |
A short English reason for a message. |
On an error, the resolve functions leave BM_PIN_NONE in the output. bm_pin_mode(), bm_pin_set() and
bm_pin_get() return BM_ERR_INVALID_ARG for BM_PIN_NONE, and a provider may return BM_ERR_NOT_SUPPORTED for
a pin that cannot do what is asked (an input of the expander, for example).
A GPIO pin may be set and read from any task. A pin of a provider is for the PHP task only, unless its package says otherwise: the provider talks to its chip over a bus that the PHP task owns.
Write a provider
A provider is a bm_pin_provider_t that a package registers into pin_providers:
#include <string.h>
#include "bm_pin.h"
typedef struct { unsigned char out; } my_exp_t;
static my_exp_t s_exp;
/* "p0" .. "p7" are ours. */
static int my_lookup(const char *pin)
{
return (pin[0] == 'p' && pin[1] >= '0' && pin[1] <= '7' && pin[2] == '\0') ? pin[1] - '0' : -1;
}
static void *my_open(bm_err_t *err)
{
/* start the chip; on failure: *err = the error, return NULL (the next resolve tries again) */
(void) err;
return &s_exp;
}
static bm_err_t my_mode(void *ctx, int index, bm_pin_mode_t mode)
{
(void) ctx; (void) index;
return mode == BM_PIN_OUTPUT ? BM_OK : BM_ERR_NOT_SUPPORTED;
}
static bm_err_t my_set(void *ctx, int index, bool level)
{
my_exp_t *e = ctx;
e->out = (unsigned char) (level ? (e->out | (1u << index)) : (e->out & ~(1u << index)));
return BM_OK; /* and write e->out to the chip */
}
static bm_err_t my_get(void *ctx, int index, bool *level)
{
*level = (((my_exp_t *) ctx)->out >> index) & 1;
return BM_OK;
}
const bm_pin_provider_t my_provider = { "my-expander", my_lookup, my_open, my_mode, my_set, my_get };
and in its composer.json:
{
"require": { "php-baremetal/ext-gpio": "^1.2" },
"extra": {
"baremetal": {
"register": { "pin_providers": ["my_provider"] }
}
}
}
| Field | Meaning |
|---|---|
name |
The provider's name, for messages. |
lookup(pin) |
The index of a pin name it owns, or -1. The first provider that owns a name wins (providers are ordered by package name). |
open(&err) |
Called at the first resolve of one of its pins; returns the context passed to the other callbacks, or NULL with *err set. A failed open is not remembered. A chip that does not answer (BM_ERR_NOT_FOUND) is reported as BM_ERR_INVALID_STATE: its provider did not start. |
mode, set, get |
Configure, drive and read the pin index. A NULL callback answers BM_ERR_NOT_SUPPORTED. |
The callbacks run in the PHP task. Only the first 8 providers of the registry can be used (it is ordered by
package name); a pin of a later one is BM_ERR_NO_MEM. The build generates the registry from the installed packages; pin_provider_desc.h declares it
(pin_providers, NULL-terminated, and pin_providers_count).
Related
- bm-common Headers
- Functions and constants PHP API