carldani
Posts: 17
Joined: Wed Jan 11, 2012 12:08 am

Re: I wrote a SPI driver, now what?

Fri Feb 10, 2012 6:22 pm

I sat down and reused some of Gert's example source code for GPIO access, and now I have a reasonably fast userspace bitbanging SPI driver which works on a selectable set of GPIOs. Admittedly the code is geared towards interfacing SPI flash EEPROMs, so if you have any dead mainboard (or dead graphics card) with a SPI flash chip, you might try out my code with that flash chip.

Any takers for a test on hardware? You should be able to compile C code and apply a patch, that's it.

carldani
Posts: 17
Joined: Wed Jan 11, 2012 12:08 am

Re: I wrote a SPI driver, now what?

Sun Feb 12, 2012 1:00 pm

By the way, what's the state of the in-kernel SPI driver for the Raspberry Pi? Is that driver already written or is that still one of the "the community will do it" tasks? I might be interested in writing such a driver as well.

I'd need someone to test the driver on real hardware, though.

dom
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5347
Joined: Wed Aug 17, 2011 7:41 pm
Location: Cambridge

Re: I wrote a SPI driver, now what?

Sun Feb 12, 2012 1:14 pm

carldani said:


By the way, what's the state of the in-kernel SPI driver for the Raspberry Pi? Is that driver already written or is that still one of the "the community will do it" tasks? I might be interested in writing such a driver as well.

I'd need someone to test the driver on real hardware, though.



That is a "community" task. Feel free to have a go.

User avatar
Gert van Loo
Posts: 2486
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: I wrote a SPI driver, now what?

Sun Feb 12, 2012 2:40 pm

Without a Pi you can't do much. You could put the code somewhere and ask if there is a pi-owner who would be willing to test it. (I can't as I don't have my board here. In fact Eben still has my board). Problem is that it requires the tester to connect it up to either another SPI (slave) interface or to a scope.

As we are on the subject of SPI:
I have written some SPI access code and tested it a while ago. I was planning to put that side-by-side with the GPIO code. The reason why I did not, was that the datasheet was imminent. (Turned out not to be as imminent as I first thought but it is out now. ) Of course that can not work on arbitrary GPIOs but is bound to the real SPI pins. Also it is not in the form of a real driver. Just C-code you have to call directly.

I did manage to exchange a single byte with a PIC with it and the signals look fine on my scope. That is all I can guarantee about it. I just found it on my lap top so if somebody still wants to see it, I can put it somewhere.

carldani
Posts: 17
Joined: Wed Jan 11, 2012 12:08 am

Re: I wrote a SPI driver, now what?

Sun Feb 12, 2012 10:03 pm

Gert: Sure, that would be cool. I can incorporate that into my userspace SPI driver as well (i.e. supporting native SPI on SPI pins and bitbanged SPI on GPIO pins), which would make testing the code easier than retrying the code in the kernel until it works.

User avatar
nick.mccloud
Posts: 804
Joined: Sat Feb 04, 2012 4:18 pm

Re: I wrote a SPI driver, now what?

Mon Feb 13, 2012 10:22 am

I too would like to get in on the SPI action - rather than reinventing the wheel, can the code produced to date be posted somewhere accessible please

User avatar
Gert van Loo
Posts: 2486
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: I wrote a SPI driver, now what?

Mon Feb 13, 2012 11:25 am

I still need a place to put it. I tried to paste it here preceded by HTML tags but it seems these wordpress windows do no follow the HTML rules very much. literal does not work, pre does not work (removes my new lines). Each time I put a few lines of the code in the HTML window and press update, it hangs and never recovers. Also if you paste in the plain text it removes all leading spaces.

User avatar
nick.mccloud
Posts: 804
Joined: Sat Feb 04, 2012 4:18 pm

Re: I wrote a SPI driver, now what?

Mon Feb 13, 2012 11:49 am

pastebin?

User avatar
Gert van Loo
Posts: 2486
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: I wrote a SPI driver, now what?

Mon Feb 13, 2012 12:13 pm

OK, here it is:

http://pastebin.com/6xSBYb2u

Valid for one month.

Just to preempt all you SW gurus: I know there will be lots of things I did wrong. Fine, I don't care. You don have to tell me because I will ignore all comments.

carldani
Posts: 17
Joined: Wed Jan 11, 2012 12:08 am

Re: I wrote a SPI driver, now what?

Mon Feb 13, 2012 9:41 pm

Awesome, thank you!

What license applies to your code, and may I reuse it in the flashrom project under the GPLv2?

carldani
Posts: 17
Joined: Wed Jan 11, 2012 12:08 am

Re: I wrote a SPI driver, now what?

Mon Feb 13, 2012 9:42 pm

