Alan Johnstone
Posts: 45
Joined: Tue Jan 08, 2013 4:35 pm

SPI not working

Sun Jan 27, 2013 7:50 pm

I have tried and tried to get dtoa.py to work without success.

I can at most measure .5V when it should be over 2V
The "r" value in the program gives [0,0].
What does this mean? Where can I get some documentation on spidev?

Unfortunately I found the progam very hard to make sense of because what it is doing
is hidden in the "numlist" and "common" variables.
Below is a version that uses a function "changeLevel(channel,onOff,percent)"
which lets the user change the level to a % of max and explains what it is doing

Code: Select all

# Python 2.7 version by Alex Eames of http://RasPi.TV
# functionally equivalent to the Gertboard dtoa test by Gert Jan van Loo & Myra VanInwegen
# Use at your own risk - I'm pretty sure the code is harmless, but check it yourself.
# This will not work unless you have installed py-spidev as in the README.txt file
# spi must also be enabled on your system

import spidev
from time import sleep

spi = spidev.SpiDev()
spi.open(0,1)        # The Gertboard DAC is on SPI channel 1 (CE1 - aka GPIO7)


def changeLevel( ch,  onOff, percent):

    # the information is split into two bytes (8 bits) containing some info
    # on the channel, gain and ON/OFF and then the level requierd

    # for the first byte need to set the info for the chip
    # Note <<n moves the bits n places to the left and is the same as multiplying by 2^n
    # bits numbered 0-7 below

    bit7 = ch    << 7     # first bit is the channel 0 or 1
    bit6 = 0     << 6     # is ignored
    bit5 = 1     << 5     # gain set to 1X  (0 for 2X)
    bit4 = onOff << 4     # 1=available 0 = shut down


    # set the size of your chip 8,10,12 bits
    # if you do not know just leave as the size only
    # changes how finely you can set the voltage
    size=8

    # now take the percent and chip size and create the number we need
    # as integer
    number=(2**size-1)*percent/100

    # that number needs to be shifted left depending on the size
    # to butt up with the other info (bit4 of the first byte)
    number= number<<(12-size)

    # that number needs to be shifted left depending on the size
    # to butt up with the other info (bit4 of the first byte)
    number= number<<(12-size)

    # get the part of the number that goes into the second byte
    bottomPart= number % 256

    # the rest goes into the first byte
    topPart=(number-bottomPart)>>8

    # put it all together
    firstByte=bit7+bit6+bit5+bit4+topPart
    secondByte=bottomPart

    return spi.xfer2([firstByte,secondByte])


def which_channel():
    channel = raw_input("Which channel do you want to test? Type 0 or 1.\n")  # User inputs channel number
    while not channel.isdigit():                                              # Check valid user input
        channel = raw_input("Try again - just numbers 0 or 1 please!\n")      # Make them do it again if wrong
    return channel


channel = 3                         # set initial value to force user selection



while not (channel == 1 or channel == 0):       # channel is set by user input
    channel = int(which_channel())              # continue asking until answer 0 or 1 given

print "These are the connections for the digital to analogue test:"
print "jumper connecting GP11 to SCLK"
print "jumper connecting GP10 to MOSI"
print "jumper connecting GP9 to MISO"
print "jumper connecting GP7 to CSnB"
print "Multimeter connections (set your meter to read V DC):"
print "  connect black probe to GND"
print "  connect red probe to DA%d on J29" % channel
raw_input("When ready hit enter.\n")
percent=[0,25,75,100]
for p in percent:

    r = changeLevel(channel,1,p)
    print "Your meter should read about {0:.2f}V".format(p*2.048/100.0)
    raw_input("When ready hit enter.\n")

r = changeLevel(0,0,0)  # switch off channel A = 00010000 00000000 [16,0]
r = changeLevel(1,0,0)  # switch off channel B = 10010000 00000000 [144,0]

# The DAC is controlled by writing 2 bytes (16 bits) to it.
# So we need to write a 16 bit word to DAC
# bit 15 = channel, bit 14 = ignored, bit 13 =gain, bit 12 = shutdown, bits 11-4 data, bits 3-0 ignored
# You feed spidev a decimal number and it converts it to 8 bit binary
# each argument is a byte (8 bits), so we need two arguments, which together make 16 bits.
# that's what spidev sends to the DAC. If you need to delve further, have a look at the datasheet. :)

