data readout of a SPI gyroscope

Hacking multi-tool. Get one for $30, including worldwide shipping.

data readout of a SPI gyroscope

Postby clchow83 » Tue Nov 20, 2012 3:47 am

Hi, I'm currently working on a project to readout a gyroscope with SPI interface, am thinking to communicate the device to PC using this bus pirate. By the way, I'm new to this area and not sure whether the bus pirate can be used for my device. Can anyone please give some useful advises.

My gyroscope has a 4 wire SPI interface and operates as a slave. The SPI register map consists of 6 read only registers, with address ranging from address 0x0 to 0x5. The SPI register address is a 4 bits word. Each SPI register is an 8-bits width data (from bit 0 to 7). The first 4 registers (0x0 to 0x3)are for gyro output, while the last 2 registers (0x4 to 0x5) are for temperature output.

My project is to continuously read out the gyro data (degree/sec) and temperature data (degree) from the gyroscope for a period of hours. Can anyone advise on whether the bus pirate can achieve this function? And whether an in-depth programming is needed to achieve this function?

Thanks a lot~
clchow83
Newbie
Newbie
 
Posts: 6
Joined: Tue Nov 20, 2012 3:35 am

Re: data readout of a SPI gyroscope

Postby tayken » Tue Nov 20, 2012 9:46 pm

Binary mode can be used for that pretty easily. In that mode it acts like a USB-SPI converter. Then with the program, you can write the data to a text file (or csv for excel plotting).

Only computer side programming is needed. Check out http://dangerousprototypes.com/docs/Bus ... pting_mode
User avatar
tayken
Developer
Developer
 
Posts: 1384
Joined: Sat Dec 12, 2009 10:58 am
Location: Tokyo, Japan

Re: data readout of a SPI gyroscope

Postby clchow83 » Sun Dec 09, 2012 9:41 pm

Thanks for your reply. I just received my bus pirate and start playing with it. Happily, i'm able to read out the data from gyroscope using the user terminal TeraTerm, by setting the bus pirate into SPI mode.

Next step is to automate continuous data reading from the gyroscope for a long period and log all the data into a text or csv file. However, I'm still have no idea on how to do this? Can anyone help by referring more information for me? In case i need to use computer side programming, which language is more user-friendly? I'm a noob in programming.

thanks
clchow83
Newbie
Newbie
 
Posts: 6
Joined: Tue Nov 20, 2012 3:35 am

Re: data readout of a SPI gyroscope

Postby ian » Mon Dec 10, 2012 2:41 am

We have a number of scripting examples in different languages, but it's not the easiest thing to get started for a noob. You could try a simple scripting language like Perl or Python:

http://dangerousprototypes.com/docs/Bus ... pting_mode
User avatar
ian
Crew
Crew
 
Posts: 10578
Joined: Mon Jul 06, 2009 6:14 am

Re: data readout of a SPI gyroscope

Postby clchow83 » Tue Dec 11, 2012 11:09 pm

Hi ian, thanks for your reply, i'm now trying to use pybuspiratelite to script the data readout of my gyroscope.
everything is fine, it's able to enter Binmode, SPI mode and configure SPI. And i also explore the file I/O function from the python beginner guide to output my data to a text file.

But i face i problem regarding the SPI data readout using python.
from Tera Term terminal, i able to read the data from my gyroscope: (0x50 is my reading command, read 4 bytes)
SPI>[0x50 r:4]
/CS ENABLED
WRITE: 0x50
READ: 0xFF 0xFF 0x6E 0x00
/CS DISABLED

but when i use python to get the same output, it cannot read anything. The script is as below:
print "Reading GYRO..."
spi.CS_Low()
gyro_data = spi.bulk_trans(4, [0x54, 0x00, 0x00, 0x00])
print "Data is ", gyro_data
spi.CS_High()

can anyone help me with this? thanks
clchow83
Newbie
Newbie
 
Posts: 6
Joined: Tue Nov 20, 2012 3:35 am

Re: data readout of a SPI gyroscope

Postby tayken » Wed Dec 12, 2012 4:37 am

I believe there is a typo, you are sending 0x54, not 0x50.
User avatar
tayken
Developer
Developer
 
Posts: 1384
Joined: Sat Dec 12, 2009 10:58 am
Location: Tokyo, Japan

Re: data readout of a SPI gyroscope

Postby clchow83 » Wed Dec 12, 2012 5:38 am

sorry, this was just the typo in my last post. This is the actual script i executed:
print "Reading GYRO..."
spi.CS_Low()
gyro_data = spi.bulk_trans(4, [0x50, 0x00, 0x00, 0x00])
print "Data is ", gyro_data
spi.CS_High()

and this is the outcome, i really have no idea what is stored in the variable "gyro_data". It displays nothing...

Entering binmode: OK.
Entering raw SPI mode: OK.
Configuring SPI peripherals: OK.
Configuring SPI speed: OK.
Configuring SPI configuration: OK.
Starting...
Reading GYRO...
Data is
Reset Bus Pirate to user terminal: OK.
>>>
clchow83
Newbie
Newbie
 
Posts: 6
Joined: Tue Nov 20, 2012 3:35 am

Re: data readout of a SPI gyroscope

Postby tayken » Wed Dec 12, 2012 7:08 am

How about sth like:
Code: Select all
spi.CS_Low()
gyro_data = spi.bulk_trans(4, [0x50, 0x00, 0x00, 0x00])
spi.CS_High()
print "Data is: "
for each in gyro_data:
   print hex(each)


The reason for that is gyro_data is an array. If this does not work, try
Code: Select all
spi.CS_Low()
gyro_data = spi.bulk_trans(5, [0x50, 0x00, 0x00, 0x00, 0x00])
spi.CS_High()
print "Data is: "
for each in gyro_data[1:]:
   print hex(each)
User avatar
tayken
Developer
Developer
 
Posts: 1384
Joined: Sat Dec 12, 2009 10:58 am
Location: Tokyo, Japan

Re: data readout of a SPI gyroscope

Postby ian » Wed Dec 12, 2012 8:43 am

I am not familiar witht he python library anymore, but maybe there is a seperate read command?

so you

send(0x50)

and then

for(i=0 to 3){gyro_data[i]=read();}

Sorry for C pseudo code, have not worked in python in some years.
User avatar
ian
Crew
Crew
 
Posts: 10578
Joined: Mon Jul 06, 2009 6:14 am

Re: data readout of a SPI gyroscope

Postby tayken » Wed Dec 12, 2012 9:26 am

I am checking out spi_test.py under pyBusPirateLite for the library. Line 124 is related to EEPROM read operation spi.bulk_trans(size, data) is the prototype for I/O. data is the values to be written as an array and size is the size of array. If we want to read some stuff, we can send 0x00's and the data returned by this function is the data read from SPI bus.

clchow83, if the codes above does not work, can you post the python program (attachment, .py is not fine, so probably you have to .zip it) and sensor datasheet (link is fine)? Also a complete capture of user mode is appreciated.
User avatar
tayken
Developer
Developer
 
Posts: 1384
Joined: Sat Dec 12, 2009 10:58 am
Location: Tokyo, Japan

Re: data readout of a SPI gyroscope

Postby clchow83 » Thu Dec 13, 2012 2:49 am

Hi all, i modified my script a bit and it's working now.
Code: Select all
spi.CS_Low()
gyro_data = spi.bulk_trans(5, [0x50, 0x00, 0x00, 0x00, 0x00])
spi.CS_High()
print "Data is: "
for each in gyro_data[1:]:
   print ord(each)


I found that if i want to read 4 bytes of data from my gyroscope, i need to transfer 5 bytes in the bulk_trans command. The first byte is the reading command, followed by 4 zero. And the displayed data is converted back to number form using the function ord().
clchow83
Newbie
Newbie
 
Posts: 6
Joined: Tue Nov 20, 2012 3:35 am

Re: data readout of a SPI gyroscope

Postby tayken » Thu Dec 13, 2012 3:15 am

Nice!

I believe the reason for that is: In user interface, you used [0x50 r:4] command. Which sends 0x50, discards what it received at that time, then reads 4 bytes which is done by sending 0x00 4 times. Oppes, one thing I missed was the returned data is a char array, you need ord() to change it to a number. Also you can use hex(ord(each)) for hexadecimal display.
User avatar
tayken
Developer
Developer
 
Posts: 1384
Joined: Sat Dec 12, 2009 10:58 am
Location: Tokyo, Japan


Return to Bus Pirate Support