Skip to content

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

Merged
merged 11 commits into from
Feb 25, 2019
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Added
- Proper `ostream operator <<` for `nullptr`
- Proper comparison operations fro `nullptr`
- Mocks for avr/sleep.h and avr/wdt.h
- Definitions for ISR and ADCSRA

### Changed
- `Compare.h` heavily refactored to use a smallish macro
Expand Down
10 changes: 10 additions & 0 deletions SampleProjects/TestSomething/test/adc.cpp
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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is ADCSRA defined?

Copy link
Contributor Author

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


assertEqual(123, ADCSRA);
}

unittest_main()
8 changes: 8 additions & 0 deletions SampleProjects/TestSomething/test/isr_declaration.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <ArduinoUnitTests.h>
#include <Arduino.h>

// just check if declaration compiles
ISR (WDT_vect) {
Copy link
Collaborator

@ianfixes ianfixes Feb 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move this to interrupts.cpp, and add a comment with some explanation of WDT_vect.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}

unittest_main()
65 changes: 65 additions & 0 deletions SampleProjects/TestSomething/test/sleep.cpp
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()
41 changes: 41 additions & 0 deletions SampleProjects/TestSomething/test/wdt.cpp
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()
3 changes: 3 additions & 0 deletions cpp/arduino/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ typedef uint8_t byte;
// Math and Trig
#include "AvrMath.h"

#include "AvrAdc.h"
#include "avr/interrupt.h"

#include "Godmode.h"


Expand Down
4 changes: 4 additions & 0 deletions cpp/arduino/AvrAdc.cpp
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;
8 changes: 8 additions & 0 deletions cpp/arduino/AvrAdc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef _AVR_ADC_H_
#define _AVR_ADC_H_

// mock storage to allow access to ADCSRA
extern unsigned char sfr_store;
#define _SFR_MEM8(mem_addr) sfr_store

#endif // _AVR_ADC_H_
42 changes: 41 additions & 1 deletion cpp/arduino/Godmode.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@ class GodmodeState {
uint8_t mode;
};

struct SleepDef {
bool sleep_enable = false;
unsigned int sleep_enable_count = 0;
unsigned int sleep_disable_count = 0;
unsigned char sleep_mode = 0;
unsigned int sleep_cpu_count = 0;
unsigned int sleep_mode_count = 0;
unsigned int sleep_bod_disable_count = 0;
};

struct WdtDef {
bool wdt_enable = false;
unsigned char timeout = 0;
unsigned int wdt_enable_count = 0;
unsigned int wdt_disable_count = 0;
unsigned int wdt_reset_count = 0;
};

public:
unsigned long micros;
unsigned long seed;
Expand All @@ -52,6 +70,8 @@ class GodmodeState {
struct PortDef serialPort[NUM_SERIAL_PORTS];
struct InterruptDef interrupt[MOCK_PINS_COUNT]; // not sure how to get actual number
struct PortDef spi;
struct SleepDef sleep;
struct WdtDef wdt;

void resetPins() {
for (int i = 0; i < MOCK_PINS_COUNT; ++i) {
Expand Down Expand Up @@ -85,12 +105,32 @@ class GodmodeState {
spi.readDelayMicros = 0;
}

void resetSleep() {
sleep.sleep_enable = false;
sleep.sleep_enable_count = 0;
sleep.sleep_disable_count = 0;
sleep.sleep_mode = 0;
sleep.sleep_cpu_count = 0;
sleep.sleep_mode_count = 0;
sleep.sleep_bod_disable_count = 0;
}

void resetWdt() {
wdt.wdt_enable = false;
wdt.timeout = 0;
wdt.wdt_enable_count = 0;
wdt.wdt_disable_count = 0;
wdt.wdt_reset_count = 0;
}

void reset() {
resetClock();
resetPins();
resetInterrupts();
resetPorts();
resetSPI();
resetSleep();
resetWdt();
seed = 1;
}

Expand All @@ -114,7 +154,7 @@ int analogRead(uint8_t);
void analogWrite(uint8_t, int);
#define analogReadResolution(...) _NOP()
#define analogWriteResolution(...) _NOP()
void attachInterrupt(uint8_t interrupt, void ISR(void), uint8_t mode);
void attachInterrupt(uint8_t interrupt, void isr(void), uint8_t mode);
void detachInterrupt(uint8_t interrupt);

// TODO: issue #26 to track the commanded state here
Expand Down
10 changes: 10 additions & 0 deletions cpp/arduino/avr/interrupt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef _AVR_INTERRUPT_H_
#define _AVR_INTERRUPT_H_

// allows the production code to define an ISR method
#define _VECTOR(N) __vector_ ## N
#define ISR(vector, ...) \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a link to the ISR() docs. Also, what is the extern doing here? What does this macro perform?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)

#endif // _AVR_INTERRUPT_H_
40 changes: 40 additions & 0 deletions cpp/arduino/avr/sleep.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ifndef _AVR_SLEEP_H_
#define _AVR_SLEEP_H_

#include <Godmode.h>

void sleep_enable() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please link to sleep-mode docs

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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();
}

#endif /* _AVR_SLEEP_H_ */
35 changes: 35 additions & 0 deletions cpp/arduino/avr/wdt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef _AVR_WDT_H_
#define _AVR_WDT_H_

#include <Godmode.h>

#define WDTO_15MS 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please link to WDT docs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

#define WDTO_30MS 1
#define WDTO_60MS 2
#define WDTO_120MS 3
#define WDTO_250MS 4
#define WDTO_500MS 5
#define WDTO_1S 6
#define WDTO_2S 7
#define WDTO_4S 8
#define WDTO_8S 9

void wdt_enable(unsigned char timeout) {
GodmodeState* godmode = GODMODE();
godmode->wdt.wdt_enable = true;
godmode->wdt.timeout = timeout;
godmode->wdt.wdt_enable_count++;
}

void wdt_disable() {
GodmodeState* godmode = GODMODE();
godmode->wdt.wdt_enable = false;
godmode->wdt.wdt_disable_count++;
}

void wdt_reset() {
GodmodeState* godmode = GODMODE();
godmode->wdt.wdt_reset_count++;
}

#endif /* _AVR_WDT_H_ */