FLiRS - flapping digital pins after wakeup

Discussion about Z-Uno product. Visit http://z-uno.z-wave.me for more details.
michap
Posts: 437
Joined: 26 Mar 2013 10:35
Contact:

FLiRS - flapping digital pins after wakeup

Post by michap »

Hello,

would be nice to know the default state of digital pins after wakeup.

Additional - after init via
pinMode(x, OUTPUT);
would be helpful if the pins would be in definded state - as sample "LOW" without any flapping after init.

In this time some pins will change the state itself after init - what will be a problem for some applications.

Maybe there is any workaround while this can be fixed?
( petergebruers have seen the same - look at https://github.com/petergebruers/Z-Uno-FLiRS-LED-DEMO )

Thanks!
Michael
petergebruers
Posts: 255
Joined: 26 Jul 2015 17:29

Re: FLiRS - flapping digital pins after wakeup

Post by petergebruers »

Almost all MCUs based on 8051 default to "weak pull up" and I think the Z-Uno is no exception to that rule. I occasionally program STC MCUs and they exhibit the same behaviour. In any case, one of the guys of Z-Uno confirmed there is no workaround.

I have build an RGBW and the gates of the LED drivers connect directly to the Z-Uno. So when you reset the Z-Uno, the RGBW LEDsflash briefly, because the weak pull up is active until setup() runs and I turn off the drivers. It does not bother me enough to fix that. But I have been thinking... probably a 1k resistor from gate to ground will fix that problem. You'll often see LEDs have anode to VCC, partially because some MCUs have stronger pull down but probably also to avoid that "blinking" effect.

For my FLiRS sketch, the problem is more difficult to solve, because you want to keep the previous state during reset. So I still think you either need a latch or 74HC595 for example... I would use that 595 because it is dirt cheap ;)
michap
Posts: 437
Joined: 26 Mar 2013 10:35
Contact:

Re: FLiRS - flapping digital pins after wakeup

Post by michap »

petergebruers wrote:
28 Apr 2018 10:06
Almost all MCUs based on 8051 default to "weak pull up" and I think the Z-Uno is no exception to that rule.
No, it isn't the case...

Some ports remember about the state - and even if Z-Uno will go into sleep the digital pin is "HIGH" - so LED is still on.
But - again, not every port :)

And - some ports do not flap - as sample port 4,5 will go clear on/off, but as sample port 13 will flap ("blink").

So there should be any difference between the port initialisations, I think.

Maybe the developers have any idea?

Thanks
Michael
petergebruers
Posts: 255
Joined: 26 Jul 2015 17:29

Re: FLiRS - flapping digital pins after wakeup

Post by petergebruers »

Ah, yes, I see what you mean! I wasn't clear when I said "default to weak pull up". My bad. That is assuming only the hardware reset!

I fully agree with you, Either the Z-Uno or the framework does extra initialisation, I observed what you said when I wrote my sketch, but I do not remember the details. I seem to remember, some pins float and (can) cause extra quiescent current. That is why I set all unused pins in that sketch to a defined level...

So we agree ;)

I am sure the develpers will give you all the details, the are very helpful!

Anyway, it was a pleasure discussing this with you. Have fun with your Z-Uno!
petergebruers
Posts: 255
Joined: 26 Jul 2015 17:29

Re: FLiRS - flapping digital pins after wakeup

Post by petergebruers »

Quick update... A bit more detail in the ZM5202 datasheet... But this does not explain the "flapping"...

"Some reset methods deliberately leave the state of GPIO pins unchanged, while other GPIO pins are set to high impedance with an internal
pull-up."
ZM5202 reset.png
ZM5202 reset.png (80.91 KiB) Viewed 7312 times
petergebruers
Posts: 255
Joined: 26 Jul 2015 17:29

Re: FLiRS - flapping digital pins after wakeup

Post by petergebruers »

Because the developers haven't answered yet, I decided to do some measurements myself:

During reset (press and hold reset), all pins pull-up about 66uA, except:
23 = service (184 uA)
18 = INT1 (97 uA)

Service has a 100 k resistor to the 3V regulator plus some additional circuitry.
INT1 has a 100 k resistor to the 3V regulator, this explains the higher current.

So reset is as explained in the datasheet.

After reset, with minimal sketch:

23 = SERVICE = 183 uA

22,21,20,19 = float

18 = INT1 = 30 uA
13 = PWM1 = LED = 0 driven low! About 30 mA to override. Don't do that ;-)

3,4,5,6,7,8 = float

25 = RX0 = 60 uA
24 = TX0 = driven high, about 30 mA to override. Don't do that ;-)

