ESP32 + DEEP SLEEP + I2C-中断问题

时间:2019-03-22 00:08:21

标签: arduino microcontroller esp32

我在以ISR(中断模式)执行深度睡眠和i2c通信时遇到问题。

我正在使用此库在Arduino IDE中对其进行编码:

https://github.com/espressif/arduino-esp32

https://techtutorialsx.com/2017/09/30/esp32-arduino-external-interrupts/

当我在void loop()函数中运行i2c时,它对i2c正常工作(例如打开LED),但是当我移植它以中断它时,则不起作用。

与深度睡眠相同,我无法在中断模式下执行它。解决方法是在中断模式下设置一个标志,以表明我想进入深度睡眠状态,然后在void loop()函数中执行该标志。

有人如何解决这项工作吗? (代码仅适用于i2c和esp32)

#include <Wire.h>

#if defined(ARDUINO_ARCH_SAMD)
// for Zero, output on USB Serial  console, remove line below if using programming port to program the Zero!
   #define Serial SerialUSB
#endif

// Interrupt Setup - TIMER
hw_timer_t * timer = NULL; //configure the timer, need pointer to a variable type of hw_timer_t
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED; // used to sync main loop and ISR
RTC_DATA_ATTR bool should_sleep = false;

// Setting ADC Properties - BATTERY
int voltage_amplifier = 0; 
int battery_percentage = 0; 

// Set i2c Address - I/O EXPANDER
const int address = 0x20;
uint16_t led_status = word(B11111111,B11111111);

// INTERRUPT MODE - INSERT INBETWEEN portENTER and portEXIT
void IRAM_ATTR onTimer() {
  portENTER_CRITICAL_ISR(&timerMux);
  // led_battery();        led doesn't update if used here
  portEXIT_CRITICAL_ISR(&timerMux);
}

void led_battery(){
    voltage_amplifier = analogRead(34);
    Serial.println(voltage_amplifier);
    int bit_max = 4096;
    int battery_percentage = voltage_amplifier*100/bit_max;

    // If battery is below 20%
    if (battery_percentage <= 20){
      led_status &= word(B00111111,B11111111); // clearing the bits that we want to change whilst preserving the other unchanged bits
      led_status |= ~word(B11000000,B00000000); // setting up the bits that we want to change
      pf575_write(led_status);
    }

    else if (battery_percentage <= 40){
      led_status &= word(B00011111,B11111111); // clearing the bits that we want to change whilst preserving the other unchanged bits
      led_status |= ~word(B11100000,B00000000); // setting up the bits that we want to change
      pf575_write(led_status);
    }

    else if (battery_percentage <= 60){
      led_status &= word(B00001111,B11111111); // clearing the bits that we want to change whilst preserving the other unchanged bits
      led_status |= ~word(B11110000,B00000000); // setting up the bits that we want to change
      pf575_write(led_status);
    }

    else if (battery_percentage <= 80){
      led_status &= word(B00000111,B11111111); // clearing the bits that we want to change whilst preserving the other unchanged bits
      led_status |= ~word(B11111000,B00000000); // setting up the bits that we want to change
      pf575_write(led_status);
    }

    else if (battery_percentage <= 100){
      led_status &= word(B00000011,B11111111); // clearing the bits that we want to change whilst preserving the other unchanged bits
      led_status |= ~word(B11111100,B00000000); // setting up the bits that we want to change
      pf575_write(led_status);
    }
}

void ioexpander_setup(){
  while (!Serial);             // Leonardo: wait for serial monitor
  Serial.println("\n Blinker Ready");
  Wire.begin();
}

void pf575_write(uint16_t data) {
  Wire.beginTransmission(address);
  Wire.write(lowByte(data));
  Wire.write(highByte(data));
  Wire.endTransmission();
}

void timer_setup(){
  // Base Clock Frequency = 80MHz ; Timer Frequency = 1MHz | Clock Cycle = 1us [in this case]
  timer = timerBegin(0,80,true); // return a pointer to a structure of type hw_timer_t

  // Timer binded to a handling function
  timerAttachInterrupt(timer, &onTimer, true); // Parameter : (timer_initialization, address_interrupt,flag_to_activate - true(edge)/false(level))

  // Specify the counter value in which the timer interrupt will be generated (set every 10 ms)
  timerAlarmWrite(timer, 10000, true); // Parameter : (timer_initialization, when_to_interrupt (us), flag_to_reload)

  // Enable the timer
  timerAlarmEnable(timer);
}

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

  // IO Expander
  ioexpander_setup();

  // Timer
  timer_setup();

}

void loop() {  
  led_battery();    //led update if used here
}

1 个答案:

答案 0 :(得分:0)

当您从中断处理程序中调用led_battery()时,您在这里做了很多工作。

该中断可以中断所有没有锁定的中断。

假设您的代码正在使用Serial输出某些内容,并且发生了计时器中断。现在您的代码正在Serial内的某个地方运行代码,并且您再次调用Serial ...而软硬件可能处于不一致状态。

从中断处理程序执行的每个子例程和硬件访问都是这种情况。防止这种情况的唯一方法是,只要您的代码可能正在访问硬件或可能已经修改了数据结构,就禁用中断。

不幸的是,禁用中断很容易出错-如果您忘记这样做,将会遇到神秘的崩溃。如果您忘记重新启用它们,则会遇到大麻烦-网络,计时器和串行端口都将停止工作。这也增加了代码的开销。而且它会降低整体系统性能-会延迟或导致您错过网络和计时器事件。您可以从串行端口删除字符。您可以确定Arduino Core中没有代码为您这样做。

因此,长话短说,锁定中断以使您可以在中断处理程序中做很多事情只是不切实际。

您还希望尽量减少在中断处理程序上花费的时间,因为这会抢占网络堆栈,计时器,串行和其他硬件处理,并且可能会阻塞其他

您在原始帖子中指出了我们的处理方式:在中断处理程序中设置一个标志(确保它为volatile)并在任务中处理它。除非您真的非常了解自己在做什么以及系统中所有软件的工作方式,否则这是处理此问题的唯一实用方法。如果您尝试执行大量工作并从中断处理程序中调用要调用的内容,则程序将发生故障并崩溃。