I am having issue with Arduino Leonardo board (ATmega32u4) resetting itself when I write series of pulses on PORTF (analog pins A0-A3), configured as digital outputs ?
The code illustrating the issue:
#define GOOD_VAL1 0x01
#define GOOD_VAL2 0x10
#define BAD_VAL1 0x5A
#define BAD_VAL2 0xA5
void setup() {
Serial.begin(9600);
DDRF = 0xFF;
// I also tried this:
/*
MCUCR = (1<<JTD);
_NOP();
MCUCR = (1<<JTD);
*/
// Which compiles as:
// ldi r24, 0x80
// out 0x35, r24
// nop
// out 0x35, r24
// Based on ATmega32u4 datasheet:
// 26.5.1 MCU Control Register – MCUCR
// Bits 7 – JTD: JTAG Interface Disable
// When this bit is zero, the JTAG interface is enabled if the JTAGEN Fuse is programmed.
// If this bit is one, the JTAG interface is disabled.
// In order to avoid unintentional disabling or enabling of the JTAG interface,
// a timed sequence must be followed when changing this bit:
// The application software must write this bit to the desired value twice within four cycles
// to change its value. Note that this bit must not be altered when using the On-chip Debug system
}
void loop() {
while (Serial.available()) Serial.read();
Serial.println("Send something to begin...");
while (!Serial.available());
noInterrupts();
for (char i=0; i<40; i++) {
PORTF = GOOD_VAL1;
_NOP();
_NOP();
_NOP();
PORTF = GOOD_VAL2;
}
PORTF = 0x00;
interrupts();
Serial.println("Now you see me...");
// Shows as series of 20 puleses @2MHz on logic analyzer
while (Serial.available()) Serial.read();
Serial.println("USE AT YOUR OWN RISK!");
Serial.println("Send something to reboot the Leonardo...");
while (!Serial.available());
PORTF = 0xFF;
// The problem seems to be below ?
noInterrupts();
for (char i=0; i<40; i++) {
PORTF = BAD_VAL1;
_NOP();
_NOP();
_NOP();
PORTF = BAD_VAL2;
}
PORTF = 0x00;
delayMicroseconds(16);
interrupts();
Serial.println("Now you don't");
// Actual result, LED is blinking rapidly for few seconds...
delay(1000);
}
There is noting connected to the board except the USB cable for power and serial communication. If I upload the sketch above and press Enter two times in the serial monitor, the board reboots. I also used logic analyzer and all I can see is the board all pins (A0-A3) go high 1us after last port output...
I would appreciate any help in reproducing the issue and possible causes for the reboot ? I am new to the forum so please accept my apologies if I am not following rules of the house.
Thanks,
Ivan