-create two tasks, each pinned to a core
-on core 0 I launch a hardware timer interrupt to signal to the task to toggle a pin every 100 us
-on core 1 I use the SerialBT.println() function from BluetoothSerial.h to send some gibberish on Bluetooth
I measured the pin with an oscilloscope, and it toggles totally irregularly, the frequency changes constantly. HOWEVER, when I delete the SerialBT.println() function, or it's not connected to the pc via bluetooth, or just use Serial.println() instead, it works well and toggles at a constant frequency. I tried many things and I really don't know why it affects something on the other core.
EDIT: After finding the run/stop button on the oscilloscope, I realised that it still mostly toggles at a regular 100us interval, but every after every few toggle it forgets to toggle for a few ms usually. The interval and the pulses between these glitches seem to be irrergular. So the problem remains, but I just added this info.
I also noticed the interruptCounter goes up like it's supposed to during these stops. So it's just the core 0 function somehow not responding to that. My theory is that while the Bluetooth is sending the packets, it needs both cores so the original core 0 task gets suspended. I don't know if this is the case, but of it is, is there anything I can do about it? Maybe change something in the BluetoothSerial library.
- #include "BluetoothSerial.h"
- #include "esp_task_wdt.h"
- volatile int interruptCounter = 0;
- hw_timer_t * timer = NULL;
- portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
- BluetoothSerial SerialBT;
- //interrupt routine
- void IRAM_ATTR onTimer() {
- portENTER_CRITICAL_ISR(&timerMux);
- interruptCounter++;
- portEXIT_CRITICAL_ISR(&timerMux);
- }
- void setup() {
- Serial.begin(2000000);
- SerialBT.begin("ESP32"); //Bluetooth device name
- Serial.println("The device started, now you can pair it with bluetooth!");
- pinMode(26, OUTPUT);
- disableCore0WDT();
- disableCore1WDT();
- xTaskCreatePinnedToCore(
- adc_read, /* Function to implement the task */
- "adc_read_task", /* Name of the task */
- 1024, /* Stack size in words */
- NULL, /* Task input parameter */
- 1, /* Priority of the task */
- NULL, /* Task handle. */
- 0); /* Core where the task should run */
- xTaskCreatePinnedToCore(
- bl_send, /* Function to implement the task */
- "bl_send_task", /* Name of the task */
- 1024, /* Stack size in words */
- NULL, /* Task input parameter */
- 2, /* Priority of the task */
- NULL, /* Task handle. */
- 1); /* Core where the task should run */
- }
- void loop() {vTaskDelete(NULL);}
- static void adc_read (void *pvParameters )
- {
- //launch the timer interrupt on core 0
- timer = timerBegin(0, 80, true);
- timerAttachInterrupt(timer, &onTimer, true);
- timerAlarmWrite(timer, 100, true);
- timerAlarmEnable(timer);
- for(;;) {
- if (interruptCounter > 0) {
- portENTER_CRITICAL(&timerMux);
- interruptCounter=0;
- portEXIT_CRITICAL(&timerMux);
- digitalWrite(26, !digitalRead(26));
- }
- }
- }
- static void bl_send (void *pvParameters )
- {
- for( ;; ) {
- SerialBT.println("1");
- }
- }