Getting SPI on RockPI 4 to work as spidev


p-i-u.de
 Share

0

Recommended Posts

Hi,

 

i really tried to get SPI as spidev to work on my RockPI 4A (Rev. 1.4),  using armbianEnv.txt and DT overlays however nothing works as it seems it is just ignored. So does anybody have an idea of how to get SPI working in spidev mode? Because I have 

board which I want to connect via SPI.

 

 

Link to post
Share on other sites

Armbian is a community driven open source project. Do you like to contribute your code?

2 hours ago, p-i-u.de said:

using armbianEnv.txt and DT overlays

You not only need to add overlays to /boot/armbianEnv.txt, but also parameters, check /boot/dtb/rockchip/overlay/README.rockchip-overlays :

### spi-spidev

Activates SPIdev device node (/dev/spidevX.Y) for userspace SPI access,
where X is the bus number and Y is the CS number

SPI 0 pins (MOSI, MISO, SCK, CS): GPIO3_A5, GPIO3_A4, GPIO3_A6, GPIO3_A7
SPI 1 pins (MOSI, MISO, SCK, CS): GPIO1_A7, GPIO1_B0, GPIO1_B1, GPIO1_B2
SPI 2 pins (MOSI, MISO, SCK, CS): GPIO1_C0, GPIO1_B7, GPIO1_C1, GPIO1_C2
SPI 3 pins (MOSI, MISO, SCK, CS): GPIO2_B2, GPIO2_B1, GPIO2_B3, GPIO2_B4

Parameters:

param_spidev_spi_bus (int)
	SPI bus to activate SPIdev support on
	Required
	Supported values: 0, 1

param_spidev_spi_cs (int)
	SPI chip select number
	Optional
	Default: 0
	Supported values: 0, 1
	Using chip select 1 requires using "spi-add-cs1" overlay

param_spidev_max_freq (int)
	Maximum SPIdev frequency
	Optional
	Default: 1000000
	Range: 3000 - 100000000

 

 

Link to post
Share on other sites

Somehow the README.rockchip-overlays seems to be lost on my system. Does this also work on stable 4.4 or just in Dev Branch?

Currently I'm on stable 4.4 branch. So if this is just in Dev is there any timeframe it is expected to get into stable?

 

Otherwise great it works, as I really thought this is just not working

 

 

Link to post
Share on other sites

On 11/11/2019 at 10:23 PM, martinayotte said:

You not only need to add overlays to /boot/armbianEnv.txt, but also parameters, check /boot/dtb/rockchip/overlay/README.rockchip-overlays :



param_spidev_spi_cs (int)
	SPI chip select number
	Optional
	Default: 0
	Supported values: 0, 1
	Using chip select 1 requires using "spi-add-cs1" overlay

 

 

 

 

I'm using the DEV branch right now and the overlay spi-add-cs1 is missing, do you have any idea how to get the information to create that overlay for RockPI? Furthermore I looked into rk3399-rock-pi-4.dts from there I see that CS1 is only in the section for SPI0, however when I use SPI0 I have no ethernet device anymore, so I thought I use SPI2 and CS1. Currently I'm not sure if this will work at all. Do you know if SPI2 and CS1 will work and how to get it to work? 

 

I connected my board which has definatley worked on Raspberry Pi before but I get no output

 

 

Link to post
Share on other sites

4 hours ago, martinayotte said:

This is things on the TODO list ...

BTW, what is your hat board ? Do you have schematic for it ?

 

I'm using this board

 

https://widgetlords.com/products/pi-spi-8ai-raspberry-pi-analog-input-interface

 

the Pi-SPi-8AI  4-20 mA Variant, till now I've not found any way to get it working. Do you have any hints how to make the correct overlay or where to start?

That is the overlay what they use for Raspberry PI https://github.com/widgetlords/libwidgetlords/blob/master/overlays/pi-spi.dts however I was not able to translate it to rk3399.

 

Note I have an older Variant with 2 Jumpers to select CE1 and 22

Link to post
Share on other sites

1 hour ago, p-i-u.de said:

Do you have any hints how to make the correct overlay or where to start?

With a quick look, it seems to be difficult to achieve : CE_1/pin26 is the default, but on RockPi, it is ADC_IN0, so unusable ...

You will need to to some of the CS_Spare by moving jumper. But I'm not even sure if RK3399 support SPI "cs-gpios", I will have to try to figure out.

Second option is to not connect that board directly to RockPi header, but create an adaptor which will re-route the SPI to use SPI2 available in RockPi at pins GPIO2_B1 to GPIO2_B4.

Link to post
Share on other sites

13 minutes ago, martinayotte said:

With a quick look, it seems to be difficult to achieve : CE_1/pin26 is the default, but on RockPi, it is ADC_IN0, so unusable ...

You will need to to some of the CS_Spare by moving jumper. But I'm not even sure if RK3399 support SPI "cs-gpios", I will have to try to figure out.

Second option is to not connect that board directly to RockPi header, but create an adaptor which will re-route the SPI to use SPI2 available in RockPi at pins GPIO2_B1 to GPIO2_B4.

