tfr91
Posts: 2
Joined: Thu May 07, 2015 4:17 pm

Change SPI frequency

Thu May 07, 2015 4:22 pm

Hello RPi community,

i connected a hallsensor to my RPi2 via SPI. I am recieving data but i would like to change the frequency to 2Mhz to use some more features of the sensor. I dont use spidev, because i dont get the sensor working with it. Is there a possibility to change the SPI frequency manually in python?

Best regards

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Change SPI frequency

Thu May 07, 2015 5:15 pm

tfr91 wrote:...
i connected a hallsensor to my RPi2 via SPI. I am recieving data but i would like to change the frequency to 2Mhz to use some more features of the sensor. I dont use spidev, because i dont get the sensor working with it. Is there a possibility to change the SPI frequency manually in python?
...
Doesn't the software you are using to receive data have to set the speed?

tfr91
Posts: 2
Joined: Thu May 07, 2015 4:17 pm

Re: Change SPI frequency

Thu May 07, 2015 6:25 pm

Hey,
i am not 100% sure what you are thinking of.
I am writing and reading every bit of the MOSI/MISO message by myself.

Here is my code, maybe it helps you.

Code: Select all

import time
import RPi.GPIO as GPIO
import math



GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

def read(msgtype, SCLKPin, MOSIPin, MISOPin, CSPin, DELAY):
    

    if(msgtype == NOP):
        message = [0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0xD0, -141]
    else:
    
        message = [0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x93, -247]    
    
    #Fallende Flanke
    GPIO.output (CSPin, GPIO.HIGH)
    GPIO.output (CSPin, GPIO.LOW)
    GPIO.output (SCLKPin, GPIO.LOW)
    readValue = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]

    
    
    for k in range(8):
        sendbyte = message[k]    #jeweiliges byte der message
    
        
        #1 Byte Senden fuer Sensor
            
        for i in range(8):
            if(sendbyte & 0x80):    #Bit nr8 pruefen
                GPIO.output(MOSIPin, GPIO.HIGH)    #1schreiben
                #print(1)

                
            else:
                GPIO.output(MOSIPin, GPIO.LOW)
                
            
            
            GPIO.output(SCLKPin, GPIO.HIGH)
            
            sendbyte <<= 1 #Bitfolge verschieben damit naechstes Bit geprueft werden kann

        
            readValue[k] <<=1
            if(GPIO.input(MISOPin) == GPIO.HIGH):
                readValue[k] |= 0x01
                                                    
            GPIO.output(SCLKPin, GPIO.HIGH)
            
            GPIO.output(SCLKPin, GPIO.LOW)    
            
        

    
    GPIO.output(CSPin, GPIO.HIGH)
    time.sleep(DELAY)
    return readValue

def setupGPIO(SCLKPin, MOSIPin, MISOPin, CSPin):
    GPIO.setup(SCLKPin, GPIO.OUT)
    GPIO.setup(MOSIPin, GPIO.OUT)
    GPIO.setup(MISOPin, GPIO.IN)
    GPIO.setup(CSPin, GPIO.OUT)




SCLK = 11
MOSI = 10
MISO = 9
CS = 8
DELAY = 0
NOP = 1
XYZ = 2






setupGPIO(SCLK, MOSI, MISO, CS)

GPIO.output(SCLK, GPIO.HIGH)
nopmessage = read(NOP, SCLK, MOSI, MISO, CS, DELAY)
print(nopmessage)

try:

    while True:

  
        GPIO.output(SCLK, GPIO.HIGH)
        XYZMessage = read(XYZ, SCLK, MOSI, MISO, CS, DELAY)    

        
       except KeyboardInterrupt:

    print 'Beendet'

ghp
Posts: 1517
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Change SPI frequency

Thu May 07, 2015 6:55 pm

Hello,

think it will be difficult to achieve frequencies up to 2MHz with python code. There are reports in the net of up to 70kHz, which is optimistic if you want to make some decisions in between.

Would be better to head for spidev, and installing the necessary drivers.
- with raspi-config, enable SPI
- sudo apt-get install python-pip python-dev
- sudo pip install spidev


Regards,
Gerhard

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Change SPI frequency

Thu May 07, 2015 7:11 pm

Your code is setting the frequency by toggling SCLK. If you want a higher frequency then toggle SCLK faster.

Return to “Python”