Optimizing problems (volatile hardware register)

I have had a rather ugly problem caused due to the optimization.

I am reading data from a fifo. The read port is at address 0x8200 (read only access). It also has a read reset port. Its address is also 0x8200 (both xdata) (write onle access).

When I declare the varables for accessing the fifo like this:

xdata uchar fifo_data     _at_ 0x8200; xdata uchar fifo_rres     _at_ 0x8200;
It will result in a linker warning memory overlap. Thus I have decided to do it like this:
xdata uchar fifo_data     _at_ 0x8200;
#define fifo_rres fifo_data

In a routine I have programmed this code:
  fifo_rres = 0xff;                // reset fifo read
  jpg_adr = 1;                     // set jpg file address to the next address to be read
  fbuf_start = 0;                  // set buffer start pointer to zero
  fbuf_in = 1;                     // set buffer input pointer to next free byte in buffer
  fbuf_pos = 0;                    // set buffer read pointer to zero
  fbuf_startadr = 0;               // set buffer startadress to zero
  fbuf[0] = fifo_data;             // read first byte from jpg file

The result was, that fbuf[0] was always 0xff. Then I have read the disassembly and found that when I want to set fbuf[0], I don't access the fifo read register, but it is written with 0xff.

I think, this is matter of the compiler optimization. The compiler thinks, that when I write 0xff into some address and read it some lines later, it will still be 0xff.

How can I tell the compiler, that it is not the same? I didn't find anything in my C51 manual about that topic. Maybe I just didn't find it.

Parents
  • xdata uchar fifo_data     _at_ 0x8200; 
    xdata uchar fifo_rres     _at_ 0x8200;

    As far as the 'C' language is concerned, these are two separate definitions creating 2 distinct variables - so you will obviously get the Linker warning!

    An alternative might be to use a Union - not sure how that might affect the efficiency of access, though?

    Other options would be to do it in Assembler, or leave it to the Linker.

Reply
  • xdata uchar fifo_data     _at_ 0x8200; 
    xdata uchar fifo_rres     _at_ 0x8200;

    As far as the 'C' language is concerned, these are two separate definitions creating 2 distinct variables - so you will obviously get the Linker warning!

    An alternative might be to use a Union - not sure how that might affect the efficiency of access, though?

    Other options would be to do it in Assembler, or leave it to the Linker.

Children
More questions in this forum