NFC Breakout Board via SPI

Moderators: odroid, mdrjr

NFC Breakout Board via SPI

Unread postby timok » Thu Jan 29, 2015 5:39 pm

Im trying to get this Sunfounder NFC Breakout board running on the C1 using SPI Interface:
http://www.sunfounder.com/index.php?c=s ... &model=NFC

I tried using libnfc 1.7.1 and the Sunfounder library which can be found here:
https://www.adrive.com/public/ANUQAs/Su ... ode.tar.gz

On a Raspberry, both libraries work out of the box. But on the C1 I can't get it running. For the Sunfounder lib I also tried using the odroid version of wiringPi.

With a logic analyzer I can see, that there is a CS between each byte on the odroid. So I guess that this is the problem.

Would be nice if anyone could give me a hint, since this close to hardware things are really new to me :-)
Attachments
c1_spi_signal.png
C1 SPI single byte
c1_spi_signal.png (9.38 KiB) Viewed 2313 times
c1_spi_signals.png
C1 SPI signals
c1_spi_signals.png (9.49 KiB) Viewed 2313 times
c1_spi_data.png
C1 SPI data
c1_spi_data.png (55.3 KiB) Viewed 2313 times
timok
 
Posts: 11
Joined: Tue Jul 08, 2014 4:52 pm
languages_spoken: german,english
ODROIDs: U2, X2, C1

Re: NFC Breakout Board via SPI

Unread postby timok » Thu Jan 29, 2015 5:40 pm

Here is the SPI data recording from a raspberry
Attachments
read_fw.png
Raspberry SPI data
read_fw.png (65.28 KiB) Viewed 2312 times
timok
 
Posts: 11
Joined: Tue Jul 08, 2014 4:52 pm
languages_spoken: german,english
ODROIDs: U2, X2, C1

Re: NFC Breakout Board via SPI

Unread postby odroid » Thu Jan 29, 2015 6:32 pm

We will try the multiple byte sending and check the behavior of CS pin tomorrow.

Edit:
Please check your code... something should be wrong.

We've tested the sample source of kernel/Documentation/spi/spidev_test.c

Code: Select all
gcc –o spidev_test spidev_test
sudo modprobe spicc
sudo ./spidev_test –D /dev/spidev0.0 –b 8


Look at the CS and CLK signals.
wave_spi_cs_clk20140224_584029.jpg
wave_spi_cs_clk20140224_584029.jpg (358.75 KiB) Viewed 2289 times
User avatar
odroid
Site Admin
 
Posts: 27984
Joined: Fri Feb 22, 2013 11:14 pm
languages_spoken: English
ODROIDs: ODROID

Re: NFC Breakout Board via SPI

Unread postby timok » Thu Jan 29, 2015 10:43 pm

I tracked it down to the way, the Sunfolder NFC lib sends the data. It submits each byte separately via wiringPiSPI. And the spidev, in the background, handles the CS on its own and toggles it between each write operation.
So I thought I found a way to disable this behaviour for spidev by setting the mode SPI_NO_CS. But setting this mode always returns an error.

Code: Select all
  const static uint8_t     spiMode  = SPI_NO_CS ;
  .....
  if (ioctl (fd, SPI_IOC_WR_MODE, &spiMode)         < 0)
    return wiringPiFailure (WPI_ALMOST, "SPI Mode Change failure: %s\n", strerror (errno)) ;


Is there any other way to disable the CS for spidev, so that i can get the Sunfoulder library working?


In the libnfc source code, there is a nice comment about the SPI communication with the NFC chip PN532: https://code.google.com/p/libnfc/source ... _spi.c#379
Using this library the signals on SPI look fine, but the library fails because of timeouts. I'll have to take a look into that :shock:
timok
 
Posts: 11
Joined: Tue Jul 08, 2014 4:52 pm
languages_spoken: german,english
ODROIDs: U2, X2, C1

Re: NFC Breakout Board via SPI

Unread postby mlinuxguy » Fri Jan 30, 2015 9:17 am

I'm not sure about this but the odroid-c1 DTS lists this under SPI:
cs_gpios = "GPIOX_20", "GPIOX_21";
num_chipselect = <2>;

When I take my spi_loader program for the logiPI and try to modify it to say no CS I get the same
"inappropriate ioctl for device message"

I found this:
https://www.kernel.org/doc/htmldocs/dev ... aster.html
num_chipselect
chipselects are used to distinguish individual SPI slaves, and are numbered from zero to num_chipselects. each slave has a chipselect signal, but it's common that not every chipselect is connected to a slave.

Perhaps it could be set to zero for no chipselect?
mlinuxguy
 
Posts: 793
Joined: Thu Feb 28, 2013 10:28 am
languages_spoken: english
ODROIDs: X, X2, XU, XU3, XU4, C1, C1+, C2, N1, USB-IO

Re: NFC Breakout Board via SPI

Unread postby odroid » Fri Jan 30, 2015 11:10 am

We will consider to add the "SPI_NO_CS" option in the SPI driver soon.
It seems to be a correct direction.
User avatar
odroid
Site Admin
 
