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.
u8SalveEnable
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.
bLsbFirst
If TRUE
, the data starts from LSB (lower bit).
bPolarity
If TRUE
, reverse the clock.
bPhase
If TRUE
, use the falling edge.
u8ClockDiviser
Specifies the clock frequency, 0...63, where 0 means 16MHz drive, and the rest are divided by 2*u8ClockDiviser
.
bInterruptEnable
Generates an interrupt at the end of SPI transfer. (It is not used in the samples.)
bAutoSlaveSelect
If TRUE
, control of the select pin is set to automatic control only during SPI transfer. If FALSE
, it is set at the timing of calling vAHI_SpiSelect()
.
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
bPolarity
bPhase
0
FALSE
FALSE
1
FALSE
TRUE
2
TRUE
FALSE
3
TRUE
TRUE
vAHI_SpiSelect()
Activate select pins.
u8SalveMask
1
: SPISEL0, 2
:SPISEL1, 4
:SPISEL3, 0
: cancel select.
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.
u8CharLen
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.
u32Out
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.
最終更新