Change SPI word size not work


21 posts
by pasifus » Sat Jan 19, 2013 5:38 pm
Hi,
I'm use wiringPi library and I'm try to change SPI word size.
I change in wiringPiSPI.c variable spiBPW from 8 to 4,12 and 16 but when i try change it SPI device don't generate output signals (sckl, miso, mosi). only when SPI word size 8 it's work.

What i do wrong?
Thanks.
Posts: 17
Joined: Sat Jan 19, 2013 9:35 am
by pasifus » Tue Jan 22, 2013 10:00 am
Someone tried to change spi word size?
Posts: 17
Joined: Sat Jan 19, 2013 9:35 am
by gordon@drogon.net » Tue Jan 22, 2013 12:35 pm
I'm not sure the hardware actually supports it...

So it's multiples of 8 bits only.

You can only write 8, 16, 24, 32, ... bits in a single transaction though, as long as its a multiple of 8.

(that I'm aware of)

What are you trying to interface to that only need 4 bits? (or 12, etc.)

-Gordon
--
Gordons projects: https://projects.drogon.net/
User avatar
Posts: 1541
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
by pasifus » Tue Jan 22, 2013 2:04 pm
i have 12 bit adc and i was thinking that send only 12 bit it more easy.
Posts: 17
Joined: Sat Jan 19, 2013 9:35 am
by Gert van Loo » Tue Jan 22, 2013 2:09 pm
No, that is not supported by that SPI interface.
There are two other SPI interfaces which do not have that problem**,
but the pins are not brought out so that does not help you.

** Sorry could not help pointing that out as I designed the other SPI interface type. :ugeek:
User avatar
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2080
Joined: Tue Aug 02, 2011 7:27 am
by gordon@drogon.net » Tue Jan 22, 2013 2:23 pm
pasifus wrote:i have 12 bit adc and i was thinking that send only 12 bit it more easy.


Probably not.... Because what you might find is that to set the ADC you need to send it 16 bits. e.g. 4 bits of 'command' data (select channel, mode maybe?) then keep the clock going for another 12 bits to clock out the data, so needing 16 bits in-all.

At least that's how the ones I've used & seen so-far work.

-Gordon
--
Gordons projects: https://projects.drogon.net/
User avatar
Posts: 1541
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
by pasifus » Tue Jan 22, 2013 2:55 pm
ok. if i send 16bit and generate CS with some GPIO. if it possible to generate CS. (SPI_CLK=1MHz)

Image
here is the adc http://www.ti.com/lit/ds/symlink/ads7863.pdf
Posts: 17
Joined: Sat Jan 19, 2013 9:35 am
by gordon@drogon.net » Tue Jan 22, 2013 7:53 pm
pasifus wrote:ok. if i send 16bit and generate CS with some GPIO. if it possible to generate CS. (SPI_CLK=1MHz)

Image
here is the adc http://www.ti.com/lit/ds/symlink/ads7863.pdf


So it's 16 clocks per sample. You need to output the command byte at the same time as reading in the first reply byte, then output a 2nd byte (zeros) to force anothe 8 bits of clock to read in the 2nd data byte.

Look at my gertboard.c driver in wiringPi - it's fairly similar to how that works. (the one in the wiringPi directory, not the one in the examples)

You can only read one converter though - the Pi only has one SPI port. (So you can read 2 inputs on one convertor, but not the inputs on the 2nd convertor), so connect up, Clock, serial in/serial out and one of the Pi's enable pins to the chip (CS) and I think that's all you'll need (other than the usual power, ground, decoupling,etc.) Connect RD to CONVST by the looks of it too.

You could read the 2nd convertor, but you'll need a couple of tristate buffers to enable one at a time to the Pi's MISO pin.

Simples :)

