10 years, 3 months ago.

SPI Slave problem 8 bit limit

I have question about the spi slave mode for lpc1768, my spi master is arduino , it sends 32bit for each time, for example:

SPI master: Arduino ino

#include <SPI.h>
void setup()
{
  pinMode(CS,OUTPUT);
  SPI.begin();// Initiate an SPI communication instance
  SPI.setDataMode(SPI_MODE0); //configure the spi connection 
  SPI.setClockDivider(SPI_CLOCK_DIV2); // 16/2=8Mhz
}
void loop()
{
   digitalWrite(CS, LOW);
   SPI.transfer(0x04);
   SPI.transfer(0x05);
   SPI.transfer(0x06);
   SPI.transfer(0x07);
   digitalWrite(CS, HIGH);
}

SPI Slave(lpc1768):

#include "mbed.h"
SPISlave device(p5, p6, p7, p8); // mosi, miso, sclk, ssel
InterruptIn event0(p15);  // connect p8 to p15, using ssel as interrupt
Serial pc(USBTX,USBRX);
int data1,data2,data3,data4;
int count = 0;

void Exirq()
{
    count = 1;
    }

int main() {
    device.format(16,0);
    device.frequency(8000000); //8MHz
    pc.baud(9600);
    //device.reply(0x00);              // Prime SPI with first reply
    event0.fall(&Exirq); 
    while(1) {
                if(count == 1)
                {
                    __disable_irq();    // Disable Interrupts
                    // do something that can't be interrupted
                    data1 = device.read();   //read the data
                    data2 = device.read();
                    //pc.printf("%x\n\r", data);
                    __enable_irq();
                    }
                count = 0;
               if(count == 0)
               pc.printf("%x %x\n\r", data1,data2);
                }
    }

device.read() can only read the first 8 bits if I set device.format(8,0) as 0x04 , can only read the first 16 bits if I set device.format(16,0) as 0x405, how can I read 32 bit for each chip select from the master?

if spi master sends 16 bits for each chip select as

  digitalWrite(CS, LOW);
   SPI.transfer(0x04);
   SPI.transfer(0x05);
  digitalWrite(CS, HIGH);
  
  digitalWrite(CS, LOW);
   SPI.transfer(0x06);
   SPI.transfer(0x07);
   digitalWrite(CS, HIGH);

I can get 0x405, 0x607, but I don't know which received data is the first one since i want use the master reads a sensor and transmits the data to lpc1768

1 Answer

10 years, 3 months ago.

Every microcontroller I have looked at it has that behaviour: CS must toggle between every received word. Since the LPC1768 can do up to 16-bit, you can receive two 8-byte words with one read transaction, but not more than that.

So what you could do is make a software implementation of SPI slave (using interruptIn, should be fairly well do-able), use something else than SPI, or add something else to make sure you know which data is the first one. Also you could let the LPC1768 directly read those sensors.

Accepted Answer