SPI communication from userspace

Gergely Czuczy gergely.czuczy at harmless.hu
Tue Mar 7 20:00:09 UTC 2017



On 2017. 03. 07. 20:04, Oleksandr Tymoshenko wrote:
> Gergely Czuczy (gergely.czuczy at harmless.hu) wrote:
>> Hello,
>>
>> I would like to ask for some help, on how to communicate with an
>> Adafruit MAX31856 breakout board over the SPI bus under freebsd, on an
>> RPi3. As far as I can see, the spibus(4) driver is there, but there's no
>> driver for this chip, and I can't access the SPI calls from userspace
>> either. I could try to write a driver, however I don't have the
>> experience working with the FreeBSD kernel.
>>
>> SPI communication is quite simple, you send a single byte to the device,
>> and if the MSB was 1 it's a write, if it's 0 it's a read operation,
>> remaining 7 bits is the address. Then you either write a byte to the
>> address, or wait for the device to respond by the next cycle.
>>
>> Other difficulty is, the RPi3 provides 2 PINs for CS, and I have 3
>> devices, so I either have to use generic GPIO PINs, or de/multiplex it,
>> which might require a userspace call for device selection on the bus.
>>
>> So, how should I start? What would be a good way to do the
>> userspace-kernelspace communication? Also, I'm not sure whether I should
>> do a specific driver for this device, or a generic one, and writing the
>> specifics in userspace.
>>
>> Also, if you could point me to some documentation which would explain
>> how to work with the kernel, make a custom module, the possible
>> user/kernelspace communication possibilities, and how to call another
>> kernel module's functions, would be quite appreciated. Also, if someone
>> knows where could I find docs on the spibus driver, would be quite nice
>> since, it's just code so far.
> Hi Gregory,
>
> To access raw SPI from userland you can use spidev. Unfortunately
> functionality of this driver is limited and there is not much
> documentation around. There are plans to replace it with more
> versatile linux-like API but no actual implementation yet.
This is quite interesting, actually seems like the stuff I wanted to 
write. However, I've found spigen.c in the kernel source, but I couldn't 
find how to load it. There are no modules under /boot/kernel/ and 
kldload -v doesn't show it up either. Also, there are no /dev/spi* 
devices, however spibus is loaded:
# kldstat -v | grep spi
                 259 simplebus/bcm2835_spi
                 109 spi/spibus
                 108 spi/ofw_spibus

Could you please give me some help how to load it? Might be the stuff 
I'm actually looking for. Only question is, whether does it allows me to 
do a manual CS, instead of automatically using the dedicated pins.
>
> I put together some demos for spidev:
> https://github.com/gonzoua/freebsd-embedded-demos
>
> More info on these demos: https://kernelnomicon.org/?p=757
>
> As an alternative you can code custom driver that talks to SPI
> bus in kernel and exposes control to userland via sysctls.
>
> You can get some references on how to use in-kernel SPI API looking
> at mx25l flash driver:
> https://github.com/freebsd/freebsd/blob/master/sys/dev/flash/mx25l.c
>
> And use DS3231 driver as a reference for reporting temperature as
> sysctl:
> https://github.com/freebsd/freebsd/blob/master/sys/dev/iicbus/ds3231.c
>
> Actual implementation will depend on your requirements
Thank you, I will take a look at these as well.



More information about the freebsd-hackers mailing list