Using Digital I/O


Reading states of Digital I/O

When reading status of digital I/O ports, you can get multiple states of them with pid_read function or a single state with pid_ioctl function. You can also read the type of a digital I/O port.

pid_read($pid, VALUE); // read multiple states(in 32bits unit)
pid_ioctl($pid, "get N ITEM"); // read a single state(in a bit unit)

In the way of reading a single state, available ITEMs are as follows:

ITEM Description
mode Return the port status
in string type
I/O pin: "in", "out", "led_xxx" and so on
pins while using by UART, SPI or I2C: "hdev"
Designated to output pin of ST: "st_out"
input Return the input port status in integer (0: LOW, 1: HIGH)
output Return the output port status in integer (0: LOW, 1: HIGH)

example of reading multiple digital I/O states

The example below prints status of port 0 to 7 after setting them to input and getting the status.

<?php
$value = 0;
$pid = pid_open("/mmap/uio0");          // open UIO0
pid_ioctl($pid, "set 0-7 mode in_pu");  // set port 0 ~ 7 to input(pull-up)
pid_read($pid, $value);                 // read digital I/O status(32bits unit)
printf("0x%x\r\n", $value);             // output example : 0xffffffff
?>

example of reading a single I/O state

The example below prints a state of port 0 of UIO0 after setting it to output and getting the mode and state.

<?php
$pid = pid_open("/mmap/uio0");             // open UIO0
pid_ioctl($pid, "set 0 mode out high");    // set port 0 to output
$mode = pid_ioctl($pid, "get 0 mode");     // read a digital I/O mode
$output = pid_ioctl($pid, "get 0 output"); // read a digital I/O state
printf("%s, %d\r\n", $mode, $output);      // output : out, 1
?>

※ When reading a port state with pid_ioctl function, you must use "get N input" if it is set to input port and use "get N output" if it is set to output port.

Writing Values to Digital I/O

When writing values to digital I/O ports, you can set a value to multiple ports with pid_write function or a single port with pid_ioctl function.

pid_write($pid, VALUE); // write to multiple ports(32 bits unit)
pid_ioctl($pid, "set N output TYPE"); // write to a single port(a bit unit)

example of writing values to multiple ports

The following example prints the states of digital I/O ports after setting 0 ~ 7 pins of UIO0 to output ports and writing a given value.

<?php
$value = 0;
$pid = pid_open("/mmap/uio0");                       // open UIO0
pid_ioctl($pid, "set 0-7 mode out");                 // set port 0 ~ 7 to output
pid_read($pid, $value);                              // read status
pid_write($pid, ($value & 0xffffff00) | 0x00000055); // write 0x00000055
pid_read($pid, $value);                              // read status
printf("0x%0x\r\n", $value);                         // output example : 0x00000055
?>

example of writing a value to a single port

The following example prints a state of UIO0's port 0 after setting it to digital output with default LOW and writing HIGH.

<?php
$pid = pid_open("/mmap/uio0");             // open UIO0
pid_ioctl($pid, "set 0 mode out low");     // set port 0 to output(LOW)
pid_ioctl($pid, "set 0 output high");      // write HIGH
$output = pid_ioctl($pid, "get 0 output"); // read state of port 0
printf("%d\r\n", $output);                 // output : 1
?>

example of setting output lock

The following example shows the difference between locked and unlocked state of port 0.

<?php
$pid = pid_open("/mmap/uio0");              // open UIO0
pid_ioctl($pid, "set 0 mode out low");      // set port 0 to output(LOW)
pid_ioctl($pid, "set 0 lock");              // enable port 0 to output lock
pid_ioctl($pid, "set 0 output high");       // write HIGH to port 0
$output1 = pid_ioctl($pid, "get 0 output"); // read state of port 0
pid_ioctl($pid, "set 0 unlock");            // disable the output lock
pid_ioctl($pid, "set 0 output high");       // write HIGH to port 0 again
$output2 = pid_ioctl($pid, "get 0 output"); // read state of port 0
printf("%d, %d\r\n", $output1, $output2);   // output : 0, 1
?>