Jump to content
  • 0

Orange Pi Zero Kernel SPI & GPIO


Ribster
 Share

Question

Hey everybody.

I'm quite new to allwinner, my first experience was with the A13 (sun4i) from olimex.

I wrote a kernel driver for the sunxi kernel, which uses the spi and some GPIO.

It talks to an SPI RF chip and works just fine.

 

I want to port my kernel driver for the Orange Pi Zero, but have some issues.

My main question is: how do i access the fex file, how do i manipulate the gpio and spi ?

 

In the sun4i there are helper functions in the arch/arm/mach-sun4i/sys_config.c file.

#define PIN_DIR(handler,dir) gpio_set_one_pin_io_status(handler, dir, NULL)
#define PIN_SET(handler,val) gpio_write_one_pin_value(handler, val, NULL)
#define PIN_GET(handler) gpio_read_one_pin_value(handler, NULL)
I don't seem to get them in the sun8i sys_config.c file that is located in #include <mach/sys_config.h>
 
I normally get the gpio pins as such:
my_sx1272_data->pin_led = gpio_request_ex("sx1272_para", "pin_led");
And i manipulate the IO with these defines, that also use the functions from sys_config.
PIN_DIR(my_sx1272_data->pin_led, PIN_DIR_OUT);
PIN_SET(my_sx1272_data->pin_led, 0);
For the SPI i use this way of setup, i guess this will work, but i need to get rid of the sys_config implicit function compilation error first.
struct spi_board_info spi_device_info = {
    .modalias = "DURALINK",
    .max_speed_hz = 350*1000, //speed your device (slave) can handle
    .bus_num = 2,
    .chip_select = 0,
    .mode = 0,
};

      master = spi_busnum_to_master( spi_device_info.bus_num );
      if( !master ){
          printk(KERN_ERR "[LoRa] MASTER not found.\n");
              return -ENODEV;
      }

      // create a new slave device, given the master and device info
      spi_device = spi_new_device( master, &spi_device_info );

      if( !spi_device ) {
          printk(KERN_ERR "[LoRa] FAILED to create slave.\n");
          return -ENODEV;
      }

      spi_device->bits_per_word = 8;

      ret = spi_setup( spi_device );

    if( ret ){
        printk(KERN_ERR "[LoRa] FAILED to setup slave.\n");
        spi_unregister_device( spi_device );
        return -ENODEV;
    }
 
Currently using Linux OPI-3.4.113 3.4.113-sun8i #50 SMP PREEMPT Mon Nov 14 08:41:55 CET 2016 armv7l GNU/Linux
So it's the Debian armbian image 3.4.113.
 
Thanks in advance.
Link to comment
Share on other sites

11 answers to this question

Recommended Posts

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

  • 0

Yes, i've got the python blinky sketch working.

Changed the mapping.h for the correct io and was up and running.

 

i've tried a simple c file, but i've got some errors.

Once i get this up and running, i can port it to my kernel module...

 

my test.c

#include <stdlib.h>
#include <stdio.h>
#include "gpio_lib.h"

sunxi_gpio_init();
sunxi_gpio_set_cfgpin(SUNXI_GPA(17), SUNXI_GPIO_OUTPUT);
while(1) {
    sunxi_gpio_output(SUNXI_GPA(17), 1);
    sleep(1);
    sunxi_gpio_output(SUNXI_GPA(17), 0);
    sleep(1);
}

and the shell output

root@OPI-3:~/git/orangepi_PC_gpio_pyH3/pyA20/gpio# gcc -O test.c
test.c:5:1: warning: data definition has no type or storage class
 sunxi_gpio_init();
 ^
In file included from test.c:3:0:
gpio_lib.h:122:24: error: expected declaration specifiers or ‘...’ before ‘(’ token
 #define SUNXI_GPA(_nr) (SUNXI_GPIO_A_START + (_nr))
                        ^
test.c:6:23: note: in expansion of macro ‘SUNXI_GPA’
 sunxi_gpio_set_cfgpin(SUNXI_GPA(17), SUNXI_GPIO_OUTPUT);
                       ^
gpio_lib.h:143:27: error: expected declaration specifiers or ‘...’ before ‘(’ token
 #define SUNXI_GPIO_OUTPUT (1)
                           ^
test.c:6:38: note: in expansion of macro ‘SUNXI_GPIO_OUTPUT’
 sunxi_gpio_set_cfgpin(SUNXI_GPA(17), SUNXI_GPIO_OUTPUT);
                                      ^
test.c:7:1: error: expected identifier or ‘(’ before ‘while’
 while(1) {
 ^
Link to comment
Share on other sites

  • 0

So i'm stupid as fuck... Why did i not add the main function....

 

Got it working! For future reference:

 

test.c file:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "gpio_lib.h"

int main(){

int ret;

ret = sunxi_gpio_init();
ret = sunxi_gpio_set_cfgpin(SUNXI_GPA(17), SUNXI_GPIO_OUTPUT);

while(1) {
    sunxi_gpio_output(SUNXI_GPA(17), 1);
    usleep(100000);
    sunxi_gpio_output(SUNXI_GPA(17), 0);
    usleep(100000);
    sunxi_gpio_output(SUNXI_GPA(17), 1);
    usleep(100000);
    sunxi_gpio_output(SUNXI_GPA(17), 0);
    usleep(600000);
}

}

the command line compile:

gcc test.c gpio_lib.c -o test

Then just ./test and the red led will blink!!

 

Next step: get this working in the kernel driver...

Link to comment
Share on other sites

  • 0

I found the implementation here (It's for arduino but should work also on orangepi with some modifications):

https://github.com/RowlandTechnology/MULTIUART

 

or maybe to use MAX14830 (https://www.maximintegrated.com/en/products/interface/controllers-expanders/MAX14830.html) 4 uarts to SPI

http://www.spinics.net/lists/linux-serial/msg10232.html

 

I'm working on scientific project where we must acquire datas over 5 uarts. Each uart is connected to specific sensor (oxygen, temp, salinity, pressure, chlorophile,..)..

 

 

Link to comment
Share on other sites

  • 0

Hi, need some help.. I'am familar with Java but with c/c++ not :)

 

So.. have downloaded source from:  https://github.com/duxingkei33/orangepi_PC_gpio_pyH3  to my orangepi..

Than I have installed the library with python and all is ok..

 

On the remote PC I have setup Netbeans IDE and now want to include gpio_lib.h

The problem is that IDE can't find header files in directories listed below...

 

 

screenshot_2263.jpg

 

So.. what is the procedure to use orangepi_PC_gpio_pyH3 library?  Can I only copy/paste files to /usr/include folder on my OP?

 

 

 

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...