[SOLVED] AT32U4 custom board some pins are not working

Hi all,

Full disclaimer: I'm a complete newbie and this is the first time I'm designing an electronics project.

I have been working on a MIDI controller project. Basically it's 16 buttons and 16 RGB LEDs. Nothing fancy. You press a button, computer gets a MIDI message over USB and a LED lights up.

I have done all my prototyping on a Leonardo board and once I made sure it works as intended I have switched to my own design which can be shown below.

I have 2 problems with the design.

1- There is something wrong with my RESET design. The RESET button doesn't do anything and the board does not boot/work at all until I manually connect 5V directly to the MCU RESET pin after which everything seems to work perfectly. I can upload sketches, read pins etc...
Where/how can I start debugging this?

2- Since I've done all my prototyping on Leonardo and I didn't want to mess setting up Arduino IDE for a new board, I wanted it to work out-of-the-box just by flashing the Leonardo bootloader hence extra attention to pin mappings.

However it looks like I have missed something. Although most of the pins work as intended, I can't read from D14, D15, D16 and D17 pins.
(I have them set up as INPUT_PULLUP)

After looking into it I have noticed that, these pins are actually not mentioned in the official Leonardo schematics, they are only mentioned as A0, A1, A2 and A3 however if you look at the pinout in diagram in arduino.cc you can see that they are also called D14-D17.

After digging some more I have come across variants/lenoardo/pins_arduino.h file in which it says Map SPI port to 'new' pins D14..D17.
So looks like these pins have some special meaning. At this point I think you can guess where I'm going: How can I use them as INPUT_PULLUP?

Any help would be appreciated
Thank you

It looks like there is no problem with the reset pin circuit to me.
If you have a DMM, measure the level of the pins before manually connecting to 5V.
Is it 5V?

If PF4-7 is not available as GPIO, JTAG may be enabled.
Did you use the Arduino IDE when flashing the boot loader?
The Arduino IDE bootloader flash command sets the fuse at the same time.
JTAG can be disabled with a fuse.
Or disable JTAG by software.
To do this, add the following two lines of code at the beginning of setup().

MCUCR |= 0x80; // disable JTAG interface
MCUCR |= 0x80;

Copying the same two lines makes sense, It doesn't work with just one line.

Have you tested the original Leonardo board you have to see if it works as shown in your post? You’ll likely find it does not. My boards, while clones, do in fact map to the SPI pins which match the Pololu documents, not the Arduino website picture.

While the reset circuit should work as shown, why did you modify the Arduino design? There is absolutely no need for the voltage divider and capacitor approach as switch bounce is not a factor here.

The operation is as basic as it comes, the voltage on the processor reset pin is either 5V or about 0.3 volts with the button pressed due to the divider. The minimum processor threshold is 0.2 * Vcc which is 1 volt so the voltage divider should work if the parts are the values shown. Not ideal, but it should work.

One the things you must learn in electronics is to develop a methodology and follow it. Always. Never assume anything. You must always test. Then verify. That’s means you must know how to use a multimeter, digital or analog, because you cannot see electrons.

If you don’t have a multimeter, it’s time to buy one. Virtually anything will work for basic troubleshooting. But, be careful if you buy a $5 Harbor Freight special. They’re okay for your needs right now but don’t go shoving the probes in a wall outlet. They’re fine at 50 volts and less but I wouldn’t trust my life to one of them at 120 or 240 volts ac.

Quote It looks like there is no problem with the reset pin circuit to me.
If you have a DMM, measure the level of the pins before manually connecting to 5V.
Is it 5V?

While the reset circuit should work as shown, why did you modify the Arduino design? There is absolutely no need for the voltage divider and capacitor approach as switch bounce is not a factor here.

The operation is as basic as it comes, the voltage on the processor reset pin is either 5V or about 0.3 volts with the button pressed due to the divider. The minimum processor threshold is 0.2 * Vcc which is 1 volt so the voltage divider should work if the parts are the values shown. Not ideal, but it should work.

One the things you must learn in electronics is to develop a methodology and follow it. Always. Never assume anything. You must always test. Then verify. That's means you must know how to use a multimeter, digital or analog, because you cannot see electrons.

I have bought a MM and I can clearly see that I messed up the button pins. It's one of those 4 leg buttons, I have accidentally used the same legs causing RESET pin to be LOW all the time.
The design is from the datasheet If I remember correctly. It even had a diode parallel to 4.7K that I've got rid off. To be honest I don't think I even need the reset button so I will be getting rid of all that.

If PF4-7 is not available as GPIO, JTAG may be enabled.
Did you use the Arduino IDE when flashing the boot loader?
The Arduino IDE bootloader flash command sets the fuse at the same time.
JTAG can be disabled with a fuse.
Or disable JTAG by software.
To do this, add the following two lines of code at the beginning of setup().

