Skip to content

Commit 5724f38

Browse files
committed
Initial batch of mocks for host side testing
1 parent 1c9f9c3 commit 5724f38

File tree

8 files changed

+10033
-0
lines changed

8 files changed

+10033
-0
lines changed

tests/host/common/Arduino.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// Arduino.cpp
3+
// esp8266-host-tests
4+
//
5+
// Created by Ivan Grokhotkov on 02/03/16.
6+
// Copyright © 2016 esp8266.com. All rights reserved.
7+
//
8+
#define CATCH_CONFIG_MAIN
9+
#include <catch.hpp>
10+
#include <sys/time.h>
11+
#include "Arduino.h"
12+
13+
14+
extern "C" unsigned long millis()
15+
{
16+
timeval time;
17+
gettimeofday(&time, NULL);
18+
return (time.tv_sec * 1000) + (time.tv_usec / 1000);
19+
}
20+
21+
22+
extern "C" void yield()
23+
{
24+
}
25+
26+
27+
extern "C" void __panic_func(const char* file, int line, const char* func) {
28+
abort();
29+
}

tests/host/common/Arduino.h

Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
//
2+
// Arduino.hpp
3+
// esp8266-host-tests
4+
//
5+
// Created by Ivan Grokhotkov on 02/03/16.
6+
// Copyright © 2016 esp8266.com. All rights reserved.
7+
//
8+
9+
#ifndef Arduino_h
10+
#define Arduino_h
11+
12+
#ifdef __cplusplus
13+
extern "C" {
14+
#endif
15+
16+
#include <stdlib.h>
17+
#include <stdint.h>
18+
#include <stdbool.h>
19+
#include <stddef.h>
20+
#include <stdarg.h>
21+
#include <stdio.h>
22+
#include <string.h>
23+
#include <math.h>
24+
25+
#include "binary.h"
26+
#include "twi.h"
27+
#include "core_esp8266_features.h"
28+
29+
#define HIGH 0x1
30+
#define LOW 0x0
31+
32+
#define PWMRANGE 1023
33+
34+
//GPIO FUNCTIONS
35+
#define INPUT 0x00
36+
#define INPUT_PULLUP 0x02
37+
#define INPUT_PULLDOWN_16 0x04 // PULLDOWN only possible for pin16
38+
#define OUTPUT 0x01
39+
#define OUTPUT_OPEN_DRAIN 0x03
40+
#define WAKEUP_PULLUP 0x05
41+
#define WAKEUP_PULLDOWN 0x07
42+
#define SPECIAL 0xF8 //defaults to the usable BUSes uart0rx/tx uart1tx and hspi
43+
#define FUNCTION_0 0x08
44+
#define FUNCTION_1 0x18
45+
#define FUNCTION_2 0x28
46+
#define FUNCTION_3 0x38
47+
#define FUNCTION_4 0x48
48+
49+
#define PI 3.1415926535897932384626433832795
50+
#define HALF_PI 1.5707963267948966192313216916398
51+
#define TWO_PI 6.283185307179586476925286766559
52+
#define DEG_TO_RAD 0.017453292519943295769236907684886
53+
#define RAD_TO_DEG 57.295779513082320876798154814105
54+
#define EULER 2.718281828459045235360287471352
55+
56+
#define SERIAL 0x0
57+
#define DISPLAY 0x1
58+
59+
#define LSBFIRST 0
60+
#define MSBFIRST 1
61+
62+
//Interrupt Modes
63+
#define DISABLED 0x00
64+
#define RISING 0x01
65+
#define FALLING 0x02
66+
#define CHANGE 0x03
67+
#define ONLOW 0x04
68+
#define ONHIGH 0x05
69+
#define ONLOW_WE 0x0C
70+
#define ONHIGH_WE 0x0D
71+
72+
#define DEFAULT 1
73+
#define EXTERNAL 0
74+
75+
//timer dividers
76+
#define TIM_DIV1 0 //80MHz (80 ticks/us - 104857.588 us max)
77+
#define TIM_DIV16 1 //5MHz (5 ticks/us - 1677721.4 us max)
78+
#define TIM_DIV265 3 //312.5Khz (1 tick = 3.2us - 26843542.4 us max)
79+
//timer int_types
80+
#define TIM_EDGE 0
81+
#define TIM_LEVEL 1
82+
//timer reload values
83+
#define TIM_SINGLE 0 //on interrupt routine you need to write a new value to start the timer again
84+
#define TIM_LOOP 1 //on interrupt the counter will start with the same value again
85+
86+
#define timer1_read() (T1V)
87+
#define timer1_enabled() ((T1C & (1 << TCTE)) != 0)
88+
#define timer1_interrupted() ((T1C & (1 << TCIS)) != 0)
89+
90+
typedef void(*timercallback)(void);
91+
92+
void timer1_isr_init(void);
93+
void timer1_enable(uint8_t divider, uint8_t int_type, uint8_t reload);
94+
void timer1_disable(void);
95+
void timer1_attachInterrupt(timercallback userFunc);
96+
void timer1_detachInterrupt(void);
97+
void timer1_write(uint32_t ticks); //maximum ticks 8388607
98+
99+
// timer0 is a special CPU timer that has very high resolution but with
100+
// limited control.
101+
// it uses CCOUNT (ESP.GetCycleCount()) as the non-resetable timer counter
102+
// it does not support divide, type, or reload flags
103+
// it is auto-disabled when the compare value matches CCOUNT
104+
// it is auto-enabled when the compare value changes
105+
#define timer0_interrupted() (ETS_INTR_PENDING() & (_BV(ETS_COMPARE0_INUM)))
106+
#define timer0_read() ((__extension__({uint32_t count;__asm__ __volatile__("esync; rsr %0,ccompare0":"=a" (count));count;})))
107+
#define timer0_write(count) __asm__ __volatile__("wsr %0,ccompare0; esync"::"a" (count) : "memory")
108+
109+
void timer0_isr_init(void);
110+
void timer0_attachInterrupt(timercallback userFunc);
111+
void timer0_detachInterrupt(void);
112+
113+
// undefine stdlib's abs if encountered
114+
#ifdef abs
115+
#undef abs
116+
#endif
117+
118+
#define abs(x) ((x)>0?(x):-(x))
119+
#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
120+
#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
121+
#define radians(deg) ((deg)*DEG_TO_RAD)
122+
#define degrees(rad) ((rad)*RAD_TO_DEG)
123+
#define sq(x) ((x)*(x))
124+
125+
void ets_intr_lock();
126+
void ets_intr_unlock();
127+
128+
#ifndef __STRINGIFY
129+
#define __STRINGIFY(a) #a
130+
#endif
131+
132+
// these low level routines provide a replacement for SREG interrupt save that AVR uses
133+
// but are esp8266 specific. A normal use pattern is like
134+
//
135+
//{
136+
// uint32_t savedPS = xt_rsil(1); // this routine will allow level 2 and above
137+
// // do work here
138+
// xt_wsr_ps(savedPS); // restore the state
139+
//}
140+
//
141+
// level (0-15), interrupts of the given level and above will be active
142+
// level 15 will disable ALL interrupts,
143+
// level 0 will enable ALL interrupts,
144+
//
145+
#define xt_rsil(level) (__extension__({uint32_t state; __asm__ __volatile__("rsil %0," __STRINGIFY(level) : "=a" (state)); state;}))
146+
#define xt_wsr_ps(state) __asm__ __volatile__("wsr %0,ps; isync" :: "a" (state) : "memory")
147+
148+
#define interrupts() xt_rsil(0)
149+
#define noInterrupts() xt_rsil(15)
150+
151+
152+
#define clockCyclesPerMicrosecond() ( F_CPU / 1000000L )
153+
#define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() )
154+
#define microsecondsToClockCycles(a) ( (a) * clockCyclesPerMicrosecond() )
155+
156+
#define lowByte(w) ((uint8_t) ((w) & 0xff))
157+
#define highByte(w) ((uint8_t) ((w) >> 8))
158+
159+
#define bitRead(value, bit) (((value) >> (bit)) & 0x01)
160+
#define bitSet(value, bit) ((value) |= (1UL << (bit)))
161+
#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
162+
#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))
163+
164+
// avr-libc defines _NOP() since 1.6.2
165+
#ifndef _NOP
166+
#define _NOP() do { __asm__ volatile ("nop"); } while (0)
167+
#endif
168+
169+
typedef unsigned int word;
170+
171+
#define bit(b) (1UL << (b))
172+
#define _BV(b) (1UL << (b))
173+
174+
typedef uint8_t boolean;
175+
typedef uint8_t byte;
176+
177+
void init(void);
178+
void initVariant(void);
179+
180+
int atexit(void (*func)()) __attribute__((weak));
181+
182+
void pinMode(uint8_t pin, uint8_t mode);
183+
void digitalWrite(uint8_t pin, uint8_t val);
184+
int digitalRead(uint8_t pin);
185+
int analogRead(uint8_t pin);
186+
void analogReference(uint8_t mode);
187+
void analogWrite(uint8_t pin, int val);
188+
void analogWriteFreq(uint32_t freq);
189+
void analogWriteRange(uint32_t range);
190+
191+
unsigned long millis(void);
192+
unsigned long micros(void);
193+
void delay(unsigned long);
194+
void delayMicroseconds(unsigned int us);
195+
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout);
196+
unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout);
197+
198+
void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val);
199+
uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder);
200+
201+
void attachInterrupt(uint8_t pin, void (*)(void), int mode);
202+
void detachInterrupt(uint8_t pin);
203+
204+
void setup(void);
205+
void loop(void);
206+
207+
void yield(void);
208+
void optimistic_yield(uint32_t interval_us);
209+
210+
#define digitalPinToPort(pin) (0)
211+
#define digitalPinToBitMask(pin) (1UL << (pin))
212+
#define digitalPinToTimer(pin) (0)
213+
#define portOutputRegister(port) ((volatile uint32_t*) &GPO)
214+
#define portInputRegister(port) ((volatile uint32_t*) &GPI)
215+
#define portModeRegister(port) ((volatile uint32_t*) &GPE)
216+
217+
#define NOT_A_PIN -1
218+
#define NOT_A_PORT -1
219+
#define NOT_AN_INTERRUPT -1
220+
#define NOT_ON_TIMER 0
221+
222+
#ifdef __cplusplus
223+
} // extern "C"
224+
#endif
225+
226+
#ifdef __cplusplus
227+
228+
#include "pgmspace.h"
229+
230+
#include "WCharacter.h"
231+
#include "WString.h"
232+
233+
#include "HardwareSerial.h"
234+
#include "Esp.h"
235+
#include "Updater.h"
236+
#include "debug.h"
237+
238+
#ifndef _GLIBCXX_VECTOR
239+
// arduino is not compatible with std::vector
240+
#define min(a,b) ((a)<(b)?(a):(b))
241+
#define max(a,b) ((a)>(b)?(a):(b))
242+
#endif
243+
244+
#define _min(a,b) ((a)<(b)?(a):(b))
245+
#define _max(a,b) ((a)>(b)?(a):(b))
246+
247+
uint16_t makeWord(uint16_t w);
248+
uint16_t makeWord(byte h, byte l);
249+
250+
#define word(...) makeWord(__VA_ARGS__)
251+
252+
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);
253+
unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);
254+
255+
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0);
256+
void noTone(uint8_t _pin);
257+
258+
// WMath prototypes
259+
long random(long);
260+
long random(long, long);
261+
void randomSeed(unsigned long);
262+
long map(long, long, long, long, long);
263+
264+
extern "C" void configTime(long timezone, int daylightOffset_sec,
265+
const char* server1, const char* server2 = nullptr, const char* server3 = nullptr);
266+
267+
#endif
268+
269+
#include "pins_arduino.h"
270+
271+
#endif /* Arduino_h */

