T_3.6 EEPROM write in HSRUN sketch via SNOOZE
UPDATE: After going to the trouble of writing post 1325 I figured I should try it. I pulled down
duff2013/Snooze_V6_Beta
THIS WORKS - it has been tested only as far as seeing the code below work. I have not read anything about SNOOZE (there are pretty pictures in the README.md), just opened the SIMPLE sample and put in some EEPROM[] array read writes.
The code below is the main code to replace that in "
{sketchbook}\libraries\Snooze_V6\examples\REDUCED_CPU_BLOCK\simple", you'll need the other two DUFF files in that sample.
@DUFF : Good Work!
> All I had to Add was to EXIT and RESUME HSRUN, even when it was at 2MHz with HSRUN set the write failed.
> Is there a way to set to other than 2 MHz or is that a magic value?
> Is there a single hunk of code to extract to perform your magic for this 'special case'?
> Is the world really unsafe at this point - supposing anything time based was in use ( SPI, SDHC, I2C, USB, Ethernet, ...)
@WORLD :
> I have no idea of the state of the TEENSY after this is done. Are we safely back on 'terra firma' or if after this is done a RESET should be done?
> This confirms that EEPROM read works even at HSRUN
What the sketch does:: For DEBUG I just blink the LED as I do a test READ / WRITE / READ of 10 EEPROM values.:
> When a Flash read of a value doesn't match the
TestVal you get a 250ms on/off, otherwise darkness (run it twice in a row values will match here)
> delay(1250);
> When a FLASH WRITE is done a quick 50ms on/off appears
> delay(1000);
> If write worked you see darkness, otherwise When a Flash read of a value doesn't match the
TestVal you get a 250ms on/off
> LED turns ON and test is over
> delay(2000);
> If SNOOZE was used and HSRUN cycled OFF/ON then the LED does a fast blink
Seeing it yourself - read and write TestVal to 10 EEPROM locations at any compile speed:
> Set "const byte TestVal = 8;" to a BYTE value of your choice
> Set "const byte StartLoc = 3400;" to an EEPROM address 0 to 3089
> To see it fail on a T_3.6 over 120 MHz, comment out the line: "DoSnooze = 1;"
Code:
/***************************************
REDUCED_CPU_BLOCK allows you to run
code at 2 MHz (VLPR). See micro_lp and
delay_lp to see how to convert core
functionality at F_CPU to work at 2 MHz.
****************************************/
#include <EEPROM.h>
#include <Snooze.h>
#include "delay.h"
// REDUCED_CPU_BLOCK needs a SnoozeBlock passed to it
// so we pass a dummy SnoozeBlock with no Drivers installed.
SnoozeBlock dummyConfig;
const byte TestVal = 12;
const byte StartLoc = 2200;
bool DoSnooze = 0;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
TestFlash(); // Test Read Flash at F_CPU
delay(1250); // wait for a second
#if defined(__MK66FX1M0__)
#if F_CPU > 120000000
DoSnooze = 1;
#endif
#endif
if ( DoSnooze ) {
REDUCED_CPU_BLOCK(dummyConfig) {
SMC_PMCTRL = SMC_PMCTRL_RUNM(0); // EXIT HSRUN mode
while (SMC_PMSTAT == SMC_PMSTAT_HSRUN) ; // wait for !HSRUN
WriteFlash();
SMC_PMCTRL = SMC_PMCTRL_RUNM(3); // enter HSRUN mode
while (SMC_PMSTAT != SMC_PMSTAT_HSRUN) ; // wait for HSRUN
}
}
else
WriteFlash();
delay(1000); // wait for a second
TestFlash(); // Test Read Flash at F_CPU
digitalWrite(LED_BUILTIN, HIGH); // turn the LED_BUILTIN on (HIGH is the voltage level)
delay(2000); // wait 2 seconds
}
void loop() {
if ( DoSnooze ) {
QuickBlink(); // Now may be a good time to leave
}
}
void TestFlash() {
for ( int ii = StartLoc; ii < StartLoc + 10; ii++ ) {
if ( TestVal != EEPROM[ ii ] ) {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED_BUILTIN on (HIGH is the voltage level)
delay(0250); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED_BUILTIN off by making the voltage LOW
delay(0250); // wait for a second
}
}
}
void WriteFlash() {
for ( int ii = StartLoc; ii < StartLoc + 10; ii++ ) {
EEPROM[ ii ] = TestVal;
QuickBlink();
}
}
void QuickBlink() {
for ( int8_t ii = 0; ii < 2; ii++ ) {
digitalWrite(LED_BUILTIN, (ii % 2));
if ( DoSnooze )
delay_lp(50); // wait for a second
else
delay(50);
}
}
BTW: I did this with TD_1.30 release version on IDE 1.6.11 and the current SNOOZE_v6_Beta on my T_3.6 beta 3 board. Also compiled for T_3.5 at 168 MHz and the #ifdef kept it from using snooze as it isn't needed on T_3.5 as it doesn't have the HSRUN high voltage switchover that blocks EEPROM write like the T_3.6.
Posted on hopefully the right thread for DUFF to offer any insight on this SNOOZE case