I already used jumper cables and tried to exacty do that which also doesn't work, as far as my understanding of the Chip Select = 1 is, that CS = 1 means active high getting to low and CS = 0 being active low, so it seems without spi-add-cs1 overlay I am quite lost on this

 

BTW, could it be that the uart4 overlay is also not yet functioning? as I get Input/Output error on ttyS4

Link to post
Share on other sites

2 hours ago, p-i-u.de said:

I already used jumper cables and tried to exacty do that

GPIO2_B1 = SPI2_MISO

GPIO2_B2 = SPI2_MOSI

GPIO2_B3 = SPI2_CLK

GPIO2_B4 = SPI2_CS0

So, you don't need spi-add-cs1 overlay in such case.

2 hours ago, p-i-u.de said:

BTW, could it be that the uart4 overlay is also not yet functioning? as I get Input/Output error on ttyS4

The UART4 overlay is working fine on both my RockPro64 and RockPi. Check "dmesg | grep tty" to see if it became active. You can also use a looback wire to test it.

 

EDIT: I've just tested UART4 overlay also on my NanoPiM4 ...

Link to post
Share on other sites

1 hour ago, martinayotte said:

The UART4 overlay is working fine on both my RockPro64 and RockPi. Check "dmesg | grep tty" to see if it became active. You can also use a looback wire to test it.

Yes, that's what I've done to see if it's active after I got the Input/Output error and it was not there only ttyS0, ttyS2

Regarding SPI, thanx I'll try that above once again, maybe the error lies somewhere else, as i had cabling like that already before but get always "Bad address" however /dev/spidev2.0 is there

Link to post
Share on other sites

14 hours ago, p-i-u.de said:

Yes, that's what I've done to see if it's active after I got the Input/Output error and it was not there only ttyS0, ttyS2

How did you specified UART4 overlay in /boot/armbianEnv.txt ? It should be :

overlays=uart4

EDIT: Also, "uart4" should be placed AFTER any spi-spidev, since this UART sharing pins with SPI.

Link to post
Share on other sites

8 hours ago, martinayotte said:

overlays=uart4

How did you specified UART4 overlay in /boot/armbianEnv.txt ? It should be :

EDIT: Also, "uart4" should be placed AFTER any spi-spidev, since this UART sharing pins with SPI.

 

That part with AFTER seems an important one, however here comes the sad story short in hope that maybe at least another one who has this specific setup reads this post and finds this helpful. So the reason why i was always getting "Failed to perform SPI transfer": Bad Address had nothing to do with hardware but with the fact that we are on aarch64 on Rock PI and simply the libwidgetlords libraries where obiously not tested under 64bit as they used (uint32_t) instead of (unsigned long) in their code.

 

So everybody who has a RockPI 4 and Pi-SPi-8AI Raspberry Pi Analog Input (4 - 20 mA) Interface follow this to get it to work

Note you currently have to use the Armbian DEV branch so that overlays work

 

put this in /boot/armbianEnv.txt (Note I also enabled uart4 as I use this for another device), you cannot use the 40 gpio to 26gpio Ribbon cable as we use SPI2 instead of SPI0 so you have to connect the board using jumper cables. So connect PIN 26 of the 8AI Board to PIN 33 of the RockPI,  Pin 19 to 29 on RockPI,  Pin 21 to 31 on RockPI  and Pin 23 to 7 on RockPI

 

For those of you who wonder how to connect the sensors to this board, using 2 wire sensors INPUT (voltage supply) goes in the V+ Terminal and the Output from the sensor goes into A1 - A8

overlays=spi-spidev uart4
param_spidev_spi_bus=2

 

Clone the git repository 

git clone https://github.com/widgetlords/libwidgetlords.git

change includes (don't know if that's necessary but these are the ones used in spidev_test.c program by Linus Torvalds) and the following in src/spi.c 

#include <errno.h>
#include <fcntl.h>
#include <linux/spi/spidev.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <linux/ioctl.h>
#include <unistd.h>

from this

static const char prefix[] = "/dev/spidev0.";
	spi.tx_buf        = (uint32_t)data;
	spi.rx_buf        = (uint32_t)data;

to this

static const char prefix[] = "/dev/spidev2.";
	spi.tx_buf        = (unsigned long)data;
	spi.rx_buf        = (unsigned long)data;

 

change SPI_8AI to 0 because we use /dev/spidev2.0 not /dev/spidev2.1 include/pi_spi.h

#define SPI_8AI     0

Please find also attached a Java Implementation using MRAA to get access to this board (then there is in fact no need anymore for the libwidgetloard libraries at all)

 

pi_spi.h spi.c

 

SensorMRAA.java

Link to post
Share on other sites

On 11/19/2019 at 11:54 AM, richardk said:

Instead of (unsigned long) you might choose (intptr_t); specifically, that's the C data type for "an int the same size as a pointer".

 

Thanx for the tip, I've used what Linus Torvalds used in his spidev_test.c program so I thought that is the correct way (I'm not a C Coder)

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

0