-Gordon
--
Gordons projects: https://projects.drogon.net/
User avatar
Posts: 1541
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
by TTT » Wed Jan 23, 2013 6:16 pm
Hi Gordon,
So if I wanted to connect my RPi to a slave device which communicates with 24 bits. How would I go about that? I tried changing the bits per word but I keep getting an error. I'm using the example that most people on the internet spidev_test.c but it only sends 8 bit words.
Any help would be greatly appreciated.
Thanks
Posts: 1
Joined: Wed Jan 23, 2013 6:14 pm
by pasifus » Wed Jan 23, 2013 6:27 pm
simple try to send 8 bit three time
Posts: 17
Joined: Sat Jan 19, 2013 9:35 am
by gordon@drogon.net » Wed Jan 23, 2013 6:31 pm
TTT wrote:Hi Gordon,
So if I wanted to connect my RPi to a slave device which communicates with 24 bits. How would I go about that? I tried changing the bits per word but I keep getting an error. I'm using the example that most people on the internet spidev_test.c but it only sends 8 bit words.
Any help would be greatly appreciated.
Thanks


Just write 3 bytes at once. The enable line will stay low for the entire transaction which is all you need, really.

In my world:

Code: Select all
#include <wiringPiSPI.h>
// stuff

  unsigned char buf [3] ;

  if ((fd = wiringPiSPISetup (chan, speed)) < 0)
    return -1 ;

// You can then simply use write() or read() to write or read 3 bytes at a time, to the fd, or do a
//  simultaneous read/write operation:

// write only:

  write (fd, buf, 3) ;

// or simultaneous w/r transaction

  wiringPiSPIDataRW (chan, buf, 3) ;

// buf updated with the 3 bytes returned at the same time



If you're just writing data (e.g. to those LED strings), then write() is marginally faster than the read/write operation.

-Gordon
--
Gordons projects: https://projects.drogon.net/
User avatar
Posts: 1541
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
by gordon@drogon.net » Wed Jan 23, 2013 6:35 pm
pasifus wrote:simple try to send 8 bit three time


you have to make sure you do it in one transaction - otherwise the enable line will toggle on each transction and some devices use this to reset their input bit counter, etc.

-Gordon
--
Gordons projects: https://projects.drogon.net/
User avatar
Posts: 1541
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
by pasifus » Wed Jan 23, 2013 7:08 pm
gordon@drogon.net wrote:
pasifus wrote:simple try to send 8 bit three time


you have to make sure you do it in one transaction - otherwise the enable line will toggle on each transction and some devices use this to reset their input bit counter, etc.

-Gordon


yes you right, must to send then by one transaction, also i thin you need to order bytes before sending (Big-Endian/Little-Endian)
Posts: 17
Joined: Sat Jan 19, 2013 9:35 am
by pasifus » Wed Jan 23, 2013 7:51 pm
gordon@drogon.net wrote:
pasifus wrote:ok. if i send 16bit and generate CS with some GPIO. if it possible to generate CS. (SPI_CLK=1MHz)

Image
here is the adc http://www.ti.com/lit/ds/symlink/ads7863.pdf


So it's 16 clocks per sample. You need to output the command byte at the same time as reading in the first reply byte, then output a 2nd byte (zeros) to force anothe 8 bits of clock to read in the 2nd data byte.

Look at my gertboard.c driver in wiringPi - it's fairly similar to how that works. (the one in the wiringPi directory, not the one in the examples)

You can only read one converter though - the Pi only has one SPI port. (So you can read 2 inputs on one convertor, but not the inputs on the 2nd convertor), so connect up, Clock, serial in/serial out and one of the Pi's enable pins to the chip (CS) and I think that's all you'll need (other than the usual power, ground, decoupling,etc.) Connect RD to CONVST by the looks of it too.

You could read the 2nd convertor, but you'll need a couple of tristate buffers to enable one at a time to the Pi's MISO pin.

Simples :)

-Gordon


