8 years ago.

KL25Z - Does anyone have a working example of an interrupt driven SPI Slave?

I cannot get the KL25Z to recognize SPI Master messages.

In the following code example I am trying to get the SPI1 module, configured as a Slave, to receive a byte from the SPI Master, and illuminate the tri-color LEDs based on the value of the data sent.

/media/uploads/PowerIC/spi_slave.c

If I strip out the device.Receive() command, I can see that the interrupt service is taking place, so I can only conclude that the SPI message from the Master is never being recognized.

Does anyone have any ideas for figuring this one out?

1 Answer

6 years, 3 months ago.

I tried your code on a Nucleo 152RE which is clocked slower (32Mhz) than the KL25z (48Mhz), you are using and it also did not work. Looking at he code would lead one to expect that it should work....So what is the problem?

Two cups of coffee and a cigar in the garage gave me an idea. Perhaps it is a timing issue with NOT enough elapsed time from SS or chip select being asserted by the master and detected, by the SPI slave board , and an interrupt being generated and data being received. There is some amount of code to be processed after an interrupt before the called isr can run , and how long before the called functions finish and fetch the receive data .

So I tried adding a small delay, wait(0.0001); in the SPI master code between cs =0; and the spi.write statement and the code now works.

Oddly enough the SPI Master is running on a KL25Z

I needed a interrupt driven SPI Slave running in a project using the .Nucleo 152RE and while searching for a solution I discovered your posting from 21 months ago.. What was your solution to the problem you were experiencing?

David G.

David your problem is the two cups of coffee, replace those with two beers and the problem will be solved. If not at least the two beers are better than coffee :) Other wise find the nearest bin and place the Nucleo board there, I can't get anything to run on these properly.

But seriously have you tried using the same platforms to test assuming you are using separate devices ? Also I haven't looked at the code, but is there much code in the ISR you are using to receive the data. I had similar issues with UART serial between two devices and found I had to remove almost all code from the ISR call back, simply receive the data in a container, set a flag there then process the received data elsewhere in my program. Or even try slowing the SPI clock.

posted by Paul Staron 06 Jul 2015

I ran out and got some beer to try your suggestion (but I drank three, thinking if two is good , than three is better!, ....It didn't look like there was a lot of code in the isr just three lines, but the, if(device.receive()) statement seemed to be the time killer. I changed the isr code to just setting a flag , removed the delay, I had inserted, on the master side and all is well!

(at least I knew what was wrong, but I was not experienced enough to solve the problem in your elegant manner!)

Best regards and thanks

David G.

posted by David Garrsion 06 Jul 2015