SAMD21 SPI

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am trying to set up SPI data transfer between 2 SAMD21s on different boards. One acts as master and other as slave.

Created both projects using Atmel Start ( ASF4) and here is the code snippet.

Both are fed with generic clock of 24 MHz and selected a baudrate of 80000 in Atmel start.

 

Master

-------

Config

    spi_m_sync_get_io_descriptor(&SPI_2, &io);
    spi_m_sync_enable(&SPI_2);
    spi_m_sync_set_mode(&SPI_2, SPI_MODE_0);

Send Data in Loop

static uint8_t buffer_tx_byte[1] = {0xAA};
static uint8_t buffer_rx_byte[1] = {0x00};

void sendReceive() {
    //Enable SS
    int32_t status = io_write(io, buffer_tx_byte, 1);
    delay_ms(20);
    memset(buffer_rx_byte,0x00,1);
    io_read(io, buffer_rx_byte, 1);
    //Disable CS pin
}

 

Slave

=====

Config

    spi_s_sync_get_io_descriptor(&SPI_0, &io);
    spi_s_sync_enable(&SPI_0);
    spi_s_sync_set_mode(&SPI_0, SPI_MODE_0);

 

Data send/rcv loop

static uint8_t buf_tx_byte[1] = {0xAA};
static uint8_t buf_data_byte[1];

void spiReadWrite() {
    memset(buf_data_byte,0x00,1);
    int32_t status = io_read(io, buf_data_byte, 1);
    delay_ms(10);
    io_write(io, buf_tx_byte , 1);
}

 

Both at master and slave IO_READ returns status 1 , indicating 1 byte read successfully.

But when i print them , Slave always prints FF ( probably empty) , Master prints 7F.

 

Could any one help with this ?

 

Last Edited: Thu. Nov 21, 2019 - 05:58 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Get yourself a logic analyser. Can be got for < $10usd of aliexpress and elsewhere.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks. I managed to get one and here is how the signals look.

 

I am sending a byte (0xCD) from master. MOSI shows it correctly.

 

Current baud rate is 9600. I also tried 20000/80000 and different clock frequencies from 1MHz-24MHz.

Both master and slave are configured to SPI Mode 0 ( CPOL - 0, CPHA - 0 )

 

Slave receives the data and sends it out. It is on MISO and is wrong most of the time. (It works occasionally and reads CD though).

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Not sure what you expect but if you need the more typical SPI bidirectional transfer then calling the spi_m_sync_transfer function directly will work better than io_write/io_read.

/Lars

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Do you see anything wrong with the way i read or slave configuration. The transmitted "0xCD" seems to be read as "9B" , "0x DD" etc.

Last Edited: Sat. Nov 23, 2019 - 06:36 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If I have to guess then the slave has a different SPI mode, i.e., in start under the SPI advanced config "Clock Polarity" and/or "Clock Phase" is different.

/Lars

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks. I did figure it out. Advanced Configuration for Slave had clock phase 1 selected by default. I thought setting SPI mode to 0 in code will change it. But it wasn't the case.