Embedded C - Read/Write Registers - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Embedded C - Read/Write Registers

RoboBobbleRoboBobble Posts: 29Member
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

Comments

  • LundinLundin Posts: 3,711Member
    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.
  • RoboBobbleRoboBobble Posts: 29Member
    : 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:

    [code]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 */

    }[/code]

    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!
  • LundinLundin Posts: 3,711Member
    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.
Sign In or Register to comment.