-
Notifications
You must be signed in to change notification settings - Fork 0
Using SPI
SPI MASTER <port>, <mode>, LSB|MSB <speed in MHz> [, <wordsize>]
SPI TRANSFER <pin> <array variable>
SPI (Serial Peripheral Interface - wikipedia entry) is a de-facto standard for connecting the CC254X to peripheral devices. BlueBasic supports the use of a single SPI interface in Master mode (no Slave support at this time), chosen from the 4 available on the device. Additionally, a pin is used to select the device for communication. The pin must be set as an output and value set to 1.
The SPI is configured as follows:
// Set P0(0) as output to select the SPI device
PINMODE P0(0) OUTPUT
// Deselect it first
P0(0) = 1
// Set the SPI mode
SPI MASTER 0, 0, MSB 1
This configures the SPI interface to use port 0, SPI mode 0, most significant bit first, and running at 1Mhz. Other options are:
port | USART |
---|---|
0 | USART 0 |
1 | USART 0, alternate |
2 | USART 1 |
3 | USART 1, alternate |
See pinout reference for more information. (naming convention)
mode | CPOL | CPHA |
---|---|---|
0 | 0 | 0 |
1 | 0 | 1 |
2 | 1 | 0 |
3 | 1 | 1 |
Endian | |
---|---|
LSB | bit 0 read/written first |
MSB | bit 7 read/written first |
Speed | Bus speed |
---|---|
1 | 1 MHz |
2 | 2 MHz |
4 | 4 Mhz |
Size | |
---|---|
None | Chip select not toggled |
8 | Chip select toggled after each byte transferred |
16 | Chip select toggled every two bytes transferred |
32 | Chip select toggled every four bytes transferred |
Once the SPI interface is initiated, data can be transferred to and from device as follows:
SPI TRANSFER P0(0) D
Before the transfer begins, pin P0(0) is set to 0 to select the device connected to this pin. The data in array "D" is then transferred to the selected device, and the contents of the array replaced by the data read from the device during the transfer. Once the transfer is completed, pin P0(0) is set to 1. Specifying the pin to use to select the device allows the SPI interface to be used with many different devices, each selected by different pins.
5 //
6 // "Read the vendor info from the MB85RS64V FRAM memory chip"
7 //
10 DIM D(5)
20 D(0) = 0X9F
30 PINMODE P0(1) OUTPUT
40 P0(1) = 1
50 SPI MASTER 3, 0, MSB 1
60 SPI TRANSFER P0(1) D
70 PRINT D(0)," ",D(1)," ",D(2)," ",D(3)," ",D(4)