Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SPI setup
25-04-2012, 06:47 PM, (This post was last modified: 25-04-2012, 07:50 PM by KiloOne.)
#1
SPI setup
My LT6803-4 battery management chip that I want to talk to with my MICRO says that these conditions need to exist (from LT6803-4 datasheet):

"The LTC6803 SPI compatible interface is configured to operate in a system using
CPHA = 1 and CPOL = 1. Consequently, data on SDI must be stable during the rising edge of SCKI."

I can not comfirm that the default spi.c setting of SPICONF of 0x8120 is correct with regards to the above.

Looking at 'Section 23. Serial Peripheral Interface (SPI)' which details the PIC's SPI setup, I can not find reference to CPHA and CPOL. My logic analyser SPI protocol has a setting to parse the bits using CPHA and CPOL but they are not mentioned at all in this Pic datasheet.

I have tried many combinations of SPI2CON bits 6,9,29,30,31 but I have not been able to get the protocol analyser with CPHA = 1 and CPOL = 1 to parse my sent data correctly, nor the LT6803 to respond to my requests.

Is anyone more familiar with these modes that can help?

Thanks,
Dale

More info here (CPHA and CPOL were both 0 to get the protocol to recognize these correct hex values):
   

Here is a relevant section of the 6803 datasheet:
   

The timing diagrams confuse me a little, do the HIGH and LOW entry and exit lines of SCKI mean that it does not matter if the clock pulses are active HIGH or LOW?

And, how do CPHA and CPOL influence these timings?

NOTE, in order to produce the required LOW value of CSBI (SS2 in pic words), I had to set pin 39 up as an output and do a digitalwrite to LOW before the commands are sent and to HIGH after they are sent. This was because I could not get the pic to do that in any SPI mode I tried. This could be a bad kludge.

AND, some code that made data string in the above image (large indented lines will be removed after debugged (only remmed lines or printing to LCD lines)):
Code:
void getTemps(byte ibb) {
                            //SPI_init();        //actually this is done in void setup
                            //NHDsetCursor(0, 0);
                            //sprintf(buff, "%8X",  SPI2BRG );  
                            //NHDprintf(buff);
                            NHDsetCursor(1, 0);
                            NHDprintf("adr ");
    Din = addbase | ibb;                                       // addbase is 0b10000000 and idd is address of board I want temps from
                            sprintf(buff, "%3X", Din);
                            NHDprintf(buff);
    setPEC(Din);                    // set PEC of board address
                            NHDsetCursor(1, 10);
                            NHDprintf(" + PEC ");
                            sprintf(buff, "%3X", PEC);
                            NHDprintf(buff);
                    
                            //NHDsetCursor(0, 0);
                            //sprintf(buff, "%8X",  SPI2CON );  
                            //NHDprintf(buff);
                            //SPI2CONSET = 0x80000000;        // FRMPOL framed SPI bit(31) to 1=ON , hex of 2^31
                            //SPI2CONSET = 0x40000000;        // FRMPOL frame sync pulse dir bit(30) to 1=slave mode , hex of 2^30
                            //SPI2CONSET = 0x20000000;        // FRMPOL frame polarity bit(29) to 1=active high , hex of 2^29
                            //SPI2CONSET = 0x200;                // set phase bit(9) to 1, hex of 2^9
                            //SPI2CONSET = 0x40;                // clock polarity bit(6) to 1
                            NHDsetCursor(0, 10);
                            sprintf(buff, "%8X",  SPI2CON );  
                            NHDprintf(buff);
    pinMode(39, OUTPUT);            // PIC pin RF0 is pin 39 in digitalw.c
    digitalWrite(39, LOW);            // set CSBI low
                            //SPI_write(Din);                 // send board address
                            //SPI_write(PEC);                 // send PEC of board address
                            //SPI_write(0x30);                // send start temp conversion command
                            //SPI_write(0x50);                // send PEC of temp command
                            //Delayms(1000);
    SPI_write(Din);                 // send board address
    SPI_write(PEC);                 // send PEC of board address
    SPI_write(0x0E);                // send request temp command
    SPI_write(0xEA);                // send PEC of temp command
    for (i = 0; i < 5; i ++){            // get temp reg 0-4
        TMPR[i] = SPI_read();    
        packet[i] = TMPR[i];        
    }
    PEC = SPI_read();                // get PEC of data packet
    setPECpacket(5);                // set PECpacket for the 5 bytes in packet[]
    pinMode(39, OUTPUT);            // PIC pin RF0 is pin 39 in digitalw.c
    digitalWrite(39, HIGH);            // set CSBI back HIGH
                            for (i = 0; i < 5; i ++) {
                                NHDsetCursor(2, 4*i);
                                sprintf(buff, "%3X", TMPR[i]);
                                NHDprintf(buff);
                            }
                            NHDsetCursor(3, 0);
                            NHDprintf("PECpacket ");
                            sprintf(buff, "%3X", PECpacket);
                            NHDprintf(buff);
                            Delayms(1500);
                            NHDclear();
                            Delayms(150);
}

