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

EEPROM usage hangs sometimes #930

Closed
jgromes opened this issue Oct 29, 2019 · 18 comments · Fixed by #931
Closed

EEPROM usage hangs sometimes #930

jgromes opened this issue Oct 29, 2019 · 18 comments · Fixed by #931
Assignees
Labels
bug 🐛 Something isn't working
Milestone

Comments

@jgromes
Copy link
Contributor

jgromes commented Oct 29, 2019

Hi, I'm trying to use this library together with EEPROM, but for some reason, calls to EEPROM methods (put/get) seem to hang when using LowPower - the following sketch hangs at EEPROM.put() and never reaches EEPROM.get() or another loop.

Board: Nucleo L452RE-P
STM32 core version: cloned from repo
Sketch to reproduce:

#include <STM32LowPower.h>
#include <EEPROM.h>

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(115200);
  LowPower.begin();
}

void loop() {
  // print stuff to serial port
  Serial.println("loop");
  delay(100);

  // sleep twice
  digitalWrite(LED_BUILTIN, HIGH);
  LowPower.deepSleep(1000);
  digitalWrite(LED_BUILTIN, LOW);
  LowPower.deepSleep(1000);

  // EEPROM put - hangs here
  Serial.println("eeprom put");
  delay(100);
  EEPROM.put(0, 0xFF);

  // EEPROM get
  Serial.println("eeprom get");
  delay(100);
  uint8_t val = 0;
  EEPROM.get(0, val);
  Serial.println(val);
}

Thanks!

@jgromes
Copy link
Contributor Author

jgromes commented Oct 29, 2019

It seems to be the write operation that's causing the hangup. EEPROM.put();, EEPROM.write();, EEPROM[i] = value; and eeprom_buffer_flush() will all hang, while read operations are fine.

@fpistm
Copy link
Member

fpistm commented Oct 30, 2019

This is not link to EEPROM.
Using debug in the optimization menu does not hang.
The main issue I guess is the deepSleep().
If we kept only the blink part, delay is not correct.
Unfortunately, I will not have time to check that soon. I put it in my huge list... 😭

@jgromes
Copy link
Contributor Author

jgromes commented Oct 30, 2019

This is not link to EEPROM.

I respectfully disagree, hangup only appears when using both EEPROM and LowPower at the same time. When either one is removed, the sketch no longer hangs.

The main issue I guess is the deepSleep().

I don't think that's the case - the hang also appears when using idle(), sleep() or shutdown().

Using debug in the optimization menu does not hang.

Sorry, I'm not entirely sure what you mean there - could you please point me to some more information on this? Thank you.

@fpistm
Copy link
Member

fpistm commented Oct 30, 2019

This is not link to EEPROM.

I respectfully disagree, hangup only appears when using both EEPROM and LowPower at the same time. When either one is removed, the sketch no longer hangs.

I told that because, as a first issue the low power mode seems not properly set, in that case before digging in EEPROM. This should be fixed to ensure be in a proper clock and power config.

The main issue I guess is the deepSleep().

I don't think that's the case - the hang also appears when using idle(), sleep() or shutdown().

That's what I said, there is an issue with low power mode.

Using debug in the optimization menu does not hang.

Sorry, I'm not entirely sure what you mean there - could you please point me to some more information on this? Thank you.

In the Arduino Tools menu: Optimize instead of Smallest choose Debug.

Anyway, even if it's not hangs the timing is not correct and rely on the fact Low Power mode is not properly set. I've tested RTC and the timing is correct so all drive me to issue with Low Power mode config.

@jgromes
Copy link
Contributor Author

jgromes commented Oct 30, 2019

I will check using the debug menu, thanks. In the meantime, is there a workaround? For example, calling HAL functions directly or changing the configuration?

@fpistm
Copy link
Member

fpistm commented Oct 30, 2019

Don't know. This will require some investigation. And as said I will not be able to dig into this soon. Any help are welcome.

@jgromes
Copy link
Contributor Author

jgromes commented Oct 30, 2019

Seems the behavior depends on selected optimization, it hangs with -Os, but doesn't with -O1, -O2, -O3 and -g.

As was mentioned previously, the timing is off - LowPower.sleep(1000) only sleeps for about 650 ms.

@fpistm
Copy link
Member

fpistm commented Nov 6, 2019

Hi @jgromes
Some update
@Yannick-Marietti met the same issue on an Disco L475VG.
But without LowPower bust simply calling the RTC begin and after a write to the EEPROM.
As the LowPower init the RTC, I guess this is linked.
It seems an interupt is raised while it should not:
RTC_WKUP_IRQn = 3, /*!< RTC Wakeup interrupt through the EXTI line */

I will have to investigate deeply.

@jgromes
Copy link
Contributor Author

jgromes commented Nov 7, 2019

Thank you very much for keeping an eye on this issue. Please let me know if there are any updates, or if you need help with something (e.g. testing).

@fpistm fpistm transferred this issue from stm32duino/STM32LowPower Nov 20, 2019
@fpistm fpistm changed the title Using LowPower with EEPROM EEPROM usage hangs when used with RTC Nov 20, 2019
@ABOSTM ABOSTM transferred this issue from stm32duino/STM32RTC Feb 10, 2020
@ABOSTM ABOSTM changed the title EEPROM usage hangs when used with RTC EEPROM usage hangs sometimes Feb 10, 2020
@ABOSTM ABOSTM added the bug 🐛 Something isn't working label Feb 10, 2020
@ABOSTM ABOSTM added this to the 1.9.0 milestone Feb 10, 2020
ABOSTM added a commit to ABOSTM/Arduino_Core_STM32 that referenced this issue Feb 10, 2020
Due to some cast on (uint64_t *) of eeprom_buffer,
compiler may use asm instruction LDRD which required an alignment:
4 bytes on Armv7 architecture,
and 8 bytes (depending on SCTLR.U) on Armv6 architecture.
To fit to all cases, alignment is made on 8 bytes.

Fixes stm32duino#930
@ABOSTM
Copy link
Contributor

ABOSTM commented Feb 10, 2020

Hi @jgromes ,
Problem is not linked to RTC, just a combination of circumstances:
Sometimes, eeprom_buffer[] address was aligned on 2 bytes (16 bits),
but due to cast on (uint64_t *) compiler uses a asm instruction LDRD to read Double word.
This instruction needs some 4bytes/8bytes alignment (depending on core/architecture used).
Misalignment cause hardfault.
It is now fixed

@jgromes
Copy link
Contributor Author

jgromes commented Feb 10, 2020

That's absolutely brilliant, thank you so much!

ABOSTM added a commit to ABOSTM/Arduino_Core_STM32 that referenced this issue Feb 11, 2020
Due to some cast on (uint64_t *) of eeprom_buffer,
compiler may use asm instruction LDRD which required an alignment:
4 bytes on Armv7 architecture,
and 8 bytes (depending on SCTLR.U) on Armv6 architecture.
To fit to all cases, alignment is made on 8 bytes.

Fixes stm32duino#930
ABOSTM added a commit to ABOSTM/Arduino_Core_STM32 that referenced this issue Feb 11, 2020
Due to some cast on (uint64_t *) of eeprom_buffer,
compiler may use asm instruction LDRD which required an alignment:
4 bytes on Armv7 architecture,
and 8 bytes (depending on SCTLR.U) on Armv6 architecture.
To fit to all cases, alignment is made on 8 bytes.

Fixes stm32duino#930
fpistm pushed a commit that referenced this issue Feb 11, 2020
Due to some cast on (uint64_t *) of eeprom_buffer,
compiler may use asm instruction LDRD which required an alignment:
4 bytes on Armv7 architecture,
and 8 bytes (depending on SCTLR.U) on Armv6 architecture.
To fit to all cases, alignment is made on 8 bytes.

Fixes #930
@HappySamuel
Copy link

Hi

Is that mean EEPROM.write() from <EEPROM.h> is safe to use now? Does this apply the same to using on STM32F103Cx series?

Best,
Samuel

@ABOSTM
Copy link
Contributor

ABOSTM commented Jul 30, 2021

Hi @HappySamuel,
Yes, bug is now fixed.
Even for STM32F103Cx

@HappySamuel
Copy link

Hi @ABOSTM , @fpistm

Is there a way to check how many times left for able to do EEPROM write for the STM32F103Cx series left??

Best,
Samuel

@fpistm
Copy link
Member

fpistm commented Aug 24, 2021

Hi @HappySamuel
Unfortunately not. If you really need access several time to EEPROM, the best way is to use an I2C EEPROM, I guess.
Here the characteristic of the STM32F103Cx Flash:
image

@HappySamuel
Copy link

Hi @fpistm

Any suggested examples or libraries to use I2C EEPROM?

Best,
Samuel

@fpistm
Copy link
Member

fpistm commented Aug 25, 2021

Hi @fpistm

Any suggested examples or libraries to use I2C EEPROM?

Best,
Samuel

https://www.arduino.cc/reference/en/libraries/exteeprom/

@HappySamuel
Copy link

Hi @fpistm

Thank you for the sharing. Will test it out when i have an I2C EEPROM.

Best,
Samuel

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants