Safeties and pin mode issue

A bit of background: I have some relays connected to the spark which control a 350V contactor in an electric vehicle, among other things. It is crucial that this particular output be glitch-free, or it can weld the contactor closed and slowly damage the capacitor bank.

Problem: There appears to be no way with the stock API to set the state of an output before enabling push/pull mode (OUTPUT). The safety-check code in the digitalWrite function prevents setting the port state unless it is actually an output, which means that it will briefly output the wrong state until you are able to set it.

In my case, my output needs to be high, and I have an external pull-up which keeps the output high during reset and programming. My workaround for now is to set the BSRR / BRR registers directly, which lets me put the pin in the correct state before enabling it.

Solution: Remove the safeties check for the “wrong” pin mode to allow the state to be set, even if it’s NONE. The checks for SPI / I2C / Serial can stay because they seem useful.

If this makes sense, I can submit a pull request.

Hmmm… @brianw1,

That’s cool! How about you send a PR with some explanation and let :spark: HQ review?

Thanks for bringing this up!

1 Like