PIC32MZ Spi fun

General discussion on mikroC PRO for PIC32.
Post Reply
Author
Message
Dakta
Posts: 53
Joined: 07 Sep 2013 16:08

PIC32MZ Spi fun

#1 Post by Dakta » 23 Jun 2020 00:07

I bought my easypic fusion board a few weeks ago, and apart from some initial issues I'm growing to really like it, I've explored the general set up of the chip, UART reception, recieiving, interrupts, buttons input, piezo speaker output, on the whole everythings going well and it has been fun. Eventually want to work up to CANBUS but whilst I wait for some additional bits to enable me to do that, I thought i'd take a look at a new challenge - the SPI interface, and just as importantly, the onboard M25P80 SPI flash chip. (You never know when you might want to save something).

So I did some reading and made a few projects but for the last few days I've got a bit stuck, and I'm running out of ideas to try. I can initialise the SPI module, I can write to the flash chip (0x9F for ID) however I only get 3 bytes in return which isn't what I should get according to the flash memory datasheet, additionally I get the same 3 bytes if I repeat the read without writing anything else, so if I write 0x9F i will get 0x1C 0x30 0x14 in return even if i read multiple times without writing a second time.

Here's my init code (using spi module 6):

Code: Select all

TRISD.b14 = 0;  //slave select output
TRISF.B8 = 0; //mosi output
TRISF.b2 = 1; //miso input
TRISD.B15 = 0; //clock output
PPS config:

Code: Select all

Unlock_IOLOCK();
PPS_Mapping(_RPF8, _Output, _SDO6);
PPS_Mapping(_RPF2, _Input, _SDI6);
Lock_IOLOCK();
SPI6_Init_Advanced(_SPI_MASTER, _SPI_8_BIT, 64, _SPI_SS_DISABLE, _SPI_DATA_SAMPLE_END, _SPI_CLK_IDLE_HIGH, _SPI_active_2_IDLE);

and here is the code to test the read and write:

Code: Select all

LATD.B14 = 0; //set the CS pin low
SPI6_Write(0x9F); //write command for chip ID
//we are expecting 20 bytes back
for (i = 0; i< 20; i++){
RX_Buff[i] = SPI6_Read(0x00);
}
LATD.B14 = 1; //set the CS pin high

When that executes, the array RX_Buff is full of {0x1C, 0x30, 0x14, 0x1C, 0x30, 0x14,0x1C, 0x30, 0x14...}

I understand I am likely missing something really basic here, unfortunately I don't have a logic analyser so goosed as to what :( any ideas?

hexreader
Posts: 1786
Joined: 27 Jun 2010 12:07
Location: England

Re: PIC32MZ Spi fun

#2 Post by hexreader » 24 Jun 2020 00:29

The attached project works OK for me..
Attachments
forum.zip
(358.72 KiB) Downloaded 70 times
Start every day with a smile...... (get it over with) :)

Dakta
Posts: 53
Joined: 07 Sep 2013 16:08

Re: PIC32MZ Spi fun

#3 Post by Dakta » 24 Jun 2020 09:47

Thanks - this one is strange, I've just gone through yours and mine line by line and apart from my code being a bit untidy I can't see any functional difference.

Pin directions are set, ADC is disabled, the spi config is identical, the read routine to the receive buffer pretty much the same.

Yet again I'm just getting a loop of the same 3 bytes in succession. odd.

My project is a bit larger because it dumps the received bytes over the USB UART A port where I can easily visualise the receive buffer in realtime but all the serial side is working fine.

hexreader
Posts: 1786
Joined: 27 Jun 2010 12:07
Location: England

Re: PIC32MZ Spi fun

#4 Post by hexreader » 24 Jun 2020 12:03

Did you spot the change to "Nolock"?

Code: Select all

    PPS_Mapping_Nolock(_RPF8, _Output, _SDO6);                                  // F8 is MOSI
    PPS_Mapping_Nolock(_RPF2, _Input, _SDI6);                                   // F2 is MISO
Does my project return 3 bytes repeated same as your project, or 12 different bytes?

Did you set "allow multiple reconfigurations" in configuration settings?
Start every day with a smile...... (get it over with) :)

User avatar
stefan.filipovic
mikroElektronika team
Posts: 1135
Joined: 18 Dec 2018 10:30

Re: PIC32MZ Spi fun

#5 Post by stefan.filipovic » 24 Jun 2020 14:43

Hi,

I've attached the Serial Flash example from the compiler adapted for PIC32MZ2048EFH144.

