Embedded C/C++

Moderators: Lundin
Number of threads: 199
Number of posts: 374

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Embedded C - Read/Write Registers Posted by RoboBobble on 9 Jul 2008 at 11:13 AM
I'm looking at example code from Diamond Systems made for their Prometheus PC/104 board, and I'm trying to figure out the proper C functions to read and write registers. From the sample code's included "manual" :


The example code is written in generic C using outp() and inp() functions. For users with compilers that don’t support these two functions, be sure to define them in the following format:

#define outp(x, y)  _outp(x, y)
#define inp(x)  _inp(x)

Be sure to define outp and inp to register read/write functions that your compiler does support.

Not really sure how to define outp() and inp() since compiling with those functions not properly declared didn't work. If anyone has any thoughts I'd greatly appreciate it.
Report
Re: Embedded C - Read/Write Registers Posted by Lundin on 9 Jul 2008 at 11:35 AM
Edited your post since the text placed within the code tags made it hard to read.

As for those functions, they are common non-standard C functions. The format is usually like this:

void outp (unsigned char* address, unsigned char data);

unsigned char inp (unsigned char* address);


So those functions are pretty easy to implement on most systems, you just read/write data to an address. Could as well write

*address = data;

The only kind of systems that will moan about that syntax is systems without direct memory access, ie Windows.
Report
Re: Embedded C - Read/Write Registers Posted by RoboBobble on 9 Jul 2008 at 12:33 PM
: Edited your post since the text placed within the code tags made it
: hard to read.
:
: As for those functions, they are common non-standard C functions.
: The format is usually like this:
:
: void outp (unsigned char* address, unsigned char data);
:
: unsigned char inp (unsigned char* address);
:
:
: So those functions are pretty easy to implement on most systems, you
: just read/write data to an address. Could as well write
:
: *address = data;
:
: The only kind of systems that will moan about that syntax is systems
: without direct memory access, ie Windows.

Thanks for the quick response, and the edit on my post for clarity.

I tried just using the functions within the program, but I get the errors "undefined reference to 'outp'" (or 'inp') If it helps, this is the sample code they give for it:

void PROM_AD_Sample()

{

	unsigned char channel;	/* AD channel to be sampled. Range 0-15 */

	unsigned char mode;		/* AD mode for the range, polarity, gain */

	unsigned char LSB;		/* Least Significant Byte. The 8 rightmost bits of 

								the 16 bit A/D code */

	unsigned char MSB;		/* Most Significant Byte. The 8 leftmost bits of

								the 16 bit A/D code */

	short Data;				/* The 16 bit AD sample resulting from the combination of 

								LSB and MSB. Data range (-32768)-32767. Refer to user manual

								on how to calculate voltage */



	outp(base, 0x10);							/* FIFO reset. Write 1 to RSTFIFO bit (bit 4).

													Does not affect any of the other bits */

	outp(base + 2, (channel << 4) | channel);	/* set A/D channel; Low = High */

	outp(base + 3, mode);						/* set A/D mode which determines the gain */

	while(inp(base + 3) & 0x20)					/* wait for WAIT bit (bit 5) = 0 */

		/*empty while*/;

	outp(base + 0, 0x80); 						/* start A/D conversion; set STRTAD bit (bit 7) to high */

	while(inp(base + 3) & 0x80)  				/* wait for A/D busy bit (bit 7) = 0 */

		/*empty while*/;

	LSB = inp(base + 0);						/* read data LSB */

	MSB = inp(base + 1);						/* read data MSB */

	Data = (MSB << 8) + LSB;					/* combine LSB + MSB */

}


Problem is my compiler will not recognize outp() or inp() without some form of definition, and I'm not really sure how to do that, exactly. Sorry, I'm a little new to programming on this level, as you can probably tell. I understand the prototypes in that reply, but I get the messages "passing arg 1 of 'outp' makes pointer from integer without a cast" and "undefined reference to 'outp'".

for the record, I'm compiling for BlueCat embedded Linux, using their "Luminosity" dev kit. I'd use a more friendly compiler if I could, but in compiling specifically for BlueCat it's my only real choice.

Thanks again for the help!
Report
Re: Embedded C - Read/Write Registers Posted by Lundin on 9 Jul 2008 at 1:33 PM
That likely means that the system is a 32-bit one. On 32-bit computers, the default int type will be 32 bit, and so will addresses/pointers. If you send char pointers to such functions you will get warnings about no typecasts.

Such systems usually wants 32 bit of data at a time... I don't know your particular system, so I can't tell.

What you need to know to write those functions yourself is roughly the following:

- What is the address bus of the system? (8/16/32/64 bit)
- Does the system support direct memory access?
- Does the system require memory alignment, ie do you have to write/read at even addresses?

All of this depends on the CPU really, though the OS could add further restrictions.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.