element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi
  • Products
  • More
Raspberry Pi
Raspberry Pi Forum Spidev problems in xfer2()
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi to participate - click to join for free!
Featured Articles
Announcing Pi
Technical Specifications
Raspberry Pi FAQs
Win a Pi
Raspberry Pi Wishlist
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 5 replies
  • Subscribers 675 subscribers
  • Views 3003 views
  • Users 0 members are here
  • python;
  • raspberypi
  • spidev
Related

Spidev problems in xfer2()

Former Member
Former Member over 9 years ago

Hi,

  I am trying to use the spi bus from Python on my Raspberry pi 2 model B and my python script just stops when I try to do an xfer2.

Background: 

  I have an MCP3008 hooked up to the spi bus

  When I do an  ls /dev  I see both spidev0.0 and spidev 0.1 so I know the bus is enabled.

  I have used the C spi bus loopback test that is out on the web and it runs successfully.

  I have installed the python wrapper for spidev.

  I am running Python 3

 

To debug the problem I simplified the code to the following (it is in a file called spi_testcode.py)

import sys

import spidev

print('Starting test code')

adc_channel = 7
print('initializing the spi bus instance')

spi_bus = spidev.SpiDev()

print('opening the spi bus')

spi_bus.open(0, 0) 
print('getting the adc output ')

adc_output = spi_bus.xfer2(1, 8 + adc_channel << 4, 0 )

print('adc read complete')

 

The following is what happens when I run the code:

 

pi@raspberrypi: ~ software $sudo python3 spi_testcode.py

starting test code

initializing the spi bus instance

opening the spi bus

getting the adc output

pi@raspberrypi: ~ software $

 

Notice that final print statement of the program is never executed.  The program exits during the xfer2() call with no error message or warning.   Consequently it is hard to figure out what to do to debug this....  any suggestions?

  • Sign in to reply
  • Cancel
  • clem57
    clem57 over 9 years ago

    Can you check the return codes? Maybe the spi bus is not opening and then your xfer2 hangs.

    Clem

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Former Member
    Former Member over 9 years ago in reply to clem57

    Clem,

    I am a newbe so forgive a basic question:  How do I check the return codes?

     

    I tried this:

    !

        import sys

        import spidev

        print('Starting test code')

        print('initializing the SPI bus instance')

        spi_bus = spidev.Spider()

        print('opening the spi bus')

        exit_code = spi_bus.open(0, 0)

        print ("exit code from open is", exit_code )

        print('getting the adc output ')

        adc_output = spi_bus.xfer2(1, 240, 0 )

        print('adc read complete')

    !

    When I run it I get:

        pi@raspberrypi:~/Software $ sudo python3 spi_testcode.py

        Starting test code

        initializing the SPI bus instance

        opening the spi bus

        exit code from open is None

        getting the adc output

        pi@raspberrypi:~/Software $

     

    The exit code of the open() was 'None'.  In python I believe that means there was no exit code.   Is there another way to check?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • clem57
    clem57 over 9 years ago in reply to Former Member
    import spidev
    import time
    spi = spidev.SpiDev()
    spi.open(0,0)
    while True:
       resp = spi.xfer2([0x00])
       print resp[0]
       time.sleep(1)

    I googled this from https://www.raspberrypi.org/forums/viewtopic.php?f=44&t=56669

     

    See how after the open, A truth statement will not execute unles Sspi.open works? I have not tried this, but it expresses the intent. Reading furtherer they say a permission problem. You may not be running this with root permission. On the raspberry Pi you cannot as a plain user affect the hardware. Linux tries to allow only root or users with root permissions(sudo) to do hardware things.

    Clem

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Former Member
    Former Member over 9 years ago in reply to clem57

    Clem,

      I don't see how the 'while True' could be affected by the previous open() statement.  'True' is a python keyword that is always 'true'

     

    However,  you still helped tremendously!!!  I tried the code in your reply and things started running more or less as you would expect... I got a print statement every second.   That tells me it was getting through the xfer2() code.    Hmmmmmm..... a clue!!!

     

    I looked closely at the code you suggested and noticed it was only passing one hex byte in brackets to xfer2():  xfer2([0x00])

    My code was passing 3 decimal bytes without brackets:

    xfer2(1,240,0)

     

    After playing with different combinations I discovered the brackets made the difference.  The following seems to work:

    xfer2([1,240,0])

     

    So.... xfer2() is looking for a list and I was passing 3 separate variables.

     

    Now the embarrassing thing:   The code examples on the web that I was copying had the brackets!!   I visually compared my code to the examples several time but never noticed that difference..... :-(.

     

    I caused the problem but it is too bad the spidev code did not flag the error better

     

    Problem solved.... now I am able to move forward with the build.

     

    Thanks!!

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • clem57
    clem57 over 9 years ago in reply to Former Member

    Glad to help you realize the situation. Please help me get points by marking this answer as correct

    Clem

    PS here is why:

    Anything in a bracket in Python is a list of items. Without the list means more parameters which the wrapper cannot expect. The bracketed list is just one parameter.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube