PIC32 SPI slave receive buffer is empty despite good data on the bus - Page 1
Forum section: electronics / projects

author: ricko_uk
author_rank: Super Contributor
timestamp: July 18, 2021, 10:56:30 pm
content: Hi,I am using a PIC32MZ (100 pins package - info relevant for the configuration) and configured the SPI3 with Harmony configurator as shown in the attached screenshot.But what happens is that the callback function is called every time I send data from the master (say every 7 seconds) but the receive buffer is empty (both the PLIB buffer and consequently my buffer I fill using the PLIB function to do so).Data is correctly sent on the bus because I see it on the logic analyser as shown in the second and third screenshots.What could cause that?Below is the code I reduced to a minimum just to test it.Code:void SPIEventHandler(uintptr_t context ){//size_t bytesReadFromBuffer = 0;if (SPI3_ErrorGet() == SPI_SLAVE_ERROR_NONE){_nop();//bytesReadFromBuffer =SPI3_Read(APP_RxData, SPI3_ReadCountGet());switch(APP_RxData){case 'A':{Wired_TEST_LED_Set();break;}case 'B':{Wired_TEST_LED_Clear();break;}}_nop();}}Thank you as always

author: AaronLee
author_rank: Regular Contributor
timestamp: July 19, 2021, 02:38:49 am
content: You're trying to read data back on the MISO line? I don't see any MISO line in your config screen capture.I really despise Harmony and never use it except a few cases where it's absolutely mandatory. It just seems to be riddled with bugs, and when they fix one bug, they create two more.Anyways, I've always found it extremely useful to just bitbang SPI or I2C communications first, to verify everything's working correctly in the hardware, and with the correct registers and handling of data. Then once it's perfect using bitbang ports, switch over to hardware. There's just way too many variables, with inconsistent Microchip hardware and software libraries to jump directly in with Harmony to begin with. I'm sure others will have differing opinions, but in my experience, even before Harmony, bitbanging first always saves lots of time and headaches.

author: ricko_uk
author_rank: Super Contributor
timestamp: July 19, 2021, 08:28:20 am
content: Thank you Aaron,What do you mean exactly about the MISO? Do you mean a loopback? If so, I am not trying a loopback but comms from another micro.The micro I am coding on is a slave and at the moment I am using only the MOSI line. As mentioned the hardware is ok because I see the data on the logic analyser as per screenshot and all the way to the micro's pin.Thank you

author: dmills
author_rank: Super Contributor
timestamp: July 19, 2021, 08:47:46 am
content: Check the processor errata, my recollection is that there is something a little squirrely in some of the PIC SPI interrupt timing (It goes one SPI clock too early or something like that).Not sure exactly which chip that applied to, but I know it has bitten me.

author: mikeselectricstuff
author_rank: Super Contributor
timestamp: July 19, 2021, 09:31:01 am
content: That NOP makes me suspicious there is a workaround for a device errata - could be that this was done for the MX, and maybe not enough delay for the faster MZ?Also, it is generally a bad idea to use manufacturer libraries for simple stuff like SPI.

author: ricko_uk
author_rank: Super Contributor
timestamp: July 19, 2021, 11:32:57 am
content: Thank you Mike and Dmills,@Mike,that NOP() is just so I can have a breakpoint (still there from when I had much more code earlier).@Dmills,I checked the errata but the only issue with the SPI for that micro is for the transmit and also does not apply.Any other suggestions?Thank you

author: AaronLee
author_rank: Regular Contributor
timestamp: July 19, 2021, 12:51:19 pm
content: Ok, Ricko, I think I understand now what you're trying to do. The PIC32MZ is just a slave, and some different MCU is the master. But you're not getting data on the PIC32MZ. I initially wrongly assumed you were sending data out the PIC32MZ and not receiving any return data. For your use case, I really don't have any experience, as I've always used PICs as master devices.Likewise, most people use PIC MCUs as master devices, and those using them as slave devices are probably just a small fraction compared to master devices. So while there might not be an errata for your situation, it still could be that there's an issue that just hasn't been made public yet. When the PIC32MZ first came out, I recall it taking quite a while for some of the hardware bugs to be publicized.Are you initializing the SPI port with Harmony, but then using PLIB functions? I've never tried that, but imagine it could be a problem. I have some projects using the PIC32MZ with graphics, and for that there's no choice but to use Harmony, being there's no documentation about how the proprietary graphics work, or their register maps, etc., so I use Harmony for everything. For the other projects I have using the PIC32MZ, which don't use graphics, I don't use Harmony at all. I only use at most PLIB functions, but preferably not even that, but just direct writing to the hardware registers myself.

author: ricko_uk
author_rank: Super Contributor
timestamp: July 19, 2021, 02:20:03 pm
content: Thank you Aaron,yes I configure/initialize it with MHC (Harmony configurator) then use the PLIB because although there is a driver for SPI master there isn't one for the SPI slave, I assume that's because the PLIB already has all the functionality needed for the slave configuration.Thank you

author: AaronLee
author_rank: Regular Contributor
timestamp: July 20, 2021, 01:40:54 am
content: Quote from: ricko_uk on July 19, 2021, 02:20:03 pmIf you still can't get it to work, I'd recommend looking on the Microchip forum, or elsewhere, to see if you can find an example for any PIC MCU for SPI slave using PLIB or direct to the hardware, and try to adapt that to your particular PIC32MZ. I've done that many times when I couldn't get something to work, and sometimes it helps, but sometimes there's some particular quirk in the PIC MCU I'm using that still prevents it from working.