SPI pins:

0 = SCK = 60 uA
1 = MOSI = 60 uA
2 = MOSI = 60 uA

So I'd say all pins are in fact initialized by the Z-Uno firmware, otherwise
they would all stay at weak pull up. Not sure about the SPI pins though...
michap
Posts: 437
Joined: 26 Mar 2013 10:35
Contact:

Re: FLiRS - flapping digital pins after wakeup

Post by michap »

Hi,

thanks, I didn't found time to check it in detail ;)

This are the same values for wake-up? So, not after reset, but after going sleeping and then wake-up procedure?

There maybe a difference - if pin was defined as output - and it was high / low before going sleep.
And maybe different if was defined as input before ...

And,...I really see that (as sample) pin15 will be still HIGH after going sleep (FLiRS), but pin13 not.
Same for the flapping - not clear why for different pins it is different....

Thanks!
Michael
petergebruers
Posts: 255
Joined: 26 Jul 2015 17:29

Re: FLiRS - flapping digital pins after wakeup

Post by petergebruers »

I'll do some more tests but here is a quick result, wake up tested on pin 9. I use the test sketch at the end of this post. To wake up the device, I pull INT1 to ground.

- When the Z-Uno goes to sleep, state is preserved.
- When the Z-Uno wakes up, the pin goes to weak pull-up mode after some time. I did a quick test and from triggering INT to pull-up was about 20 ms
- Then your setup routine runs, so it depends on where and when you set the pin state of the device... Then it goes back to the state you want. But there is always a short delay, so it will always "flap" as you call it.

For Example, In my test sketch, it takes 7 seconds before I drive the test pin high. There is a 1k resistor from pin 9 to GND, so this pulls the pin low even when there is a weak pull-up. This is the result on my logic analyzer:

Top = pin 9
Bottom = INT1 (wake up trigger)

EDIT: I am unable to attach 2 pictures... Oh. Well. You just see pin 9 is low for 7 seconds after wake up.

If I zoom in, you can see when the pin goes to pull-up after waking up:
WakeUpZoom.png
WakeUpZoom.png (4.62 KiB) Viewed 7282 times

Code: Select all

/*
   // WAKEUP REASONS, number of blinks

  1 ZUNO_WAKEUP_REASON_HARDRESET
  2 ZUNO_WAKEUP_REASON_WUT,
  3 ZUNO_WAKEUP_REASON_RADIO,
  4 ZUNO_WAKEUP_REASON_SOFTRESET,
  5 ZUNO_WAKEUP_REASON_INT1,
  6 ZUNO_WAKEUP_REASON_POR,
  7 ZUNO_WAKEUP_REASON_USBSUSPEND

*/
#define TEST_PIN 9

int BlinkCount;

void setup() {
  delay(1500);
  BlinkCount = zunoGetWakeReason() + 1;
  while (BlinkCount != 0) {
    BlinkCount--;
    digitalWrite(LED_BUILTIN, 1);
    delay(50);
    digitalWrite(LED_BUILTIN, 0);
    delay(750);
  }
  delay(1500);
  pinMode(TEST_PIN, OUTPUT);
  digitalWrite(TEST_PIN, 1);
}

void loop() {
  BlinkCount = 5;
  while (BlinkCount != 0) {
    BlinkCount--;
    digitalWrite(LED_BUILTIN, 1);
    delay(1500);
    digitalWrite(LED_BUILTIN, 0);
    delay(1500);
  }
  BlinkCount = 10;
  while (BlinkCount != 0) {
    BlinkCount--;
    digitalWrite(LED_BUILTIN, 1);
    delay(50);
    digitalWrite(LED_BUILTIN, 0);
    delay(50);
  }
  zunoSendDeviceToSleep();
}

void setter(uint8_t value) {
}

uint8_t getter() {
  return 42;
}

// Set Sleeping Mode to ZUNO_SLEEPING_MODE_FREQUENTLY_AWAKE = FLiRS
ZUNO_SETUP_SLEEPING_MODE(ZUNO_SLEEPING_MODE_FREQUENTLY_AWAKE);

// Set Channel to either Siren of Flowstop for FLiRS
//ZUNO_SETUP_CHANNELS(ZUNO_SIREN(getter, setter));
ZUNO_SETUP_CHANNELS(ZUNO_FLOWSTOP(getter, setter));
Attachments
PinInitWakeUpZoom.png
PinInitWakeUpZoom.png (4.62 KiB) Viewed 7283 times
PinInitWakeUpZoom.png
PinInitWakeUpZoom.png (4.62 KiB) Viewed 7284 times
michap
Posts: 437
Joined: 26 Mar 2013 10:35
Contact:

