Pergunta

I was able to control GPIO using mmap system call to control LED operation directly from the user space. Now I want to implement driver in kernel space.

I am trying to write my first kernel space device driver for 16*2 line of LCD in Linux for ARM controller RPi. Now i need to access the GPIO for this purpose.

In AVR i use to access the Port like this.

#define PORTA  *(volatile unsigned char*)0x30

I was reading LLD it tells to use inb() & outb() function to access the i/o port.
http://www.makelinux.net/ldd3/chp-9-sect-2

1> Can we not use #define address of port to access the GPIO ?

2> What is the advantages to use use inb() & outb() functions for controlling the GPIO ?

Please suggest.

Foi útil?

Solução 2

1) the use of defines simplifies your task often. You could, of course, not use define for your port and use this construction literally everywhere you need to access the port. But then you will have to replace the 0x30 everywhere with another address if you change the design of your device, for example, if you decide to connect your LED to port B. Also, it will make your code less readable. Alternatively you could declare a function that will access your port. If such a simple function is declared inline (if your compiler supports inlines) then there is no difference in performance.

2) the advantage of using inb() and outb() is portability of your program. If this is not an issue, then it is fine to access your port directly.

Outras dicas

In AVR i use to access the Port like this.

#define PORTA  *(volatile unsigned char*)0x30

That's an improper definition that overloads the symbol PORTA.
Besides defining the port address as 0x30, you are also dereferencing that location.
So it is actually a read operation, but there's no indication of that in the name, i.e. you have really defined a macro for READ_PORTA.

1> Can we not use #define address of port to access the GPIO ?

Of course you can (and should).

 #define PORTA (unsigned char *)0x30

You'll find similar statements in header files for device registers in the Linux source tree. When developing a new device driver, I look for a header file of #defines for all of the device's registers and command codes, and start writing one if no file is already available.

2> What is the advantages to use use inb() & outb() functions for controlling the GPIO ?

The code is then an unambiguous statement that I/O is being performed, regardless of whether the architecture uses I/O ports or memory-mapped I/O.
Anyone reading the following should be able to deduce what is going on:

x = inb(PORTA);

versus the confusion when using your macro:

x = PORTA;

The above statement using an overloaded macro would not pass a code review conducted by competent coders.

You should also get familiar with and use the Linux kernel coding style.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top