SPI
The SPI bus is used for reading and writing sensors, memory devices, etc. Although it uses more pins than I2C, it is often used for devices that require high-speed transfer.
Please refer to the Samp_SPI sample.
When using the SPI bus, only enough SPI select pins are allocated for the required device. The pins are allocated in order, starting with the SEISEL0 pin.
The following are some examples of initialization and read/write operations; SPI also allows simultaneous write and read operations.
A common example when acquiring data from a sensor is the procedure of sending a command followed by getting the result.
Initialize SPI (start using device)
Enable select pin for SPI
SPI Transfer 1 (8 bits), intended for transmission.
SPI transfer 2 (16 bits), intended for reception.
You should always wait for polling (while(bAHI_SpiPollBusy());
) on each transfer.
functions
vAHI_SpiConfigure()
This function initializes the SPI device and makes it available, and calls vAHI_SpiDisable()
to release it.
At least one select pin, SPISEL0, is assigned exclusively to SPI. In other words, when using SPI, SPISEL0 (DIO19) cannot be used for any other purpose.
Name | Remark |
---|---|
| Specifies the chip select pin to be enabled. 0 specifies SPISEL0 to be reserved, 1 specifies SPISEL0,1 to be reserved, and 2 specifies SPISEL0,1,2 to be reserved. |
| If |
| If |
| If |
| Specifies the clock frequency, 0...63, where 0 means 16MHz drive, and the rest are divided by |
| Generates an interrupt at the end of SPI transfer. (It is not used in the samples.) |
| If |
The values of bLsbFirst
, bPolarity
, bPhase
, and u8ClockDiviser
should be determined from the datasheet of the device to be connected.
Relationship between SPI Mode and bPolarity, bPhase.
SPI Mode |
|
|
0 |
|
|
1 |
|
|
2 |
|
|
3 |
|
|
vAHI_SpiSelect()
Activate select pins.
Name | Remark |
---|---|
|
|
vAHI_SpiStop()
and vAHI_SpiSelect(0)
are the same call.
vAHI_SpiStartTransfer()
Start SPI transfer. The transfer starts immediately after this function is called, but the control is returned without waiting for the transfer to finish. It waits for the end of transfer by bAHI_SpiPollBusy()
described below.
Name | Remark |
---|---|
| Specifies the transfer length. 0...31 can be specified, and the specified number + 1 bit is transferred. For example, for a 16-bit transfer, specify 15. |
| The bit sequence to be output. For transfers of less than 32 bits, the data is right-justified to the LSB side. (In the case of 8-bit data, the data is stored in bit0 to bit7.) |
bAHI_SpiPollBusy()
Waiting for polling of SPI transfer, while TRUE means SPI transfer is in progress.
u32AHI_SpiReadTransfer32()
Called after the SPI transfer is completed, this function reads the received data (SPIMISO) at the time of transfer. The read data is right justified to the LSB side.
最終更新