I have used the Arduino IDE and I can see that it sets the high fuse to 0xD8 which means looks like JTAG is already disabled. I tried to enable it just to test if it affects anything but then my sketch stopped working all together. Took it back and it started to work again. and PORTF stopped working completely so not just D14 to D17 but Also D18 and D19 too. Otherwise everything seems to work OK.
The sketch in question is below. I short the pins with GND to test if it works or not.

int pins[16] = {0, 1, 2, 3, 4, 5, 6, 10, 12, 13, 14, 15, 16, 17, 18, 19};
int values[16] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};

void setup() {
  for (int i = 0; i < 16; i++) {
    pinMode(pins[i], INPUT_PULLUP);
  }
  Serial.begin(115200);
  while (!Serial) { };
  Serial.println("Started...");
}


void loop() {
  for (int i = 0; i < 16; i++) {
    int v = digitalRead(pins[i]);
    if (v != values[i]) {
      Serial.println(pins[i]);
      values[i] = v;    
    }
  }
}

Also disabling it in software didn't work (could be optimisation related, I think it takes more than 4 cycles)
I have tried this to overcome that problem.

#define jtd_set(x) \
{ \
        __asm__ __volatile__ ( \
                "in __tmp_reg__,__SREG__" "\n\t" \
                "cli" "\n\t" \
                "out %1, %0" "\n\t" \
                "out __SREG__, __tmp_reg__" "\n\t"\
                "out %1, %0" "\n\t" \
                : /* no outputs */ \
                : "r" ((uint8_t)(x ? (1<<JTD) : 0)), \
                  "M" (_SFR_IO_ADDR(MCUCR)) \
                : "r0"); \
}

I tried setting it to true or false but that didn't matter. It doesn't seem to do anything.

Any pointers?
Thank you

This is now solved. For future reference, Arduino really messed up with pins an their documentation. For anybody who's working on this PORTF of A32U4 is mapped as this

// A0 D18 PF7
// A1 D19 PF6
// A2 D20 PF5
// A3 D21 PF4
// A4 D22 PF1
// A5 D23 PF0

Nevermind the pinout on the official page. Nevermind the D14-D17 pins. They don't exist unless JTAG is enabled (I'm guessing) which is disabled by default.

The pins are defined in the pins_arduino.h file, part of which is the following:

#define LED_BUILTIN 13
#define LED_BUILTIN_RX 17
#define LED_BUILTIN_TX 30

// Map SPI port to 'new' pins D14..D17
#define PIN_SPI_SS    (17)
#define PIN_SPI_MOSI  (16)
#define PIN_SPI_MISO  (14)
#define PIN_SPI_SCK   (15)

static const uint8_t SS   = PIN_SPI_SS;
static const uint8_t MOSI = PIN_SPI_MOSI;
static const uint8_t MISO = PIN_SPI_MISO;
static const uint8_t SCK  = PIN_SPI_SCK;

// Mapping of analog pins as digital I/O
// A6-A11 share with digital pins
#define PIN_A0   (18)
#define PIN_A1   (19)
#define PIN_A2   (20)
#define PIN_A3   (21)
#define PIN_A4   (22)
#define PIN_A5   (23)
#define PIN_A6   (24)
#define PIN_A7   (25)
#define PIN_A8   (26)
#define PIN_A9   (27)
#define PIN_A10  (28)
#define PIN_A11  (29)

static const uint8_t A0 = PIN_A0;
static const uint8_t A1 = PIN_A1;
static const uint8_t A2 = PIN_A2;
static const uint8_t A3 = PIN_A3;
static const uint8_t A4 = PIN_A4;
static const uint8_t A5 = PIN_A5;
static const uint8_t A6 = PIN_A6; // D4
static const uint8_t A7 = PIN_A7; // D6
static const uint8_t A8 = PIN_A8; // D8
static const uint8_t A9 = PIN_A9; // D9
static const uint8_t A10 = PIN_A10; // D10
static const uint8_t A11 = PIN_A11; // D12

Pins D14-D17 are usable, but are located on the ICSP connector, with D17 being used to drive the Rx LED. I've used them in the past for a softwareSerial port because D14-D16 support the needed pin change interrupts, so no fuse alterations are required.

Nevermind the pinout on the official page. Nevermind the D14-D17 pins. They don't exist unless JTAG is enabled (I'm guessing) which is disabled by default.

Yes, no and no.

The Arduino documentation page is wrong but digital pins D14-17 do indeed exist and INPUT_PULLUP work as expected in the Arduino default environment. This functionality has nothing to do with JTAG. In fact, if you do set the JTAG fuses, those pins cannot be used for any other function.

Note that D17, the SS pin, is not directly accessible on the Leonardo design and it has been used for a status RX LED.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.