use interrupts instead of FreeRTOS tasks -- seems to work with both cores?

pull/33/head
doctea 9 months ago
parent 53419fa89c
commit 99d1845201
  1. 8
      examples/RP2040ClockBlink/RP2040ClockBlink.ino
  2. 30
      examples/RP2040UsbMasterMidiClock/RP2040UsbMasterMidiClock.ino
  3. 43
      src/platforms/rp2040.h

@ -11,6 +11,8 @@
#include <uClock.h>
#define ATOMIC(X) { uint32_t __interrupt_mask = save_and_disable_interrupts(); X; restore_interrupts(__interrupt_mask); }
uint8_t bpm_blink_timer = 1;
void handle_bpm_led(uint32_t tick)
{
@ -50,14 +52,14 @@ void setup() {
// A led to count bpms
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
/*digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
delay(500);*/
Serial.begin(115200);
@ -88,5 +90,5 @@ void loop() {
//MIDI_USB.read();
//count++;
//if (millis()%1000==0)
// Serial.println("looped!");
// ATOMIC(Serial.println("looped!"));
}

@ -9,11 +9,16 @@
Adafruit_USBD_MIDI usb_midi;
MIDI_CREATE_INSTANCE(Adafruit_USBD_MIDI, usb_midi, MIDI_USB);
//MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI);
#include <uClock.h>
#define ATOMIC(X) { uint32_t __interrupt_mask = save_and_disable_interrupts(); X; restore_interrupts(__interrupt_mask); }
//#define LED_BUILTIN PIN_LED_B
//#define WAIT_FOR_SERIAL
#define ENABLE_MULTICORE
//MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI);
#include <uClock.h>
volatile uint32_t count = 0;
uint8_t bpm_blink_timer = 1;
void handle_bpm_led(uint32_t tick)
@ -36,6 +41,8 @@ void onSync24Callback(uint32_t tick) {
// Send MIDI_CLOCK to external gears
MIDI_USB.sendRealTime(midi::Clock);
handle_bpm_led(tick);
Serial.printf("ticked with %u\n", tick);
}
void onClockStart() {
@ -46,6 +53,16 @@ void onClockStop() {
MIDI_USB.sendRealTime(midi::Stop);
}
#ifdef ENABLE_MULTICORE
void setup1() {
}
void loop1() {
if (count%1000==0)
ATOMIC(Serial.println("loop1()!"); Serial.flush());
}
#endif
void setup() {
#if defined(ARDUINO_ARCH_MBED) && defined(ARDUINO_ARCH_RP2040)
// Manual begin() is required on core without built-in support for TinyUSB such as mbed rp2040
@ -58,8 +75,10 @@ void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
#ifdef WAIT_FOR_SERIAL
while (!Serial)
delay(1);
#endif
// wait until device mounted
/*while( !TinyUSBDevice.mounted() ) {
@ -78,19 +97,18 @@ void setup() {
uClock.setOnClockStart(onClockStart);
uClock.setOnClockStop(onClockStop);
// Set the clock BPM to 126 BPM
uClock.setTempo(126);
uClock.setTempo(60);
// Starts the clock, tick-tac-tick-tac..
Serial.println("about to uClock.start()..."); Serial.flush();
uClock.start();
Serial.println("uClock.start()ed!"); Serial.flush();
ATOMIC(Serial.println("uClock.start()ed!"); Serial.flush();)
}
uint32_t count = 0;
// Do it whatever to interface with Clock.stop(), Clock.start(), Clock.setTempo() and integrate your environment...
void loop() {
MIDI_USB.read();
count++;
if (millis()%1000==0)
Serial.println("looped!!!");
ATOMIC(Serial.println("loop()!"); Serial.flush(););
}

@ -1,8 +1,45 @@
#include <Arduino.h>
#include "pico/sync.h"
#define MULTICORE
#ifdef MULTICORE
// use interrupt version -- works for 2 cores ie can run loop1() and loop() simultaneously as well as the clock callback?
// RPi-specific timer
struct repeating_timer timer;
#define ATOMIC(X) { uint32_t __interrupt_mask = save_and_disable_interrupts(); X; restore_interrupts(__interrupt_mask); }
// forward declaration of uClockHandler
void uClockHandler();
// ISR handler -- called when tick happens
bool handlerISR(repeating_timer *timer)
{
uClockHandler();
return true;
}
void initTimer(uint32_t init_clock)
{
// set up RPi interrupt timer
// todo: actually should be -init_clock so that timer is set to start init_clock us after last tick, instead of init_clock us after finished processing last tick!
add_repeating_timer_us(init_clock, &handlerISR, NULL, &timer);
}
void setTimer(uint32_t us_interval) {
cancel_repeating_timer(&timer);
// todo: actually should be -init_clock so that timer is set to start init_clock us after last tick, instead of init_clock us after finished processing last tick!
add_repeating_timer_us(us_interval, &handlerISR, NULL, &timer);
}
#else
// use FreeRTOS scheduling/mutex version -- doesn't work (task starts but does not run) if using loop1() ie core 2
#include "FreeRTOS.h"
#include <task.h>
#include <semphr.h>
#include "pico/sync.h"
// RPi-specific timer
struct repeating_timer timer;
@ -48,10 +85,14 @@ void initTimer(uint32_t init_clock)
xTaskCreate(clockTask, "clockTask", CLOCK_STACK_SIZE, NULL, 1, &taskHandle);
// set up RPi interrupt timer
// todo: actually should be -init_clock so that timer is set to start init_clock us after last tick, instead of init_clock us after finished processing last tick!
add_repeating_timer_us(init_clock, &handlerISR, NULL, &timer);
}
void setTimer(uint32_t us_interval) {
cancel_repeating_timer(&timer);
// todo: actually should be -init_clock so that timer is set to start init_clock us after last tick, instead of init_clock us after finished processing last tick!
add_repeating_timer_us(us_interval, &handlerISR, NULL, &timer);
}
#endif
Loading…
Cancel
Save