confused
confused

Reputation: 761

C Why is passing the address of this volatile variable failing

I have what I thought was rather simple code, I have a volatile variable, data, that my i2c hardware will write a byte to when it comes in. So in my init function I have:

volatile unsigned char data;
data = 0x55;
i2c_write(I2C_ADDR, ENABLE, 1, &data);

The function prototype for the i2c_write function is:

void i2c_write(unsigned char dev_address, unsigned char reg_address, unsigned char len, volatile unsigned char *data);

This worked fine when data was unsigned char for both of them, but when I realized I forgot to make them volatile, I started getting the compiler message:

Description Resource Path Location Type

#169 argument of type "volatile unsigned char *" is incompatible with parameter of type "unsigned char *"

Now I made them both volatile unsigned char, I'm not sure why this doesn't work. I suspect maybe I'm about to learn that you can't do this with volatiles for some reason :) So what did I do wrong? This is for TI CC studio.

Upvotes: 0

Views: 537

Answers (1)

Jonathon Reinhart
Jonathon Reinhart

Reputation: 137398

I would write the code like this, without volatile on any of the I2C APIs, although I'm open to arguments indicating why this is wrong.

volatile unsigned char g_buf[100];

void i2c_read(void *buf, size_t len)
{
     // ...
}

void i2c_interrupt_handler(void)
{
    // i2c_read is going to write to the buffer,
    // so we can cast away the `volatile`
    i2c_read((void*)g_buf, 1); 
}

Upvotes: 2

Related Questions