Just a short question about the code: AFAICS you're using memory mapped I/O. I wonder why you're allocating gpio_mem and specify it as mmap parameter. Is that a programming trick I didn't know yet?

gpio_map = (unsigned char *)mmap((caddr_t)gpio_mem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, mem_fd, GPIO_BASE);

The other memory mapped I/O code I've written in the past simply used NULL as first mmap parameter and had no MAP_FIXED. I'm curious to learn more about your approach.

error404
Posts: 351
Joined: Wed Dec 21, 2011 11:49 pm

Re: I wrote a SPI driver, now what?

Tue Feb 14, 2012 9:43 pm

Can I just suggest that we put the kibosh on userspace SPI access now, before it gets out of hand? Or at the very least write a library. Let's use the proper interfaces folks... this also discourages people from running things as root with full system memory access.

carldani
Posts: 17
Joined: Wed Jan 11, 2012 12:08 am

Re: I wrote a SPI driver, now what?

Tue Feb 14, 2012 9:56 pm

I'll tackle writing a Linux kernel SPI driver once I have the hardware and once I know that the userspace driver works. I've written Linux kernel drivers in the past, but debugging them is a bit harder (load module, test, unload module) than debugging a userspace driver.

Fortunately the programming interface of the Raspberry Pi SPI controller is one of the simplest interfaces I ever saw and completing a kernel driver for it should take less than a day.

Wooloomooloo
Posts: 92
Joined: Fri Nov 25, 2011 10:52 am

Re: I wrote a SPI driver, now what?

Wed Feb 15, 2012 11:20 am

I'm somewhat curious about why the referenced example is assumed to output a byte ten times, and not a 32-bit (4 byte) word. This whole part is not very clear at all in the published spec (especially whether the SPI0 can output any other bit length than 32bit, as the aux SPIs seem to be able to do), but the pertinent section referring to the SPI FIFO register says:

31:0 DATA - DMA Mode (DMAEN set)
If TA is clear, the first 32-bit write to this register
will control SPIDLEN and SPICS. Subsequent
reads and writes will be taken as four-byte data
words to be read/written to the FIFOs
Poll/Interrupt Mode (DMAEN clear, TA set)
Writes to the register write bytes to TX FIFO.
Reads from register read bytes from the RX
FIFO

Granted, this seems to refer to the DMA mode, but no other mode is described there - does it work differently in non-DMA modes? Is there something we don't know (like what SPIDLEN / SPICS is, as they're not referred to anywhere else)?

User avatar
Gert van Loo
Posts: 2486
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: I wrote a SPI driver, now what?

Wed Feb 15, 2012 5:49 pm

carldani said:


Awesome, thank you!

What license applies to your code, and may I reuse it in the flashrom project under the GPLv2?



Sorry for late answer. Have been traveling and a bit very busy.

Is there a level above that? Sort of "Go ahead, do whatever you want, leave my name in or not, just don't sue me if it does not work." If no GPLV2 is OK with me.

User avatar
Gert van Loo
Posts: 2486
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: I wrote a SPI driver, now what?

Wed Feb 15, 2012 5:53 pm

carldani said:


Just a short question about the code: AFAICS you're using memory mapped I/O. I wonder why you're allocating gpio_mem and specify it as mmap parameter. Is that a programming trick I didn't know yet?

gpio_map = (unsigned char *)mmap((caddr_t)gpio_mem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, mem_fd, GPIO_BASE);

The other memory mapped I/O code I've written in the past simply used NULL as first mmap parameter and had no MAP_FIXED. I'm curious to learn more about your approach.



That part of the code was given to my by Dom. I have read up on mmap and the examples I have seen all allocate memory which they put in the first argument. As to the FIXED: I assumed the MAP_FIXED is in there to prevent the MMU from re-arranging or optimizing the memory map. It has to be absolutely that address only because there is hardware present. For details I have to ask Dom.

User avatar
Gert van Loo
Posts: 2486
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: I wrote a SPI driver, now what?

Wed Feb 15, 2012 6:01 pm

Wooloomooloo said:


I'm somewhat curious about why the referenced example is assumed to output a byte ten times, and not a 32-bit (4 byte) word. This whole part is not very clear at all in the published spec (especially whether the SPI0 can output any other bit length than 32bit, as the aux SPIs seem to be able to do), but the pertinent section referring to the SPI FIFO register says:

31:0 DATA - DMA Mode (DMAEN set)
If TA is clear, the first 32-bit write to this register
will control SPIDLEN and SPICS. Subsequent
reads and writes will be taken as four-byte data
words to be read/written to the FIFOs
Poll/Interrupt Mode (DMAEN clear, TA set)
Writes to the register write bytes to TX FIFO.
Reads from register read bytes from the RX
FIFO

