(no subject)

Page 1.0 of 1.24
  • BG_Automation

    BG_Automation - 2019-11-30

    Suggested Edit:

    SPI Transfer

    You might have noticed the few lines above:

    aby[0] := 1; <<
    aby[1] := 16#80 + SHL(usiChannel AND 7, 4);
    aby[2] := 0;
    aby[3] := 0;

        IF NOT transfer(pabyTxBuffer:=ADR(aby) , pabyRxBuffer:=ADR(aby) , udiLen:=3 , uiDelayus:=0) THEN
            _iState := 1000;
        END_IF
    
     
    • Ingo

      Ingo - 2019-11-30

      Thanks for the report!

       
  • BG_Automation

    BG_Automation - 2019-12-31

    I am having trouble writing to the SPI bus.

    I have a python script I am trying to translate to codesys.

    SPI Master settings in codesys:

    Max Hz 500000
    bits per word is 8
    port mode is 1

    Python
    
       addr = 0
       cmd = 0x12 //Toggle Relay
       param1 = 1 //Relay 1
       param2 = 0
    
    GPIO.setmode(GPIO.BCM)
    RELAYbaseADDR=24
    ppFRAME = 25
    ppINT = 22
    GPIO.setup(ppFRAME,GPIO.OUT) 
    GPIO.output(ppFRAME,False)  #Initialize FRAME signa
    time.sleep(.001)            #let Pi-Plate reset SPI engine if necessary
    GPIO.setup(ppINT, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    spi = spidev.SpiDev()
    spi.open(0,1)   
    localPath=site.getsitepackages()[0]
    RPversion=1.1
    
    ppCMDr(addr,cmd,param1,param2,bytes2return):
        global RELAYbaseADDR
        arg = list(range(4))
        resp = []
        arg[0]=addr+RELAYbaseADDR;
        arg[1]=cmd;
        arg[2]=param1;
        arg[3]=param2;
        GPIO.output(ppFRAME,True)
        null=spi.xfer(arg,300000,60)
        #null = spi.writebytes(arg)
        if bytes2return>0:
            time.sleep(.0001)
            for i in range(0,bytes2return): 
                dummy=spi.xfer([00],500000,20)
                resp.append(dummy[0])
        time.sleep(.001)
        GPIO.output(ppFRAME,False)
        time.sleep(.001)
        return resp   
    
    
    def relayTOGGLE(addr,relay):
        VerifyADDR(addr)
        VerifyRELAY(relay)
        ppCMDr(addr,0x12,relay,0,0)   
    
    
    
    
    
       Codesys 
       (Inside Method AfterReadInputs)
          iAddress := 0
    
    
       ppFRAME := TRUE;
                        aby[0] := iAddress + Relay_Base_Address;
                        aby[1] := 16#12; //Toggle Relay
                        aby[2] := 1;
                        aby[3] := 0;
    
    
                     transferEXT(pabyTxBuffer:=ADR(aby) , pabyRxBuffer:=ADR(aby) , udiLen:=4 , uiDelayus:=60,udiSpeedHz := 300000);
    
    ppFRAME := FALSE;
    

    It seems to transfer with no errors, but nothing happens.
    If I run the code in python then everything works. I can toggle the relay on and off with the following code.

    import piplates.RELAYplate as RELAY
    import time
    RELAY.relayTOGGLE(0,1)

    Do I need to open the spi port? Is there something more that I need to do?

     
  • Ingo

    Ingo - 2020-01-01

    Your code looks good. I expect, that you are missing some init code. Did you already try "strace" on your python script?

    I would either try strace or a python debugger to get a better understanding of the python code.

    In CODESYS you can't do much wrong. Except that you might have the wrong device or frequency configured in the SPI master of your project.

    Especially the frequency can be important, as on some raspbian versions, the highest possible frequency was not limited correctly. Therefore you need to configure it in the SPI master device in your CODESYS project. (Took me some hours once ;) )

    But you don't need to open anything. This is done by the SPI master already.

     
  • BG_Automation

    BG_Automation - 2020-01-02

    I figured out the problem with communicating over the SPI bus.

    I though this line of code in Python [spi.open(0,1) ] meant open port 0 in mode 1. I found out that this actually means open port 0, device 1. In the SPI master device I needed to replace the value in the SPI Port parameter from '/dev/spidev0.0' with '/dev/spidev0.1'. Once I did this everything started working. I am moving on to complete the driver.

    Thanks for your help, confirming the code told me to look in a new direction.

     
    👍
    1
  • BG_Automation

    BG_Automation - 2020-01-03

    Thanks for confirming the code. Knowing the code is good forced me to look in a new direction. The python line of code [spi.open(0,1)] was the area I needed to focus on. I read some website that said spi.open(0,1) meant open port 0 in mode 1. Then I found out it meant open port 0 using device 1. I changed the settings in the SPI master on the in the device parameters. I put as spidev0.1 instead of spidev0.0. Then everything started to work.

    Thanks for the help!

     

Log in to post a comment.