r65emu/timed.cpp

51 lines
1.3 KiB
C++
Raw Normal View History

2018-08-17 14:22:49 +00:00
#include <Arduino.h>
2018-08-10 12:36:41 +00:00
2018-08-17 14:22:49 +00:00
#if defined(__LM4F120H5QR__)
2014-11-13 19:20:01 +00:00
#include <inc/hw_ints.h>
#include <driverlib/interrupt.h>
#include <driverlib/sysctl.h>
#include <driverlib/timer.h>
2018-11-11 11:21:12 +00:00
#elif defined(ESP8266)
#include <ets_sys.h>
#include <osapi.h>
#include <os_type.h>
2018-08-17 14:22:49 +00:00
#endif
2014-11-13 19:20:01 +00:00
#include "timed.h"
2018-08-17 14:22:49 +00:00
#if defined(__LM4F120H5QR__)
2018-11-11 11:21:12 +00:00
static handler_t client_handler;
2018-09-06 09:12:03 +00:00
2014-11-13 19:20:01 +00:00
static void timer0isr(void) {
ROM_TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
2018-09-06 09:12:03 +00:00
client_handler();
2014-11-13 19:20:01 +00:00
}
2018-11-11 11:21:12 +00:00
void timer_create(unsigned freq, handler_t handler) {
2018-09-06 09:12:03 +00:00
client_handler = handler;
2014-11-13 19:20:01 +00:00
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
ROM_TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);
TimerIntRegister(TIMER0_BASE, TIMER_A, timer0isr);
ROM_TimerEnable(TIMER0_BASE, TIMER_A);
ROM_IntEnable(INT_TIMER0A);
ROM_TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
ROM_TimerLoadSet(TIMER0_BASE, TIMER_A, ROM_SysCtlClockGet() / freq);
}
2018-08-17 14:22:49 +00:00
#elif defined(ESP_PLATFORM)
2018-11-11 11:21:12 +00:00
void timer_create(unsigned freq, handler_t handler) {
2018-08-17 14:22:49 +00:00
hw_timer_t *timer = timerBegin(3, 80, true); // prescaler of 80
2018-09-06 09:12:03 +00:00
timerAttachInterrupt(timer, handler, true);
2018-08-17 14:22:49 +00:00
timerAlarmWrite(timer, 1000000 / freq, true);
timerAlarmEnable(timer);
}
2018-11-11 11:21:12 +00:00
#elif defined(ESP8266)
void timer_create(unsigned freq, handler_t handler) {
static os_timer_t t;
os_timer_setfn(&t, (os_timer_func_t *)handler, 0);
os_timer_arm(&t, 1000 / freq, true);
}
2018-08-10 12:36:41 +00:00
#endif