Skip to content

Commit cd7acd7

Browse files
authored
Add getter function for Timer and LPTimer (#387)
With this API it is possible to retrieve the timer from sketch and start/stop it using mbed APIs.
1 parent 4eef7c8 commit cd7acd7

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

cores/arduino/Arduino.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ extern analogin_config_t adcCurrentConfig;
100100
#endif
101101

102102
#include "Serial.h"
103+
#include "timer.h"
103104
#if defined(SERIAL_CDC)
104105
#define Serial _UART_USB_
105106
#define SerialUSB _UART_USB_

cores/arduino/timer.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "Arduino.h"
2+
#include "timer.h"
3+
#include "mbed.h"
4+
5+
using namespace arduino;
6+
7+
struct _mbed_timer {
8+
mbed::Timer* obj;
9+
};
10+
11+
ArduinoTimer::ArduinoTimer(void* _timer) {
12+
13+
if (timer != NULL) {
14+
delete timer;
15+
}
16+
17+
timer = new mbed_timer;
18+
timer->obj = (mbed::Timer*)_timer;
19+
}
20+
21+
ArduinoTimer::~ArduinoTimer() {
22+
if (timer != NULL) {
23+
delete timer;
24+
}
25+
}
26+
27+
void ArduinoTimer::start() {
28+
timer->obj->start();
29+
}
30+
31+
void ArduinoTimer::stop() {
32+
timer->obj->stop();
33+
}

cores/arduino/timer.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "Arduino.h"
2+
3+
#ifndef __ARDUINO_TIMER_H__
4+
#define __ARDUINO_TIMER_H__
5+
6+
enum TimerType {
7+
TIMER = 0x1,
8+
LPTIMER = 0x2
9+
};
10+
11+
typedef struct _mbed_timer mbed_timer;
12+
13+
namespace arduino {
14+
15+
class ArduinoTimer {
16+
public:
17+
ArduinoTimer(void* _timer);
18+
~ArduinoTimer();
19+
void start();
20+
void stop();
21+
22+
private:
23+
mbed_timer* timer = NULL;
24+
};
25+
26+
}
27+
28+
arduino::ArduinoTimer getTimer(TimerType t = TIMER);
29+
30+
#endif //__ARDUINO_TIMER_H__
31+
32+

cores/arduino/wiring.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ void init()
6868
lowPowerTimer.start();
6969
}
7070

71+
ArduinoTimer getTimer(TimerType t)
72+
{
73+
if (t == LPTIMER) {
74+
return ArduinoTimer((mbed::Timer*)(&lowPowerTimer));
75+
} else {
76+
return ArduinoTimer(&timer);
77+
}
78+
}
79+
7180
void yield() {
7281
#ifndef NO_RTOS
7382
rtos::ThisThread::yield();

0 commit comments

Comments
 (0)