ESP 8266 gotcha to avoid

Computers and stuff like that

ESP 8266 gotcha to avoid

Postby Doug Coulter » Mon Dec 04, 2017 10:57 am

Recently I decided to use some ESP 8266 -07 modules to control some Tekpower TP-3005 power supplies, finding their USB implementation wanting (severely). I chose the -07 version as it has an external antenna connector, which would be useful since the ESP wants to be inside the supply's metal case. Wrote the code, got over-the-air updates going (beware that the -07 needs a different memory config than the more common -12 series), and in testing had "sometimes it won't boot" failures - intermittents are the worst.

I had used some level shifters to convert between the ESP's 3.3v levels and the supply's 5v levels, a pretty nifty TXB0104 Ti chip on a breakout from Adafruit. This slick little number is bidirectional - if either side forces a level, it'll convert to the other level on the other side, based on some "weak" latches. A fairly low current pull-up or down will force it to the resting level you want.

But "fairly low" isn't as low as the 100k+ current levels of ESP or PIC chip internal pullups (the power supply has a PIC in it). This means the usual pin pullups (or whatever is normal for the chip family in question) that are often on the uP pins at powerup while waiting for the initialization code to finish won't necessarily drive this level shifter, among other issues.

Normally this wouldn't matter when connecting, say, the PICs serial output through a level shifter to the ESPs serial input - a little odd level or bouncing during init shouldn't matter, things are all going the right direction and aren't being looked at during init.

And I wouldn't expect Tekpower to give us detailed documentation on the PIC code - they have to assume they're selling a finished product and you're never going to crack the case as I am. And here's what it looks like with the case cracked, for reference. The lousy USB interface board I'm replacing is in the foreground here, the board I built is in the supply, lower right.
Messy bench, but I get things done anyway.

I must have been holding that wrong, I never noticed that pincusion effect from this camera before! The supply isn't bent up.

On the other hand, you'd hope that the sellers of the ESP variants, which are sold to hobbyists - and pros - with the intention of them using them in designs would have some documentation on what has to happen at power up to make a successful project, especially if they do something very weird and have a strange requirement for a pin status. Such IS the case for the pins that tell the ESP what mode to be in after a reset, for example.

What they don't tell you is that the RXD INPUT isn't always an input during initialization! It needs to be allowed to "move" under the influence of weak pullups and adjustments to the tri-statedness of that pin for some milliseconds after power up. And if that isn't satisfied, you are utterly hosed - even chip reset or the powerdown pin have no effect, it's dead till the next power cycle where that RXD pin IS allowed to toggle some during initialization. This knowledge cost me a day of my life and considerable skull sweat and stress due to another co-incidence, which was that the PIC data output was also toggling during init - and sometimes did just the right thing to let the ESP work! So it was far from obvious what the issue was and a lot of things were tried till finally I just pulled the level shifter out of the socket and after that it always worked.

Here's the relevant scope traces during powerup. Note the toggling on what is supposed to be a pure input on the ESP. If this can't happen (almost as if it were being used to clock a flipflop or a counter) - we're dead till the next power cycle - nothing whatever short of that will pull us out of a locked up state.
Scope - green is the PIC output, Purple is the ESP input, level shifter removed.
DS0016.PNG (10.83 KiB) Viewed 364 times

Note the near-coincidence of some garbage on the PIC output signal...this timing is somewhat variable depending on just how fast the power supply ramped, and fairly often, coincided well enough for the thing to boot, making this one hell of a tough troubleshooting session. Hopefully I can save some readers that level of hair pulling/whitening.

As it turns out, all was not lost and I didn't need to add yet another uP or relay to switch things around during init time - the TI chip has an output enable, and I created a time delay with a pretty large electrolytic capacitor to keep it from doing its thing for 150ms or so on power up. Simple in hindsight, but why in heck did I have to prevent my circuit from driving an input for awhile after power up? The documentation never mentions any such thing. And why, after that, does reset or CH_PD not work till another power cycle? Are these in some way virtualized "soft" pins, unlike all other uP's in existence? I don't know, but forewarned is forearmed. These are nice chips and very useful, but, this is one of the nastiest bugs I've seen - Grace Hopper and her moth step aside.

For what it's worth, in addition to the non-unique USB id the old interface board emitted, making this useless if you want to control more than one and be sure what you're applying power to, the supply itself locks out the front panel until the next power cycle if you use the remote interface. I added a reset driver on the ESP hardware and software to create a reset for the PIC in the supply so I can have the software "release" the use of the supply without having to turn it off and on again (shades of Steve in a call center in India). Is is usual with a PIC, they simply tied its reset pin high through a pullup - PICs notoriously have about the best power -on reset logic there is and it's really easy to make that work well.

More on the software side, but worthy of noting - in their description of the protocol to talk to this thing, they mention using \n as the end of command indicator. To roughly 100% of all software guys ever - and in every language, and pretty much all hardware guys who've done anything with a serial port, keyboard, or display - this means <newline>, or 0x0A, or 10(decimal) as a single character.
To the Chinese guy who wrote the pic meant a literal backslash followed by a literal lower case n. There's some python hinted at on the amazon page that figured this one out, and while I don't speak python - it didn't matter - as I said, every language uses \n to mean newline, and a second slash means the following slash is to be treated literally - so seeing \\n was the tipoff, even though I don't speak python. You'd have to go pretty far back in computer language history to find a case where \n meant two characters..

Note, I see that Adafruit's Feather Huzzah design uses a 1k resistor in series between the CP2102 chip and the RXD line on the ESP8266-12e chip. That seems to do it for that chip, but even 4.7k didn't suffice to isolate the -07 chip I was using here - and I didn't have the luxury of being able to program the CP2102 chip to not output noise for awhile on power up, which probably is the case for the commercial board, or simply luck...
Posting as just me, not as the forum owner. Everything I say is "in my opinion" and YMMV -- which should go for everyone without saying.
User avatar
Doug Coulter
Posts: 3038
Joined: Wed Jul 14, 2010 7:05 pm
Location: Floyd county, VA, USA

Re: ESP 8266 gotcha to avoid

Postby Doug Coulter » Tue Dec 05, 2017 8:51 pm


See also: viewtopic.php?f=33&t=1067&start=0

Second one was easy...
I have a nice little gui for this, and you can either run both from one instance, or just spin up an instance per supply, no sweat, they don't eat a lot of screen space. They just let you set settings, turn things on and off, and monitor actual voltage and current output.
Posting as just me, not as the forum owner. Everything I say is "in my opinion" and YMMV -- which should go for everyone without saying.
User avatar
Doug Coulter
Posts: 3038
Joined: Wed Jul 14, 2010 7:05 pm
Location: Floyd county, VA, USA

Return to Digital

Who is online

Users browsing this forum: No registered users and 1 guest