mirror of
https://github.com/sipeed/Maixduino.git
synced 2026-03-03 00:53:59 +01:00
Add Ticker lib (Hardware Timer)
This commit is contained in:
@@ -107,6 +107,7 @@ UARTClass::read(void)
|
||||
while(this->_buff->available()){
|
||||
return this->_buff->read_char();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
26
libraries/Ticker/examples/Arguments/Arguments.ino
Normal file
26
libraries/Ticker/examples/Arguments/Arguments.ino
Normal file
@@ -0,0 +1,26 @@
|
||||
#include <Arduino.h>
|
||||
#include <Ticker.h>
|
||||
|
||||
// attach a LED to GPIO 21
|
||||
#define LED_PIN 13
|
||||
|
||||
Ticker tickerSetHigh(TIMER1);
|
||||
Ticker tickerSetLow(TIMER2);
|
||||
|
||||
void setPin(int state) {
|
||||
digitalWrite(LED_PIN, state);
|
||||
}
|
||||
|
||||
void setup() {
|
||||
pinMode(LED_PIN, OUTPUT);
|
||||
|
||||
// every 25 ms, call setPin(0)
|
||||
tickerSetLow.attach_ms(25, setPin, 0);
|
||||
|
||||
// every 26 ms, call setPin(1)
|
||||
tickerSetHigh.attach_ms(26, setPin, 1);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
}
|
||||
42
libraries/Ticker/examples/Blinker/Blinker.ino
Normal file
42
libraries/Ticker/examples/Blinker/Blinker.ino
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <Arduino.h>
|
||||
#include <Ticker.h>
|
||||
|
||||
// attach a LED to pPIO 13
|
||||
#define LED_PIN 13
|
||||
|
||||
Ticker blinker(TIMER0);
|
||||
Ticker toggler(TIMER1);
|
||||
Ticker changer(TIMER2);
|
||||
float blinkerPace = 0.1; //seconds
|
||||
const float togglePeriod = 5; //seconds
|
||||
|
||||
void change() {
|
||||
blinkerPace = 0.5;
|
||||
}
|
||||
|
||||
void blink() {
|
||||
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
|
||||
}
|
||||
|
||||
void toggle() {
|
||||
static bool isBlinking = false;
|
||||
if (isBlinking) {
|
||||
blinker.detach();
|
||||
isBlinking = false;
|
||||
}
|
||||
else {
|
||||
blinker.attach(blinkerPace, blink);
|
||||
isBlinking = true;
|
||||
}
|
||||
digitalWrite(LED_PIN, LOW); //make sure LED on on after toggling (pin LOW = led ON)
|
||||
}
|
||||
|
||||
void setup() {
|
||||
pinMode(LED_PIN, OUTPUT);
|
||||
toggler.attach(togglePeriod, toggle);
|
||||
changer.once(30, change);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
}
|
||||
22
libraries/Ticker/keywords.txt
Normal file
22
libraries/Ticker/keywords.txt
Normal file
@@ -0,0 +1,22 @@
|
||||
#######################################
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
|
||||
Ticker KEYWORD1
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
attach KEYWORD2
|
||||
attach_ms KEYWORD2
|
||||
once KEYWORD2
|
||||
once_ms KEYWORD2
|
||||
detach KEYWORD2
|
||||
|
||||
#######################################
|
||||
# Constants (LITERAL1)
|
||||
#######################################
|
||||
TIMER0 LITERAL1
|
||||
TIMER1 LITERAL1
|
||||
TIMER2 LITERAL1
|
||||
9
libraries/Ticker/library.properties
Normal file
9
libraries/Ticker/library.properties
Normal file
@@ -0,0 +1,9 @@
|
||||
name=Ticker
|
||||
version=1.0
|
||||
author=BigBits
|
||||
maintainer=BigBits <bigbits@bigbits.cc>
|
||||
sentence=Allows to call functions with a given interval.
|
||||
paragraph=Allows to call functions with a given interval.
|
||||
category=Timing
|
||||
url=
|
||||
architectures=k210
|
||||
38
libraries/Ticker/src/Ticker.cpp
Normal file
38
libraries/Ticker/src/Ticker.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
#include "Ticker.h"
|
||||
#include "sysctl.h"
|
||||
|
||||
Ticker::Ticker(timer_id_t id)
|
||||
:timer_id(id)
|
||||
{
|
||||
timer_init(timer_device_number_t(timer_id));
|
||||
}
|
||||
|
||||
Ticker::~Ticker()
|
||||
{
|
||||
detach();
|
||||
}
|
||||
|
||||
void
|
||||
Ticker::_attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, size_t arg)
|
||||
{
|
||||
user_callback = callback;
|
||||
_arg = arg;
|
||||
timer_set_interval(timer_device_number_t(timer_id), TIMER_CHANNEL_0, milliseconds * 1000000);
|
||||
timer_irq_register(timer_device_number_t(timer_id), TIMER_CHANNEL_0, !repeat, 4, timer_callback, this);
|
||||
timer_set_enable(timer_device_number_t(timer_id), TIMER_CHANNEL_0, 1);
|
||||
sysctl_enable_irq();
|
||||
}
|
||||
|
||||
void
|
||||
Ticker::detach()
|
||||
{
|
||||
timer_irq_unregister(timer_device_number_t(timer_id), TIMER_CHANNEL_0);
|
||||
timer_set_enable(timer_device_number_t(timer_id), TIMER_CHANNEL_0, 0);
|
||||
}
|
||||
|
||||
static int timer_callback(void* ctx)
|
||||
{
|
||||
auto &driver = *reinterpret_cast<Ticker *>(ctx);
|
||||
driver.user_callback((void *)driver._arg);
|
||||
return 0;
|
||||
}
|
||||
93
libraries/Ticker/src/Ticker.h
Normal file
93
libraries/Ticker/src/Ticker.h
Normal file
@@ -0,0 +1,93 @@
|
||||
#ifndef TICKER_H
|
||||
#define TICKER_H
|
||||
|
||||
|
||||
#include "utils.h"
|
||||
#include "timer.h"
|
||||
#include "stdio.h"
|
||||
#include "stdint.h"
|
||||
|
||||
typedef enum{
|
||||
TIMER0 = 0,
|
||||
TIMER1,
|
||||
TIMER2,
|
||||
TIMER_MAX
|
||||
}timer_id_t;
|
||||
|
||||
class Ticker
|
||||
{
|
||||
public:
|
||||
Ticker(timer_id_t id = TIMER2);
|
||||
~Ticker();
|
||||
typedef void (*callback_t)(void);
|
||||
typedef void (*callback_with_arg_t)(void*);
|
||||
|
||||
void attach(float seconds, callback_t callback)
|
||||
{
|
||||
_attach_ms(seconds * 1000, true, reinterpret_cast<callback_with_arg_t>(callback), 0);
|
||||
}
|
||||
|
||||
void attach_ms(uint32_t milliseconds, callback_t callback)
|
||||
{
|
||||
_attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), 0);
|
||||
}
|
||||
|
||||
template<typename TArg>
|
||||
void attach(float seconds, void (*callback)(TArg), TArg arg)
|
||||
{
|
||||
configASSERT(sizeof(TArg) <= sizeof(size_t));
|
||||
size_t arg32 = (size_t)arg;
|
||||
_attach_ms(seconds * 1000, true, reinterpret_cast<callback_with_arg_t>(callback), arg32);
|
||||
}
|
||||
|
||||
template<typename TArg>
|
||||
void attach_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg)
|
||||
{
|
||||
configASSERT(sizeof(TArg) <= sizeof(size_t));
|
||||
size_t arg32 = (size_t)arg;
|
||||
_attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), arg32);
|
||||
}
|
||||
|
||||
void once(float seconds, callback_t callback)
|
||||
{
|
||||
_attach_ms(seconds * 1000, false, reinterpret_cast<callback_with_arg_t>(callback), 0);
|
||||
}
|
||||
|
||||
void once_ms(uint32_t milliseconds, callback_t callback)
|
||||
{
|
||||
_attach_ms(milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), 0);
|
||||
}
|
||||
|
||||
template<typename TArg>
|
||||
void once(float seconds, void (*callback)(TArg), TArg arg)
|
||||
{
|
||||
configASSERT(sizeof(TArg) <= sizeof(size_t));
|
||||
size_t arg32 = (size_t)(arg);
|
||||
_attach_ms(seconds * 1000, false, reinterpret_cast<callback_with_arg_t>(callback), arg32);
|
||||
}
|
||||
|
||||
template<typename TArg>
|
||||
void once_ms(uint32_t milliseconds, void (*callback)(TArg), TArg arg)
|
||||
{
|
||||
configASSERT(sizeof(TArg) <= sizeof(size_t));
|
||||
size_t arg32 = (size_t)(arg);
|
||||
_attach_ms(milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), arg32);
|
||||
}
|
||||
|
||||
void detach();
|
||||
bool active();
|
||||
|
||||
callback_with_arg_t user_callback = NULL;
|
||||
size_t _arg;
|
||||
|
||||
protected:
|
||||
void _attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, size_t arg);
|
||||
|
||||
private:
|
||||
timer_id_t timer_id;
|
||||
|
||||
}__attribute__((packed));
|
||||
|
||||
static int timer_callback(void* ctx);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user