Question

This is my program:

#include <stdio.h>

#define uart0 0x860
#define uart1 0x880

void send_char( int output )
{
    int nothing;
    //while write on uart1 is not ready, wait
    while(!((*(uart1+8) & 0x0040) && 0x0040)) //+8 eller +2? 2 byte (8 bitar)
    {
        nothing = 0;

    }
    *(uart1+4) = output; // +4 eller +1? en byte (4 bitar)
}

int rec_charx(void)
{
    if(!((*(uart1+8) & 0x0040) && 0x0040)) //+8 eller +2? 2 byte (8 bitar)
    {
        return *(uart1) & 0x000f;
    }
    else
    {
        return -1;
    }
}

The compiler complains:

expected ')' before 'output'

Why? How can I fix it? I don't understand the complaint.

Update

We've rewritten to program but we still get compilation errors:

#include <stdio.h>

static int* const uart1 = (int*) 0x880;

void send_char( int output )
{
    int nothing;
    //while write on uart1 is not ready, wait
    while(!((uart1[2] & 0x0040) && 0x0040)) //+8 eller +2? 2 byte (8 bitar)
    {
        //do nothing
        nothing = 0;

    }
    uart1[1] = output; // skriv till uart1
}

int rec_charx(void)
{
    if(!((*(uart1+8) & 0x0040) && 0x0040)) //+8 eller +2? 2 byte (8 bitar)
    {
        return *(uart1) & 0x000f;
    }
    else
    {
        return -1;
    }
}
Was it helpful?

Solution

You need to use constants instead of those defines:

#define uart0 0x860
#define uart1 0x880

like so:

const int * uart0 = 0x860;    
const int * uart1 = 0x868;

Also noted that incrementing a pointer of type int * by 1, moves it pointing to and address +sizeof(int*). So assuming an int being 4 bytes long, uart1 + 1 would point to address 0x884.

OTHER TIPS

#define uart0 (int*)0x860
#define uart1 (int*)0x880

Also have to change

while(!((*(uart1+2) & 0x0040) == 0x0040)) 
                 ^            ^^

But this definitely isn't the most beautiful way to access memory. The macro stuff works now, but embedded it to another expression may produce unexpected results or fail to compile.

(int*) xxx + y calculates the expression in the size of (int) i.e. 4.

Best way to fix your issue: read the documentation of your compiler. Your compiler probably comes with an header file that already defines a set of macros to access the UART hardware.

Otherwise here is how to declare your macros:

#include <stdint.h>

#define UART0 (*(volatile uint8_t *) 0x860)
#define UART1 (*(volatile uint8_t *) 0x880)

This assumes your UART registers are 8-bit. If they are 16 or 32-bit, change the uint8_t type in the macro definitions with the appropriate type.

You will need to adapt your code to use these macros.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top