doceme / py-spidev

MIT License
461 stars 203 forks source link

How to add delay between tranfer #4

Closed droorzn closed 10 years ago

droorzn commented 11 years ago

i use your project to make a communication between Raspberry Pi (MASTER) and AVR (SLAVE). I noticed the data from raspberry is sucessfully sent but the data from AVR only the first communication success. Rest of the byte is read as what i send, i think AVR don't have enough time to to change SPI Data Register or SPDR before being read by the Raspberry. so i want to add a delay, i found the delay_usec in the spi.xfer but how i change it. thanks

droorzn commented 11 years ago

one more thing, how do i select slave with Slave Select (SS) or Chip Select (CS) in Raspberry Pi?

doceme commented 10 years ago

Sorry for the delayed response. I haven't had much need to make changes to this project lately.

I don't have an RPi, so I can't say for sure if it works, but you should be able to call xfer and pass extra parameters to specify a delay in microseconds between data blocks. For example:

spi = spidev.SpiDev() spi.xfer([0xde, 0xad, 0xbe, 0xef], 8000000, 1000, 8)

That code should set the speed to 8MHz with 1 millisecond delay and 8 bits per word. I haven't tested it, but feel free to give it a try. xfer2 should behave the same.

Chip select is handled outside the scope of this project. It's either a GPIO pin you have to toggle, or it's done in hardware and you just have to use the correct pin on your board.

dany0792 commented 5 years ago

Hi, I use your project(spidev 3.4) to make a communication between (personal development board) and (ASUS Tinker Board S). I use xfer2 function to read spi data on ASUS Board from my board. So i want to add a delay_usec in between blocks, I found the delay_usec in the spi.xfer2 but it is not working for me. I have tried several ways to add this delay, but I didn't succeed. One of my attempts I used is:

spi = spidev.SpiDev()
spi.open(2,0) 
spi.mode = 1
#spi.max_speed_hz = 250000
spiResp = spi.xfer2([0, 1, 2, 3], 250000, 1000, 8)

or:

#spiResp = spi.xfer2([1])
#spiData = range(spiResp[0])
#spiResp = spi.xfer2(spiData, 250000, 1000)

Please, help me with that ... Thank you!