Granted, this seems to refer to the DMA mode, but no other mode is described there - does it work differently in non-DMA modes? Is there something we don't know (like what SPIDLEN / SPICS is, as they're not referred to anywhere else)?



I wrote some simple code where I tried to send a byte the the external PIC. I must admit that the specification of the SPI interface is 'sub optimal' (Euphemism for badly written). I had to look at the port test code to get it working. I did not want to use DMA as it complicates the code and is not required if you just want to send a byte. In fact I added the "wait before asserting cs" code because I noticed the chip select being very, very short high on my scope.To send 16 or 32 bits you just pump 2 or 4 bytes in the FIFO before you wait for it to be ready. (I think)

It's a shame we do not have GPIO19 and 20 as that gives access to the second SPI core which I wrote so of course it is much, much better

Vitali_s
Posts: 8
Joined: Mon Feb 13, 2012 11:29 am

Re: I wrote a SPI driver, now what?

Wed Feb 15, 2012 6:13 pm

I have some Avr based hobby stuff and SPI looks like an ideal thing for that. Will SPI be supported from Python level?

Bakul Shah
Posts: 321
Joined: Sun Sep 25, 2011 1:25 am

Re: I wrote a SPI driver, now what?

Thu Feb 16, 2012 12:20 am

Gert said:


carldani said:


Just a short question about the code: AFAICS you're using memory mapped I/O. I wonder why you're allocating gpio_mem and specify it as mmap parameter. Is that a programming trick I didn't know yet?

gpio_map = (unsigned char *)mmap((caddr_t)gpio_mem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, mem_fd, GPIO_BASE);

The other memory mapped I/O code I've written in the past simply used NULL as first mmap parameter and had no MAP_FIXED. I'm curious to learn more about your approach.


That part of the code was given to my by Dom. I have read up on mmap and the examples I have seen all allocate memory which they put in the first argument. As to the FIXED: I assumed the MAP_FIXED is in there to prevent the MMU from re-arranging or optimizing the memory map. It has to be absolutely that address only because there is hardware present. For details I have to ask Dom.


MAP_FIXED has no bearing on the hardware address being mapped. If MAP_FIXED is not specified, the first arg to mmap() is considered just a hint. With MAP_FIXED the system will map to exactly the virtual address range specified or fail (useful when you share an address range with another process and want to store pointers into it).

Since we don't care where in the virtual space the hardware addresses are mapped, the first argument to mmap can be 0  (and skip the MAP_FIXED flag) and we can let the kernel worry about which virtual address to use . malloc() prior to mmap is not required and may in fact cause a slow leak with some mallocs or confuse them. mmap() will unmap any older mapping in any case prior to the new map.

mdamen
Posts: 3
Joined: Mon Apr 23, 2012 10:42 am
Contact: Website

Re: I wrote a SPI driver, now what?

Mon Apr 23, 2012 11:45 am

Any news on this? Now that the first PI's are out there, did anyone get the chance to test Gert's code?

benosteen
Posts: 28
Joined: Mon Jan 02, 2012 6:10 am

Re: I wrote a SPI driver, now what?

Tue Apr 24, 2012 10:10 pm

Love to test Gert's code, but the pastebin linked to early has expired.

Any pointers? Google has failed me so far.

User avatar
rew
Posts: 425
Joined: Fri Aug 26, 2011 3:25 pm

Re: I wrote a SPI driver, now what?

Wed Apr 25, 2012 8:20 am

I have a user-space level driver. Pretty  basic right now, just as a proof-of-concept of driving the hardware.

I have a kernel-space driver. Pretty basic, but it does not yet fit in with the SPI framework of the kernel. That's today's job.

It's difficult for me on this forum to find my old posts.
Check out our raspberry pi addons: https://www.bitwizard.nl/shop/

User avatar
csoutreach
Posts: 32
Joined: Mon Nov 28, 2011 1:06 pm
Contact: Website

Re: I wrote a SPI driver, now what?

Wed Apr 25, 2012 10:42 am

We've got an SPI driver http://piface.openlx.org.uk/ that we're testing today.
http://piface.openlx.org.uk/ Raspberry Pi IO Interface Board

User avatar
csoutreach
Posts: 32
Joined: Mon Nov 28, 2011 1:06 pm
Contact: Website

Re: I wrote a SPI driver, now what?

Wed Apr 25, 2012 10:42 am

http://piface.openlx.org.uk/ Raspberry Pi IO Interface Board

mdamen
Posts: 3
Joined: Mon Apr 23, 2012 10:42 am
Contact: Website

Re: I wrote a SPI driver, now what?

Wed Apr 25, 2012 11:15 am

csoutreach said:

Nice, I am looking forward to your results! Please keep us posted.

Finding posts on this forum is indeed very hard, Google works ok though!

Try: "spi site:raspberrypi.org" as search query.

Return to “Other projects”