This is the mail archive of the crossgcc@cygnus.com mailing list for the crossgcc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Direct I/O reads/writes from C programs




On Wed, 17 Mar 1999, Dave wrote:

> How does one directly access an I/O address from C?

With GNU C, you can use the inline assembly macro capability.  There are
versions of these macros included with Linux and I know the i386 port of
RTEMS includes some.  Here is one example:

#define i386_inport_byte( _port, _value ) \
   { register unsigned short __port  = _port; \
     register unsigned char  __value = 0; \
     \
     asm volatile ( "inb %1,%0" : "=a" (__value), "=d" (__port) \
                                : "0"   (__value), "1"  (__port) \
                  ); \
     _value = __value; \
   }

> For example if I want to write a char to port 0x80 (or read a char from a
> port) what is the statement? 

Assuming the above macro, you would do something like:

i386_outport_byte( 0x80, my_char );
i386_inport_byte( 0x80, my_char );

> How about memory mapped I/O? If one wants to directly access a specific
> memory address what are the statements? 

Others have shown how to do this directly in portable ANSI C.

> Is direct I/O and direct memory access part of ANSI C?

i386 IO and any DMA is not.

> I have an embedded X86 project that has to directly access the hardware. I
> would prefer doing it in C rather than ASM.

Careful use of inline assembly macros can go a long way to eliminating
assembly language.  The RTEMS and Linux sources have plenty of examples
of doing just this.

--joel
Joel Sherrill                    Director of Research & Development
joel@OARcorp.com                 On-Line Applications Research
Ask me about RTEMS: a free RTOS  Huntsville AL 35805
   Support Available             (256) 722-9985



_______________________________________________
New CrossGCC FAQ: http://www.objsw.com/CrossGCC
_______________________________________________
To remove yourself from the crossgcc list, send
mail to crossgcc-request@cygnus.com with the
text 'unsubscribe' (without the quotes) in the
body of the message.