diff --git a/cores/arduino/Arduino.h b/cores/arduino/Arduino.h index 38cc70664..d6774dda0 100644 --- a/cores/arduino/Arduino.h +++ b/cores/arduino/Arduino.h @@ -100,6 +100,7 @@ extern analogin_config_t adcCurrentConfig; #endif #include "Serial.h" +#include "timer.h" #if defined(SERIAL_CDC) #define Serial _UART_USB_ #define SerialUSB _UART_USB_ diff --git a/cores/arduino/timer.cpp b/cores/arduino/timer.cpp new file mode 100644 index 000000000..6a58694f8 --- /dev/null +++ b/cores/arduino/timer.cpp @@ -0,0 +1,33 @@ +#include "Arduino.h" +#include "timer.h" +#include "mbed.h" + +using namespace arduino; + +struct _mbed_timer { + mbed::Timer* obj; +}; + +ArduinoTimer::ArduinoTimer(void* _timer) { + + if (timer != NULL) { + delete timer; + } + + timer = new mbed_timer; + timer->obj = (mbed::Timer*)_timer; +} + +ArduinoTimer::~ArduinoTimer() { + if (timer != NULL) { + delete timer; + } +} + +void ArduinoTimer::start() { + timer->obj->start(); +} + +void ArduinoTimer::stop() { + timer->obj->stop(); +} \ No newline at end of file diff --git a/cores/arduino/timer.h b/cores/arduino/timer.h new file mode 100644 index 000000000..0b6a436be --- /dev/null +++ b/cores/arduino/timer.h @@ -0,0 +1,32 @@ +#include "Arduino.h" + +#ifndef __ARDUINO_TIMER_H__ +#define __ARDUINO_TIMER_H__ + +enum TimerType { + TIMER = 0x1, + LPTIMER = 0x2 +}; + +typedef struct _mbed_timer mbed_timer; + +namespace arduino { + + class ArduinoTimer { + public: + ArduinoTimer(void* _timer); + ~ArduinoTimer(); + void start(); + void stop(); + + private: + mbed_timer* timer = NULL; + }; + +} + +arduino::ArduinoTimer getTimer(TimerType t = TIMER); + +#endif //__ARDUINO_TIMER_H__ + + diff --git a/cores/arduino/wiring.cpp b/cores/arduino/wiring.cpp index c25c296b8..c3326adbc 100644 --- a/cores/arduino/wiring.cpp +++ b/cores/arduino/wiring.cpp @@ -68,6 +68,15 @@ void init() lowPowerTimer.start(); } +ArduinoTimer getTimer(TimerType t) +{ + if (t == LPTIMER) { + return ArduinoTimer((mbed::Timer*)(&lowPowerTimer)); + } else { + return ArduinoTimer(&timer); + } +} + void yield() { #ifndef NO_RTOS rtos::ThisThread::yield();