0

I've been having troubles to use several readings using SPI on a raspberry pi 3B and I would like to get any solution, all my code is in python since everything then goes to a GUI

So I have three AD7793 modules in a project as well as an MCP3008 to make several readings, i need to do four analog readings with the MCP3008 and one reading for each AD7793, the issue is that the SPI0 as I know can only use 2 slaves, my inicial idea was to use the SPI0 for the MCP3008 and enable the SPI1 for the AD7793, but the AD7793 use a SPI MODE 3 and several forums said that the SPI1 does not work in 1 or 3 mode

Now I've seen that the spidev0 can use as CS any GPIO, I'm not sure if it can hold the four slaves, or it can hold at least three and the MCP3008 can go to the SPI1

I'm really not sure how to get this to work, my AD7793 is in a board to only use the SPI so can't use analog from it

here's the code I'm using to communicate to the AD7793

from time import sleep import math import spidev

STATUS_REG = [0x40] CONFIG_WRITE_REG = [0x10] CONFIG_READ_REG = [0x50] ID_REG = [0x60] MODE_WRITE_REG = [0x08] ADC_READ_REG =[0x58] IO_READ_REG = [0x68] IO_WRITE_REG = [0x28] RESET = [0xFF, 0XFF, 0XFF, 0XFF]

class ADC7793():

def __init__(self,spi,spi_channel):
    self.spi = spi
    spi.open(spi_channel,0)   # Using channel SPI channel 0 of the Raspberry pi , GPIO08 connected to Slave select
    spi.max_speed_hz = 1000000
    spi.mode = 0b11
    spi.bits_per_word = 8

def RST(self,RS):
    self.spi.writebytes(RESET)
    RS = self.spi.readbytes(1)
    return RS
  
def ID(self,ID2):
    self.spi.writebytes(ID_REG) 
    ID2 = self.spi.readbytes(1)
    return ID2

def STATUS(self,ST):
    self.spi.writebytes(STATUS_REG)
    ST = self.spi.readbytes(2)
    return ST

def SET_MODE(self,MD):
    self.spi.writebytes(MODE_WRITE_REG)
    self.spi.writebytes([MD[0]]) 
                         
    self.spi.writebytes([MD[1]]) 
    MD = self.spi.readbytes(2)
    return MD
    
def READ_CONFIG(self,CFR):
    self.spi.writebytes(CONFIG_READ_REG)
    CFR = self.spi.readbytes(2)
    return CFR

def WRITE_CONFIG(self,CFW):
    self.spi.writebytes(CONFIG_WRITE_REG)
    self.spi.writebytes([CFW[0]])
    self.spi.writebytes([CFW[1]]) 
    CFW = self.spi.readbytes(2)
    return CFW
    
def READ_IO(self,CFR):
    self.spi.writebytes(IO_READ_REG)
    CFR = self.spi.readbytes(1)
    return CFR

def WRITE_IO(self,IO):
    self.spi.writebytes(IO_WRITE_REG)
    self.spi.writebytes([IO[0]])
    CFW = self.spi.readbytes(1)
    return CFW
    
def READ_DATA(self,V):   
    self.spi.writebytes(ADC_READ_REG)
    value = self.spi.readbytes(3)
    return value

def round_up(self, n, decimals=0):
    multiplier = 10 ** decimals
    return math.ceil(n * multiplier) / multiplier

1 Answer 1

0

The kernel driver only supports 2 CS but in fact these are implemented in software (the hardware SPI CS are not used).

It may be possible to write your own overlay.

Alternatively you could use spi0-0cs and select the appropriate chip by asserting CS in your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.