Setting and Using SPI

It is required to call pid_ioctl function when you set or use SPI. Available commands of pid_ioctl function for SPI are as follows:

Command Sub Command Description
set lsb 0 set a bit transmission order: MSB first
1 set a bit transmission order: LSB first
data 8 set a size of data unit: 8 bits
16 set a size of data unit: 16 bits
div [N] set a division: 2/4/8/16/32/64/128/256
mode [M] set an SPI mode: 0/1/2/3
nss id N select a SPI slave, 0 ~ 7
dev uio0 #pin assign a uio pin as a Slave Select(SS) pin
get rxlen get the number of pending bytes in receive buffer
txlen get the number of pending bytes in send buffer
txfree get the free send buffer size
req start request to write data
reset request to reset bus

Setting SPI

You can set SPI mode, bit transmission order, size of data unit and division by using a "set" command.

Set a Bit Transmission Order

You can set 0 or 1 for setting SPI bit transmission order and the default value is 0. If this value is set to 0, MSB will be transmitted first. How to set the bit transmission order is as follows:

Bit Transmission Order Syntax
MSB > LSB pid_ioctl($pid, "set lsb 0");
LSB > MSB pid_ioctl($pid, "set lsb 1");

Set a Size of Data Unit

You can set 8 or 16 for setting size of data unit and the default value is 8. How to set the size of data unit is as follows:

Size of Data Unit Syntax
8 bits pid_ioctl($pid, "set data 8");
16 bits pid_ioctl($pid, "set data 16");

Set a Data Rate

Data rate depends on division rate of PHPoC's basic clock and you can choose one of the division rate values of 2 / 4 / 8 / 16 / 32 / 64 / 128 / 256. The default value is 256. How to set the data rate is as follows:

Division Rate Syntax
1 of N pid_ioctl($pid, "set div N");

※ Basic clock of P4S-341/P4S-342 is 42MHz. Thus, data rate is approximately 164Kbps when the division rate is set to 256.

SPI Mode

You can set one of SPI modes of 0 to 3 and the default value is 3. How to set SPI mode is as follows:

SPI Mode Syntax
mode 0 pid_ioctl($pid, "set mode 0");
mode 1 pid_ioctl($pid, "set mode 1");
mode 2 pid_ioctl($pid, "set mode 2");
mode 3 pid_ioctl($pid, "set mode 3");

Select Slaves and Assign Pins

When the PHPoC communicates with more than one slave, you can select a slave using the "set nss id" command. PHPoC can communicate with up to 8 SPI slaves. ID can be selected from 0 ~ 7 and the default is 0.

Division Syntax
Select N th slave pid_ioctl($pid, "set nss N");

The each slave select(SS) pin of the slave which has an ID 1 to 7 can be set using the "set nss dev" command. A uio device name and a pin number should be specified after this command.

pid_ioctl($pid, "set nss id 1");
pid_ioctl($pid, "set nss dev uio0 4");
pid_ioctl($pid, "set nss id 2");
pid_ioctl($pid, "set nss dev uio0 5");

***※ Note: Slave select pin (SS) of slave ID 0 is fixed to 0th pin of uio0 and can not be changed to another pin.

Getting SPI Status

You can get status of SPI by using "get" command.

Get the Number of Pending Bytes in Receive and Send Buffer

How to get the number of pending bytes in receive and send buffer as follows:

Division Syntax
Send buffer pid_ioctl($pid, "get txlen");
Receive buffer pid_ioctl($pid, "get rxlen");

Get the Remaining Size of Send Buffer

How to get the remaining size of send buffer in byte-unit is as follows:

Division Syntax
remaining size of send buffer pid_ioctl($pid, "get txfree");

Using SPI

Request to Write Data

You can request to write data with this command. You should input data to send buffer before using this command. After writing data, you should read data from an SPI slave as much as you sent. How to use this command is as follows:

Command Syntax
Request to Write Data pid_ioctl($pid, "req start");

Request to Reset Bus

You can reset an SPI bus with this command when communication is bad.

Command Syntax
Request to Request Bus pid_ioctl($pid, "req reset");