Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

int not write or read properly #2

Closed
hyperion11 opened this issue Dec 26, 2017 · 11 comments
Closed

int not write or read properly #2

hyperion11 opened this issue Dec 26, 2017 · 11 comments

Comments

@hyperion11
Copy link
Contributor

hello. My hands got to DS3231.
I try read and write to eeprom. Float and char work fine. Strange, but int - not.
void setup() {
int inttmp = 32101;
float floattmp = 3.14159;
char chartmp = 'A';
if (!rtc.eeprom_write(0, inttmp)) {
Serial.println("Failed to store INT");
}
if (!rtc.eeprom_write(4, floattmp)) {
Serial.println("Failed to store FLOAT");
}
if (!rtc.eeprom_write(8, chartmp)) {
Serial.println("Failed to store CHAR");
}
nttmp = 0;
floattmp = 0;
chartmp = 0;
Serial.print("int: ");
rtc.eeprom_read(0, &inttmp);
Serial.print(inttmp);
Serial.print("; float: ");
rtc.eeprom_read(4, &floattmp);
Serial.print(floattmp);
Serial.print("; char: ");
rtc.eeprom_read(8, &chartmp);
Serial.print(chartmp);
Serial.println();

receive this
int: 65535; float: 3.14; char: A

@hyperion11
Copy link
Contributor Author

fixed by PR #3

@Naguissa
Copy link
Owner

Ues, maybe deleting the define and writing a note on README and examples is better option. I'll merge changes and update all ASAP, today was holiday in Catalonia.

@Naguissa
Copy link
Owner

P.D.: I'll test on Maple, as I have a board here...

Naguissa added a commit that referenced this issue Dec 28, 2017
Removed old code for STM32 that seems not be needed anymore on new Arduino_STM32 versions.
@Naguissa
Copy link
Owner

Tested with new release, 4.0.2:

Serial OK
Board: STM32
int: 65535; float: 3.14; char: A
RTC DateTime: 15/5/2 16:42:19 DOW: 6 ---- 0: 101
RTC DateTime: 15/5/2 16:42:20 DOW: 6 ---- 1: 125
RTC DateTime: 15/5/2 16:42:21 DOW: 6 ---- 2: 0
RTC DateTime: 15/5/2 16:42:22 DOW: 6 ---- 3: 0
RTC DateTime: 15/5/2 16:42:23 DOW: 6 ---- 4: 208
RTC DateTime: 15/5/2 16:42:24 DOW: 6 ---- 5: 15
RTC DateTime: 15/5/2 16:42:25 DOW: 6 ---- 6: 73
RTC DateTime: 15/5/2 16:42:26 DOW: 6 ---- 7: 64
RTC DateTime: 15/5/2 16:42:27 DOW: 6 ---- 8: 65
RTC DateTime: 15/5/2 16:42:28 DOW: 6 ---- 9: 9
RTC DateTime: 15/5/2 16:42:29 DOW: 6 ---- 10: 10
RTC DateTime: 15/5/2 16:42:30 DOW: 6 ---- 11: 11
RTC DateTime: 15/5/2 16:42:31 DOW: 6 ---- 12: 12
RTC DateTime: 15/5/2 16:42:32 DOW: 6 ---- 13: 13
RTC DateTime: 15/5/2 16:42:33 DOW: 6 ---- 14: 14
RTC DateTime: 15/5/2 16:42:34 DOW: 6 ---- 15: 15
RTC DateTime: 15/5/2 16:42:35 DOW: 6 ---- 16: 16
RTC DateTime: 15/5/2 16:42:36 DOW: 6 ---- 17: 17
RTC DateTime: 15/5/2 16:42:37 DOW: 6 ---- 18: 18
RTC DateTime: 15/5/2 16:42:38 DOW: 6 ---- 19: 19
RTC DateTime: 15/5/2 16:42:39 DOW: 6 ---- 20: 20
RTC DateTime: 15/5/2 16:42:40 DOW: 6 ---- 21: 21
RTC DateTime: 15/5/2 16:42:41 DOW: 6 ---- 22: 22
RTC DateTime: 15/5/2 16:42:42 DOW: 6 ---- 23: 23
RTC DateTime: 15/5/2 16:42:43 DOW: 6 ---- 24: 24
RTC DateTime: 15/5/2 16:42:44 DOW: 6 ---- 25: 25
RTC DateTime: 15/5/2 16:42:45 DOW: 6 ---- 26: 26
RTC DateTime: 15/5/2 16:42:46 DOW: 6 ---- 27: 27
RTC DateTime: 15/5/2 16:42:47 DOW: 6 ---- 28: 28
RTC DateTime: 15/5/2 16:42:48 DOW: 6 ---- 29: 29
RTC DateTime: 15/5/2 16:42:49 DOW: 6 ---- 30: 30
RTC DateTime: 15/5/2 16:42:50 DOW: 6 ---- 31: 31
RTC DateTime: 15/5/2 16:42:51 DOW: 6 ---- 32: 32

If still failing recheck wiring and its length.

Also, I didn't integrate read/write in template to save code space at expense of one stack space; if you use multiple datatypes program space will grow too much if you do all procesing on template instead using the 1-byte function.

@Naguissa
Copy link
Owner

Wow! Still failing on int!

@Naguissa Naguissa reopened this Dec 28, 2017
@Naguissa
Copy link
Owner

OK, see the problem:

On Arduino IDE default int size is 2 bytes: https://www.arduino.cc/reference/en/language/variables/data-types/int/

But when loading STM32 layer int size is changed to 4 bytes.

This leads to this warning:

Line 23: 	int inttmp = 0x00101010101010101010101010101010;

Compiling:


/tmp/arduino_modified_sketch_222052/uRTCLib_example.ino:23:15: warning: integer constant is too large for its type [enabled by default]
  int inttmp = 0x00101010101010101010101010101010;
               ^
/tmp/arduino_modified_sketch_222052/uRTCLib_example.ino: In function 'void setup()':
/tmp/arduino_modified_sketch_222052/uRTCLib_example.ino:23:15: warning: overflow in implicit constant conversion [-Woverflow]

Problem is that when running the sketch "sizeof(int)" gives 4, as it is in STM32.

The only fix I can find is to hardcode this case on STM32 (only), but still will fail in a direct assignment in sketch because compiler.

@hyperion11
Copy link
Contributor Author

wow. thank you! may be using int16_t \ uint16_t instead int may help..

@Naguissa
Copy link
Owner

Naguissa commented Jan 2, 2018

No, it is something related to typecasting when using integer values (floats doesn't fail).

I've accomplished what seems to be a correct write functionality using bit displacement, now I'm working on read functionality, as seems to fail too.

@Naguissa
Copy link
Owner

Naguissa commented Jan 4, 2018

I accomplished to fix everything on STM32. I need to re-add Wire.begin fix to STM32 in order to 1st read don't fail. I've no idea why, but if I remove it program fails reading 1st EEPROM value.

Issues seems to be typecasting and byte ordering.

Still needs to be tested on ESP8266 and AVR, so I have not created a release.

@Naguissa
Copy link
Owner

Solved on trunk for STM32, Ardiono AVR and Arduino SAM. Checking for ESP8266....

@Naguissa
Copy link
Owner

OK! Tested on:

STM32
ESP8266
AVR
SAM

All works.

If yo have any trouble with wire.init() on library constructor you can call constructor with a FALSE on 1st parameter (all constructors) to skip it.

Released as 4.1.0: https://github.com/Naguissa/uRTCLib/releases/tag/4.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants