Permalink
Browse files

Sleep mode now works w/o Serial

Apparently calling standBy() from loop() instead of the RTC alarm
interrupt service routine works.
  • Loading branch information...
3x10e8 committed May 27, 2016
1 parent 68998da commit 5ed57190e5d4e44358e3e33bc32946bad89861f7
@@ -17,11 +17,12 @@
#include <RTCZero.h>
//#define USING_SERIAL // comment if testing w/o native USB
#define USING_SERIAL
// uncomment if indicating alarms through Serial writes
// skips putting the processor to sleep
#define redLED 13
#define grnLED 8
boolean ledStatus = false;
#define alSize 6 // hhmmssDDMMYY data stored per each alarm (6 bytes)
#define numAlarmsMax 30 // can be increased to fill unused memory
@@ -30,130 +31,86 @@ boolean ledStatus = false;
byte rxBuffer[rxBufferSize]; // use flash memory: https://github.com/cmaglie/FlashStorage
int bytesStored = 0;
int alarmNdx = 1; // ndx 0 is used to set the time
int alarmNdx = 0; // ndx 0 is used to set the time
RTCZero rtc; // create an rtc object
void setup() {
Serial.begin(9600);
//while (!Serial);
Serial.begin(9600); //while (!Serial);
pinMode(redLED, OUTPUT); // configure as PWM?
pinMode(grnLED, OUTPUT);
serialEventOnSetup(); // couldn't get serialEvent to work...
}
timeConfig(); // apparently need rtc.begin() first else alarm writes won't work...
alarmConfig();
void loop() {
//Serial.println(NVMCTRL->CTRLB.bit.SLEEPPRM); // = 48 (ASCII) = 0 (decimal)
// could set to 3: http://community.atmel.com/forum/samd20-problem-waking-systemsleep
digitalWrite(redLED, 1); delay(100); // to check if the processor hangs up post alarm!
digitalWrite(redLED, 0); delay(100);
//Serial.begin(9600);
// while(!Serial);
//Serial.begin(9600); while(!Serial); Serial.println();
//Serial.print('@');
rtc.standbyMode();
// Could Serial.end() and/or USBDevice.disable() but standBy forces that...
}
void timeConfig() { // set RTC time based on the received data
rtc.begin();
void loop() { // Processor is awake!
int i = 0;
rtc.setTime(rxBuffer[i + 0], rxBuffer[i + 1], rxBuffer[i + 2]); // hh, mm, ss
rtc.setDate(rxBuffer[i + 3], rxBuffer[i + 4], rxBuffer[i + 5]); // DD, MM, YY
// USBDevice.attach(); Serial.write('@');
// Serial.begin(9600); while(!Serial); Serial.write('@'); Serial.end();
// nope doesn't come up...
rtc.enableAlarm(rtc.MATCH_YYMMDDHHMMSS); // other modes are available, see RTCZero.h
rtc.attachInterrupt(alarmMatch);
#ifndef USING_SERIAL
digitalWrite(grnLED, 1); delay(100);
digitalWrite(grnLED, 0); delay(100);
digitalWrite(grnLED, 1); delay(100);
digitalWrite(grnLED, 0); delay(100);
rtc.standbyMode();
#endif
}
void alarmConfig() { // set alarms on startup or with each alarm trigger
int i = alarmNdx * alSize; //Serial.println(i);
rtc.setAlarmTime(rxBuffer[i + 0], rxBuffer[i + 1], rxBuffer[i + 2]); // hh, mm, ss
rtc.setAlarmDate(rxBuffer[i + 3], rxBuffer[i + 4], rxBuffer[i + 5]); // DD, MM, YY
alarmNdx++;
void timeConfig() { // set RTC time based on the received data
/*
Serial.write(rtc.getAlarmHours()); //Serial.write('_');
Serial.write(rtc.getAlarmMinutes()); //Serial.write('_');
Serial.write(rtc.getAlarmSeconds()); //Serial.write('`');
Serial.write(rtc.getAlarmMonth()); //Serial.write('|');
Serial.write(rtc.getAlarmDay()); //Serial.write('|');
Serial.write(rtc.getAlarmYear());
*/
rtc.begin();
rtc.setTime(rxBuffer[alarmNdx + 0], rxBuffer[alarmNdx + 1],
rxBuffer[alarmNdx + 2]); // hh, mm, ss
rtc.setDate(rxBuffer[alarmNdx + 3], rxBuffer[alarmNdx + 4],
rxBuffer[alarmNdx + 5]); // DD, MM, YY
//#ifdef USING_SERIAL
// Serial.end();
// USBDevice.detach(); // https://forums.adafruit.com/viewtopic.php?f=22&t=86144
// delay(500);
//#endif
//USBDevice.detach(); // https://forums.adafruit.com/viewtopic.php?f=22&t=86144
// delay(500);
NVMCTRL->CTRLB.bit.SLEEPPRM = 3; //NVMCTRL_CTRLB_SLEEPPRM_DISABLED_Val; // http://community.atmel.com/forum/samd20-problem-waking-systemsleep
//SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk;
//PM->SLEEP.reg = 2;
//__WFI();
rtc.standbyMode();
rtc.enableAlarm(rtc.MATCH_YYMMDDHHMMSS); // see RTCZero.h for other modes
rtc.attachInterrupt(alarmMatch);
}
void alarmConfig2() { // set alarms on startup or with each alarm trigger
int i = alarmNdx * alSize; //Serial.println(i);
rtc.setAlarmTime(rxBuffer[i + 0], rxBuffer[i + 1], rxBuffer[i + 2]); // hh, mm, ss
rtc.setAlarmDate(rxBuffer[i + 3], rxBuffer[i + 4], rxBuffer[i + 5]); // DD, MM, YY
void alarmConfig() { // set alarms on startup or with each alarm trigger
alarmNdx++;
int i = alarmNdx * alSize; //Serial.println(i);
rtc.setAlarmTime(rxBuffer[i + 0], rxBuffer[i + 1],
rxBuffer[i + 2]); // hh, mm, ss
rtc.setAlarmDate(rxBuffer[i + 3], rxBuffer[i + 4],
rxBuffer[i + 5]); // DD, MM, YY
//NVMCTRL->CTRLB.bit.SLEEPPRM = 3; //NVMCTRL_CTRLB_SLEEPPRM_DISABLED_Val; // http://community.atmel.com/forum/samd20-problem-waking-systemsleep
//rtc.standbyMode();
// rtc.standbyMode(); // <--- wooo badddd. Hangs up if called outside loop() or setup().
}
void alarmMatch() { // delay() doesn't work in ISRs...?
//USBDevice.attach();
void alarmMatch() { // can't flash LED here, delay() doesn't work in ISRs...
#ifdef USING_SERIAL
//digitalWrite(grnLED, 1);
//USBDevice.attach();
//Serial.begin(9600);
while(!Serial);
//Serial.begin(9600); while(!Serial); Serial.println();
Serial.print('@');
//digitalWrite(grnLED, 0);
#endif
ledStatus = !ledStatus;
digitalWrite(grnLED, ledStatus);
digitalWrite(redLED, !ledStatus);
//digitalWrite(grnLED, 1); //delay(100);
//digitalWrite(grnLED, 0);
// generate SD card file
// enable recording
// configure next alarm // <--- wooo badddd. Need to test this w/o USB.
// if ((alarmNdx +1)*alSize <= bytesStored) alarmConfig(); // THIS hangs up the processor post wake-up!
alarmConfig2();
// configure next alarm
if ((alarmNdx + 1)*alSize <= bytesStored) alarmConfig();
}
void serialEventOnSetup() {
//___,_|_,-.____,_|_,-.____,_|_,-.____,_|_,-.__//
// This is redundant: just to have a nice wait //
// heartBeat() // somehow messes up call to Serial.write()
int waitCounter = millis(), waitFor = 450, toFlip = 200;
while (Serial.available() == 0 && Serial.read() != '$') { // waiting to receive data
digitalWrite(redLED, 0); delayMicroseconds(millis() - waitCounter); digitalWrite(redLED, 1);
@@ -163,52 +120,42 @@ void serialEventOnSetup() {
waitFor += toFlip;
}
}
digitalWrite(redLED, ledStatus); // off
digitalWrite(redLED, 0); // off
while (Serial.read() == '$'); // clear the header
int bytesRxd = 0; // store received data to memory
int bytesRxd = 0; // store received data to memory. flash would be preferable
while (Serial.available()) { // to deal with the 64 byte s/w rx buffer
bytesRxd = Serial.available();
//Serial.println(bytesRxd);
//Serial.println(bytesStored); // convert ASCII to int, read until '\n' (which is 1310)
Serial.readBytes(rxBuffer +bytesStored, bytesRxd); // get the date/time data
//Serial.println(bytesRxd);
//Serial.println(bytesStored); // convert ASCII to int, read until '\n' (which is 1310)
Serial.readBytes(rxBuffer + bytesStored, bytesRxd); // get the date/time data
bytesStored += bytesRxd;
}
int bytesTxd = 0;//, i;
while (bytesTxd < bytesStored) { // just to check the data
// i = Serial.write(rxBuffer[bytesTxd]); // i returned 48 (decimal 0)...what...
// i = Serial.write(rxBuffer[bytesTxd]); // i returned 48 (decimal 0)...not bytes written...
Serial.write(rxBuffer[bytesTxd]);
bytesTxd++;
}
}
// failing attempts at resolving the standbyMode problem...
//#ifndef USING_SERIAL
//USBDevice.detach(); // https://forums.adafruit.com/viewtopic.php?f=22&t=86144
//delay(500);
//Serial.end();
//USB->DEVICE.CTRLA.bit.RUNSTDBY = 0;
// https://github.com/arduino/ArduinoCore-samd/blob/master/cores/arduino/USB/samd21_host.c
//#endif
timeConfig(); // apparently need rtc.begin() first else alarm write syncs won't work...
alarmConfig();
}
void alarmConfig_debug() { // set alarms on startup or with each alarm trigger
/*
void heartBeat(){
int waitCounter = millis(), waitFor = 450, toFlip = 200;
while (Serial.available() == 0 && Serial.read() != '$') { // waiting to receive data
digitalWrite(redLED, 0); delayMicroseconds(millis() - waitCounter); digitalWrite(redLED, 1);
if ((millis() - waitCounter) > waitFor) { //Serial.println("waiting...");
waitCounter = millis();
toFlip *= -1;
waitFor += toFlip;
}
}
int i = alarmNdx * alSize; //Serial.println(i);
rtc.setAlarmTime(rxBuffer[i + 0], rxBuffer[i + 1], rxBuffer[i + 2]); // hh, mm, ss
rtc.setAlarmDate(rxBuffer[i + 3], rxBuffer[i + 4], rxBuffer[i + 5]); // DD, MM, YY
alarmNdx++;
/*
Serial.write(rtc.getAlarmHours()); Serial.write(rtc.getAlarmMinutes());
Serial.write(rtc.getAlarmSeconds());
Serial.write(rtc.getAlarmMonth()); Serial.write(rtc.getAlarmDay());
Serial.write(rtc.getAlarmYear());
*/
rtc.standbyMode();
}
*/
@@ -4,7 +4,7 @@
import processing.serial.*;
Serial port;
char[] alarmTimes = { // to be generated by a GUI'
char[] alarmTimes = { // to be generated by a GUI
// check to guarantee valid numbers (after additions)
'$', '$', '*', //char(4), '>', // for syncing
@@ -13,11 +13,11 @@ char[] alarmTimes = { // to be generated by a GUI'
char(hour()), char(minute()), char(second()),
char(day()), char(month()), char(year()-2000), //'>',
char(hour()), char(minute()+1), char(0),
char(hour()), char(minute()), char(second() + 10),
char(day()), char(month()), char(year()-2000), //'>',
char(hour()), char(minute()+2), char(0),
char(hour()), char(minute()+1), char(0),
char(day()), char(month()), char(year()-2000), //'>',
char(hour()), char(minute()+3), char(0),
char(hour()), char(minute()+1), char(5),
char(day()), char(month()), char(year()-2000), //'>',
/*
char(hour() +1), char(0), char(0),

0 comments on commit 5ed5719

Please sign in to comment.