And the LCD output:
   

WOW, a lot of times when I have a problem I will sit down and compose a post, but by the time I have finished the post I have organized the problem so well that I have solved it and never actually post, not this time. Smile

Dale
PIC32-Pinguino-OTG Rev C and PIC32-PINGUINO-MICRO rev.B
Win XP SP3
r381 x.3 Big Grin
AND spi.c {} error fixed
AND sdmmc.c pin error fixed
AND diskio.c fixed, MICRO can't use the RTCC
AND analog.c fixed for MICRO
Reply
25-04-2012, 09:25 PM, (This post was last modified: 25-04-2012, 10:03 PM by pingotg.)
#2
RE: SPI setup
I think you want bits CKP (6) and CKE (8).

I googled for things like:
PIC32 CPOL

I'd do some similar googling to figure out the other issues...

John

More thoughts:

SD card is via SPI so look at its files

the chip's SS2 (Arduino D10) is CON5-3

John
Reply
26-04-2012, 12:11 AM, (This post was last modified: 26-04-2012, 02:47 AM by KiloOne.)
#3
RE: SPI setup
Hi John,

Thanks for continuing to follow this. If you are interested, here is the project I am doing that needs this to work: http://www.rcgroups.com/forums/showthread.php?t=1412424

I finally took a little break today but back at it now. I read your post and re-read mine a few times and agree that CKP (6) and CKE (8) make the most sense, I guess I was stuck on playing with the framing bits until I kludged CSBI.

So I tried CKP (6) HIGH and CKE (8) HIGH and got this:
   

As you can see, the protocol analyzer still chokes at the first byte. Should be parsed as 0x80 (it misses the first bit), likely the LT6803 is doing the same thing as you can see also that there is no response from it. I am making a bit of a big assumption that the 6803 will respond if I get it right. Since this will be my 'first contact' Big Grin and I am not totally sure it is listening.

Dale

And I found this little table by googling:

Standard SPI Mode | Microchip PIC
Terminology | Control Bits
Using CPOL,CPHA | CKP CKE
--------------------+--------------
0,0 (0) | 0 1
0,1 (1) | 0 0
1,0 (2) | 1 1
1,1 (3) | 1 0

So I tried CKP (6) HIGH and CKE (8) LOW, still no luck:
   

The strange thing to me is the fact that on both of these timings, the data bit starts before the clock starts by the same amount. Shouldn't the change of CKE in the two cases change this timing?

Dale
PIC32-Pinguino-OTG Rev C and PIC32-PINGUINO-MICRO rev.B
Win XP SP3
r381 x.3 Big Grin
AND spi.c {} error fixed
AND sdmmc.c pin error fixed
AND diskio.c fixed, MICRO can't use the RTCC
AND analog.c fixed for MICRO
Reply
26-04-2012, 03:03 AM, (This post was last modified: 26-04-2012, 04:19 AM by KiloOne.)
#4
RE: SPI setup
HALF a BINGO! Idea My last statement led me to answer why there was no change in timing. Well, again my lack of being a bithead (to which I aspire) caused it. I finally saw that SPI_init sets bit 8 and I forgot to clear bit 8 when I wanted it LOW Blush

It also appears that I need to turn off the SPI with SPI2CONCLR = 0x8000 before changing SPI2CON or any SPI register for that matter. I think I will do this for good practice even if I don't have to.

At least now the protocol analyzer parses the data correctly with it set on CPHA=1 and CPOL=1.

Here is the corrected analyzer timings when CKP (6) HIGH and CKE (8) LOW:
   

I still need the other half of the bingo! to figure out why the LT6803-4 is not responding Huh
PIC32-Pinguino-OTG Rev C and PIC32-PINGUINO-MICRO rev.B
Win XP SP3
r381 x.3 Big Grin
AND spi.c {} error fixed
AND sdmmc.c pin error fixed
AND diskio.c fixed, MICRO can't use the RTCC
AND analog.c fixed for MICRO
Reply
26-04-2012, 09:01 AM,
#5
RE: SPI setup
KiloOne:
Which board are you actually using? The OTG, as noted in you signature...?