Posts: 27984
Joined: Fri Feb 22, 2013 11:14 pm
languages_spoken: English
ODROIDs: ODROID

Re: NFC Breakout Board via SPI

Unread postby Clx » Fri Jan 30, 2015 5:58 pm

odroid wrote:We will consider to add the "SPI_NO_CS" option in the SPI driver soon.
It seems to be a correct direction.


That's a right decision, a lot of devices require it to work properly.
Also, would you please make two spidev's devices by default? RPi has spidev0.0 and spidev0.1 and a lot of libraries and code use those. While not everyone uses ads, two spidevs seem the right choice.
Clx
 
Posts: 50
Joined: Fri Jan 16, 2015 9:23 pm
languages_spoken: english
ODROIDs: ODROID-C1

Re: NFC Breakout Board via SPI

Unread postby timok » Fri Jan 30, 2015 6:37 pm

Is it possible that SPI just works in MODE 1 for the moment? I used the source code of spidev_test to set the MODE 0 and it does not return any error, but it doesn't seem to have any effect, either.

Code: Select all
static uint8_t     spiMode = SPI_MODE_0 ;
....
  /*
   * spi mode
   */
  ret = ioctl(fd, SPI_IOC_WR_MODE, &spiMode);
  if (ret == -1)
    printf("can't set spi mode");

  ret = ioctl(fd, SPI_IOC_RD_MODE, &spiMode);
  if (ret == -1)
    printf("can't get spi mode");


If thats true, I think, it may be the cause why libnfc isn't working. The NFC chip PN532 just works in mode 0.

// Edit:
I tried the same source code on the Solidrun Hummingboard with the Ubuntu Image from Igor Pecovnik and it works as expected. Also libnfc works there.
timok
 
Posts: 11
Joined: Tue Jul 08, 2014 4:52 pm
languages_spoken: german,english
ODROIDs: U2, X2, C1

Re: NFC Breakout Board via SPI

Unread postby Clx » Thu Mar 05, 2015 8:06 pm

timok wrote:Is it possible that SPI just works in MODE 1 for the moment? I used the source code of spidev_test to set the MODE 0 and it does not return any error, but it doesn't seem to have any effect, either.

Code: Select all
static uint8_t     spiMode = SPI_MODE_0 ;
....
  /*
   * spi mode
   */
  ret = ioctl(fd, SPI_IOC_WR_MODE, &spiMode);
  if (ret == -1)
    printf("can't set spi mode");

  ret = ioctl(fd, SPI_IOC_RD_MODE, &spiMode);
  if (ret == -1)
    printf("can't get spi mode");


If thats true, I think, it may be the cause why libnfc isn't working. The NFC chip PN532 just works in mode 0.

// Edit:
I tried the same source code on the Solidrun Hummingboard with the Ubuntu Image from Igor Pecovnik and it works as expected. Also libnfc works there.


Have you had any success with changing the SPI mode?
I also stumbled upon this, even though IOCTL returns that the mode has been set, it does not actually change.
Clx
 
Posts: 50
Joined: Fri Jan 16, 2015 9:23 pm
languages_spoken: english
ODROIDs: ODROID-C1

Re: NFC Breakout Board via SPI

Unread postby timok » Thu Mar 05, 2015 10:18 pm

Not yet. I'm hoping to get any feedback from the hardkernel team when they have time to take another look into this :)
timok
 
Posts: 11
Joined: Tue Jul 08, 2014 4:52 pm
languages_spoken: german,english
ODROIDs: U2, X2, C1

Re: NFC Breakout Board via SPI

Unread postby kinsa » Wed Apr 08, 2015 3:18 pm

I tried fiddling with the registers and it seems the SPI module can only run on either MODE 1 or 3 (SPI_CPOL = 0, SPI_CPHA = 1 or SPI_CPOL = 1, SPI_CPHA = 1).

Setting bit[5] of the CONREG (0xc1108d88) register has no effect on the clock/data phase control.
42
kinsa
 
Posts: 40
Joined: Fri Sep 26, 2014 4:32 pm
languages_spoken: english
ODROIDs: U3 C1

Re: NFC Breakout Board via SPI

Unread postby kinsa » Mon Apr 13, 2015 12:57 pm

I've got all SPI MODES working now on my userspace SPI driver.

If SPI_CPOL==0 (modes 0 and 1), then the pull-down on the SPI_SCLK must be enabled.
If SPI_CPOL==1 (modes 2 and 3), then the pull-up on the SPI_SCLK must be enabled.

I don't know how to modify the kernel driver but a quick hack is just add the appropriate pull-up/down resistor to the SPI_SCLK pin (pin 23).

Also, the pull-down on the MOSI pin (pin 19) has to be enabled at all times. Just add a 10k resistor to GND on pin19 for a quick fix.

Cheers
42
kinsa
 
Posts: 40
Joined: Fri Sep 26, 2014 4:32 pm
languages_spoken: english
ODROIDs: U3 C1


Return to Hardware and peripherals

Who is online

Users browsing this forum: Bing [Bot] and 0 guests

cron