Z-uno is running much slower on PC power than on charger

Discussion about Z-Uno product. Visit http://z-uno.z-wave.me for more details.
risca
Posts: 10
Joined: 11 Feb 2018 05:53

Re: Z-uno is running much slower on PC power than on charger

Post by risca »

I'm not using the builtin UART, I'm doing software (bitbang) UART. I disable all interrupts before sending a byte and enable it again after 1 byte is sent. Basically, before and after the time critical section of my code. See the code in my initial post.

The only pins I have connected are 5 V, GND, and pin 15. Pin 13, for the LED, is connected internationally if I'm not mistaken. I had the z-uno soldered to a prototype board when I tested it. Similar to this one: https://cdn-images-1.medium.com/max/160 ... h0Q-6g.png
I measured the 5 V line with my oscilloscope, but I never noticed any noise
petergebruers
Posts: 255
Joined: 26 Jul 2015 17:29

Re: Z-uno is running much slower on PC power than on charger

Post by petergebruers »

Can I ask you to consider what I said anyway? I do understand what you are doing. I understand you disable interrupts and what pins you use. But your Z-Uno runs a lot of code that is invisible to you. Also, you do not get to decide which peripherals get initizialized, and how... Controlling interrupts might cause issues too.

We are trying to solve a pretty strange issue...

Do I make sense, or do you think my I idea is too crazy too try?
petergebruers
Posts: 255
Joined: 26 Jul 2015 17:29

Re: Z-uno is running much slower on PC power than on charger

Post by petergebruers »

Here is a piece of code I use in my FLiRS sketch:

Code: Select all

// Force all pins to output, with some exceptions,
  // notably the input pin and the serial ports if
  // debugging has been enabled. This saves power.
  for (i = 0; i <= 6; i++)
    pinMode(i, OUTPUT);
  // Leave Serial1 pins alone if they are used for debugging.
#if PG_DEBUG_OUT != 11
  for (i = 7; i <=8; i++)
    pinMode(i, OUTPUT);
#endif
  for (i = 9; i <= 17; i++)
    pinMode(i, OUTPUT);
  // Skip 18 = INT1 = Service Button
  for (i = 19; i <= 22; i++)
    pinMode(i, OUTPUT);
  // Skip 23 = Service Button
  // Leave Serial0 pins alone if they are used for debugging.
#if PG_DEBUG_OUT != 10
  for (i = 24; i <=25; i++)
    pinMode(i, OUTPUT);
#endif
risca
Posts: 10
Joined: 11 Feb 2018 05:53

Re: Z-uno is running much slower on PC power than on charger

Post by risca »

I'll see what I can do. We have guests visiting for the weekend so it might have to wait until next week. Additionally, I've already switched to a different design that uses the builtin UART and a separate MCU to add parity. I don't need the software UART anymore.
However, it would be most interesting to know what actually causes the difference in behavior.

Here's a picture of the working board to control the projector in our cinema room :D
Image
p0lyg0n1
Posts: 242
Joined: 04 Aug 2016 07:14

Re: Z-uno is running much slower on PC power than on charger

Post by p0lyg0n1 »

Hi,
I have checked this issue finally.
I slightly modified your source:

Code: Select all

// LED pin number
#define LED_PIN 13

#define START_CODE ((char)0xA9)
#define END_CODE   ((char)0x9A)

static byte lastValue = 0;
static byte currentValue = 0;
//static const char onCmd[] = { START_CODE, 0x17, 0x2E, 0x00, 0x00, 0x00, 0x3F, END_CODE };
//static const char offCmd[] = { START_CODE, 0x17, 0x2F, 0x00, 0x00, 0x00, 0x3F, END_CODE };
static const char onCmd[] = "UUUUUUUU";
static const char offCmd[] = "UUUUUUUU";

s_pin txPin = 15;
unsigned long timestamp = 0;

#define INTERBIT_26_US_SLEEP delayMicroseconds(41); NOPS(23)
#define SET_TX_PIN(level) digitalWrite(txPin, level); INTERBIT_26_US_SLEEP

