Atmega32U2 and pIO

Hi there,
I´ve just soldered a small project for myself, and i´ve just encountered some issues.
Due to the general unavailability of most MCUs, I´ve chosen the Atmega32U2, because for my application, its more the sufficient.
Is there any way to get PIO and this mcu working together, or do I have to change the MCU

with best regards

Well in the standard way, when just using a e.g. ATMega32U4 board and changing the MCU field to U2, you’ll get lots of compiler errors everywhere where GPIO pins or registers are referenced that the U2 does not have.

avr-gcc -o .pio\build\leonardo\FrameworkArduino\wiring_digital.c.o -c -std=gnu11 -fno-fat-lto-objects -Os -Wall -ffunction-sections -fdata-sections -flto -mmcu=atmega32u2 -DPLATFORMIO=50300 -DARDUINO_AVR_LEONARDO -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO=10808 -DUSB_VID=0x2341 -DUSB_PID=0x8036 "-DUSB_PRODUCT=\"Arduino Leonardo\"" -DUSB_MANUFACTURER=\"Arduino\" -IC:\Users\Max\.platformio\packages\framework-arduino-avr\cores\arduino -IC:\Users\Max\.platformio\packages\framework-arduino-avr\variants\leonardo C:\Users\Max\.platformio\packages\framework-arduino-avr\cores\arduino\wiring_digital.c
In file included from C:\Users\Max\.platformio\packages\framework-arduino-avr\cores\arduino\Arduino.h:258:0,
                 from C:\Users\Max\.platformio\packages\framework-arduino-avr\cores\arduino\wiring_private.h:31,
                 from C:\Users\Max\.platformio\packages\framework-arduino-avr\cores\arduino\wiring_digital.c:26:
C:\Users\Max\.platformio\packages\framework-arduino-avr\variants\leonardo/pins_arduino.h:213:14: error: 'DDRE' undeclared here (not in a function); did you mean 'DDRB'?
  (uint16_t) &DDRE,
              ^~~~
              DDRB
C:\Users\Max\.platformio\packages\framework-arduino-avr\variants\leonardo/pins_arduino.h:214:14: error: 'DDRF' undeclared here (not in a function); did you mean 'DDRE'?
  (uint16_t) &DDRF,
              ^~~~
              DDRE

You can ‘fix’ those by using a new variant based on the e.g. Leonardo but removing every reference to the non-existing pins.

I’ve created GitHub - maxgerhardt/pio-atmega32u2-test: Test project for ATMega32U2 + Arduino that just minimalyl removes these references from the pin arrays, but leaves the original pin mapping intact. (Meaning, just use the pins that the U2 really has, so Arduino pins that map to no Port E or F pins)

There may be a possibility that regular firmwares compiled for a U4 work, provided you never use a register that the U2 does not physically have (and all registers are at the same place). I have not tried this.

Thanks for your awesome help :slightly_smiling_face:
I wasn´t aware that it´s done in this way, but i´ll try my best to understand it :slightly_smiling_face:
Actually I´m trying get the Pinmapping right, but there is something I dont get : I´ve used PortC5 in my Schematic, but i can´t find any reference to it in the pins_arduino.h .
What do I have to do that I can adress that Pin and how can I make sure that the other Pins I found are logically connected to the right pin ?

There are two critical arrays for the pin mapping:

Imagine a list of Arduino pin numbers, 0 (meaning ‘D0’) to say, 30, (meaning D30). And lets say we decide that Arduino pin 0 is “PD2”. Then the first array, digital_pin_to_port_PGM, would have at the 0th (= arduino pin number) /first index the value PD, and the second array, digital_pin_to_bit_mask_PGM, would have the value _BV(2) (the macro is “bit value” and evaluates _BV(n) = 1 << n). To set the pin “D1” to e.g. PC5, we would write PC as the second element (index = 1) in the first array, and in the second array’s second element to _BV(5).

So what you could do is add D31 as a new pin and add the above mentioned values to the arrays, then refer to your pin as number 31. (pinMode(31, OUTPUT);) etc. to control the therefore mapped PC5 pin.