Author Topic: K64 SPI Slave example  (Read 4652 times)

Offline kingston

  • Newbie
  • *
  • Posts: 26
    • View Profile
K64 SPI Slave example
« on: July 03, 2018, 01:26:41 PM »
Hi all,

has anyone already used the K64 as a SPI slave?
I'm trying to talk to an ESP8266 as a SPI-wifi bridge but can't get the SPI on the K64 to work in slave mode.

So far I have set the module to slave mode like this:
SPI0_MCR = (SPI_MCR_SLAVE | SPI_MCR_DCONF_SPI | SPI_MCR_CLR_RXF | SPI_MCR_CLR_TXF);

and have configured the CS line as an input with a ISR when the edge is falling.

In my ISR I check for SPI0_SR & SPI_SR_RFDF to see if there is data available but there never is.

Also when I put data into SPI0_PUSHR_SLAVE I never see a change in the SDO line on my logic analyzer.

I know there are some CMSIS examples for a DSPI Slave on K64 and it is also mentioned in the reference manual but I couldn't get it to work.

Any help would be appreciated.

Thanks

Paul

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3234
    • View Profile
    • uTasker
Re: K64 SPI Slave example
« Reply #1 on: July 03, 2018, 11:15:32 PM »
Paul

Remember that the CS line of the SPI slave is used to enable data reception and therefore ensure that you configure it as CS function and not a GPIO.
Also it is best to use the reception interrupt since it will fire when there is data received (the CS interrupt is too early since it hasn't yet clocked in any data).
Could you tell me the pin-out that you are using for the SPI interface and I may be able to show you complete code for it.

Regards

Mark


Offline kingston

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: K64 SPI Slave example
« Reply #2 on: July 04, 2018, 08:46:02 AM »
Hi Mark,

thank you for the quick response!

I'm using SPI0 on a K64 with CS4 as chip select:

PTC0 SPI0_CS4
PTD1 SPI0_SCK
PTD2 SPI0_MOSI (SOUT)
PTD3 SPI0_MISO (SIN)

Right now I have configured the RX interrupt but it never occurs.

I have only little time today so I will try again tomorrow.

Regards

Paul


Here is my code for the initialisation:

Code: [Select]
              _CONFIG_PERIPHERAL(C, 0, (PC_0_SPI0_PCS4));
_CONFIG_PERIPHERAL(D, 1, (PD_1_SPI0_SCK | PORT_SRE_FAST | PORT_DSE_HIGH));
_CONFIG_PERIPHERAL(D, 2, (PD_2_SPI0_SOUT));
_CONFIG_PERIPHERAL(D, 3, (PD_3_SPI0_SIN));
SPI0_MCR = SPI_MCR_HALT;
SPI0_CTAR0 = (SPI_CTAR_FMSZ_8 | SPI_CTAR_CPHA | SPI_CTAR_CPOL);
SPI0_RSER = (SPI_SRER_RFDF_RE);
SPI0_MCR = (SPI_MCR_SLAVE | SPI_MCR_DCONF_SPI | SPI_MCR_CLR_RXF | SPI_MCR_CLR_TXF);
CLEAR_RECEPTION_FLAG();
fnEnterInterrupt(irq_SPI0_ID, PRIORITY_TIMERS, fnHandleSPI);

Offline kingston

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: K64 SPI Slave example
« Reply #3 on: July 05, 2018, 10:32:44 AM »
Hi,

my colleague found the solution:

On page 1446 in the K64 reference manual there is a table which describes that only CS0 can be used in slave mode...
Would be nice if they thought about mentioning this somewhere more prominent.

Anyway with CS0 I get all the RX interrutps that didn't work before.

Thanks for your help!

Regards

Paul

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3234
    • View Profile
    • uTasker
Re: K64 SPI Slave example
« Reply #4 on: July 05, 2018, 02:17:15 PM »
Hi Paul

I am glad that you discovered that detail - I was going to start some testing with CS4 as you indicated and would have also had the same difficulty initially.
Now that we know that only CS0 can be used in slave mode I will make this as evident in the code as possible to avoid such attempts to use other ones.

Regards

Mark