thanks for trick to send 16 bit by one session (4bit zeros and 12bit control data )
but i confuse about RD/CONVST. it's needs to see a clock edge.
if i can to generate gpio in first 4bites(zeros) of spi transmit or maybe you now better solution?

i really need read only from one convertor
Posts: 17
Joined: Sat Jan 19, 2013 9:35 am
by gordon@drogon.net » Wed Jan 23, 2013 7:56 pm
pasifus wrote:thanks for trick to send 16 bit by one session (4bit zeros and 12bit control data )
but i confuse about RD/CONVST. it's needs to see a clock edge.
if i can to generate gpio in first 4bites(zeros) of spi transmit or maybe you now better solution?

i really need read only from one convertor


I read the data sheet. It seemed to suggest you can connect the RD (output) to CONVST (input).

-Gordon
--
Gordons projects: https://projects.drogon.net/
User avatar
Posts: 1541
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
by pasifus » Wed Jan 23, 2013 8:12 pm
gordon@drogon.net wrote:
pasifus wrote:thanks for trick to send 16 bit by one session (4bit zeros and 12bit control data )
but i confuse about RD/CONVST. it's needs to see a clock edge.
if i can to generate gpio in first 4bites(zeros) of spi transmit or maybe you now better solution?

i really need read only from one convertor


I read the data sheet. It seemed to suggest you can connect the RD (output) to CONVST (input).

-Gordon



uuups it's my fault i thought that RD it also input.

Thanks a lot of. I will try your solution.
Posts: 17
Joined: Sat Jan 19, 2013 9:35 am
by gordon@drogon.net » Wed Jan 23, 2013 10:07 pm
pasifus wrote:yes you right, must to send then by one transaction, also i thin you need to order bytes before sending (Big-Endian/Little-Endian)


Left as an excercise to the user ...

-Gordon
--
Gordons projects: https://projects.drogon.net/
User avatar
Posts: 1541
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
by pasifus » Sat Jan 26, 2013 4:15 pm
pasifus wrote:
gordon@drogon.net wrote:
pasifus wrote:ok. if i send 16bit and generate CS with some GPIO. if it possible to generate CS. (SPI_CLK=1MHz)

Image
here is the adc http://www.ti.com/lit/ds/symlink/ads7863.pdf


So it's 16 clocks per sample. You need to output the command byte at the same time as reading in the first reply byte, then output a 2nd byte (zeros) to force anothe 8 bits of clock to read in the 2nd data byte.

Look at my gertboard.c driver in wiringPi - it's fairly similar to how that works. (the one in the wiringPi directory, not the one in the examples)

You can only read one converter though - the Pi only has one SPI port. (So you can read 2 inputs on one convertor, but not the inputs on the 2nd convertor), so connect up, Clock, serial in/serial out and one of the Pi's enable pins to the chip (CS) and I think that's all you'll need (other than the usual power, ground, decoupling,etc.) Connect RD to CONVST by the looks of it too.

You could read the 2nd convertor, but you'll need a couple of tristate buffers to enable one at a time to the Pi's MISO pin.

Simples :)

-Gordon


thanks for trick to send 16 bit by one session (4bit zeros and 12bit control data )
but i confuse about RD/CONVST. it's needs to see a clock edge.
if i can to generate gpio in first 4bites(zeros) of spi transmit or maybe you now better solution?

i really need read only from one convertor



i tested today spi and find that spi delay between write sessions it very long

Image

Image

Code: Select all
while(TRUE)
        {
                buff[0] = 0x32;
                buff[1] = 0x10;

                // send data to SPI
                wiringPiSPIDataRW(0, buff, 2);
        }


spi_speed = 1MHz
kernel: Linux raspberrypi 3.2.27+ #307 PREEMPT Mon Nov 26 23:22:29 GMT 2012 armv6l
WiringPi from: https://github.com/WiringPi/WiringPi

