Arm Mbed OS support forum

Mbed SPI.transfer

I am trying to use spi.transfer function on NUCLEO-F44ZE to read daisy chained ads8864 data.
It seems OK for 1 or 2 bytes read. It reports an error for 3 bytes transfer.
4 bytes can be transferred but with no event generated.
After the error, only one transaction possible.

The test code is following.
Is there anything wrong?


#include “mbed.h”

#define RX_LEN 16

//SPI spi(SPI1_MOSI, SPI1_MISO, SPI1_SCLK);
SPI spi(PA_7,PA_6,PA_5);
//
// ST-LINK USB 9600, 14400, 19200, 38400, 57600, 115200
Serial pc(SERIAL_TX, SERIAL_RX, 115200);

Timer t;
int time0, time1, time2;
uint8_t rx_buf[RX_LEN]={};

bool flag = false;

void eventCb(int event);
event_callback_t event = eventCb;

// Notify when spi trasfer is completed
void eventCb(int event)
{
if (event == SPI_EVENT_COMPLETE)
{
flag=true;
printf("\r\nTransfer completed 0x%x\r\n",flag);
}
else
{
printf(“SPI event status: 0x%x\r\n”, event);
if(event & SPI_EVENT_ERROR) printf(“SPI_EVENT_ERROR \r\n”);
if(event & SPI_EVENT_RX_OVERFLOW) printf(“SPI_EVENT_RX_OVERFLOW \r\n”);
}
}

int main()
{
int t1, t2, t3,i, j;
t.start();

spi.format(8, 1);
spi.frequency(20000000);

pc.printf("\r\n\n****** Start length=%d\r\n",RX_LEN);

//
for(i=1;i<8;i++)
{
for(j=0;j<RX_LEN;j++) rx_buf[j]=0;
printf("\r\n* n= %d \r\n",i);
t1=t.read_us();
flag=false;
spi.transfer((uint8_t )NULL, 0, (uint8_t )rx_buf, i, event, SPI_EVENT_ALL);
t2=t.read_us();
printf(“flag= 0x%x\t after spi_read, dt=%d us\r\n”, flag, t2-t1);
wait_us(10); // Wait for a while; interrupt event may happen
t3=t.read_us();
printf(“flag= 0x%x\t after spi_read, dt=%d us\r\n”, flag, t3-t1);
wait_us(500
1000); // Enough long wait
for(j=0;j<RX_LEN;j++) printf(" %2x",rx_buf[j]);
printf("\r\n");
}
//
printf("\r\n
***** END\r\n");
return 0;
}