I'm asking, because you are using SPI2CON** registers in your code... these are for a second SPI bus, I reckon.
But the pic32 used on the OTG (PIC32MX440F256H) only has one SPI (probably hooked up to the SD-card reader). At least according to microchip: See the chip's page http://www.microchip.com/wwwproducts/Dev...e=en534168.

The comparison table here is interesting: http://www.microchip.com/ParamChartSearc...&pageId=74
...the PIC32MX220F032D used on the Micro has 2 SPI, PIC32MX795F512H in that PIC32-T795 thingy has 3xSPI and... WTF!? ... it even has an ethernet MAC onboard! Exclamation
Reply
26-04-2012, 09:20 AM, (This post was last modified: 26-04-2012, 09:22 AM by mf01.)
#6
RE: SPI setup
Hi bjoernp;

I believe that you are correct about there only being one SPI bus on the PIC32MX440F256H used on the PIC32-Pinguino OTG board.

However, MicroChip label the SPI bus on the PIC32MX440F256H as SPI2 (SDI2, SD02, SCK2 & SS2) and this terminology is followed in the Pinguino IDE. If you look at the spic.c file in /p32/include/pinguino/core folder you will see that for the PIC32-Pinguino OTG board SPIx 2 is defined which results in the various SPI2 registers being used.

Regards
Board = PIC32-Pinguino-OTG Rev C
OS = Linux Unbuntu 11.10 till 26 Apr 2012
OS = Linux Unbuntu 12:04 from 27 Apr 2012
Reply
26-04-2012, 09:45 AM, (This post was last modified: 26-04-2012, 09:47 AM by pingotg.)
#7
RE: SPI setup
^ what he said.

Any PIC32 family member potentially has quite a lot of SPIs but only some will be present. For the OTG it's SPI2. Easiest way I found is to look at the chip pinout in the Microchip doc or Olimex schematic (which generally seem good).

Microchip tend to provide more peripherals on the chips with higher numbers so expect the 795 to have lots. The number after the F seems to be the flash size (excluding bootflash). H & L hint at more or fewer pins and it's the pin count that tends to allow more peripherals actually to be present.

For SPI, I really would be looking at the SD card code. And Microchip will have sample(s). Possibly also look at the Duinomite's sources as I think it has an SPI command.

John
Reply
26-04-2012, 09:49 AM,
#8
RE: SPI setup
mf01: True. Total confusion... funny!
So does SPI2 work then?

The header of the retrobsd project simply defines both SPI: http://retrobsd.googlecode.com/svn-histo.../pic32mx.h
On a quick search I found this confusion reflected only here: http://www.microchip.com/forums/m617739.aspx - lone question unanswered.
And indeed, the datacheat(!) on page 61 says so: SPI1 is based on 5800, SPI2 on 5A00 and in footnote 2:
SPI2 Module is not present on PIC32MX420FXXXX/440FXXXX devices.
Ya, whatever! xD

pingotg:
Then they at least screwed up their datasheet in that place. I still find it irritating, that this has not been fixed yet...
Reply
26-04-2012, 10:06 AM,
#9
RE: SPI setup
Hi bjoernp,

I agree it is poor when you can't trust a manufacturer's datasheet, but I think it is only that comment that is inconsistent with the rest of the information on the PIC32MX440F256H.

I have no experience of using the SPI functions directly (i.e. calling them from within one of my own programs) but I have successfully used the SD card functions that make use of the SPI functions so I would say that they do work.

Regards
Board = PIC32-Pinguino-OTG Rev C
OS = Linux Unbuntu 11.10 till 26 Apr 2012
OS = Linux Unbuntu 12:04 from 27 Apr 2012
Reply
26-04-2012, 10:34 AM,
#10
RE: SPI setup
mf02: If that works, then all should be fine. Just a quirk in the specs, no biggy.

kiloTwo: For your "second half of the bingo" Smile then...
Maybe it's a cip-select...? Or something about addressing the device?
I don't know much about SPI, but in I2C that kind of thing can lead to similar results - a deaf device not doing anything (and they are a bit similar on that protocol level, as I remember vaguely).

(oops, sorry: mf01, kiloOne... or was it...? Wink)
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)