I have a module that requires SPI communication and I'm trying to get it to communicate with the Arduino. Based on the specs provided below can someone verify I have the code setup properly? Once I get confirmation, I can then ask a question regarding the commands.
Specs on SPI device and Mega as Master:
First bit of the packet is always set to 0
Bitorder: MSB
Data Mode: Mode 11 (1,0/Mode 2)
Device Max Clock Freq: 2mhz (recommended 1mhz)
Mega Clock Speed 16 MHz so need /16 divider
SPI: 50 (MISO), 51 (MOSI), 52 (SCK), 53 (SS/CS)
Running Arduino 1.0 with build in SPI support (spi.h)
So next part. The module has a dataReadyPin which goes high when I need to read it. I have that setup as a INPUT pin 31.
So in the loop I have the following:
// don't do anything until the data ready pin is high:
if (digitalRead(dataReadyPin) == HIGH) {
// need to read register 00.
}
So when the dataReadyPin goes high, I need to issue a read command. This is where I'm not sure what to do. I've been reading up on SPI and looking at examples however I'm not sure I really understand.
Here is where I'm stuck.
I need read Register 00 when the dataReadyPin goes high.
A read command format is:
7 bit 8 bit 16 bit
---------------------------------------------------------------
| 0 | CMD | Reg Addr | Reg Data |
---------------------------------------------------------------
/ \
/ \
/ \
3 bit 4 bit
----------------------
| chip addr | 1 |
----------------------
So I'm guessing I need to send:
digitalWrite(SS, LOW);
int tempData = SPI.transfer(0b01000000);
digitalWrite(SS, HIGH);
Serial.println(tempData);
But that isn't doing anything -- does it look right?
digitalWrite(SS, LOW);
int tempData = SPI.transfer(0b01000000);
digitalWrite(SS, HIGH);
Serial.println(tempData);
You appear to have 32 bits, right? 4 bytes. So you need 4 x SPI.transfer. Something like:
digitalWrite(SS, LOW);
SPI.transfer(1); // read
SPI.transfer(0x22); // register 22
byte a = SPI.transfer(0); // get 8 bits
byte b = SPI.transfer(0); // get other 8 bits
digitalWrite(SS, HIGH);
Serial.println(tempData);
I would read that as the read/write bit being the low-order one, but reading the datasheet might help clear that up. If I knew what the link to the datasheet was. Hint.
I believe you "poll" after you send -- look at the timing in the previous post. You send a read, then you have to poll with 0xFF until you get back a correct message? This is all I have -- copied and pasted everything... not sure how to proceed.
Excerpt:
SPI Specifics
The NOP character is defined as 0xFF. While sending a command to the system the controller
receives the data that is set to one. Until valid data is available after a read register command was issued,
the system returns only data set to one. To get data from the system the controller polls
after sending a read register command until the controller receives valid data (not all data are set to one).
While polling, the controller must send only data set to one. In the beginning, read answer should be
received after the master has clocked for 16 bytes.
The system is SPI slave and the Host MCU is SPI master. The Host MCU sends a
command to the system at any time. If something changes in the system, then the Host MCU
should be notified. When Host MCU is notified; there is a hardware signal called SPI_REQ, which enables
the system to bring high to indicate that the Host MCU should read the system registers. The complete SPI
interface consists of:
• From Host MCU to system
Chip select
Receive data
Clock
• From system to Host MCU
Transmit data
SPI_REQ
Read Register Answer Polling
After the Host MCU sends the read register command, the controller starts polling the system to get the
read register answer by sending NOPs (OxFF). The read register answer is valid; when the first byte of the
controller receives information is not a NOP. The examples below (see file attachment) illustrate the read register sequence in the
case that the read register answer is valid and in the case the read register answer is not ready.
Start-up Sequence
Start-up sequence is important to establish between the Host MCU and the system. Start-up
sequence is as follows:
• Power up system
• SPI_REQ signal stays low
• Host MCU watches SPI_REQ signal
• If SPI_REQ signal is low, then Host MCU keeps watching. Do not start SPI communications
• When SPI_REQ goes high, then Host MCU issues read commands
• Host MCU must empty the message buffer by reading register 00 until SPI_REQ goes low
• Host MCU must run two operations in parallel
If SPI_REQ goes high, then read the message buffer by reading register 00 until SPI_REQ goes low
Read Reg_17 to check system status. If Reg_17 is not equal to 0x21 then Host MCU runs two
operations in parallel. The Host MCU must not send a write command.
If Reg_17 is "ready", 0x21, then Host MCU can start normal operation by sending write commands
• Normal operational loop, watch SPI_REQ signal, read Reg_00 when SPI_REQ goes high. Read or
write individual registers as needed.