tests/host/common/WMath.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2+
3+
/*
4+
Part of the Wiring project - http://wiring.org.co
5+
Copyright (c) 2004-06 Hernando Barragan
6+
Modified 13 August 2006, David A. Mellis for Arduino - http://www.arduino.cc/
7+
8+
This library is free software; you can redistribute it and/or
9+
modify it under the terms of the GNU Lesser General Public
10+
License as published by the Free Software Foundation; either
11+
version 2.1 of the License, or (at your option) any later version.
12+
13+
This library is distributed in the hope that it will be useful,
14+
but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+
Lesser General Public License for more details.
17+
18+
You should have received a copy of the GNU Lesser General
19+
Public License along with this library; if not, write to the
20+
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
21+
Boston, MA 02111-1307 USA
22+
23+
$Id$
24+
*/
25+
26+
extern "C" {
27+
#include <stdlib.h>
28+
}
29+
30+
void randomSeed(unsigned long seed) {
31+
if(seed != 0) {
32+
srand(seed);
33+
}
34+
}
35+
36+
long random(long howbig) {
37+
if(howbig == 0) {
38+
return 0;
39+
}
40+
return (rand()) % howbig;
41+
}
42+
43+
long random(long howsmall, long howbig) {
44+
if(howsmall >= howbig) {
45+
return howsmall;
46+
}
47+
long diff = howbig - howsmall;
48+
return random(diff) + howsmall;
49+
}
50+
51+
long map(long x, long in_min, long in_max, long out_min, long out_max) {
52+
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
53+
}
54+
55+
unsigned int makeWord(unsigned int w) {
56+
return w;
57+
}
58+
59+
unsigned int makeWord(unsigned char h, unsigned char l) {
60+
return (h << 8) | l;
61+
}

0 commit comments

Comments
 (0)