12 Replies Latest reply on Jan 12, 2015 1:19 AM by howareyou

    Edison SPI: how to change SPI mode using spi.mode( ) in Python + Mraa?

    howareyou

      I am using Intel Edison mini breakout board and Python with Mraa API to  read/write an ADC.

      The ADC SPI interface is 1.8V compatible with CPOL=1 and CPHA=0.

      Therefore, I want to use spi.mode() method in MRAA to change the mode to MRAA_SPI_MODE2, which is CPOL=1 and CPHA=0.

      However, I failed to call spi.mode(2) because of argument type error. It seems that spi.mode(arg) need an argument of mraa_spi_mode_t Type.

      FYI:  spi.write(x) could send data on SPI bus and my oscilloscope shows that the clock signal SCLK is of CPOL=0 and CPHA=0 (MODE 0) as default.

      So, how could I change the SPI mode to MODE 2?

      Thanks in advance for your help!

       

      My Python is 2.7.3, Edison is running the original Intel Yocto build.

      >>> import mraa
      >>> mraa.getVersion()
      'v0.5.4-46-g74d172c'
      >>> spi = mraa.Spi(0)
      >>> spi.mode(2)
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
        File "/usr/lib/python2.7/site-packages/mraa.py", line 602, in mode
          """
      TypeError: in method 'Spi_mode', argument 2 of type 'mraa_spi_mode_t'
      >>>
      
      
      
        • 1. Re: Edison SPI: how to change SPI mode using spi.mode( ) in Python + Mraa?
          DiegoV_Intel

          Hi howareyou,

           

          I will suggest you to check these sites:

          1. mraa module — mraa v0.5.4-46-g74d172c documentation
          2. mraa: /var/lib/jenkins/workspace/mraa-doc/api/mraa/spi.h File Reference

           

          According to the second link, you should enter the modes for the SPI like the following:

           

          Argument (Mode)Description
          MRAA_SPI_MODE0CPOL = 0, CPHA = 0, Clock idle low, data is clocked in on rising edge, output data (change) on falling edge
          MRAA_SPI_MODE1CPOL = 0, CPHA = 1, Clock idle low, data is clocked in on falling edge, output data (change) on rising edge
          MRAA_SPI_MODE2CPOL = 1, CPHA = 0, Clock idle low, data is clocked in on falling edge, output data (change) on rising edge
          MRAA_SPI_MODE3CPOL = 1, CPHA = 1, Clock idle low, data is clocked in on rising, edge output data (change) on falling edge

           

          I've not tested it yet but I hope it has answered your question.

           

          Regards,

          Diego.

          • 2. Re: Edison SPI: how to change SPI mode using spi.mode( ) in Python + Mraa?
            howareyou

            Hi DiegoV_Intel,

             

                 Thanks for your help!

                 I tried to use MRAA_SPI_MODE2 as argument and I got the following error message of name 'MRAA_SPI_MODE2' is not defined.

                 FYI: I also tried to use C/C++ with MRAA and " spi->mode(MRAA_SPI_MODE2);  "  works fine in C/C++.

                 So, it sounds like "MRAA_SPI_MODE2" is not defined in mraa.pyc. 

                 I don't know how to define such a variable (of type mraa_spi_mode_t) in python.

             

             Python 2.7.3 (default, Oct  9 2014, 11:20:30) 
            [GCC 4.8.2] on linux2
            Type "help", "copyright", "credits" or "license" for more information.
            >>> import mraa
            /usr/lib/python2.7/site-packages/mraa.pyc
            >>> spi=mraa.Spi(0)
            >>> spi.mode(MRAA_SPI_MODE2)
            Traceback (most recent call last):
              File "<stdin>", line 1, in <module>
            NameError: name 'MRAA_SPI_MODE2' is not defined
            >>> 
            
            • 3. Re: Edison SPI: how to change SPI mode using spi.mode( ) in Python + Mraa?
              DiegoV_Intel

              Hi howareyou,

               

              Did you already try to update the mraa library? Please check the following thread, there is a guide about how to update the mraa library: Wish there was a Wiki and/or Sticky Posts and/or FAQ and/or ...

               

              Regards,

              Diego.

              • 4. Re: Edison SPI: how to change SPI mode using spi.mode( ) in Python + Mraa?
                howareyou

                Hi DiegoV,

                 

                    According to your suggestion, I upgraded the libmraa0 to its latest version today. New version has defined more attributes. However, I still didn't find mraa_spi_mode_t. I guess (maybe) current mraa version might not support this feature yet. 

                 

                Thanks for your help!!

                • 5. Re: Edison SPI: how to change SPI mode using spi.mode( ) in Python + Mraa?
                  Frederick Blais

                  @howareyou

                   

                  could you go in an interactive prompt and give me the outpupt of

                  import mraa

                  dir(mraa)

                  spi = mraa.Spi(0)

                  dir(spi)

                   

                  I would like to help but I'm away from my Edison at the moment

                  • 6. Re: Edison SPI: how to change SPI mode using spi.mode( ) in Python + Mraa?
                    howareyou

                    speccy88,

                     

                        Thank you for help!

                        Here are the outputs:

                     

                    Python 2.7.3 (default, Oct  9 2014, 11:20:30)
                    [GCC 4.8.2] on linux2
                    Type "help", "copyright", "credits" or "license" for more information.
                    >>> import mraa
                    >>> dir(mraa)
                    ['Aio', 'Aio_swigregister', 'DIR_IN', 'DIR_OUT', 'EDGE_BOTH', 'EDGE_FALLING', 'EDGE_NONE', 'EDGE_RISING', 'ERROR_FEATURE_NOT_IMPLEMENTED', 'ERROR_FEATURE_NOT_SUPPORTED', 
                     'ERROR_INVALID_HANDLE', 'ERROR_INVALID_PARAMETER', 'ERROR_INVALID_PLATFORM', 'ERROR_INVALID_QUEUE_TYPE', 'ERROR_INVALID_RESOURCE', 'ERROR_INVALID_VERBOSITY_LEVEL',
                    'ERROR_NO_DATA_AVAILABLE', 'ERROR_NO_RESOURCES', 'ERROR_PLATFORM_ALREADY_INITIALISED', 'ERROR_PLATFORM_NOT_INITIALISED', 'ERROR_UNSPECIFIED', 'Gpio', 'Gpio_swigregister',
                    'I2C_FAST', 'I2C_HIGH', 'I2C_STD', 'I2c', 'I2c_swigregister', 'INTEL_DE3815', 'INTEL_EDISON_FAB_C', 'INTEL_EDISON_GP109', 'INTEL_EDISON_GP110', 'INTEL_EDISON_GP111',
                    'INTEL_EDISON_GP114', 'INTEL_EDISON_GP115', 'INTEL_EDISON_GP12', 'INTEL_EDISON_GP128', 'INTEL_EDISON_GP129', 'INTEL_EDISON_GP13', 'INTEL_EDISON_GP130', 'INTEL_EDISON_GP131',
                    'INTEL_EDISON_GP134', 'INTEL_EDISON_GP135', 'INTEL_EDISON_GP14', 'INTEL_EDISON_GP15', 'INTEL_EDISON_GP165', 'INTEL_EDISON_GP182', 'INTEL_EDISON_GP183', 'INTEL_EDISON_GP19',
                    'INTEL_EDISON_GP20', 'INTEL_EDISON_GP27', 'INTEL_EDISON_GP28', 'INTEL_EDISON_GP40', 'INTEL_EDISON_GP41', 'INTEL_EDISON_GP42', 'INTEL_EDISON_GP43', 'INTEL_EDISON_GP44',
                    'INTEL_EDISON_GP45', 'INTEL_EDISON_GP46', 'INTEL_EDISON_GP47', 'INTEL_EDISON_GP48', 'INTEL_EDISON_GP49', 'INTEL_EDISON_GP77', 'INTEL_EDISON_GP78', 'INTEL_EDISON_GP79',
                    'INTEL_EDISON_GP80', 'INTEL_EDISON_GP81', 'INTEL_EDISON_GP82', 'INTEL_EDISON_GP83', 'INTEL_EDISON_GP84', 'INTEL_EDISON_MINIBOARD_J17_1', 'INTEL_EDISON_MINIBOARD_J17_10',
                    'INTEL_EDISON_MINIBOARD_J17_11', 'INTEL_EDISON_MINIBOARD_J17_12', 'INTEL_EDISON_MINIBOARD_J17_14', 'INTEL_EDISON_MINIBOARD_J17_5', 'INTEL_EDISON_MINIBOARD_J17_7',
                    'INTEL_EDISON_MINIBOARD_J17_8', 'INTEL_EDISON_MINIBOARD_J17_9', 'INTEL_EDISON_MINIBOARD_J18_1', 'INTEL_EDISON_MINIBOARD_J18_10', 'INTEL_EDISON_MINIBOARD_J18_11',
                    'INTEL_EDISON_MINIBOARD_J18_12', 'INTEL_EDISON_MINIBOARD_J18_13', 'INTEL_EDISON_MINIBOARD_J18_2', 'INTEL_EDISON_MINIBOARD_J18_6', 'INTEL_EDISON_MINIBOARD_J18_7',
                    'INTEL_EDISON_MINIBOARD_J18_8', 'INTEL_EDISON_MINIBOARD_J19_10', 'INTEL_EDISON_MINIBOARD_J19_11', 'INTEL_EDISON_MINIBOARD_J19_12', 'INTEL_EDISON_MINIBOARD_J19_13',
                    'INTEL_EDISON_MINIBOARD_J19_14', 'INTEL_EDISON_MINIBOARD_J19_4', 'INTEL_EDISON_MINIBOARD_J19_5', 'INTEL_EDISON_MINIBOARD_J19_6', 'INTEL_EDISON_MINIBOARD_J19_8',
                    'INTEL_EDISON_MINIBOARD_J19_9', 'INTEL_EDISON_MINIBOARD_J20_10', 'INTEL_EDISON_MINIBOARD_J20_11', 'INTEL_EDISON_MINIBOARD_J20_12', 'INTEL_EDISON_MINIBOARD_J20_13',
                    'INTEL_EDISON_MINIBOARD_J20_14', 'INTEL_EDISON_MINIBOARD_J20_3', 'INTEL_EDISON_MINIBOARD_J20_4', 'INTEL_EDISON_MINIBOARD_J20_5', 'INTEL_EDISON_MINIBOARD_J20_6',
                    'INTEL_EDISON_MINIBOARD_J20_7', 'INTEL_EDISON_MINIBOARD_J20_8', 'INTEL_EDISON_MINIBOARD_J20_9', 'INTEL_GALILEO_GEN1', 'INTEL_GALILEO_GEN2', 'INTEL_MINNOWBOARD_MAX',
                    'MODE_HIZ', 'MODE_PULLDOWN', 'MODE_PULLUP', 'MODE_STRONG', 'PIN_AIO', 'PIN_FAST_GPIO', 'PIN_GPIO', 'PIN_I2C', 'PIN_PWM', 'PIN_SPI', 'PIN_UART', 'PIN_VALID', 'Pwm',
                    'Pwm_swigregister', 'RASPBERRY_PI_B', 'SUCCESS', 'Spi', 'Spi_swigregister', 'UNKNOWN_PLATFORM', 'Uart', 'Uart_swigregister', '__builtins__', '__doc__', '__file__',
                    '__name__', '__package__', '_mraa', '_newclass', '_object', '_swig_getattr', '_swig_property', '_swig_repr', '_swig_setattr', '_swig_setattr_nondynamic', 'adcRawBits',
                    'adcSupportedBits', 'getPlatformName', 'getPlatformType', 'getVersion', 'pinModeTest', 'printError', 'setLogLevel', 'setPriority', 'uint8Array', 'uint8Array_frompointer',
                    'uint8Array_swigregister']
                    >>> spi = mraa.Spi(0)
                    >>> dir(spi)
                    ['__class__', '__del__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattr__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__',
                    '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__swig_destroy__', '__swig_getmethods__', '__swig_setmethods__', '__weakref__',
                    'bitPerWord', 'frequency', 'lsbmode', 'mode', 'this', 'write']
                    >>> mraa.getVersion()
                    'v0.5.4-64-g3efb7a0'
                    >>>
                    
                    • 7. Re: Edison SPI: how to change SPI mode using spi.mode( ) in Python + Mraa?
                      howareyou

                      speccy88

                       

                      Here is the output of dir(spi.mode):

                       

                      >>> dir(spi.mode)
                      ['__call__', '__class__', '__cmp__', '__delattr__', '__doc__', '__format__', '__func__', '__get__', '__getattribute__', '__hash__', '__init__', '__new__',
                      '__reduce__', '__reduce_ex__', '__repr__', '__self__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'im_class', 'im_func', 'im_self']
                      >>>
                      
                      • 8. Re: Edison SPI: how to change SPI mode using spi.mode( ) in Python + Mraa?
                        Frederick Blais

                        I took a couple minutes to take a look at this. I was having an older version of MRAA so I updated to v0.5.4.

                         

                        I'm under the impression that we won't be able to make "Spi.mode" to work for now.

                        In the source code ( mraa/spi.h at f277e7d35a311d139c86330b313f63e2a226681a · intel-iot-devkit/mraa · GitHub ) 'mraa_spi_mode_t' is an enum

                         

                        I sent an email to the person who wrote that code, let's see what he have to say

                        • 9. Re: Edison SPI: how to change SPI mode using spi.mode( ) in Python + Mraa?
                          Frederick Blais

                          howareyou Here is the reply to the mail I sent:

                           

                          Hello Frederick,

                           

                          Thanks for bringing this up. I have taken a look and there is currently

                          no way of accesing that enum in python/node. Ill create a patch later

                          today that addresses this.

                           

                          Thanks,

                          So it looks like your problem will be solved pretty soon

                          • 11. Re: Edison SPI: how to change SPI mode using spi.mode( ) in Python + Mraa?
                            Frederick Blais

                            howareyou

                             

                            Your version is : v0.5.4-64

                            I just installed the latest version : v0.5.4-66

                             

                            You can see the commit in the mraa repo : api/spi: Add Spi_Modes to CPP headers. ·  62c3264 · intel-iot-devkit/mraa · GitHub

                             

                            if you do a dir(mraa) you will find this :

                            'SPI_MODE0', 'SPI_MODE1', 'SPI_MODE2', 'SPI_MODE3'

                             

                            then you should be able to do

                            spi.mode(mraa.SPI_MODE2)

                             

                            Enjoy!

                             

                            ps:

                            thanks to Thomas Ingleby