dernasherbrezon / sx127x

Library to work with Semtech chips SX1276/SX1277/SX1278/SX1279.
Apache License 2.0
37 stars 7 forks source link

[request] configure chip select pin on Linux #5

Closed biemster closed 1 year ago

biemster commented 1 year ago

I have multiple devices on my SPI bus (on QCA9533), and need to assign a CS line to each. In the rpi example the code mentions spidev0.0 has this standard on GPIO8, where should I define the pin in my case? Or should I pull this pin up before running this code?

dernasherbrezon commented 1 year ago

It's on raspberry pi's pinout diagram:

Screenshot 2023-10-11 at 13 56 07
biemster commented 1 year ago

Thanks @dernasherbrezon for your response, but I'm afraid you did not understand my question. I'm working on non-rpi hardware: a QCA9533 based wifi repeater. There are multiple devices on my SPI bus, so I have to set the correct CS line to get the sx1278 to listen to the SPI bus.

I can of course pull down the pin before I start using your code, but it would be nice if the code handles this itself? Or is the CS pin already configurable in your code, and I just could not find it yet?

dernasherbrezon commented 1 year ago

Ah, I see. No sx127x doesn't deal with pins. It accepts only correctly configured spi device. Actually esp32 and linux correctly pull down CS line before transfer command and data. So you don't need to do anything specific.

biemster commented 1 year ago

When there is only one device on the bus right? If you have multiple, like in below figure: image one has to pull down the corresponding CS line of the device you want to talk to. I understand this is not supported yet in your code, could you give me a pointer where to add this myself? (I'll probably go the /dev/gpiochip0 route)

dernasherbrezon commented 1 year ago

This is supported by linux kernel. When it initialises "/dev/spidevX.X" it will map CS1 to "/dev/spidev0.0", CS2 to "/dev/spidev0.1" &etc. So when you use linux API:

#include <linux/gpio.h>
#include <linux/spi/spidev.h>

Linux kernel will control what application is using the controller and allow only sequential access to it. Then based on the accessed device, linux kernel will pull low corresponding CS line before transfer.

biemster commented 1 year ago

Ah, did not know that! Thanks for that info! So I guess I have to set which gpio I want to use as CS line in the DTS? Anyway thanks for clearing this up for me, I'll figure that out myself. Great project btw!