enhance esp32 usage by hidding concurrency handle implementation using freertos task

pull/26/head
midilab 9 months ago
parent 9c08252ac3
commit 78a503a629
  1. 15
      examples/ESP32UartMasterMidiClock/ESP32UartMasterMidiClock.ino
  2. 2
      library.json
  3. 2
      library.properties
  4. 57
      src/platforms/esp32.h
  5. 4
      src/platforms/samd.h
  6. 4
      src/platforms/stm32.h
  7. 4
      src/platforms/teensy.h
  8. 6
      src/uClock.cpp
  9. 2
      src/uClock.h

@ -18,8 +18,6 @@
// the blue led
#define LED_BUILTIN 2
volatile bool _midi_clk_income = false;
uint8_t bpm_blink_timer = 1;
void handle_bpm_led(uint32_t tick)
{
@ -38,8 +36,7 @@ void handle_bpm_led(uint32_t tick)
// Internal clock handlers
void ClockOut96PPQN(uint32_t tick) {
// Send MIDI_CLOCK to external gears
//Serial.write(MIDI_CLOCK);
_midi_clk_income = true;
Serial.write(MIDI_CLOCK);
handle_bpm_led(tick);
}
@ -74,11 +71,5 @@ void setup() {
// Do it whatever to interface with Clock.stop(), Clock.start(), Clock.setTempo() and integrate your environment...
void loop() {
// watch for income signal from uClock to fire the clock over midi
if (_midi_clk_income) {
Serial.write(MIDI_CLOCK);
noInterrupts();
_midi_clk_income = false;
interrupts();
}
}
}

@ -1,6 +1,6 @@
{
"name": "uClock",
"version": "1.4.1",
"version": "1.4.2",
"description": "A Library to implement BPM clock tick calls using hardware interruption. Supported and tested on AVR boards(ATmega168/328, ATmega16u4/32u4 and ATmega2560) and ARM boards(Teensy, Seedstudio XIAO M0. ESP32 and STM32)",
"keywords": "bpm, clock, timing, tick, music, generator",
"repository":

@ -1,5 +1,5 @@
name=uClock
version=1.4.1
version=1.4.2
author=Romulo Silva <contact@midilab.co>
maintainer=Romulo Silva <contact@midilab.co>
sentence=BPM clock generator for Arduino platform.

@ -1,26 +1,71 @@
#include <Arduino.h>
#include <freertos/task.h>
#include <freertos/semphr.h>
#define TIMER_ID 0
hw_timer_t * _uclockTimer = NULL;
portMUX_TYPE _uclockTimerMux = portMUX_INITIALIZER_UNLOCKED;
#define ATOMIC(X) portENTER_CRITICAL_ISR(&_uclockTimerMux); X; portEXIT_CRITICAL_ISR(&_uclockTimerMux);
// mutex control for ISR
//portMUX_TYPE _uclockTimerMux = portMUX_INITIALIZER_UNLOCKED;
//#define ATOMIC(X) portENTER_CRITICAL_ISR(&_uclockTimerMux); X; portEXIT_CRITICAL_ISR(&_uclockTimerMux);
// FreeRTOS main clock task size in bytes
#define CLOCK_STACK_SIZE 2048
// semaphore to signal the task from the ISR
SemaphoreHandle_t _semaphore;
// mutex to protect the shared resource
SemaphoreHandle_t _mutex;
// mutex control for task
#define ATOMIC(X) \
do { \
xSemaphoreTake(_mutex, portMAX_DELAY); \
X; \
xSemaphoreGive(_mutex); \
} while (0);
// forward declaration of uClockHandler
void uClockHandler();
// ISR handler
void ARDUINO_ISR_ATTR handlerISR(void)
{
// notify the clockTask using the semaphore
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
xSemaphoreGiveFromISR(_semaphore, &xHigherPriorityTaskWoken);
// context switch is required? request it
if (xHigherPriorityTaskWoken == pdTRUE)
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
// forward declaration of ISR
void ARDUINO_ISR_ATTR uclockISR();
// task for user clock process
void clockTask(void *pvParameters)
{
while (1)
if (xSemaphoreTake(_semaphore, portMAX_DELAY) == pdTRUE)
uClockHandler();
}
void initTimer(uint32_t init_clock)
{
_uclockTimer = timerBegin(TIMER_ID, 80, true);
// attach to generic uclock ISR
timerAttachInterrupt(_uclockTimer, &uclockISR, false);
timerAttachInterrupt(_uclockTimer, &handlerISR, false);
// init clock tick time
timerAlarmWrite(_uclockTimer, init_clock, true);
// activate it!
timerAlarmEnable(_uclockTimer);
// initialize the semaphore
_semaphore = xSemaphoreCreateBinary();
// initialize the mutex for shared resource access
_mutex = xSemaphoreCreateMutex();
// create the clockTask
xTaskCreate(clockTask, "clockTask", CLOCK_STACK_SIZE, NULL, 1, NULL);
}
void setTimer(uint32_t us_interval)

@ -11,14 +11,14 @@
IntervalTimer _uclockTimer;
// forward declaration of ISR
void uclockISR();
void uClockHandler();
void initTimer(uint32_t init_clock)
{
TimerTcc0.initialize(init_clock);
// attach to generic uclock ISR
TimerTcc0.attachInterrupt(uclockISR);
TimerTcc0.attachInterrupt(uClockHandler);
}
void setTimer(uint32_t us_interval)

@ -16,12 +16,12 @@ HardwareTimer * _uclockTimer = new HardwareTimer(TimerInstance);
#define ATOMIC(X) noInterrupts(); X; interrupts();
// forward declaration of ISR
void uclockISR();
void uClockHandler();
void initTimer(uint32_t us_interval)
{
_uclockTimer->setOverflow(us_interval, MICROSEC_FORMAT);
_uclockTimer->attachInterrupt(uclockISR);
_uclockTimer->attachInterrupt(uClockHandler);
_uclockTimer->resume();
}

@ -5,11 +5,11 @@
IntervalTimer _uclockTimer;
// forward declaration of ISR
void uclockISR();
void uClockHandler();
void initTimer(uint32_t init_clock)
{
_uclockTimer.begin(uclockISR, init_clock);
_uclockTimer.begin(uClockHandler, init_clock);
// Set the interrupt priority level, controlling which other interrupts
// this timer is allowed to interrupt. Lower numbers are higher priority,

@ -2,7 +2,7 @@
* @file uClock.cpp
* Project BPM clock generator for Arduino
* @brief A Library to implement BPM clock tick calls using hardware interruption. Supported and tested on AVR boards(ATmega168/328, ATmega16u4/32u4 and ATmega2560) and ARM boards(Teensy, Seedstudio XIAO M0 and ESP32)
* @version 1.4.1
* @version 1.4.2
* @author Romulo Silva
* @date 10/06/2017
* @license MIT - (c) 2022 - Romulo Silva - contact@midilab.co
@ -414,10 +414,8 @@ volatile uint32_t _timer = 0;
//
#if defined(ARDUINO_ARCH_AVR)
ISR(TIMER1_COMPA_vect)
#elif defined(ARDUINO_ARCH_ESP32) || defined(ESP32)
void ARDUINO_ISR_ATTR uclockISR()
#else
void uclockISR()
void uClockHandler()
#endif
{
// global timer counter

@ -2,7 +2,7 @@
* @file uClock.h
* Project BPM clock generator for Arduino
* @brief A Library to implement BPM clock tick calls using hardware interruption. Supported and tested on AVR boards(ATmega168/328, ATmega16u4/32u4 and ATmega2560) and ARM boards(Teensy, Seedstudio XIAO M0 and ESP32)
* @version 1.4.1
* @version 1.4.2
* @author Romulo Silva
* @date 10/06/2017
* @license MIT - (c) 2022 - Romulo Silva - contact@midilab.co

Loading…
Cancel
Save