3.1.8. pin — Digital pins

3.1.8.1. Debug file system commands

Three debug file system commands are available, all located in the directory drivers/pin/. These commands directly access the pin device registers, without using the pin driver object.

Command Description
set_mode <pin> <mode> Set the mode of the pin <pin> to <mode>, where
<mode> is one of output, output_open_drain,
output_open_drain_pull_up, input,
input_pull_up and input_pull_down.
read <pin> Read current input value of the pin <pin>.
high or low is printed.
write <pin> <value> Write the value <value> to given output pin <pin>,
where
<value> is one of high and low.

Example output from the shell:

$ drivers/basic/pin/set_mode d2 output
OK
$ drivers/basic/pin/write d2 high
OK
$ drivers/basic/pin/write d2 low
OK
$ drivers/basic/pin/set_mode d3 input
OK
$ drivers/basic/pin/read d3
low
OK

Source code: src/drivers/basic/pin.h, src/drivers/basic/pin.c

Test code: tst/drivers/hardware/basic/pin/main.c


Defines

PIN_OUTPUT

Configure the pin as an output pin.

PIN_OUTPUT_OPEN_DRAIN

Configure the pin as an output open drain pin.

PIN_OUTPUT_OPEN_DRAIN_PULL_UP

Configure the pin as an output open drain pin with the internal pull-up resistor enabled.

PIN_INPUT

Configure the pin as an input pin.

PIN_INPUT_PULL_UP

Configure the pin as an input pin with the internal pull-up resistor enabled.

PIN_INPUT_PULL_DOWN

Configure the pin as an input pin with the internal pull-down resistor enabled.

Functions

int pin_module_init(void)

Initialize the pin module. This function must be called before calling any other function in this module.

The module will only be initialized once even if this function is called multiple times.

Return
zero(0) or negative error code.

int pin_init(struct pin_driver_t *self_p, struct pin_device_t *dev_p, int mode)

Initialize given driver object with given device and mode.

This function may be called from interrupt context and with the system lock taken.

Return
zero(0) or negative error code.
Parameters
  • self_p: Driver object to be initialized.
  • dev_p: Device to use.
  • mode: Pin mode. One of the PIN_OUTPUT* and PIN_INPUT* defines.

int pin_write(struct pin_driver_t *self_p, int value)

Write given value to given pin.

This function may be called from interrupt context and with the system lock taken.

Return
zero(0) or negative error code.
Parameters
  • self_p: Driver object.
  • value: Non-zero for high and 0 for low output.

int pin_read(struct pin_driver_t *self_p)

Read the current value of given pin.

This function may be called from interrupt context and with the system lock taken.

Return
1 for high and 0 for low input, otherwise negative error code.
Parameters
  • self_p: Driver object.

int pin_toggle(struct pin_driver_t *self_p)

Toggle the pin output value (high/low).

This function may be called from interrupt context and with the system lock taken.

Return
zero(0) or negative error code.
Parameters
  • self_p: Driver object.

int pin_set_mode(struct pin_driver_t *self_p, int mode)

Set the pin mode of given pin.

This function may be called from interrupt context and with the system lock taken.

Return
zero(0) or negative error code.
Parameters
  • self_p: Driver object.
  • mode: Pin mode. One of the PIN_OUTPUT* and PIN_INPUT* defines.

static int pin_device_set_mode(const struct pin_device_t *dev_p, int mode)

Pin device mode to set.

This function may be called from interrupt context and with the system lock taken.

Return
zero(0) or negative error code.
Parameters
  • dev_p: Pin device.
  • mode: Pin mode. One of the PIN_OUTPUT* and PIN_INPUT* defines.

static int pin_device_read(const struct pin_device_t *dev_p)

Read the value of given pin device.

This function may be called from interrupt context and with the system lock taken.

Return
1 for high and 0 for low input, otherwise negative error code.
Parameters
  • dev_p: Pin device.

int pin_device_write(const struct pin_device_t *dev_p, int value)

Write given value to given pin device.

This function may be called from interrupt context and with the system lock taken.

Return
zero(0) or negative error code.
Parameters
  • dev_p: Pin device.
  • value: Non-zero for high and 0 for low output.

static int pin_device_write_high(const struct pin_device_t *dev_p)

Write high to given pin device.

This function may be called from interrupt context and with the system lock taken.

Return
zero(0) or negative error code.
Parameters
  • dev_p: Pin device.

static int pin_device_write_low(const struct pin_device_t *dev_p)

Write low to given pin device.

This function may be called from interrupt context and with the system lock taken.

Return
zero(0) or negative error code.
Parameters
  • dev_p: Pin device.

int pin_is_valid_device(struct pin_device_t *dev_p)

Check if given pin device is valid.

This function may be called from interrupt context and with the system lock taken.

Return
true(1) if the pin device is valid, otherwise false(0).
Parameters
  • dev_p: Pin device to validate.

Variables

struct pin_device_t pin_device[PIN_DEVICE_MAX]