I'm trying to understand how to work with Adafruit's Bluefruit SPI module so I can write a small driver for my next microcontroller project. I am having a hard time following their
SDEP protocol and using it to write some small program tests in Arduino (shown below). In this code, I'm attempting to send an AT command. According to Adafruit's protocol, after sending a message to the BLE, the Bluefruit would prepare a response back. The IRQ line is set high to indicate that a response is ready to be received. However, I can't seem to be able to properly read back the reponse packet and bring the IRQ line back low.
If anyone has some insight, that be great and very helpful! Thanks in advance.
#include <SPI.h>
#define BT_CS 8
#define BT_IRQ 7
#define BT_RST 4
unsigned char fifoBuffer[16];
unsigned char locFifoBuffer;
unsigned char sizeFifoBuffer;
unsigned char Flag;
void setup() {
// put your setup code here, to run once:
pinMode(BT_CS, OUTPUT);
pinMode(BT_IRQ, INPUT);
pinMode(BT_RST, OUTPUT);
// Setting SPI clock to 2 MHZ
SPI.setClockDivider(SPI_CLOCK_DIV8);
SPI.begin();
SPI.setBitOrder(MSBFIRST);
Serial.begin(115200);
locFifoBuffer = 0;
sizeFifoBuffer = 0;
// Resetting BLE Module
digitalWrite(BT_CS, HIGH);
digitalWrite(BT_RST, LOW);
delay(100);
digitalWrite(BT_RST, HIGH);
Serial.println("BLE Resetted");
}
void loop() {
Serial.print("AT Command> ");
while(!Serial.available()); //Wait for a command Input
while(!Flag)
{
if(Serial.available())
{
fifoBuffer[locFifoBuffer] = Serial.read();
if(fifoBuffer[locFifoBuffer] == '\n')
{
Flag = 1;
}
locFifoBuffer++;
}
}
fifoBuffer[locFifoBuffer] = '\0';
sizeFifoBuffer = locFifoBuffer;
locFifoBuffer = 0;
Flag = 0;
SerialWriteString((const char *)fifoBuffer); // Echo back the command
Serial.print("\r");
sendATCommand((const char *) fifoBuffer, sizeFifoBuffer); // Write the command to BLE
// If IRQ line is high, BLE has a packet for us...
if(BT_IRQ)
{
SPI.beginTransaction(SPISettings(2000000, MSBFIRST, SPI_MODE0));
digitalWrite(BT_CS, LOW); // Chip Select
delayMicroseconds(100);
while(BT_IRQ)
{
SPI.transfer(0xFF);
}
digitalWrite(BT_CS, HIGH); // Chip Select
delayMicroseconds(100);
SPI.endTransaction();
}
}
void SerialWriteString(const char *s)
{
while(*s)
{
Serial.write(*s++);
}
}
void putString(const char *s)
{
while (*s)
{
putChar(*s++);
}
}
void putChar(unsigned char ch)
{
SPI.transfer(ch);
}
void sendATCommand(const char *cmd, unsigned char len)
{
SPI.beginTransaction(SPISettings(2000000, MSBFIRST, SPI_MODE0));
digitalWrite(BT_CS, LOW); // Chip Select
delayMicroseconds(100);
SPI.transfer(0x10); // Message Type: Command
SPI.transfer(0x00); // AT Wrapper Command ID
SPI.transfer(0x0A);
SPI.transfer(len);
while(*cmd)
{
putChar(*cmd++);
}
digitalWrite(BT_CS, HIGH);
delayMicroseconds(100);
SPI.endTransaction();
}