arduino / ArduinoCore-mbed

330 stars 195 forks source link

Raspberry Pi Pico - Not allowed in ISR context #527

Closed alexhorner closed 2 years ago

alexhorner commented 2 years ago

Hi there,

I am trying to run an Arduino sketch which works fine on an ESP32 on my Pi Pico. The device unfortunately crashes when trying to do this.

The sketch contains the following code, and is intended to read Dallas iButton OneWire devices using the OneWire library found at https://www.pjrc.com/teensy/td_libs_OneWire.html by Paul Stoffregen, version 2.3.7

#include <OneWire.h>

OneWire ds(28);

void setup(void) {
  Serial.begin(115200);
}

void loop(void) {
  byte i;
  byte present = 0;
  byte data[12];
  byte addr[8];

  if ( !ds.search(addr)) {
    Serial.print("No more addresses.\n");
    ds.reset_search();
    delay(0);
    return;
  }

  Serial.print("R=");
  for( i = 0; i < 8; i++) {
    Serial.print(addr[i], HEX);
    Serial.print(" ");
  }

  if ( OneWire::crc8(addr, 7) != addr[7]) {
      Serial.print("CRC is not valid!\n");
      return;
  }

  if ( addr[0] != 0x10) {
      Serial.print("Device is not a DS18S20 family device.\n");
      return;
  }

  // The DallasTemperature library can do all this work for you!

  ds.reset();
  ds.select(addr);
  ds.write(0x44,1);         // start conversion, with parasite power on at the end

  delay(1000);     // maybe 750ms is enough, maybe not
  // we might do a ds.depower() here, but the reset will take care of it.

  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE);         // Read Scratchpad

  Serial.print("P=");
  Serial.print(present,HEX);
  Serial.print(" ");
  for ( i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] = ds.read();
    Serial.print(data[i], HEX);
    Serial.print(" ");
  }
  Serial.print(" CRC=");
  Serial.print( OneWire::crc8( data, 8), HEX);
  Serial.println();
}

I have captured the output from GPIO0

++ MbedOS Error Info ++
Error Status: 0x80010133 Code: 307 Module: 1
Error Message: Mutex: 0x20009A98, Not allowed in ISR context
Location: 0x100068D7
Error Value: 0x20009A98
Current Thread: main Id: 0x2000A6B4 Entry: 0x100067F9 StackSize: 0x8000 StackMem: 0x20000FE0 SP: 0x20008E64
For more info, visit: https://mbed.com/s/error?error=0x80010133&osver=61600&core=0x410CC601&comp=2&ver=110300&tgt=RASPBERRY_PI...

-- MbedOS Error Info --

Not sure what this means or where to take this. The error occurs immediately on start of the Pico, so no hardware should be required to reproduce this issue,

Thanks!

facchinm commented 2 years ago

Hi @alexhorner , I think that the issue might be due to the constructor OneWire ds(28) . That particualr constructor is a shorthand for

OneWire ds;
...
void setup() {
 ...
 ds.begin(28);
 ...
}

(see https://github.com/PaulStoffregen/OneWire/blob/master/OneWire.h#L73 )

Since the constructor gets called before the rtos is fully configured you'll get an error on pinMode() (which internally calles malloc).

Replacing the short version with the one with begin() should do the trick!

Let me know if it works