2020-07-23 15:19:35 +02:00
|
|
|
|
# System / `spi` SPI Tool
|
|
|
|
|
|
|
|
|
|
The I2C tool provides a way to debug SPI related problems. This README file will
|
|
|
|
|
provide usage information for the SPI tools.
|
|
|
|
|
|
|
|
|
|
## Contents
|
|
|
|
|
|
|
|
|
|
- System Requirements
|
|
|
|
|
- SPI Driver
|
|
|
|
|
- Configuration Options
|
|
|
|
|
- Help
|
|
|
|
|
- Common Line Form
|
|
|
|
|
- Common Command Options
|
|
|
|
|
- _Sticky_ Options
|
|
|
|
|
- Environment variables
|
|
|
|
|
- Common Option Summary
|
|
|
|
|
- Command summary
|
|
|
|
|
- `bus`
|
|
|
|
|
- `dev`
|
|
|
|
|
- `get`
|
|
|
|
|
- `set`
|
|
|
|
|
- `verf`
|
|
|
|
|
- I2C Build Configuration
|
|
|
|
|
- NuttX Configuration Requirements
|
|
|
|
|
- I2C Tool Configuration Options
|
|
|
|
|
|
|
|
|
|
## System Requirements
|
|
|
|
|
|
|
|
|
|
The SPI tool is designed to be implemented as a NuttShell (NSH) add-on. Read the
|
|
|
|
|
`apps/nshlib/README.md` file for information about add-ons.
|
|
|
|
|
|
|
|
|
|
### Configuration Options
|
|
|
|
|
|
|
|
|
|
- `CONFIG_NSH_BUILTIN_APPS` – Build the tools as an NSH built-in command.
|
|
|
|
|
- `CONFIG_SPITOOL_MINBUS` – Smallest bus index supported by the hardware
|
|
|
|
|
(default `0`).
|
|
|
|
|
- `CONFIG_SPITOOL_MAXBUS` – Largest bus index supported by the hardware
|
|
|
|
|
(default `3`).
|
|
|
|
|
- `CONFIG_SPITOOL_DEFFREQ` – Default frequency (default: `40000000`).
|
|
|
|
|
- `CONFIG_SPITOOL_DEFMODE` – Default mode, where
|
|
|
|
|
```
|
|
|
|
|
0 = CPOL=0, CHPHA=0
|
|
|
|
|
1 = CPOL=0, CHPHA=1
|
|
|
|
|
2 = CPOL=1, CHPHA=0
|
|
|
|
|
3 = CPOL=1, CHPHA=1
|
|
|
|
|
```
|
|
|
|
|
- `CONFIG_SPITOOL_DEFWIDTH` – Default bit width (default `8`).
|
|
|
|
|
- `CONFIG_SPITOOL_DEFWORDS` – Default number of words to exchange (default `1`).
|
|
|
|
|
|
|
|
|
|
## Help
|
|
|
|
|
|
|
|
|
|
The SPI tools supports some help output. That help output can be view by
|
|
|
|
|
entering either:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
nsh> spi help
|
|
|
|
|
```
|
2019-05-11 02:14:44 +02:00
|
|
|
|
|
|
|
|
|
or
|
|
|
|
|
|
2020-07-23 15:19:35 +02:00
|
|
|
|
```
|
|
|
|
|
nsh> spi ?
|
|
|
|
|
```
|
2019-05-11 02:14:44 +02:00
|
|
|
|
|
2020-07-23 15:19:35 +02:00
|
|
|
|
Here is an example of the help output. I shows the general form of the command
|
|
|
|
|
line, the various SPI commands supported with their unique command line options,
|
|
|
|
|
and a more detailed summary of the command SPI command options.
|
2019-05-11 02:14:44 +02:00
|
|
|
|
|
2020-07-23 15:19:35 +02:00
|
|
|
|
```
|
2019-05-11 02:14:44 +02:00
|
|
|
|
nsh> Usage: spi <cmd> [arguments]
|
2020-07-23 15:19:35 +02:00
|
|
|
|
|
2019-05-11 02:14:44 +02:00
|
|
|
|
Where <cmd> is one of:
|
|
|
|
|
|
|
|
|
|
Show help : ?
|
2020-07-23 15:19:35 +02:00
|
|
|
|
List buses : bus
|
2019-05-11 02:14:44 +02:00
|
|
|
|
SPI Exchange : exch [OPTIONS] [<hex senddata>]
|
|
|
|
|
Show help : help
|
|
|
|
|
|
2020-07-23 15:19:35 +02:00
|
|
|
|
Where common _sticky_ OPTIONS include:
|
2019-05-11 02:14:44 +02:00
|
|
|
|
[-b bus] is the SPI bus number (decimal). Default: 0 Current: 2
|
|
|
|
|
[-f freq] SPI frequency. Default: 4000000 Current: 4000000
|
|
|
|
|
[-m mode] Mode for transfer. Default: 0 Current: 0
|
|
|
|
|
[-u udelay] Delay after transfer in uS. Default: 0 Current: 0
|
|
|
|
|
[-w width] Width of bus. Default: 8 Current: 8
|
2020-07-23 15:19:35 +02:00
|
|
|
|
[-x count] Words to exchange. Default: 1 Current: 4
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
**Notes**:
|
|
|
|
|
|
|
|
|
|
- An environment variable like $PATH may be used for any argument.
|
|
|
|
|
- Arguments are _sticky_. For example, once the SPI address is specified, that
|
|
|
|
|
address will be re-used until it is changed.
|
|
|
|
|
|
|
|
|
|
**Warning**:
|
|
|
|
|
|
|
|
|
|
- The SPI commands may have bad side effects on your SPI devices. Use only at
|
|
|
|
|
your own risk.
|
|
|
|
|
|
|
|
|
|
## Command Line Form
|
|
|
|
|
|
|
|
|
|
The SPI is started from NSH by invoking the `spi` command from the NSH command
|
|
|
|
|
line. The general form of the `spi` command is:
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
spi <cmd> [arguments]
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Where `<cmd>` is a _sub-command_ and identifies one SPI operation supported by
|
|
|
|
|
the tool. `[arguments]` represents the list of arguments needed to perform the
|
|
|
|
|
SPI operation. Those arguments vary from command to command as described below.
|
|
|
|
|
However, there is also a core set of common `OPTIONS` supported by all commands.
|
|
|
|
|
So perhaps a better representation of the general SPI command would be:
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
i2c <cmd> [OPTIONS] [arguments]
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Where `[OPTIONS]` represents the common options and and arguments represent the
|
|
|
|
|
operation-specific arguments.
|
|
|
|
|
|
|
|
|
|
## Common Command Options
|
|
|
|
|
|
|
|
|
|
### _Sticky_ Options
|
|
|
|
|
|
|
|
|
|
In order to interact with SPI devices, there are a number of SPI parameters that
|
|
|
|
|
must be set correctly. One way to do this would be to provide to set the value
|
|
|
|
|
of each separate command for each SPI parameter. The SPI tool takes a different
|
|
|
|
|
approach, instead: The SPI configuration can be specified as a (potentially
|
|
|
|
|
long) sequence of command line arguments.
|
|
|
|
|
|
|
|
|
|
These arguments, however, are _sticky_. They are sticky in the sense that once
|
|
|
|
|
you set the SPI parameter, that value will remain until it is reset with a new
|
|
|
|
|
value (or until you reset the board).
|
|
|
|
|
|
|
|
|
|
### Environment Variables
|
|
|
|
|
|
|
|
|
|
**Note** also that if environment variables are not disabled (by
|
|
|
|
|
`CONFIG_DISABLE_ENVIRON=y`), then these options may also be environment
|
|
|
|
|
variables. Environment variables must be preceded with the special character
|
|
|
|
|
`$`. For example, `PWD` is the variable that holds the current working directory
|
|
|
|
|
and so `$PWD` could be used as a command line argument. The use of environment
|
|
|
|
|
variables on the I2C tools command is really only useful if you wish to write
|
|
|
|
|
NSH scripts to execute a longer, more complex series of SPI commands.
|
|
|
|
|
|
|
|
|
|
### Common Option Summary
|
|
|
|
|
|
|
|
|
|
- `[-b bus]` is the SPI bus number (decimal). Default: `0`
|
|
|
|
|
|
|
|
|
|
Which SPI bus to commiuncate on. The bus must have been initialised as a
|
|
|
|
|
character device in the config in the form `/dev/spiX` (e.g. `/dev/spi2`).
|
2019-05-11 02:14:44 +02:00
|
|
|
|
|
|
|
|
|
The valid range of bus numbers is controlled by the configuration settings
|
2020-07-23 15:19:35 +02:00
|
|
|
|
`CONFIG_SPITOOL_MINBUS` and `CONFIG_SPITOOL_MAXBUS`.
|
2019-05-11 02:14:44 +02:00
|
|
|
|
|
|
|
|
|
The bus numbers are small, decimal numbers.
|
|
|
|
|
|
2020-07-23 15:19:35 +02:00
|
|
|
|
- `[-m mode]` SPI Mode for transfer.
|
|
|
|
|
|
2019-05-11 02:14:44 +02:00
|
|
|
|
Which of the available SPI modes is to be used. Options are;
|
|
|
|
|
|
2020-07-23 15:19:35 +02:00
|
|
|
|
```
|
|
|
|
|
0 = CPOL=0, CHPHA=0
|
|
|
|
|
1 = CPOL=0, CHPHA=1
|
|
|
|
|
2 = CPOL=1, CHPHA=0
|
|
|
|
|
3 = CPOL=1, CHPHA=1
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
- `[-u udelay]` Delay after transfer in uS. Default: `0`
|
|
|
|
|
|
|
|
|
|
Any extra delay to be provided after the transfer. Not normally needed from
|
|
|
|
|
the command line.
|
|
|
|
|
|
|
|
|
|
- `[-x count]` Words to exchange Default: `1`
|
2019-05-11 02:14:44 +02:00
|
|
|
|
|
|
|
|
|
The number of words to be transited over the bus. For sanitys sake this is
|
2020-07-23 15:19:35 +02:00
|
|
|
|
limited to a relatively small number (`40` by default). Any data on the
|
|
|
|
|
command line is sent first, padded by `0xFF`'s while any remaining data are
|
|
|
|
|
received.
|
2019-05-11 02:14:44 +02:00
|
|
|
|
|
2020-07-23 15:19:35 +02:00
|
|
|
|
- `[-w width]` is the data width (varies according to target). Default: `8`
|
2019-05-11 02:14:44 +02:00
|
|
|
|
|
|
|
|
|
Various SPI devices support different data widths. This option is untested.
|
|
|
|
|
|
2020-07-23 15:19:35 +02:00
|
|
|
|
- `[-f freq]` I2C frequency. Default: `4000000` Current: `4000000`
|
2019-05-11 02:14:44 +02:00
|
|
|
|
|
2020-07-23 15:19:35 +02:00
|
|
|
|
The `[-f freq]` sets the frequency of the SPI device. The default is very
|
|
|
|
|
conservative.
|
2019-05-11 02:14:44 +02:00
|
|
|
|
|
2020-07-23 15:19:35 +02:00
|
|
|
|
## Command Summary
|
2019-05-11 02:14:44 +02:00
|
|
|
|
|
2020-07-23 15:19:35 +02:00
|
|
|
|
### List buses: `bus [OPTIONS]`
|
2019-05-11 02:14:44 +02:00
|
|
|
|
|
2020-07-23 15:19:35 +02:00
|
|
|
|
This command will simply list all of the configured SPI buses and indicate which
|
|
|
|
|
are supported by the driver and which are not:
|
2019-05-11 02:14:44 +02:00
|
|
|
|
|
2020-07-23 15:19:35 +02:00
|
|
|
|
```
|
|
|
|
|
BUS EXISTS?
|
|
|
|
|
Bus 1: YES
|
|
|
|
|
Bus 2: NO
|
|
|
|
|
```
|
2019-05-11 02:14:44 +02:00
|
|
|
|
|
|
|
|
|
The valid range of bus numbers is controlled by the configuration settings
|
2020-07-23 15:19:35 +02:00
|
|
|
|
`CONFIG_SPITOOL_MINBUS` and `CONFIG_SPITOOL_MAXBUS`.
|
2019-05-11 02:14:44 +02:00
|
|
|
|
|
2020-07-23 15:19:35 +02:00
|
|
|
|
### Exchange data: `exch [OPTIONS] <Optional TX Data>`
|
2019-05-11 02:14:44 +02:00
|
|
|
|
|
|
|
|
|
This command triggers an SPI transfer, returning the data back from the far end.
|
|
|
|
|
As an example (with MOSI looped back to MISO);
|
|
|
|
|
|
2020-07-23 15:19:35 +02:00
|
|
|
|
```shell
|
|
|
|
|
nsh> spi exch -b 2 -x 4 aabbccdd
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```
|
2019-05-11 02:14:44 +02:00
|
|
|
|
Received: AA BB CC DD
|
2020-07-23 15:19:35 +02:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Note that the `TX Data` are always specified in hex, and are always two digits
|
|
|
|
|
each, case insensitive.
|
2019-05-11 02:14:44 +02:00
|
|
|
|
|
2020-07-23 15:19:35 +02:00
|
|
|
|
## I2C Build Configuration
|
2019-05-11 02:14:44 +02:00
|
|
|
|
|
2020-07-23 15:19:35 +02:00
|
|
|
|
### NuttX Configuration Requirements
|
2019-05-11 02:14:44 +02:00
|
|
|
|
|
|
|
|
|
The SPI tools requires the following in your NuttX configuration:
|
|
|
|
|
|
|
|
|
|
1. Application configuration.
|
|
|
|
|
|
2020-07-23 15:19:35 +02:00
|
|
|
|
Using `make menuconfig`, select the SPI tool. The following definition should
|
|
|
|
|
appear in your `.config` file:
|
2019-05-11 02:14:44 +02:00
|
|
|
|
|
2020-07-23 15:19:35 +02:00
|
|
|
|
```conf
|
|
|
|
|
CONFIG_SYSTEM_SPI=y
|
|
|
|
|
```
|
2019-05-11 02:14:44 +02:00
|
|
|
|
|
|
|
|
|
2. Device-specific SPI driver support must be enabled:
|
|
|
|
|
|
2020-07-23 15:19:35 +02:00
|
|
|
|
```conf
|
|
|
|
|
CONFIG_SPI_DRIVER=y
|
|
|
|
|
```
|
2019-05-11 02:14:44 +02:00
|
|
|
|
|
2020-07-23 15:19:35 +02:00
|
|
|
|
The SPI tool will then use the SPI character driver to access the SPI bus.
|
|
|
|
|
These devices will reside at `/dev/spiN` where `N` is the I2C bus number.
|
2019-05-11 02:14:44 +02:00
|
|
|
|
|
2020-07-23 15:19:35 +02:00
|
|
|
|
**Note**: The SPI driver `ioctl` interface is defined in
|
|
|
|
|
`include/nuttx/spi/spi.h`.
|