void txByte(char b, bool parity)
{
  noInterrupts();
  // start bit
  SET_TX_PIN(LOW);
  // 8 data bits
  SET_TX_PIN(b & (1 << 0) ? HIGH : LOW);
  SET_TX_PIN(b & (1 << 1) ? HIGH : LOW);
  SET_TX_PIN(b & (1 << 2) ? HIGH : LOW);
  SET_TX_PIN(b & (1 << 3) ? HIGH : LOW);
  SET_TX_PIN(b & (1 << 4) ? HIGH : LOW);
  SET_TX_PIN(b & (1 << 5) ? HIGH : LOW);
  SET_TX_PIN(b & (1 << 6) ? HIGH : LOW);
  SET_TX_PIN(b & (1 << 7) ? HIGH : LOW);
  // parity
  SET_TX_PIN(parity ? HIGH : LOW);
  // stop bit
  NOPS(40); // compensate a bit for the lack of comparison
  SET_TX_PIN(HIGH);
  // for perfect bit timing we should wait some more here,
  // but we don't.
  interrupts();
}

bool CalculateParity(byte b)
{
  bool parity = false;
  while (b) {
    parity = !parity;
    b = b & (b - 1);
  }
  return b;
}

void TurnOnProjector()
{
  Serial.print("Turning on projector... ");
  for (int i = 0; i < 8; ++i) {
    txByte(onCmd[i], CalculateParity(onCmd[i]));
  }
  digitalWrite (LED_PIN, HIGH);
  Serial.println("done.");
}

void TurnOffProjector()
{
  Serial.print("Turning off projector... ");
  for (int i = 0; i < 8; ++i) {
    txByte(offCmd[i], CalculateParity(offCmd[i]));
  }
  digitalWrite(LED_PIN, LOW);
  Serial.println("done.");
}

// next macro sets up the Z-Uno channels
// in this example we set up 1 switch binary channel
// you can read more on http://z-uno.z-wave.me/Reference/ZUNO_SWITCH_BINARY/
ZUNO_SETUP_CHANNELS(ZUNO_SWITCH_BINARY(getter, setter));

// this function runs only once the controller sends new value
void setter (byte value) {
  currentValue = value;
}

// this function runs only once the controller asks
byte getter () {
  return currentValue;
}

void setup() {
  Serial.begin();
  pinMode(LED_PIN, OUTPUT);
  pinMode(txPin, OUTPUT);
  currentValue = lastValue = 0;
}

void loop() {
  if(millis() - timestamp > 3000) {
    Serial.print("PING - ");
    Serial.println(timestamp);
    timestamp = millis();
  }
  TurnOnProjector();
  TurnOffProjector();
  delay(100);
}
So, what I have:
1. When I connected Z-Uno to power supply using 3V/5V/Vin I always have the same frequency with your code 10.86kHz(92.0625us):
Capture1.png
Capture1.png (61.89 KiB) Viewed 8081 times
2. When I connected Z-Uno using USB and external power supply I always have this 19.2kHz (52.083us)
Capture2.png
Capture2.png (59.05 KiB) Viewed 8081 times
If I connect Z-Uno using USB cord without data cables I always have the 1 behavior too.
Looks like it's a nondocumented feature of MCU that boosts up core when we use full speed USB and decreases it after the first valid packet.
So, you can use hardware or software workaround to get it work:
1. Hardware. Just use 3V/5V/Vin as external supply of your Z-Uno (please check the reference, to apply right voltage value) or use USB cord without d+/d- wires.
2. Software. You can increase delays to fit to another frequency if you want to use external USB supply connect to USB port of Z-Uno.
petergebruers
Posts: 255
Joined: 26 Jul 2015 17:29

Re: Z-uno is running much slower on PC power than on charger

Post by petergebruers »

Although I am not the OP I have been following this with interest and I would like to thank you for your thorough investigation and workaround! BTW never heard of such a behaviour! I learn something every day...
risca
Posts: 10
Joined: 11 Feb 2018 05:53

Re: Z-uno is running much slower on PC power than on charger

Post by risca »

I got the opposite result than me. I got:

* Laptop - slow
* Power supply - fast

The results you posted seem to indicate the opposite.

Would it be possible to detect the current running speed and make code that works for both speeds?
Or maybe contact the MCU manufacturer about this issue? They might have a workaround.
p0lyg0n1
Posts: 242
Joined: 04 Aug 2016 07:14

Re: Z-uno is running much slower on PC power than on charger

Post by p0lyg0n1 »

The same results. 2-nd is with external power supply connected to USB. The smallest time period means highest frequency.
Post Reply