Re: FLiRS - flapping digital pins after wakeup

Post by michap »

I already made many devices / sketches with Z-Uno,... but the FLiRS devices do not like me at all :)

I have attached a minimal sketch for a FLiRS valve - a simple on/off switch with additional user LED on pin 13.
It does not work at all. sometimes it is going to "on" - but will go back to "off", sometimes nothing, sometimes switching.
And sometimes the status is "off" - but pin 15 is high (there is additional LED now for testing) and user LED (pin13) is off.
In ZWave log I see the right commands switching commands and also the "wrong" return state.

I have removed all what I have tried, have inserted different delay in loop(), have moved code to other place - the results are different, but nothing stable.

Maybe somebody have an idea how it should be done right?

Code: Select all

#include "EEPROM.h"

// LED pin number
// 13 pin - user LED of Z-Uno board
#define LED_PIN 13
#define LED_PIN2 15

#define currentLEDValueAddr 0x1

// Last saved LED value
byte currentLEDValue;

ZUNO_SETUP_CHANNELS(ZUNO_FLOWSTOP(getter, setter));

ZUNO_SETUP_SLEEPING_MODE(ZUNO_SLEEPING_MODE_FREQUENTLY_AWAKE);
void setup() {
  pinMode(LED_PIN, OUTPUT); // setup pin as output
  pinMode(LED_PIN2, OUTPUT); // setup pin as output
}

void loop() {
  zunoSendDeviceToSleep();
}

void setter(byte value) {
  currentLEDValue = value;
  NZRAM.put(currentLEDValueAddr, &currentLEDValue, 1);
  if (currentLEDValue > 0) {               
    digitalWrite (LED_PIN, HIGH);   //turn the LED on 
    digitalWrite (LED_PIN2, HIGH);  //turn the LED on 
    NZRAM.put(currentLEDValueAddr, &currentLEDValue, 1);
  } 
  else {     
    NZRAM.put(currentLEDValueAddr, &currentLEDValue, 1);
    digitalWrite(LED_PIN, LOW);   //turn the LED off
    digitalWrite(LED_PIN2, LOW);   //turn the LED off
  }
}

byte getter() {
  NZRAM.get(currentLEDValueAddr, &currentLEDValue, 1);
  return currentLEDValue;
}
Thanks,
Michael
petergebruers
Posts: 255
Joined: 26 Jul 2015 17:29

Re: FLiRS - flapping digital pins after wakeup

Post by petergebruers »

It is not you... wake up is tricky!

I see at least one issue... In setup, you have to read NZRAM and set the pins according to that. So, after the devices wakes up, first the pins "flap" as you said, but then in setup you set them to output and also on or off, as indicated by the NZRAM. In my sketch (see first post) you'll see I call function SetLed(); in setup(). It is defined as:
// SetLed() reads NZRAM (last set by setter or setup)
// into LedValue and turns the LED on or off.
void SetLed() {
NZRAM.get(currentLEDValueAddr, &LedValue, 1);

PG_DEBUG_LOG_DOTNEWLINE("LED value = ", LedValue);

if (LedValue == ZUNO_SWITCHED_OFF)
digitalWrite(LED_PIN, 0);
else
digitalWrite(LED_PIN, 1);
}
Then the second thing was an issue on older firmwares, I am not sure if it is still true... If you send the device to sleep immediately, the Z-Uno might do something wrong with communication. There is a post on this forum.

I solved it this way:

Code: Select all

  // If the Z-Uno wakes from a radio event, keep the
  // radio on for about 1 second after last communication.
  // LoopCount10ms is set to 0 in getter and setter.
  if(WakeUpReason==ZUNO_WAKEUP_REASON_RADIO){
    if (LoopCount10ms < MaxLoopCount10ms){
      delay(10);
      LoopCount10ms++;
      return;
    }
    PG_DEBUG_PRN_DOTNEWLINE("Radio LoopCount10ms reached");
  }
LoopCount10ms increments every 10 ms (timer interrupt) and WakeUpReason was determined in setup(). If wake up was because of radio transmission, I keep looping there for 1 second, then I do zunoSendDeviceToSleep(); So I delay the "going to sleep".

It might not be needed on 2.1.3... There might be simpler ways of doing that (I tried some, loke counting loop() iterations, but it was not as good as this solution).

I think that was the only way to make 2.1.1 behave correctly. If you do zunoSendDeviceToSleep(); immediatly, I got effects like you: bad status on controller, sometimes on/off did not work...
Post Reply