3

I'm working at this source: https://github.com/ozw1z5rd/ninepin/blob/master/iec/iec.c

int iec_init(void)
{
  int result;
  int minor;

  if ( request_mem_region(GPIO_BASE, 4096, LABEL_DEVICE) == NULL ) { 
      printk(KERN_ALERT "Unable to reserve I/O memory address");
      return -EBUSY;
  }      

  gpio = ioremap(GPIO_BASE, 4096);

And this code is part of a kernel module. It is supposed to be working but on my raspberry does not work. The area it attempts to reserve is not available. I'm new to raspberry and noticed that the GPIO, under the current Linux version, are available as /dev/gpiomem.

How can I change the code so that it can work again? What changes are required to the code so that gpio are allocate and made available to this module?

9
  • 1
    Is there a reason you aren't using the GPIO abstraction of the kernel? – Janka Nov 25 '19 at 18:28
  • 1
    @Janka: which abstraction? /dev/gpiomem ? I tried but how can I use it without using the open() function? – ozw1z5rd Nov 25 '19 at 18:39
  • 1
    You are doing gpio_request(), but you aren't using e.g. the gpio_get_value() and gpio_set_value() functions later on but manipulate the registers directly instead. – Janka Nov 25 '19 at 18:56
  • 1
    @janka sorry I did not understand it. Do I have to manipulate the register directly? – ozw1z5rd Nov 25 '19 at 22:02
  • 1
    No, you don't have to. Throw out your digitalRead(), digitalWrite() and pinMode() functions and replace them by gpio_get_value(), gpio_set_value(), gpio_direction_input() and gpio_direction_output(). You don't need your gpio.h and the request_mem_region() and ioremap() stuff then. – Janka Nov 25 '19 at 22:10
1

The correct answer was from Janka, I'm adding these info for reference to other people that will try to do the same thing with no success.

Following the Janka's directions I was able to get the job done. Well, my fault was pretty silly and was about the PIN's numbers: I was using the connector number but the correct value is the BCM pin.

I found this tool:

$ sudo apt install wiringpi

that package has the gpio command that can show a map of the pins, for example:

 +-----+-----+---------+------+---+---Pi 3B+-+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |
 |   2 |   8 |   SDA.1 |   IN | 1 |  3 || 4  |   |      | 5v      |     |     |
 |   3 |   9 |   SCL.1 |   IN | 1 |  5 || 6  |   |      | 0v      |     |     |
 |   4 |   7 | GPIO. 7 |   IN | 1 |  7 || 8  | 1 | ALT0 | TxD     | 15  | 14  |
 |     |     |      0v |      |   |  9 || 10 | 1 | ALT0 | RxD     | 16  | 15  |
 |  17 |   0 | GPIO. 0 |   IN | 0 | 11 || 12 | 0 | IN   | GPIO. 1 | 1   | 18  |
 |  27 |   2 | GPIO. 2 |   IN | 0 | 13 || 14 |   |      | 0v      |     |     |
 |  22 |   3 | GPIO. 3 |   IN | 0 | 15 || 16 | 0 | IN   | GPIO. 4 | 4   | 23  |
 |     |     |    3.3v |      |   | 17 || 18 | 0 | IN   | GPIO. 5 | 5   | 24  |
 |  10 |  12 |    MOSI |   IN | 0 | 19 || 20 |   |      | 0v      |     |     |
 |   9 |  13 |    MISO |   IN | 0 | 21 || 22 | 0 | IN   | GPIO. 6 | 6   | 25  |
 |  11 |  14 |    SCLK |   IN | 0 | 23 || 24 | 1 | IN   | CE0     | 10  | 8   |
 |     |     |      0v |      |   | 25 || 26 | 1 | IN   | CE1     | 11  | 7   |
 |   0 |  30 |   SDA.0 |   IN | 1 | 27 || 28 | 1 | IN   | SCL.0   | 31  | 1   |
 |   5 |  21 | GPIO.21 |   IN | 1 | 29 || 30 |   |      | 0v      |     |     |
 |   6 |  22 | GPIO.22 |   IN | 1 | 31 || 32 | 0 | IN   | GPIO.26 | 26  | 12  |
 |  13 |  23 | GPIO.23 |   IN | 0 | 33 || 34 |   |      | 0v      |     |     |
 |  19 |  24 | GPIO.24 |   IN | 1 | 35 || 36 | 0 | IN   | GPIO.27 | 27  | 16  |
 |  26 |  25 | GPIO.25 |   IN | 0 | 37 || 38 | 1 | OUT  | GPIO.28 | 28  | 20  |
 |     |     |      0v |      |   | 39 || 40 | 1 | OUT  | GPIO.29 | 29  | 21  |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+---Pi 3B+-+---+------+---------+-----+-----+

the correct number to use into the Linux's gpio interface are the one under the label BCM. So, if you want to read the GPIO.24 the number to use is not 24 but 19. I.E:

gpio_direction_input(19)
gpio_get_value(19)
1
  • Please accept the answer with a click on the tick on its left side. Only this will finish the question and will it not pop up again months for months. – Ingo Dec 11 '19 at 9:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.