Kind regards,
Attachments
IMG_20200624_153855.jpg
IMG_20200624_153855.jpg (2.12 MiB) Viewed 2738 times
SerialFlash_MZ.zip
(602.35 KiB) Downloaded 76 times
Stefan Filipović

Dakta
Posts: 53
Joined: 07 Sep 2013 16:08

Re: PIC32MZ Spi fun

#6 Post by Dakta » 24 Jun 2020 14:45

Thank you i appreciate it, will try it tonight!

Dakta
Posts: 53
Joined: 07 Sep 2013 16:08

Re: PIC32MZ Spi fun

#7 Post by Dakta » 24 Jun 2020 21:01

Thanks everyone for your time

Hexreader, I did overlooked the change to nolock (I was in a rush to work this morning), however implementing the change makes no difference - I still get the same 3 byte pattern. It is the same 3 bytes every time (multiple reconfiguration is enabled)

I changed your own code that you attached earlier to my MCU (PIC32MZ2048ECH144) however it doesn't seem to run at all.

I tried Stefans code with the same schema as above projects and it seems to work okay.

This is the software that I have dumping the 20 bytes to serial port (there is an offset as 3 as the serial port data comes with a 3 byte header):

Image


So I am not sure why this doesn't work.

Dakta
Posts: 53
Joined: 07 Sep 2013 16:08

Re: PIC32MZ Spi fun

#8 Post by Dakta » 25 Jun 2020 22:51

hmm. still no progress. there has to be a reason this doesn't work.



full code listing attached (including PC side software), have to admit I'm rapidly losing patience with this, I can't believe this has become so much work!

Incredibly frustrating, I didn't really want to supply the full code as it's part of whats going to be quite a large program and lots of irrelevent bits,but i suppose here we are.
Attachments
dakta spi.zip
(408.06 KiB) Downloaded 66 times

hexreader
Posts: 1786
Joined: 27 Jun 2010 12:07
Location: England

Re: PIC32MZ Spi fun

#9 Post by hexreader » 26 Jun 2020 01:41

Now I am more confused.

The same program that worked before, now returns 1C 31 14 repeated

Can't see any change in setup from when it worked

Busy for the next few days - so may not progress any further
Start every day with a smile...... (get it over with) :)

hexreader
Posts: 1786
Joined: 27 Jun 2010 12:07
Location: England

Re: PIC32MZ Spi fun

#10 Post by hexreader » 26 Jun 2020 02:42

Found out what I am doing different....

I now have oscilloscope cables connected and I get more sensible return bytes.

Will have to connect logic analyser to check detailed timing of the SPI signals.
Start every day with a smile...... (get it over with) :)

hexreader
Posts: 1786
Joined: 27 Jun 2010 12:07
Location: England

Re: PIC32MZ Spi fun

#11 Post by hexreader » 26 Jun 2020 03:27

Got it....

Connecting scope leads causes bad data, not good

Reading the datasheet for my device was the key - not the datasheet for Micron M25P80 which is the wrong datasheet.

3 hex bytes of 1C 31 14 is absolutely correct for the cFeon F80-100 chip fitted to my board

Table 5 of the Eon EN25F80 datasheet states that response to op code 9F should give identity 1C and type/size 3114 hex

Reading more than 3 bytes is allowed, but seems pointless to me.

Hex 1C 3014 might suggest that you have a different Flash chip to mine?

Hex 1C corresponds to "Flash ID 28" as shown on Stefan's picture in post #5
Start every day with a smile...... (get it over with) :)

Dakta
Posts: 53
Joined: 07 Sep 2013 16:08

Re: PIC32MZ Spi fun

#12 Post by Dakta » 26 Jun 2020 06:35

ooh - you might be onto something there. I kinda had faith in the part number on the pic fusion manual and was working religiously to the other components datasheet. I did just now look at the chip on the board but my eyes can't make it out so I'm going to have to get some brighter light and a magnifier (I am getting old I hate to say - and equally grumpy)

just pulled up a datasheet for your part, table 5 gives it - damn, perhaps not bad data after all. I don't know how I feel about that, i've ripped my nerves apart thinking I was doing something wrong. Thank you

I will have to confirm the part it seems, but this is looking more like it

Dakta
Posts: 53
Joined: 07 Sep 2013 16:08

Re: PIC32MZ Spi fun

#13 Post by Dakta » 26 Jun 2020 20:47

Well even with a magnifying glass - I can't read the part number - I MUST be getting old.

However the datasheet for EN25Q80B seems to fit

Post Reply

Return to “mikroC PRO for PIC32 General”