Teensy 4.1 - Reset EEPROM / Empty EEPROM values

AndyA

Well-known member
I have an application that uses the EEPROM library for storing various settings. Since the teensy has been used for development it will already contain data in the eeprom if I update the code.
I'd like to verify that everything works correctly on a brand new board with an empty / unused EEPROM without having to use a new board every time I want to test this.

Is there a way to empty or re-set the EEPROM storage on a Teensy4.1? Or would it be safe to assume a new board will always read 0xFF or 0x00 for all locations?
 
Yes, hold the pushbutton for 15 seconds. Watch the red LED near USB. When you see a quick blink, you're at 13 seconds which is the beginning of a 4 second window to release the button and trigger full erase and restore of the original LED blink.

Full erase is very slow, over 1 minute, so be patient and let it finish. When done, the red LED turns off and you'll see the orange LED blinking slowly. Your PC will detect the board as RawHID (same as when it was brand new) and not Serial (the default after you've programmed it) so if you use non-Arduino software that only shows Serial ports, be mentally prepared for RawHID because this erase puts everything back as it was when the board was new.
 
I had recently wanted to reset EEPROM in a T4.1 for a similar reason, but within my application. The code shown below does that using functions in eeprom.c and runs in a little under one second, but keep in mind it is not equivalent to the full flash erase Paul described. This only erases the 63 sectors assigned to EEPROM. It uses the fewest erase functions possible in the available chunks of 64K, 32K, and 4K. You can't erase the final 64K with a single call because the very last 4K is locked, and the 64K erase will fail. After the erase is complete, you must call eeprom_initialize() so the static variables in eeprom.c will be re-initialized to the correct values for the now-empty EEPROM.

Just as a comment, I did some benchmarking of EEPROM reads and writes and was surprised at how long they can take. An EEPROM write of a 32-bit value (4 bytes) to erased flash takes about 25 us, or about 6 us per byte. What was surprising to me was that an EEPROM read of a 32-bit value can take up to 10 us. I expected it to be very fast, but it takes a while to walk through the flash to find the value to be read. If an EEPROM write triggers a sector erase, that erase takes about 40 ms. Interrupts are disabled during flash write and flash erase.

Code:
  // 3 x 64K block
  eepromemu_flash_erase_64K_block( (void*)607C0000 );   
  eepromemu_flash_erase_64K_block( (void*)607D0000 );   
  eepromemu_flash_erase_64K_block( (void*)607E0000 );   
  // 1 x 32K block
  eepromemu_flash_erase_32K_block( (void*)607F0000 );
  // 7 x 4K sector
  eepromemu_flash_erase_sector( (void*)607F8000 );
  eepromemu_flash_erase_sector( (void*)607F9000 );
  eepromemu_flash_erase_sector( (void*)607FA000 );
  eepromemu_flash_erase_sector( (void*)607FB000 );
  eepromemu_flash_erase_sector( (void*)607FC000 );
  eepromemu_flash_erase_sector( (void*)607FD000 );
  eepromemu_flash_erase_sector( (void*)607FE000 );
  // re-init EEPROM static variables
  eeprom_initialize();
 
Back
Top