What do you think?

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2868
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: SPI not working

Sun Jan 27, 2013 8:08 pm

You've started a new thread on this minutes after I've posted a bugfix :lol:

The reason it was failing is that (with py-spidev) if the spi ports are used in the same session for general GPIO (not SPI) the spi drivers need to be reloaded. This came to light literally 3 hours ago in Texy's Nokia LCD thread.

If you find any documentation on spidev be sure and tell me. I was firing completely blind.

If it wasn't the spi issue, it might be a "different DAC" issue. Program was written for an MCP4802 (I had to look at the data sheet). I gather there may be boards in future with other DACs in. These won't have been tested and unless the manufacturers supply me with free boards I won't be testing them either.
Alex Eames RasPi.TV, RasP.iO

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2868
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: SPI not working

Sun Jan 27, 2013 8:49 pm

The other thing is in my Gertboard test suite my overarching aim, as it says in the README.txt file was to recreate the user experience from the originals.
All the Python versions are functionally as identical as I could make them to the
Gertboard test programs by Gert Jan van Loo & Myra VanInwegen
They might not be the same as if I had come up with them from scratch. But that was the brief I gave myself. It also makes good sense vis a vis the manual (and it forced me to learn a few tricks instead of fudging the on-screen behaviour with a "that'll do" attitude).

They're only supposed to be a starting point for people's creativity and I'm really glad to see you've got the bit between your teeth and are digging in to enjoy the interfacing experience.

I have got a tremendous amount out of having taken on this project and I'm very glad I did it.

Page 37 of the manual says...
Suggested tweaks to experiment with.
• None this time. If you change the numbers in the lists you will break the program (the voltage
output will no longer agree with the numbers that the test program prints out). If you want to
see how they are derived, you can look in the dad.py program in the dac_write function
Alex Eames RasPi.TV, RasP.iO

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2868
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: SPI not working

Sun Jan 27, 2013 9:03 pm

Just a sudden thought Alan.

Have you tried the C version of dtoa?

Is it performing as it should? That might help to eliminate hardware or software as the culpret (e.g. multimeter battery etc)
Alex Eames RasPi.TV, RasP.iO

Alan Johnstone
Posts: 45
Joined: Tue Jan 08, 2013 4:35 pm

Re: SPI not working

Mon Jan 28, 2013 9:11 am

It worked! I have spent hours looking for my stupid mistake.
Thanks a lot.
How did you find out what was required?
I find the Raspberry Pi really frustrating. Nothing seems to work as it should.No documentation.
How did you find the xref2(...) function if there is no documentation at all?

I understand the desire to make the test programs the same as before, but as they are supposed to be a jumping off point for beginners I think an overarching aim should be that they are understandable. The good point about the function I wrote is that it can be lifted as is and put into another program.

Alan

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2868
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: SPI not working

Mon Jan 28, 2013 9:51 am

Alan Johnstone wrote:It worked! I have spent hours looking for my stupid mistake.
Thanks a lot.
How did you find out what was required?
By following Texy's Nokia LCD thread in the Python section. It just so happens that Texy and bgreat have stumbled across the issue when implementing spi drivers for an LCD screen. I noticed it when I tested the new Gertboard this week, but it went away on reboot, so I didn't give it a second though (although I did mention it in the Gertboard software in Python thread).
Alan Johnstone wrote:I find the Raspberry Pi really frustrating. Nothing seems to work as it should.No documentation.
How did you find the xref2(...) function if there is no documentation at all?
Google :lol:
I believe I used a snippet of code from Jeremy Blythe's blog and messed about with it until I could make it work for my application. I think it might be helpful for you to read through the "Gertboard software in python" thread as I documented my progress quite thoroughly there. Also, I think I put some sources in one or two of the programs at the bottom (dtoa or atod?)
Alan Johnstone wrote: I understand the desire to make the test programs the same as before, but as they are supposed to be a jumping off point for beginners I think an overarching aim should be that they are understandable.
Yes I agree. And now that they're finished, they can be improved. I don't mind admitting (as it's the truth) that this was quite a stretch for me. It's a bit frustrating that you didn't come along earlier. :( But that's the way it works sometimes. For me, having worked really hard and thoroughly stretched myself (and really enjoyed it) you have to understand, that, now I feel like I've achieved something, it's a bit irksome to find that the people who could have been really helpful seemingly come along and shoot holes in it. (I realise that's not really what you're trying to do, and I want to improve the programs too).

The other thing is, how do you make something understandable to others when you barely understand it yourself? (i.e. the total lack of py-spidev documentation).

If you're a C programmer (which I think you said you were in the rev check thread) you might be able to find out something from looking at the py-spidev source code on github. I'm afraid it's gibberish to me. :D
Alan Johnstone wrote: The good point about the function I wrote is that it can be lifted as is and put into another program.
I may well use any code or suggestions that meet the aims. :D You might notice I've already used your dictionary suggestion in the leds-wp.py rev check function (and given you a credit in the README)

And there's absolutely nothing to stop you, me or anybody else writing some new and different programs - that's the idea really...

Code: Select all

while not bored:
    play_with_whats_provided()
    make_changes()
    break_it()
    fix_it()
    learn_things()
    do_new_things()
:lol:
Alex Eames RasPi.TV, RasP.iO

Alan Johnstone
Posts: 45
Joined: Tue Jan 08, 2013 4:35 pm

Re: SPI not working

Mon Jan 28, 2013 10:28 am

The frustrating thing is that the information is out there but almost inaccessable.
I would never have thought of looking in a thread about LCDs to find out about SPI.
Indeed looking through threads seems a very inefficient way of extracting information.
Your manual is the only thing that makes the Gertboard even remotely accessible.
( I apologise if there are other resources out there that I have not found.)

As a beginner it is hard enough to understand when the information is presented clearly let alone when it is dispersed through out the internet.

I have not looked at or used any of the c code. that is next on my list after I do it all in Python.

As a final thought one of the tweeks that you can make (easily with my function) is to set the gain digit. It works perfectly - up to 3.3V where it max's out as you would expect

Alan

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2868
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: SPI not working

Mon Jan 28, 2013 10:51 am

Alan Johnstone wrote:The frustrating thing is that the information is out there but almost inaccessable.
I would never have thought of looking in a thread about LCDs to find out about SPI.
Neither did I. It was a gift from God and the timing was perfect. :lol: I read a lot of threads on here and I've learnt an awful lot from people far cleverer and more knowledgeable than me. :)
Alan Johnstone wrote: Indeed looking through threads seems a very inefficient way of extracting information.
Your manual is the only thing that makes the Gertboard even remotely accessible.
( I apologise if there are other resources out there that I have not found.)
I only wrote the Python bits. Myra and Gert did the rest.
Alan Johnstone wrote: As a beginner it is hard enough to understand when the information is presented clearly let alone when it is dispersed through out the internet.
I was also a complete beginner to interfacing in September. I've spent a lot of time on this. :lol:
Alan Johnstone wrote:I have not looked at or used any of the c code. that is next on my list after I do it all in Python.

As a final thought one of the tweeks that you can make (easily with my function) is to set the gain digit. It works perfectly - up to 3.3V where it max's out as you would expect

Alan
Yes - I had a brief play with that at one point. In fact I wrote little python prog to generate a sine wave to test my new scope. It was painfully slow though ~ 2 Hz. Would need to bite the bullet and learn C to do something like that well. (But much easier to use hardware signal generator).
Alex Eames RasPi.TV, RasP.iO

Alan Johnstone
Posts: 45
Joined: Tue Jan 08, 2013 4:35 pm

Re: SPI not working

Fri Feb 01, 2013 11:42 am

Unfortunately the fix of reloading is not foolproof.
I ran the program several times and eventually it stops working.

A brief google suggests that it is inadvisable to use the PIPE command because it can block if there is too much output. I have not found a workaround apart from to remove the whole thing and not use GPIO on those pins which is hardly a good solution.

Alan

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2868
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: SPI not working

Fri Feb 01, 2013 11:45 am

Alan Johnstone wrote:Unfortunately the fix of reloading is not foolproof.
I ran the program several times and eventually it stops working.

A brief google suggests that it is inadvisable to use the PIPE command because it can block if there is too much output. I have not found a workaround apart from to remove the whole thing and not use GPIO on those pins which is hardly a good solution.

Alan
I've got someone who knows a lot more than me about this on the case Alan :) Hope to have a better solution soon. :D
Alex Eames RasPi.TV, RasP.iO

Return to “HATs and other add-ons”