-
Notifications
You must be signed in to change notification settings - Fork 34
Mock for sleep and wdt #115
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
Changes from all commits
39a028c
3fe65ba
3a2f644
2002495
16655d5
e56c2e0
062d491
364da41
7e3eedc
6321cba
2600fb5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
compile: | ||
libraries: ~ | ||
platforms: | ||
- uno | ||
- leonardo | ||
|
||
unittest: | ||
libraries: ~ | ||
platforms: | ||
- uno | ||
- leonardo |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include <avr/sleep.h> | ||
|
||
#define BUTTON_INT_PIN 2 | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
Serial.println("start"); | ||
delay(200); | ||
pinMode(BUTTON_INT_PIN, INPUT_PULLUP); | ||
attachInterrupt(digitalPinToInterrupt(BUTTON_INT_PIN), isrButtonTrigger, FALLING); | ||
} | ||
|
||
void loop() { | ||
// sleep unti an interrupt occurs | ||
sleep_enable(); // enables the sleep bit, a safety pin | ||
set_sleep_mode(SLEEP_MODE_PWR_DOWN); | ||
sleep_cpu(); // here the device is actually put to sleep | ||
sleep_disable(); // disables the sleep bit, a safety pin | ||
|
||
Serial.println("interrupt"); | ||
delay(200); | ||
} | ||
|
||
void isrButtonTrigger() { | ||
// nothing to do, wakes up the CPU | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
compile: | ||
libraries: ~ | ||
platforms: | ||
- uno | ||
- leonardo | ||
|
||
unittest: | ||
libraries: ~ | ||
platforms: | ||
- uno | ||
- leonardo |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include <avr/wdt.h> | ||
|
||
void setup() { | ||
pinMode(LED_BUILTIN, OUTPUT); | ||
digitalWrite(LED_BUILTIN, LOW); | ||
|
||
wdt_enable(WDTO_4S); | ||
// First timeout executes interrupt, second does reset. | ||
// So first LED 4s off | ||
// then LED 4s on | ||
// then reset CPU and start again | ||
WDTCSR |= (1 << WDIE); | ||
} | ||
|
||
void loop() { | ||
// the program is alive...for now. | ||
wdt_reset(); | ||
|
||
while (1) | ||
; // do nothing. the program will lockup here. | ||
|
||
// Can not get here | ||
} | ||
|
||
ISR (WDT_vect) { | ||
// WDIE & WDIF is cleared in hardware upon entering this ISR | ||
digitalWrite(LED_BUILTIN, HIGH); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#include <ArduinoUnitTests.h> | ||
#include <Arduino.h> | ||
|
||
unittest(check_ADCSRA_read_write) { | ||
ADCSRA = 123; | ||
|
||
assertEqual(123, ADCSRA); | ||
} | ||
|
||
unittest_main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#include <ArduinoUnitTests.h> | ||
#include <avr/sleep.h> | ||
|
||
GodmodeState* state = GODMODE(); | ||
|
||
unittest(sleep_enable) { | ||
state->reset(); | ||
assertFalse(state->sleep.sleep_enable); | ||
assertEqual(0, state->sleep.sleep_enable_count); | ||
|
||
sleep_enable(); | ||
|
||
assertTrue(state->sleep.sleep_enable); | ||
assertEqual(1, state->sleep.sleep_enable_count); | ||
} | ||
|
||
unittest(sleep_disable) { | ||
state->reset(); | ||
assertEqual(0, state->sleep.sleep_disable_count); | ||
|
||
sleep_disable(); | ||
|
||
assertFalse(state->sleep.sleep_enable); | ||
assertEqual(1, state->sleep.sleep_disable_count); | ||
} | ||
|
||
unittest(set_sleep_mode) { | ||
state->reset(); | ||
assertEqual(0, state->sleep.sleep_mode); | ||
|
||
set_sleep_mode(SLEEP_MODE_PWR_DOWN); | ||
|
||
assertEqual(SLEEP_MODE_PWR_DOWN, state->sleep.sleep_mode); | ||
} | ||
|
||
unittest(sleep_bod_disable) { | ||
state->reset(); | ||
assertEqual(0, state->sleep.sleep_bod_disable_count); | ||
|
||
sleep_bod_disable(); | ||
|
||
assertEqual(1, state->sleep.sleep_bod_disable_count); | ||
} | ||
|
||
unittest(sleep_cpu) { | ||
state->reset(); | ||
assertEqual(0, state->sleep.sleep_cpu_count); | ||
|
||
sleep_cpu(); | ||
|
||
assertEqual(1, state->sleep.sleep_cpu_count); | ||
} | ||
|
||
unittest(sleep_mode) { | ||
state->reset(); | ||
assertEqual(0, state->sleep.sleep_mode_count); | ||
|
||
sleep_mode(); | ||
|
||
assertEqual(1, state->sleep.sleep_mode_count); | ||
assertEqual(1, state->sleep.sleep_enable_count); | ||
assertEqual(1, state->sleep.sleep_disable_count); | ||
} | ||
|
||
unittest_main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <ArduinoUnitTests.h> | ||
#include <avr/wdt.h> | ||
|
||
GodmodeState* state = GODMODE(); | ||
|
||
unittest(taskWdtEnable_checkTimeout) { | ||
state->reset(); | ||
assertEqual(0, state->wdt.timeout); | ||
|
||
wdt_enable(WDTO_1S); | ||
|
||
assertTrue(state->wdt.wdt_enable); | ||
assertEqual(WDTO_1S, state->wdt.timeout); | ||
assertEqual(1, state->wdt.wdt_enable_count); | ||
} | ||
|
||
unittest(taskWdtEnableDisable) { | ||
state->reset(); | ||
assertEqual(0, state->wdt.wdt_enable_count); | ||
|
||
wdt_enable(WDTO_1S); | ||
|
||
assertTrue(state->wdt.wdt_enable); | ||
assertEqual(1, state->wdt.wdt_enable_count); | ||
|
||
wdt_disable(); | ||
|
||
assertFalse(state->wdt.wdt_enable); | ||
assertEqual(1, state->wdt.wdt_enable_count); | ||
} | ||
|
||
unittest(wdt_reset) { | ||
state->reset(); | ||
assertEqual(0, state->wdt.wdt_reset_count); | ||
|
||
wdt_reset(); | ||
|
||
assertEqual(1, state->wdt.wdt_reset_count); | ||
} | ||
|
||
unittest_main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#include "AvrAdc.h" | ||
|
||
// mock storage to allow access to ADCSRA | ||
unsigned char sfr_store; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#pragma once | ||
|
||
// ADCSRA is defined in the CPU specific header files | ||
// like iom328p.h. | ||
// It is liked to _SFR_MEM8 what does not exists in the test environment. | ||
// Therefore we define _SFR_MEM8 here and provide it a storage | ||
// location so that the test code can read/write on it. | ||
extern unsigned char sfr_store; | ||
#define _SFR_MEM8(mem_addr) sfr_store |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
This header file defines the macros required for the production | ||
code for AVR CPUs to declare ISRs in the test environment. | ||
See for more details | ||
https://www.nongnu.org/avr-libc/user-manual/group__avr__interrupts.html | ||
*/ | ||
#pragma once | ||
|
||
// Allows the production code to define an ISR method. | ||
// These definitions come from the original avr/interrupt.h file | ||
// https://www.nongnu.org/avr-libc/user-manual/interrupt_8h_source.html | ||
#define _VECTOR(N) __vector_ ## N | ||
#define ISR(vector, ...) \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a link to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added an explanation there |
||
extern "C" void vector (void) __VA_ARGS__; \ | ||
void vector (void) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
This header file defines the functionality to put AVR CPUs to sleep mode. | ||
For details see | ||
https://www.nongnu.org/avr-libc/user-manual/group__avr__sleep.html | ||
*/ | ||
#pragma once | ||
|
||
#include <Godmode.h> | ||
|
||
void sleep_enable() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please link to sleep-mode docs There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
GodmodeState* godmode = GODMODE(); | ||
godmode->sleep.sleep_enable = true; | ||
godmode->sleep.sleep_enable_count++; | ||
} | ||
|
||
void sleep_disable() { | ||
GodmodeState* godmode = GODMODE(); | ||
godmode->sleep.sleep_enable = false; | ||
godmode->sleep.sleep_disable_count++; | ||
} | ||
|
||
void set_sleep_mode(unsigned char mode) { | ||
GodmodeState* godmode = GODMODE(); | ||
godmode->sleep.sleep_mode = mode; | ||
} | ||
|
||
void sleep_bod_disable() { | ||
GodmodeState* godmode = GODMODE(); | ||
godmode->sleep.sleep_bod_disable_count++; | ||
} | ||
|
||
void sleep_cpu() { | ||
GodmodeState* godmode = GODMODE(); | ||
godmode->sleep.sleep_cpu_count++; | ||
} | ||
|
||
void sleep_mode() { | ||
GodmodeState* godmode = GODMODE(); | ||
sleep_enable(); | ||
godmode->sleep.sleep_mode_count++; | ||
sleep_disable(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is
ADCSRA
defined?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added explanations into this file