-
-
Notifications
You must be signed in to change notification settings - Fork 64
The pinMode(pin,OUTPUT) can't initialize the pin in HIGH state on a Nano Every #143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I do not agree. There is no need to match the behaviour of the 4809 to the behaviour of the 328 unless in emulation mode. The digitalWrite function might become something like this:
And the pinMode function:
|
I mean that the behavior should match the API as documented at https://docs.arduino.cc/learn/microcontrollers/digital-pins/ It does make mention of the processor differences, but only as differences in pull-up resistor values. This code shouldn't result in opposite outcomes on different Arduinos:
|
There are now 9 official Arduino boards in the Nano Family. Should they all need to emulate the pinMode behaviour of the original Nano? I agree with @stitech195 that only in the 328 emulation mode should the Nano Every have that behaviour. |
Another complication: when in emulation mode, and the sketch contains direct memory access to the ports, the names of the ports as known in the 328 are translated to the names in the 4809. Are these side effects also mimiced? Should they? |
In that case, the fix is to change the erroneous API documentation to document it: https://docs.arduino.cc/learn/microcontrollers/digital-pins/ |
The emulation mode is introduced with the UNO WiFi Rev2, containing the ATmega4809. The objective was that existing sketches, written for the normal UNO could run without any changes. This logically means that all manners that the new processor is better (more RAM, more timers, more USARTs etc) will not be used. And even then, the emulation can't be complete (UNO Wifi Rev2 and Nano Every provide PWM at only 5 pins, not 6, etc). |
I think it means that code written for the normal Uno that explicitly references the 328 registers would work unmodified, but it does not mean that it limits the normal API code (non direct register manipulation code). For instance the digitalWrite() statement to be affected by changes in the register emulation mode, it might need #ifndef(PORTB) or #ifndef(AVR_NANO_4809_328MODE) ... conditional macros and separate code paths to actually reference the registers. As is, the digitalWrite and rest of the API-implementing code completely ignores the extra macros and code defined in https://github.com/arduino/ArduinoCore-megaavr/blob/5e639ee40afa693354d3d056ba7fb795a8948c11/cores/arduino/NANO_compat.h and https://github.com/arduino/ArduinoCore-megaavr/blob/5e639ee40afa693354d3d056ba7fb795a8948c11/cores/arduino/NANO_compat.cpp and still provides the full featurea of more RAM, timers, USARTS, etc... On the Nano Every, |
Not necessarily, but there does need to be a way to initialize the pin at HIGH level. Currently on 4809, the digitalWrite(P, HIGH) turns on the input pullup if the pin is in input mode, but does NOT set the port output register. So when you change the output mode with pinMode(), the output register bit is still 0, and the pin will be set to zero as well. |
ArduinoCore-megaavr/cores/arduino/wiring_digital.c
Lines 33 to 47 in 5e639ee
On an Uno, you can use these lines to switch directly from INPUT_PULLUP to HIGH:
On a Nano Every, those commands switch instead to an OUTPUT LOW
See the discussion at:
https://forum.arduino.cc/t/how-to-set-output-high-without-pulse/1364909
The documentation at https://docs.arduino.cc/learn/microcontrollers/digital-pins/ says it should switch directly to HIGH:
Per this chunk of code, it looks like half of the issue was considered:
ArduinoCore-megaavr/cores/arduino/wiring_digital.c
Lines 162 to 192 in 5e639ee
But the pinMode code is where the problem happens. Instead of always assuming LOW, it should use the state on the pullup to set the level before enabling the OUTPUT direction in order to match the documentation and the behavior on the '328 and '2560.
See also #86
The text was updated successfully, but these errors were encountered: