[Solved] Strange behavior Sleep() function and interrupts in MyS library 2.1.1



  • After upgrading the MySensors library from 2.0.0 to 2.1.1 I noticed that the sleep function with interrupts was working different than before. Have a look at the simple test sketch below . In 2.0.0 the loop is called only once after an interrupt. In 2.1.1 the loop is called twice before the sleep function becomes active. Below you have the serial log for 2.0.0 and 2.1.1. for 2 interrupts. Is this a bug or do I need to change some settings or use a different code?

    //Test sleep function and interrupt in library 2.1.1 and 2.0.0
    
    #define MY_DEBUG 
    
    #include <SPI.h>
    #define MY_RADIO_NRF24
    #include <MySensors.h>
    byte countInterrupt;
    void isr() {}
                                                                                      
    void setup(void) {
      pinMode(3, INPUT_PULLUP); // intterupt 1
      attachInterrupt(digitalPinToInterrupt(3), isr,FALLING); 
    }
    
    void loop() {
      sleep(1,FALLING,0);
      Serial.print("interrupt ");
      Serial.println(countInterrupt++);
      wait(1000);
    }
    

    Serial log 2.0.0 (this one works ok).

    Starting sensor (RNNNA-, 2.0.0)
    TSM:INIT
    TSM:RADIO:OK
    TSP:ASSIGNID:OK (ID=2)
    TSM:FPAR
    TSP:MSG:SEND 2-2-255-255 s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=bc:
    TSP:MSG:READ 0-0-2 s=255,c=3,t=8,pt=1,l=1,sg=0:0
    TSP:MSG:FPAR RES (ID=0, dist=0)
    TSP:MSG:PAR OK (ID=0, dist=1)
    TSM:FPAR:OK
    TSM:ID
    TSM:CHKID:OK (ID=2)
    TSM:UPL
    TSP:PING:SEND (dest=0)
    TSP:MSG:SEND 2-2-0-0 s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=ok:1
    TSP:MSG:READ 0-0-2 s=255,c=3,t=25,pt=1,l=1,sg=0:1
    TSP:MSG:PONG RECV (hops=1)
    TSP:CHKUPL:OK
    TSM:UPL:OK
    TSM:READY
    TSP:MSG:SEND 2-2-0-0 s=255,c=3,t=15,pt=6,l=2,sg=0,ft=0,st=ok:0100
    TSP:MSG:SEND 2-2-0-0 s=255,c=0,t=17,pt=0,l=5,sg=0,ft=0,st=ok:2.0.0
    TSP:MSG:SEND 2-2-0-0 s=255,c=3,t=6,pt=1,l=1,sg=0,ft=0,st=ok:0
    TSP:MSG:READ 0-0-2 s=255,c=3,t=15,pt=6,l=2,sg=0:0100
    TSP:MSG:READ 0-0-2 s=255,c=3,t=6,pt=0,l=1,sg=0:M
    Request registration...
    TSP:MSG:SEND 2-2-0-0 s=255,c=3,t=26,pt=1,l=1,sg=0,ft=0,st=ok:2
    TSP:MSG:READ 0-0-2 s=255,c=3,t=27,pt=1,l=1,sg=0:1
    Node registration=1
    Init complete, id=2, parent=0, distance=1, registration=1
    ****************** Start first interrupt *************************************
    interrupt 0
    ***************** Start second interrupt *************************************
    interrupt 1
    

    Serial log 2.1.1

    0 MCO:BGN:INIT NODE,CP=RNNNA--,VER=2.1.1
    4 TSM:INIT
    4 TSF:WUR:MS=0
    12 TSM:INIT:TSP OK
    14 TSF:SID:OK,ID=2
    16 TSM:FPAR
    51 TSF:MSG:SEND,2-2-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
    2060 !TSM:FPAR:NO REPLY
    2062 TSM:FPAR
    2099 TSF:MSG:SEND,2-2-255-255,s=255,c=3,t=7,pt=0,l=0,sg=0,ft=0,st=OK:
    2277 TSF:MSG:READ,0-0-2,s=255,c=3,t=8,pt=1,l=1,sg=0:0
    2283 TSF:MSG:FPAR OK,ID=0,D=1
    4106 TSM:FPAR:OK
    4106 TSM:ID
    4108 TSM:ID:OK
    4110 TSM:UPL
    4116 TSF:MSG:SEND,2-2-0-0,s=255,c=3,t=24,pt=1,l=1,sg=0,ft=0,st=OK:1
    4122 TSF:MSG:READ,0-0-2,s=255,c=3,t=25,pt=1,l=1,sg=0:1
    4128 TSF:MSG:PONG RECV,HP=1
    4132 TSM:UPL:OK
    4134 TSM:READY:ID=2,PAR=0,DIS=1
    4143 TSF:MSG:SEND,2-2-0-0,s=255,c=3,t=15,pt=6,l=2,sg=0,ft=0,st=OK:0100
    4151 TSF:MSG:READ,0-0-2,s=255,c=3,t=15,pt=6,l=2,sg=0:0100
    4159 TSF:MSG:SEND,2-2-0-0,s=255,c=0,t=17,pt=0,l=5,sg=0,ft=0,st=OK:2.1.1
    4171 TSF:MSG:SEND,2-2-0-0,s=255,c=3,t=6,pt=1,l=1,sg=0,ft=0,st=OK:0
    4184 TSF:MSG:READ,0-0-2,s=255,c=3,t=6,pt=0,l=1,sg=0:M
    4190 MCO:REG:REQ
    4194 TSF:MSG:SEND,2-2-0-0,s=255,c=3,t=26,pt=1,l=1,sg=0,ft=0,st=OK:2
    4202 TSF:MSG:READ,0-0-2,s=255,c=3,t=27,pt=1,l=1,sg=0:1
    4208 MCO:PIM:NODE REG=1
    4210 MCO:BGN:STP
    4212 MCO:BGN:INIT OK,TSP=1
    4214 MCO:SLP:MS=0,SMS=0,I1=1,M1=2,I2=255,M2=255
    4220 MCO:SLP:TPD
    4222 MCO:SLP:WUP=1
    ********************* Start first interrupt ********************************
    interrupt 0
    5226 MCO:SLP:MS=0,SMS=0,I1=1,M1=2,I2=255,M2=255
    5230 MCO:SLP:TPD
    5232 MCO:SLP:WUP=1
    interrupt 1
    6234 MCO:SLP:MS=0,SMS=0,I1=1,M1=2,I2=255,M2=255
    6238 MCO:SLP:TPD
    6240 MCO:SLP:WUP=1
    ********************* Start second interrupt *******************************
    interrupt 2
    7243 MCO:SLP:MS=0,SMS=0,I1=1,M1=2,I2=255,M2=255
    7247 MCO:SLP:TPD
    7249 MCO:SLP:WUP=1
    interrupt 3
    8251 MCO:SLP:MS=0,SMS=0,I1=1,M1=2,I2=255,M2=255
    8255 MCO:SLP:TPD
    

  • Mod

    @HarryDutch What hardware did you run your test on? And, for completeness, which Arduino version (+ board package version)?


  • Admin

    @HarryDutch For indefinite sleeping, only level IRQ triggers are permitted (see AVR datasheet).

    If you change your script to:

    #define MY_DEBUG 
    #define IRQ_PIN 3
    #define MY_RADIO_NRF24
    
    #include <MySensors.h>
    byte countInterrupt;
             
    void setup(void) {
      pinMode(IRQ_PIN, INPUT_PULLUP);
    }
    
    void loop() {
      const int8_t res = sleep(digitalPinToInterrupt(IRQ_PIN), LOW,0);
      if (res == digitalPinToInterrupt(IRQ_PIN)) {
    	  Serial.print("interrupt ");
    	  Serial.println(countInterrupt++);
    	  wait(1000);
      }
    }
    

    you will get the expected behaviour (tested with 2.1.1)



  • @Yveaux I'm using my own pcb's. Nothing special. It has the ATMEGA328p (8 Mhz) and the nrf radio. The hardware worked with MyS library 2.0.0. Using Arduino 1.8.1. Board UNO 8 Mhz 3.3v.



  • @tekka Yes! This works. Thanks for your help. I'm still confused why my test script worked with 2.0.0. and not with 2.1.1. Thanks again.


  • Admin

    @HarryDutch Great! @Yveaux fixed some IRQ handling issues in 2.x, I assume this could explain the revealed behaviour.


  • Mod

    @tekka
    I tried to add your code to my test sketch on my mega2560 and I do get the "interrupt" printed to serial, but only when sleep time is over, I can't get it to wake up. I'm still on library 2.1.0.


Log in to reply
 

Suggested Topics

13
Online

11.4k
Users

11.1k
Topics

112.7k
Posts