Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SPI speed
16-11-2013, 10:37 PM,
#1
SPI speed
Hi, I've Pinguino OTG 32. I use SPI to send data to a number of serial to parallel shift registers. I use the general code: SPI.init(), SPI.transfer(),...

The data pin of SPI (D11) is set to be an "open collector" iso 3.3V (ODCG=0x00000100Wink. I use a pull resistor connected to 5V. This way I switch between 0V and 5V.

When I measure the clock speed of SPI using an oscilloscope it's only 250KHz. Is this the maximum speed for the data transfer???? Or is there a way to increase it?

Thank you,
Jack
Reply
19-11-2013, 08:44 PM,
#2
RE: SPI speed
The max. speed is theoretically 20MHz and is defined in SPIxBRG register (SPI Baud Rate Generator Register for the Module ‘x’).

In spi.c :
Code:
// Fsck = Fpb / (2 * (SPIxBRG + 1)
// SPIxBRG = (Fpb / (2 * Fsck)) - 1
// speed must be in bauds
void SPI_clock(u32 speed)
{
    u32 Fpb;
    //u32 Fsck;
    u16 clk;

    Fpb = GetPeripheralClock();
    if (speed > (Fpb / 2))
    {
        CLKSPD = 0;            // use the maximum baud rate possible
        return;
    }
    else
    {
        clk = (Fpb / (2 * speed)) - 1;
        if (clk > 511)
            CLKSPD = 511;    // use the minimum baud rate possible
        else                    // ** fix for bug identified by dk=KiloOne
            CLKSPD = clk;    // use calculated divider-baud rate
    }
}
It is easier to complain than it is to do, but it is better to do than it is to complain.
Reply
19-11-2013, 11:04 PM,
#3
RE: SPI speed
Thank you for the reply. I used the method you recommended:
Code:
void setup(){
    SPI_clock(0xffffffff);
    ...
}

There is also another way by directly writing to the registers:
(20MHz) SPIxBRG = 0;
(10MHz) SPIxBRG = 1;
...
for my chip x=2....

But your solution is way better because it works for all chips.

Unfortunately I couldn't use the max speed (20MHz) because it gave errors... I had to reduce the speed to 4MHz. I think it's because the SPI clock is switching between 0V and 3.3V... for these values the switching frequency of the shift register I'm using (MC74HC595ADG) is limited to 4.8MHz-10MHz. I'm gonna try the open drain option with a pull-up resistor to switch between 0V and 5V to boost up the data throughput.

As a slightly-related question: is it possible to push 32-bit words through the SPI? Right now I'm using 10 8-bit words to send my data, but ofcourse 5 16-bit or 3 32-bit would be nicer.

Thanks for looking into this!
Reply
20-11-2013, 08:41 PM,
#4
RE: SPI speed
(19-11-2013, 11:04 PM)JackGie Wrote: As a slightly-related question: is it possible to push 32-bit words through the SPI? Right now I'm using 10 8-bit words to send my data, but ofcourse 5 16-bit or 3 32-bit would be nicer.

Yes it's possible. PIC32MX SPI has 8-, 16- and 32-bit mode (see SPIxCON register).
You will find all useful information about SPI in this doc. : http://ww1.microchip.com/downloads/en/De...61106G.pdf

Feel free to improve the spi.c library if necessary.
It is easier to complain than it is to do, but it is better to do than it is to complain.
Reply
27-11-2013, 11:54 PM,
#5
RE: SPI speed
Hey Regis, thanks for the reply. I'm cooperating with JackGie to get the SPI working, which is now going smoothly.

I used your info to set the 10th bit of SPI2CON to 1, so the SPI transfers 16 bit words. I did need to adjust spi.c to take 32-bit integers as argument to the SPI_write function:
Code:
unsigned char SPI_write(unsigned char data_out)
was changed to
Code:
unsigned char SPI_write(unsigned int data_out)

I'll try to improve the spi.c library on google code as well, but I'll first need to wrap my head around svn (more used to git Wink ).
Reply
28-11-2013, 09:41 AM,
#6
RE: SPI speed
Good luck !

(27-11-2013, 11:54 PM)HolKann Wrote: Hey Regis, thanks for the reply. I'm cooperating with JackGie to get the SPI working, which is now going smoothly.

I used your info to set the 10th bit of SPI2CON to 1, so the SPI transfers 16 bit words. I did need to adjust spi.c to take 32-bit integers as argument to the SPI_write function:
Code:
unsigned char SPI_write(unsigned char data_out)
was changed to
Code:
unsigned char SPI_write(unsigned int data_out)

I'll try to improve the spi.c library on google code as well, but I'll first need to wrap my head around svn (more used to git Wink ).
It is easier to complain than it is to do, but it is better to do than it is to complain.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)