Author Topic: [help] Adafruit's Bluefruit SPI  (Read 1203 times)

0 Members and 1 Guest are viewing this topic.

Offline WillHuangTopic starter

  • Contributor
  • Posts: 47
[help] Adafruit's Bluefruit SPI
« on: December 19, 2016, 04:13:05 am »
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.

Code: [Select]
#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();
}

 

Offline LeonV

  • Contributor
  • Posts: 39
  • Country: nz
Re: [help] Adafruit's Bluefruit SPI
« Reply #1 on: December 21, 2016, 03:10:23 am »
Usually when i get that behaviour, the MISO pin on the master is setup incorrectly or some other preferential is using it, or the mode is wrong and the data read / clock timing is wrong.
Damn forum is making me procrastinate from work!
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf