Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 12 of 12

Thread: Teensy 4.0 and EEPROM.h problem

  1. #1
    Senior Member DD4WH's Avatar
    Join Date
    Oct 2015
    Location
    Central Europe
    Posts
    491

    Teensy 4.0 and EEPROM.h problem

    I run into problems with EEPROM.h use with the Teensy 4.0. I would like to use the EEPROM to save about 800bytes.

    The code below runs for (about) the first 20 EEPROM adresses, but fails with more. Additionally, the problem is not consistent, but the program fails to even print the first message to Serial when EEPROM_limit is too high. And the limit of 20 also seems to be a bit inconsistent, sometimes 19 is possible, sometimes not, sometimes 21 is possible, sometimes not. Am I doing something wrong?

    Maybe the EEPROM.write writes the code to the flash where also program code is stored? Do I need to use other EEPROM adresses with the T4?

    Code:
    // Arduino 1.8.9, Teensyduino 1.47
    // Teensy 4.0
    // EEPROM problem
    // which adresses to use?
    // does the EEPROM write overwrite parts of the FLASH where the program is situated?
    // it does not even print the "Before writing"-message, if I set EEPROM_limit above 20 . . . ???
    // it seems the exact limit is flexible, gets me confused
    
    #include <EEPROM.h>
    
    // this works up to 20, but fails from 21 and higher
    #define EEPROM_LIMIT  50
    
    void setup() 
    {
      // put your setup code here, to run once:
        Serial.begin(115200);
        delay(1000);
        Serial.println("Before writing");
        for(unsigned i = 0; i < EEPROM_LIMIT; i++)
        {
          EEPROM.write(i, 42);
        }
        Serial.println("After writing");
        for(unsigned i = 0; i < EEPROM_LIMIT; i++)
        {
          int r = EEPROM.read(i);
          Serial.print(i);
          Serial.print(") ");
          Serial.println(r);
        }
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
    
    }

  2. #2
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,231
    add while(!Serial); after Serial.begin() -- that should fix missing output

  3. #3
    Senior Member DD4WH's Avatar
    Join Date
    Oct 2015
    Location
    Central Europe
    Posts
    491
    Thanks! I thought a delay(1000) was enough . . . ? And why is the Serial output dependent on the number of EEPROM adresses?

  4. #4
    Senior Member DD4WH's Avatar
    Join Date
    Oct 2015
    Location
    Central Europe
    Posts
    491
    @manitou: thanks for the hint! However, it did not help.

    The real problem I had was that in the Arduino compiler setting I had "optimize: smallest code" (which I did not realize . . .). That produced code that would not run reliably.

    When I changed to: "Optimize: Faster", everything worked well!

    All the best,

    Frank DD4WH

    P.S.: this was also the problem causing T4 lock with EEPROM.h defined in the Teensy Convolution SDR. Now with the right optimization setting ("Faster") that also works well :-).
    Last edited by DD4WH; 10-07-2019 at 03:16 PM.

  5. #5
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,231
    With the while(!Serial) and optimize "smallest code" I did not see any problems with your sketch ... ? 1.8.10 and 1.48

  6. #6
    Senior Member DD4WH's Avatar
    Join Date
    Oct 2015
    Location
    Central Europe
    Posts
    491
    thanks for testing! Tested it again to be sure (with while(!Serial) :-), but same problem occurs here: if I use "Optimize: smalles code" it works with 20, but not with 50 (prints out "Before writing", but nothing follows).

    Could be my "old" version of Teensyduino . . . 1.8.9 and 1.47 ? will install the newest Arduino/Teensyduino versions and test later!

  7. #7
    Senior Member DD4WH's Avatar
    Join Date
    Oct 2015
    Location
    Central Europe
    Posts
    491
    Installed Ardunio 1.8.10 and Teensyduino 1.48

    Same effect: if I compile with "Optimize: smallest code", the script does not run properly when I set the limit above 20: it only prints out "Before writing" and bricks the Teensy.

    If I compile with "Optimize: Faster", everything runs fine.

    Confused . . .

    Code:
    // Arduino 1.8.10, Teensyduino 1.48
    // Teensy 4.0
    // EEPROM problem
    // when compiled with "Optimize: smallest code"
    // it does only print the "Before writing"-message, if I set EEPROM_limit above 20 . . . ???
    // it seems the exact limit is flexible, gets me confused
    
    #include <EEPROM.h>
    
    // this works up to 20, but fails from 21 and higher
    #define EEPROM_LIMIT  10
    
    void setup() 
    {
      // put your setup code here, to run once:
        Serial.begin(115200);
        while(!Serial);
    
        Serial.println("Before writing");
        for(unsigned i = 0; i < EEPROM_LIMIT; i++)
        {
          EEPROM.write(i, 42);
        }
        Serial.println("After writing");
        for(unsigned i = 0; i < EEPROM_LIMIT; i++)
        {
          int r = EEPROM.read(i);
          Serial.print(i);
          Serial.print(") ");
          Serial.println(r);
        }
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
    
    }

  8. #8
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,231
    Still works for me. I tried it on a 2nd T4 and the older T4B2R.

    Although the first time i built sketch on 2nd T4 and T4B2R nothing printed and i had to hit the program button to load a new sketch. But I couldn't get it to fail again. 50 EEPROM writes on all 3 with optimize "smallest code" is working ...

  9. #9
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,231
    BUT wait ... I have a sketch that prints out T4 EEPROM meta data https://github.com/manitou48/teensy4...eprom_meta.ino

    I now recall that if the EEPROM data to be written matches what's already there, then no EEPROM write is required or done .... so i need to test with new values on each run ....

    OK, if i change the value to be written, it hangs with "smallest code" -- nothing printed !!

    "Houston, i think we have a problem ..." (or is this a known problem buried somewhere in the huge T4 beta test thread?)

  10. #10
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,945
    Quote Originally Posted by manitou View Post
    ...
    "Houston, i think we have a problem ..." (or is this a known problem buried somewhere in the huge T4 beta test thread?)
    Seems new AFAIK … FWIW - I don't recall any known/open issues/concerns with Teensy 4 EEPROM behavior.

  11. #11
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,231
    I experimented with "optimization changes" to hardware/teensy/avr/cores/teensy4/eeprom.c while compiling with Optimize Smallest Code.

    adding #pragma GCC optimize ("O2") to the front of eeprom.c worked
    but the simplest change was instead adding
    static void flash_wait() __attribute__((optimize("O2")));
    suggesting timing issues may be in flash_wait()

    of course there may be some changes to the flash register manipulation or while's that would fix it, but that is above my pay grade.

  12. #12
    Junior Member
    Join Date
    Dec 2016
    Posts
    16
    I am having this issue too.
    If I choose "smallest code" for my T4 boombox I am now developing, It locks up completely as soon as I try to write to EEPROM.
    Had me a bit confused until I remembered reading this thread.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •