From owner-freebsd-hackers@freebsd.org Tue Mar 7 20:04:39 2017 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 25750D028F6 for ; Tue, 7 Mar 2017 20:04:39 +0000 (UTC) (envelope-from gonzo@bluezbox.com) Received: from id.bluezbox.com (id.bluezbox.com [45.55.20.155]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id EB2EC1C94 for ; Tue, 7 Mar 2017 20:04:38 +0000 (UTC) (envelope-from gonzo@bluezbox.com) Received: from [127.0.0.1] (helo=id.bluezbox.com) by id.bluezbox.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.87 (FreeBSD)) (envelope-from ) id 1clLLZ-000G5x-P5; Tue, 07 Mar 2017 12:04:38 -0800 Received: (from gonzo@localhost) by id.bluezbox.com (8.15.2/8.15.2/Submit) id v27K4bRi061872; Tue, 7 Mar 2017 12:04:37 -0800 (PST) (envelope-from gonzo@bluezbox.com) X-Authentication-Warning: id.bluezbox.com: gonzo set sender to gonzo@bluezbox.com using -f Date: Tue, 7 Mar 2017 12:04:36 -0800 From: Oleksandr Tymoshenko To: Gergely Czuczy Cc: freebsd-hackers@freebsd.org Subject: Re: SPI communication from userspace Message-ID: <20170307200436.GA61854@bluezbox.com> References: <313fdb93-92c6-609f-57c9-3dec0ce84798@harmless.hu> <20170307190406.GA61203@bluezbox.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Operating-System: FreeBSD/11.0-RELEASE-p2 (amd64) User-Agent: Mutt/1.6.1 (2016-04-27) X-Spam-Level: -- X-Spam-Report: Spam detection software, running on the system "id.bluezbox.com", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see The administrator of that system for details. Content preview: Gergely Czuczy (gergely.czuczy@harmless.hu) wrote: > > > On 2017. 03. 07. 20:04, Oleksandr Tymoshenko wrote: > > Gergely Czuczy (gergely.czuczy@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 [...] Content analysis details: (-2.9 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -1.0 ALL_TRUSTED Passed through trusted hosts only via SMTP 0.0 URIBL_BLOCKED ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [URIs: harmless.hu] -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 07 Mar 2017 20:04:39 -0000 Gergely Czuczy (gergely.czuczy@harmless.hu) wrote: > > > On 2017. 03. 07. 20:04, Oleksandr Tymoshenko wrote: > > Gergely Czuczy (gergely.czuczy@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. You will have to modify kernel config to add "device spigen" and recompile kernel. I will be adding module for spigen soon though. spigen API does not have neither CS control nor SPI mode control that's why there are plans to replace it with more flexible API. -- gonzo