Last active
September 8, 2019 20:35
-
-
Save tsiura/590af22f027f06e9397cdf935c350f0d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SPI.h> | |
#define PIN_MOSI 11 | |
#define PIN_MISO 12 | |
#define PIN_SCK 13 | |
#define PIN_SS_MRDY 10 | |
#define PIN_SRDY 8 | |
#define PIN_RES 7 | |
//------------------------------------------ | |
uint16_t cmd_conv(uint8_t cmd0, uint8_t cmd1) | |
{ | |
return cmd0 << 8 | cmd1; | |
} | |
void setup() | |
{ | |
pinMode(PIN_SRDY, INPUT); | |
pinMode(PIN_RES, OUTPUT); | |
pinMode(PIN_SS_MRDY, OUTPUT); | |
digitalWrite(PIN_RES, LOW); | |
SPI.setBitOrder(MSBFIRST); | |
SPI.setDataMode(SPI_MODE0); | |
SPI.setClockDivider(SPI_CLOCK_DIV8); | |
Serial.begin(9600); | |
delay(100); | |
digitalWrite(PIN_RES, HIGH); | |
digitalWrite(PIN_SS_MRDY, HIGH); | |
Serial.print("SRDY: "); | |
Serial.println(digitalRead(PIN_SRDY)); | |
} | |
void loop() | |
{ | |
if (digitalRead(PIN_SRDY) == LOW) { | |
Serial.println("POLL"); | |
// POLL | |
digitalWrite(PIN_SS_MRDY, LOW); | |
SPI.begin(); | |
SPI.transfer(0x00); | |
SPI.transfer(0x00); | |
SPI.transfer(0x00); | |
while (digitalRead(PIN_SRDY) == LOW); | |
Serial.println("AREQ"); | |
Serial.print("#1 SRDY: ");Serial.println(digitalRead(PIN_SRDY)); | |
uint8_t length = SPI.transfer(0x00); | |
uint8_t cmd0 = SPI.transfer(0x00); | |
uint8_t cmd1 = SPI.transfer(0x00); | |
Serial.print("#2 SRDY: ");Serial.println(digitalRead(PIN_SRDY)); | |
Serial.print("Data length: "); | |
Serial.println(length); | |
Serial.print("CMD: 0x"); | |
Serial.println(cmd_conv(cmd0, cmd1), HEX); | |
Serial.print("#3 SRDY: ");Serial.println(digitalRead(PIN_SRDY)); | |
Serial.print("Data: "); | |
while (length > 0) { | |
uint8_t data = SPI.transfer(0x00); | |
Serial.print(data, HEX); | |
Serial.print(" "); | |
length--; | |
Serial.print("(SRDY: ");Serial.print(digitalRead(PIN_SRDY));Serial.print(") "); | |
} | |
Serial.println("");Serial.print("#4 SRDY: ");Serial.println(digitalRead(PIN_SRDY)); | |
Serial.println(""); | |
SPI.end(); | |
Serial.print("#5 SRDY: ");Serial.println(digitalRead(PIN_SRDY)); | |
} | |
delay(10); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
SRDY: 1
POLL
AREQ
#1 SRDY: 1
#2 SRDY: 1
Data length: 6
CMD: 0x4180
#3 SRDY: 1
Data: 1 (SRDY: 1) 2 (SRDY: 1) 0 (SRDY: 1) 2 (SRDY: 1) 6 (SRDY: 0) 0 (SRDY: 0)
#4 SRDY: 0
#5 SRDY: 0
POLL
AREQ
#1 SRDY: 1
#2 SRDY: 0
Data length: 0
CMD: 0x0
#3 SRDY: 0
Data:
#4 SRDY: 0
#5 SRDY: 0
POLL
AREQ
#1 SRDY: 1
#2 SRDY: 0
Data length: 0
CMD: 0x0
#3 SRDY: 0
Data:
#4 SRDY: 0
#5 SRDY: 0
POLL
AREQ
#1 SRDY: 1
#2 SRDY: 0
Data length: 0
CMD: 0x0
#3 SRDY: 0
Data:
#4 SRDY: 0
#5 SRDY: 0
POLL
AREQ
#1 SRDY: 1
#2 SRDY: 0
Data length: 0
CMD: 0x0
#3 SRDY: 0
Data:
#4 SRDY: 0
#5 SRDY: 0