How to reduce the delay?
Posts: 17
Joined: Sat Jan 19, 2013 9:35 am
by gordon@drogon.net » Sat Jan 26, 2013 4:35 pm
pasifus wrote:i tested today spi and find that spi delay between write sessions it very long

Image

Image

Code: Select all
while(TRUE)
        {
                buff[0] = 0x32;
                buff[1] = 0x10;

                // send data to SPI
                wiringPiSPIDataRW(0, buff, 2);
        }


spi_speed = 1MHz
kernel: Linux raspberrypi 3.2.27+ #307 PREEMPT Mon Nov 26 23:22:29 GMT 2012 armv6l
WiringPi from: https://github.com/WiringPi/WiringPi

How to reduce the delay?


Firstly, please get wiringPi from https://git.drogon.net/ and not github. The github version is not maintained by me, but rather Gadgetoid who maintans the wrappers for wiringPython, etc. It's sometimes a few revisions behind my own released version. However that's not going to help you here..

But yes, there is a gap of about 1 clock cycle between bytes. It's like a stretched clock pulse. I've observed this too. I'm fairly sure this is a limitation of the hardware and nothing in software. (there are other latency issues in the current kernel SPI drivers, but I'm fairly sure this isn't part of that) Something you will have to live with in this $35 device. It's not given me any issues with the few devices I've used it with so-far.

-Gordon
--
Gordons projects: https://projects.drogon.net/
User avatar
Posts: 1541
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
by pasifus » Sat Jan 26, 2013 7:31 pm
gordon@drogon.net wrote:
pasifus wrote:i tested today spi and find that spi delay between write sessions it very long

Image

Image

Code: Select all
while(TRUE)
        {
                buff[0] = 0x32;
                buff[1] = 0x10;

                // send data to SPI
                wiringPiSPIDataRW(0, buff, 2);
        }


spi_speed = 1MHz
kernel: Linux raspberrypi 3.2.27+ #307 PREEMPT Mon Nov 26 23:22:29 GMT 2012 armv6l
WiringPi from: https://github.com/WiringPi/WiringPi

How to reduce the delay?


Firstly, please get wiringPi from https://git.drogon.net/ and not github. The github version is not maintained by me, but rather Gadgetoid who maintans the wrappers for wiringPython, etc. It's sometimes a few revisions behind my own released version. However that's not going to help you here..

But yes, there is a gap of about 1 clock cycle between bytes. It's like a stretched clock pulse. I've observed this too. I'm fairly sure this is a limitation of the hardware and nothing in software. (there are other latency issues in the current kernel SPI drivers, but I'm fairly sure this isn't part of that) Something you will have to live with in this $35 device. It's not given me any issues with the few devices I've used it with so-far.

-Gordon


You right it not problem of WiringPi version (in your repository i see same gap). I thinks it device problem, too.
Maybe you now projects that try to reduce delay between segment transmuting. (changes in spi driver)
To example http://www.open.com.au/mikem/bcm2835/ if can to help me? (or that same driver that use in this kernel)
Posts: 17
Joined: Sat Jan 19, 2013 9:35 am
by gordon@drogon.net » Sat Jan 26, 2013 7:39 pm
pasifus wrote:
You right it not problem of WiringPi version (in your repository i see same gap). I thinks it device problem, too.
Maybe you now projects that try to reduce delay between segment transmuting. (changes in spi driver)
To example http://www.open.com.au/mikem/bcm2835/ if can to help me? (or that same driver that use in this kernel)


That library pokes the SPI hardare directly rather than use the kernel driver. I'm of the opinion that it would suffer the same problems because the problem is with the hardware on the SoC and not the software driving it. I do not recommend using anything other than the standard supplied kernel drivers to drive the SPI and I2C (and serial) hardware on the Pi. My SPI acces code in wiringPi is just trivial wrappers round the kernel stuff to make it easier to use.

-Gordon
--
Gordons projects: https://projects.drogon.net/
User avatar
Posts: 1541
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK