1

Can anyone help me to connect more than 2 devices (other CE0 and CE1) and use any GPIO for the SDA pin?

  • Could you confirm you are talking about SPI? CE0 and CE1 are SPI but SDA is usually used as a term for I2C (SDA/SCL). – joan Aug 13 '15 at 6:45
2

How about using a daisy chain ?

Take a look at this pic.enter image description here

Hope it helps.

  • Do you have a reference to a working example of daisy-chaining with the RasPi SPI driver? Specifically, I would like to know whether the timing of the SS signal is handled by the driver, or you have to time it yourself. – crishoj Apr 24 '16 at 8:11
2

I'm going to assume you are talking about SPI rather than I2C (SDA is an I2C signal).

If by SDA you mean MISO or MOSI then you will have to bit bang the full SPI protocol. If it is just a typo and you mean SDA to mean slave select then do the following.

You can bit bang the SPI slave select signal but use the driver of your choice for the other signals. I.e. you decide which chip to enable but let the driver handle the tricky SCLK, MISO, and MOSI signals.

I give an example using my pigpio Python module.

You can achieve the same using any programming language and library of your choice.

#!/usr/bin/env python

import time

import pigpio # http://abyz.me.uk/rpi/pigpio/python.html

NO_SS=(7<<5) # Tell the SPI hardware not to set slave selects.

"""
cs_ad 18 pin
cs_da 22 pin
cs_ad 24 pin
cs_da 26 pin

           pin  pin
3V3         1    2      5V
0/2 (SDA)   3    4      5V
1/3 (SCL)   5    6      0V
4           7    8      14 (TXD)
0V          9   10      15 (RXD)
17 (ce1)   11   12      18 (ce0)
21/27      13   14      0V
22         15   16      23
3V3        17   18      24
10 (MOSI)  19   20      0V
9 (MISO)   21   22      25
11 (SCLK)  23   24      8 (CE0)
0V         25   26      7 (CE1)
           .......
0 (ID_SD)  27   28      1 (ID_SC)
5          29   30      0V
6          31   32      12
13         33   34      0V
19 (miso)  35   36      16 (ce2)
26         37   38      20 (mosi)
0V         39   40      21 (sclk)
"""

ADC_SS=[24, 8] # gpio numbering only
DAC_SS=[25, 7] # gpio numbering only

def xfer_adc(handle, device, data):
   # Set slave select line
   pi.write(ADC_SS[device], 0) # Select device
   result = pi.spi_xfer(handle, data)
   pi.write(ADC_SS[device], 1) # Deselect device
   return result

def xfer_dac(handle, device, data):
   # Set slave select line
   pi.write(DAC_SS[device], 0) # Select device
   result = pi.spi_xfer(handle, data)
   pi.write(DAC_SS[device], 1) # Deselect device
   return result

pi = pigpio.pi() # Connect to local Pi.

# Deselect all the SPI devices.

pi.write(ADC_SS[0], 1) # Deselect ADC 0
pi.write(ADC_SS[1], 1) # Deselect ADC 1
pi.write(DAC_SS[0], 1) # Deselect DAC 0
pi.write(DAC_SS[1], 1) # Deselect DAC 1

# As we are controlling slave selects we could just use one handle.
# I've used two in case we want different speeds for the ACD and DAC.
# Slow speed used for testing purposes.

adc = pi.spi_open(0, 10000, NO_SS)
dac = pi.spi_open(0, 10000, NO_SS)

start = time.time()

while (time.time()-start)<20:

   result = xfer_adc(adc, 0, [1, 2, 3])
   print(result)
   time.sleep(0.01)

   result = xfer_adc(adc, 1, [1, 2, 3])
   print(result)
   time.sleep(0.01)

   result = xfer_dac(dac, 0, [1, 2, 3])
   print(result)
   time.sleep(0.01)

   result = xfer_dac(dac, 1, [1, 2, 3])
   print(result)
   time.sleep(0.5)

pi.spi_close(adc)
pi.spi_close(dac)

pi.stop()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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