Is uint16_t different from short unsigned int?

I'm trying to use the eeprom_write_word function in a library. My code looks roughly like this:

uint16_t      value = 12345;      // for example
uint16_t      address = 4;      // for example

eeprom_write_word(&address, value);

I get this error when I build a sketch that includes my library:

error: invalid conversion from 'short unsigned int*' to 'uint16_t*' initializing argument 1 of 'void eeprom_write_word(uint16_t*, uint16_t)'

I even tried making the type more explicit in my library with the following changes:

uint16_t*      addressPointer = &address;
eeprom_write_word(addressPointer, value);

I get the same error:

error: invalid conversion from 'short unsigned int*' to 'uint16_t*' initializing argument 1 of 'void eeprom_write_word(uint16_t*, uint16_t)'

What can I do to get rid of the error??

uint16_t is actually defined as “unsigned int”. I’m not sure why you’re second example is failing, but I don’t think it’s what you want anyways. You don’t want the address of the variable that has 4 in it, you want the number 4 interpreted as an address:

eeprom_write_word((uint16_t *)address, value);


The Gadget Shield: accelerometer, RGB LED, IR transmit/receive, light sensor, potentiometers, pushbuttons

Thanks for your suggestion. Changing my code to read:

eeprom_write_word((uint16_t *)address, value);

creates the same error.

The documentation for eeprom_write_word reads:

void eeprom_write_word (uint16_t * __p, uint16_t __value )

Write a word __value to EEPROM address __p.

(from - http://www.nongnu.org/avr-libc/user-manual/group__avr__eeprom.html#ga38e4426e45512adb5e33bf8eff20ab41 )

Doesn't that mean that the number I pass in __p is treated as an address?

Strange...that should work. Can you post the whole sketch?

Yes, the first parameter is interpreted as an address.

-- The Quick Shield: breakout all 28 pins to quick-connect terminals

You can go on and fiddle around with your code, or you can do it the proper way which also has the benefit of working:

uint16_t* const EEBaseAddr = (uint16_t*) 2; // Warning: those are words, so it's byte 4.
...
 eeprom_write_word (EEBaseAddr, value1);
...
 eeprom_update_word (EEBaseAddr + 1, value2);

Careful, if you use word addresses, they count words when incrementing, not bytes.

Korman

Strange…that should work. Can you post the whole sketch?

The sketch (Arduino IDE code) isn’t failing per se. The compiler is generating an error when it parses my .cpp file (which include the code sample I posted.) I can get the error just by adding the #include for my library, with no other code in the sketch.

You can go on and fiddle around with your code, or you can do it the proper way which also has the benefit of working:

uint16_t* const EEBaseAddr = (uint16_t*) 2; 

// Warning: those are words, so it’s byte 4.

eeprom_write_word (EEBaseAddr, value1);

eeprom_update_word (EEBaseAddr + 1, value2);

I’m more than happy to “do it the proper way,” but your example fails with the same error as my code and RuggedCircuits’ code.

Also, I get another error using eeprom_update_word:

‘eeprom_update_word’ was not declared in this scope

My library uses #include <avr/eeprom.h> Maybe I need to #include something else?

I'm more than happy to "do it the proper way," but your example fails with the same error as my code and RuggedCircuits' code.

Then the problem is definitely on your side, that code was copied out of a working program that compiles and works perfectly fine on Arduino version 0019 and 0021. You're doing something else wrong. With the little you've posted, it's not possible to tell.

Korman

Then the problem is definitely on your side,

You’re right.

I simplified my library code with the intention of posting it for study. After stripping out most of my code, the call to eeprom_write_word works, even with my original example.

One of my other headers (which uses uint16_t) seems to be the cause of the problem. I need to investigate further, but the problem is no longer the call to eeprom_write_word.

But… using eeprom_update_word still creates:

error: ‘eeprom_update_word’ was not declared in this scope

In which header is eeprom_update_word declared? I’m including <avr/eeprom.h>, which apparently doesn’t declare eeprom_update_word.

http://www.nongnu.org/avr-libc/user-manual/group__avr__eeprom.html#ga131cff4e1ae5fcdf5685cab524ea4553

From there I have it and I can certify it works as described. Your development environment is messed up in some way

Korman

From there I have it and I can certify it works as described. Your development environment is messed up in some way

The AVR toolset that comes with Arduino 0019 does not include that function. Do later versions of Arduino include a newer version of the AVR toolset?

It could also be that it's a difference between Linux and Windows. I was using this function with Version 0018. I never really bothered to check what might be working under Windows.

Korman