From 17b389ee833642df03c721d44bec5b72f80f633c Mon Sep 17 00:00:00 2001 From: giulcioffi Date: Wed, 19 Jan 2022 12:23:20 +0100 Subject: [PATCH 1/3] Add getter function for Timer and LPTimer With this API it is possible to retrieve the timer from sketch and start/stop it using mbed APIs. --- cores/arduino/Arduino.h | 1 + cores/arduino/timer.h | 8 ++++++++ cores/arduino/wiring.cpp | 9 +++++++++ 3 files changed, 18 insertions(+) create mode 100644 cores/arduino/timer.h 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.h b/cores/arduino/timer.h new file mode 100644 index 000000000..3a97de610 --- /dev/null +++ b/cores/arduino/timer.h @@ -0,0 +1,8 @@ +#include "mbed.h" + +enum TimerType { + TIMER = 0x1, + LPTIMER = 0x2 +}; + +mbed::Timer* getTimer(TimerType t = TIMER); \ No newline at end of file diff --git a/cores/arduino/wiring.cpp b/cores/arduino/wiring.cpp index c25c296b8..91245d3e8 100644 --- a/cores/arduino/wiring.cpp +++ b/cores/arduino/wiring.cpp @@ -68,6 +68,15 @@ void init() lowPowerTimer.start(); } +mbed::Timer* getTimer(TimerType t) +{ + if (t == LPTIMER) { + return (mbed::Timer*)(&lowPowerTimer); + } else { + return &timer; + } +} + void yield() { #ifndef NO_RTOS rtos::ThisThread::yield(); From 32cb9f7102a81b5a9b666de6b7fd058ddf04f2f5 Mon Sep 17 00:00:00 2001 From: giulcioffi Date: Thu, 20 Jan 2022 11:06:55 +0100 Subject: [PATCH 2/3] Create ArduinoTimer class --- cores/arduino/timer.cpp | 29 +++++++++++++++++++++++++++++ cores/arduino/timer.h | 27 +++++++++++++++++++++++++-- cores/arduino/wiring.cpp | 6 +++--- 3 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 cores/arduino/timer.cpp diff --git a/cores/arduino/timer.cpp b/cores/arduino/timer.cpp new file mode 100644 index 000000000..02bf011ce --- /dev/null +++ b/cores/arduino/timer.cpp @@ -0,0 +1,29 @@ +#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) { + timer = new mbed_timer; + timer->obj = NULL; + } + if (timer->obj == NULL) { + timer->obj = (mbed::Timer*)_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 index 3a97de610..ed455acc4 100644 --- a/cores/arduino/timer.h +++ b/cores/arduino/timer.h @@ -1,8 +1,31 @@ -#include "mbed.h" +#include "Arduino.h" + +#ifndef __ARDUINO_TIMER_H__ +#define __ARDUINO_TIMER_H__ enum TimerType { TIMER = 0x1, LPTIMER = 0x2 }; -mbed::Timer* getTimer(TimerType t = TIMER); \ No newline at end of file +typedef struct _mbed_timer mbed_timer; + +namespace arduino { + + class ArduinoTimer { + public: + ArduinoTimer(void* _timer); + 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 91245d3e8..c3326adbc 100644 --- a/cores/arduino/wiring.cpp +++ b/cores/arduino/wiring.cpp @@ -68,12 +68,12 @@ void init() lowPowerTimer.start(); } -mbed::Timer* getTimer(TimerType t) +ArduinoTimer getTimer(TimerType t) { if (t == LPTIMER) { - return (mbed::Timer*)(&lowPowerTimer); + return ArduinoTimer((mbed::Timer*)(&lowPowerTimer)); } else { - return &timer; + return ArduinoTimer(&timer); } } From 07ee6bdfe14e856aff74dac6826e3df4e4bc9e54 Mon Sep 17 00:00:00 2001 From: giulcioffi Date: Fri, 21 Jan 2022 12:23:38 +0100 Subject: [PATCH 3/3] Add ArduinoTimer destructor --- cores/arduino/timer.cpp | 16 ++++++++++------ cores/arduino/timer.h | 1 + 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/cores/arduino/timer.cpp b/cores/arduino/timer.cpp index 02bf011ce..6a58694f8 100644 --- a/cores/arduino/timer.cpp +++ b/cores/arduino/timer.cpp @@ -10,14 +10,18 @@ struct _mbed_timer { ArduinoTimer::ArduinoTimer(void* _timer) { - if (timer == NULL) { - timer = new mbed_timer; - timer->obj = NULL; - } - if (timer->obj == NULL) { - timer->obj = (mbed::Timer*)_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() { diff --git a/cores/arduino/timer.h b/cores/arduino/timer.h index ed455acc4..0b6a436be 100644 --- a/cores/arduino/timer.h +++ b/cores/arduino/timer.h @@ -15,6 +15,7 @@ namespace arduino { class ArduinoTimer { public: ArduinoTimer(void* _timer); + ~ArduinoTimer(); void start(); void stop();