ATmega328PB Support

I’ve been working on an accessory/display board based around the ATmega328PB, mainly based around that it is one of the few small MCUs that was in stock anywhere. The 328PB is backwards compatible with 328P hex, but without the additional pins or devices.

One external hurdle is that avr-libc does not support the 328PB out-of-box (possibly due to avr-libc having not been actively maintained in years…), requiring a 2 line patch and 3 files from the Microchip ATmega device support pack.

It seems a 328PB branch for the extra pins existed back in 2019, if that can be resurrected, I’ll see about finishing and testing it.

I pushed that development branch back up to github (work-atmega328pb-20190924). Note that you would still need to compile for the atmega328p - just that now you should be able to access the additional PEx pins.

cd ~/klipper ; git fetch ; git checkout origin/work-atmega328pb-20190924 ; sudo service klipper stop ; make flash ; sudo service klipper start

Let me know your results (success or failure).

-Kevin

1 Like

Thanks. Got it compiling and flashing. Next I’ll need to build up a test board to verify functionality of the PE pins.

Also going to look in to the feasibility of adding support the additional i2c and spi interfaces.

Was this intentionally left PE0/PE1 instead of PE2/PE3?

That does appear to be a code error. I’ve updated and rebased that branch with it hopefully fixed.

Thanks,
-Kevin

Ok cool, I had thought so and changed it on my local already.

As-is, flashing doesn’t work due to the mismatch MCU name passed to avrdude vs the chip id. Even up adding the 328pb as a target. I’ll need to check if avrdude’s default config on the common distro’s has support for the 328pb, if not it’s just an additional switch on the avrdude call.

Good news, work-atmega328pb-20190924 is working as intended as far as the additional pins, ADC, and features common with the 328p. PE0-PE3 all worked as inputs. ADC functions as expected on PE2/PE3.

avrdude requires m328pb instead of m328p due to the chip id differences, but easy enough just to override that for make flash.

 make flash CONFIG_MCU=m328pb FLASH_DEVICE={...}

The second I2C and SPI interfaces, and additional timers would be nice, but I had no plans to make use of them and its not worth anybody’s effort.

The current changes in the branch are good to merge by me. :+1:

Thanks. I merged that change into the master Klipper branch.

-Kevin

1 Like

Hi there, the solution is

  • sudo nano /etc/apt/sources.list
  • Add deb http://raspbian.raspberrypi.org/raspbian/ buster main contrib non-free rpi
  • sudo nano /etc/apt/preferences.d/avr-buster

Package: avr-libc avrdud binutils-avr gcc-avr
Pin: release n=buster
Pin-Priority: 1001

-sudo apt update
-sudo apt install avr-libc avrdude binutils-avr gcc-avr
-make clean
-make and woala

image-elf-hex.zip (148.7 KB)

Now to flash our Arduino nano, we go inside the klipper folder and run the following: avrdude -C /etc/avrdude.conf -v -V -p atmega328p -carduino -P /dev/serial/by-id/usb-1a86_USB2.0-Serial-if00-port0 -b57600 -D -U flash:w:out/klipper.elf.hex:i

You have to specify the usb port of the Arduino, and also the model. The complete list is at the end

Result must be: >

avrdude: Version 6.3-20171130
Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
Copyright (c) 2007-2014 Joerg Wunsch

     System wide configuration file is "/etc/avrdude.conf"
     User configuration file is "/home/pi/.avrduderc"
     User configuration file does not exist or is not a regular file, skipping

     Using Port                    : /dev/serial/by-id/usb-1a86_USB2.0-Serial-if00-port0
     Using Programmer              : arduino
     Overriding Baud Rate          : 57600
     AVR Part                      : ATmega328P
     Chip Erase delay              : 9000 us
     PAGEL                         : PD7
     BS2                           : PC2
     RESET disposition             : dedicated
     RETRY pulse                   : SCK
     serial program mode           : yes
     parallel program mode         : yes
     Timeout                       : 200
     StabDelay                     : 100
     CmdexeDelay                   : 25
     SyncLoops                     : 32
     ByteDelay                     : 0
     PollIndex                     : 3
     PollValue                     : 0x53
     Memory Detail                 :

                              Block Poll               Page                       Polled
       Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
       ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
       eeprom        65    20     4    0 no       1024    4      0  3600  3600 0xff 0xff
       flash         65     6   128    0 yes     32768  128    256  4500  4500 0xff 0xff
       lfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
       hfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
       efuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
       lock           0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
       calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
       signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

     Programmer Type : Arduino
     Description     : Arduino
     Hardware Version: 2
     Firmware Version: 1.16
     Vtarget         : 0.0 V
     Varef           : 0.0 V
     Oscillator      : Off
     SCK period      : 0.1 us

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: safemode: lfuse reads as 0
avrdude: safemode: hfuse reads as 0
avrdude: safemode: efuse reads as 0
avrdude: reading input file “out/klipper.elf.hex”
avrdude: writing flash (30836 bytes):

Writing | ################################################## | 100% 9.66s

avrdude: 30836 bytes of flash written

avrdude: safemode: lfuse reads as 0
avrdude: safemode: hfuse reads as 0
avrdude: safemode: efuse reads as 0
avrdude: safemode: Fuses OK (E:00, H:00, L:00)

avrdude done. Thank you.

Complete list of devices:

Valid parts are:
uc3a0512 = AT32UC3A0512 [/etc/avrdude.conf:14601]
c128 = AT90CAN128 [/etc/avrdude.conf:3412]
c32 = AT90CAN32 [/etc/avrdude.conf:3796]
c64 = AT90CAN64 [/etc/avrdude.conf:3604]
pwm2 = AT90PWM2 [/etc/avrdude.conf:8989]
pwm216 = AT90PWM216 [/etc/avrdude.conf:9262]
pwm2b = AT90PWM2B [/etc/avrdude.conf:9182]
pwm3 = AT90PWM3 [/etc/avrdude.conf:9172]
pwm316 = AT90PWM316 [/etc/avrdude.conf:9209]
pwm3b = AT90PWM3B [/etc/avrdude.conf:9196]
1200 = AT90S1200 [/etc/avrdude.conf:1873]
2313 = AT90S2313 [/etc/avrdude.conf:2102]
2333 = AT90S2333 [/etc/avrdude.conf:2215]
2343 = AT90S2343 [/etc/avrdude.conf:2343]
4414 = AT90S4414 [/etc/avrdude.conf:1989]
4433 = AT90S4433 [/etc/avrdude.conf:2467]
4434 = AT90S4434 [/etc/avrdude.conf:2591]
8515 = AT90S8515 [/etc/avrdude.conf:2674]
8535 = AT90S8535 [/etc/avrdude.conf:2788]
usb1286 = AT90USB1286 [/etc/avrdude.conf:11909]
usb1287 = AT90USB1287 [/etc/avrdude.conf:12100]
usb162 = AT90USB162 [/etc/avrdude.conf:12113]
usb646 = AT90USB646 [/etc/avrdude.conf:11705]
usb647 = AT90USB647 [/etc/avrdude.conf:11896]
usb82 = AT90USB82 [/etc/avrdude.conf:12300]
m103 = ATmega103 [/etc/avrdude.conf:2909]
m128 = ATmega128 [/etc/avrdude.conf:3232]
m1280 = ATmega1280 [/etc/avrdude.conf:10014]
m1281 = ATmega1281 [/etc/avrdude.conf:10204]
m1284 = ATmega1284 [/etc/avrdude.conf:4608]
m1284p = ATmega1284P [/etc/avrdude.conf:4802]
m1284rfr2 = ATmega1284RFR2 [/etc/avrdude.conf:10639]
m128rfa1 = ATmega128RFA1 [/etc/avrdude.conf:10424]
m128rfr2 = ATmega128RFR2 [/etc/avrdude.conf:10527]
m16 = ATmega16 [/etc/avrdude.conf:3989]
m161 = ATmega161 [/etc/avrdude.conf:6134]
m162 = ATmega162 [/etc/avrdude.conf:4994]
m163 = ATmega163 [/etc/avrdude.conf:5192]
m164p = ATmega164P [/etc/avrdude.conf:4168]
m168 = ATmega168 [/etc/avrdude.conf:7964]
m168p = ATmega168P [/etc/avrdude.conf:8153]
m168pb = ATmega168PB [/etc/avrdude.conf:8165]
m169 = ATmega169 [/etc/avrdude.conf:5350]
m16u2 = ATmega16U2 [/etc/avrdude.conf:12677]
m2560 = ATmega2560 [/etc/avrdude.conf:10217]
m2561 = ATmega2561 [/etc/avrdude.conf:10411]
m2564rfr2 = ATmega2564RFR2 [/etc/avrdude.conf:10629]
m256rfr2 = ATmega256RFR2 [/etc/avrdude.conf:10478]
m32 = ATmega32 [/etc/avrdude.conf:5957]
m3208 = ATmega3208 [/etc/avrdude.conf:15744]
m3209 = ATmega3209 [/etc/avrdude.conf:15768]
m324p = ATmega324P [/etc/avrdude.conf:4192]
m324pa = ATmega324PA [/etc/avrdude.conf:4384]
m325 = ATmega325 [/etc/avrdude.conf:13047]
m3250 = ATmega3250 [/etc/avrdude.conf:13428]
m328 = ATmega328 [/etc/avrdude.conf:8366]
m328p = ATmega328P [/etc/avrdude.conf:8551]
m328pb = ATmega328PB [/etc/avrdude.conf:8559]
m329 = ATmega329 [/etc/avrdude.conf:5532]
m3290 = ATmega3290 [/etc/avrdude.conf:5731]
m3290p = ATmega3290P [/etc/avrdude.conf:5745]
m329p = ATmega329P [/etc/avrdude.conf:5717]
m32m1 = ATmega32M1 [/etc/avrdude.conf:8571]
m32u2 = ATmega32U2 [/etc/avrdude.conf:12489]
m32u4 = ATmega32U4 [/etc/avrdude.conf:11514]
m406 = ATMEGA406 [/etc/avrdude.conf:14939]
m48 = ATmega48 [/etc/avrdude.conf:7540]
m4808 = ATmega4808 [/etc/avrdude.conf:15792]
m4809 = ATmega4809 [/etc/avrdude.conf:15816]
m48p = ATmega48P [/etc/avrdude.conf:7727]
m48pb = ATmega48PB [/etc/avrdude.conf:7739]
m64 = ATmega64 [/etc/avrdude.conf:3049]
m640 = ATmega640 [/etc/avrdude.conf:9824]
m644 = ATmega644 [/etc/avrdude.conf:4400]
m644p = ATmega644P [/etc/avrdude.conf:4591]
m644rfr2 = ATmega644RFR2 [/etc/avrdude.conf:10649]
m645 = ATmega645 [/etc/avrdude.conf:13238]
m6450 = ATmega6450 [/etc/avrdude.conf:13440]
m649 = ATmega649 [/etc/avrdude.conf:5758]
m6490 = ATmega6490 [/etc/avrdude.conf:5944]
m64m1 = ATmega64M1 [/etc/avrdude.conf:8591]
m64rfr2 = ATmega64RFR2 [/etc/avrdude.conf:10540]
m8 = ATmega8 [/etc/avrdude.conf:6274]
m8515 = ATmega8515 [/etc/avrdude.conf:6435]
m8535 = ATmega8535 [/etc/avrdude.conf:6593]
m88 = ATmega88 [/etc/avrdude.conf:7752]
m88p = ATmega88P [/etc/avrdude.conf:7939]
m88pb = ATmega88PB [/etc/avrdude.conf:7951]
m8u2 = ATmega8U2 [/etc/avrdude.conf:12865]
t10 = ATtiny10 [/etc/avrdude.conf:14894]
t11 = ATtiny11 [/etc/avrdude.conf:1368]
t12 = ATtiny12 [/etc/avrdude.conf:1432]
t13 = ATtiny13 [/etc/avrdude.conf:1565]
t15 = ATtiny15 [/etc/avrdude.conf:1740]
t1604 = ATtiny1604 [/etc/avrdude.conf:15308]
t1606 = ATtiny1606 [/etc/avrdude.conf:15332]
t1607 = ATtiny1607 [/etc/avrdude.conf:15356]
t1614 = ATtiny1614 [/etc/avrdude.conf:15600]
t1616 = ATtiny1616 [/etc/avrdude.conf:15624]
t1617 = ATtiny1617 [/etc/avrdude.conf:15648]
t1634 = ATtiny1634 [/etc/avrdude.conf:14627]
t20 = ATtiny20 [/etc/avrdude.conf:14904]
t202 = ATtiny202 [/etc/avrdude.conf:15116]
t204 = ATtiny204 [/etc/avrdude.conf:15140]
t212 = ATtiny212 [/etc/avrdude.conf:15380]
t214 = ATtiny214 [/etc/avrdude.conf:15404]
t2313 = ATtiny2313 [/etc/avrdude.conf:8616]
t24 = ATtiny24 [/etc/avrdude.conf:10660]
t25 = ATtiny25 [/etc/avrdude.conf:9272]
t26 = ATtiny26 [/etc/avrdude.conf:6751]
t261 = ATtiny261 [/etc/avrdude.conf:6908]
t28 = ATtiny28 [/etc/avrdude.conf:7482]
t3214 = ATtiny3214 [/etc/avrdude.conf:15672]
t3216 = ATtiny3216 [/etc/avrdude.conf:15696]
t3217 = ATtiny3217 [/etc/avrdude.conf:15720]
t4 = ATtiny4 [/etc/avrdude.conf:14850]
t40 = ATtiny40 [/etc/avrdude.conf:14921]
t402 = ATtiny402 [/etc/avrdude.conf:15164]
t404 = ATtiny404 [/etc/avrdude.conf:15188]
t406 = ATtiny406 [/etc/avrdude.conf:15212]
t412 = ATtiny412 [/etc/avrdude.conf:15428]
t414 = ATtiny414 [/etc/avrdude.conf:15453]
t416 = ATtiny416 [/etc/avrdude.conf:15477]
t417 = ATtiny417 [/etc/avrdude.conf:15502]
t4313 = ATtiny4313 [/etc/avrdude.conf:8805]
t43u = ATtiny43u [/etc/avrdude.conf:11335]
t44 = ATtiny44 [/etc/avrdude.conf:10844]
t441 = ATtiny441 [/etc/avrdude.conf:11212]
t45 = ATtiny45 [/etc/avrdude.conf:9456]
t461 = ATtiny461 [/etc/avrdude.conf:7099]
t5 = ATtiny5 [/etc/avrdude.conf:14867]
t804 = ATtiny804 [/etc/avrdude.conf:15236]
t806 = ATtiny806 [/etc/avrdude.conf:15260]
t807 = ATtiny807 [/etc/avrdude.conf:15284]
t814 = ATtiny814 [/etc/avrdude.conf:15527]
t816 = ATtiny816 [/etc/avrdude.conf:15552]
t817 = ATtiny817 [/etc/avrdude.conf:15576]
t84 = ATtiny84 [/etc/avrdude.conf:11028]
t841 = ATtiny841 [/etc/avrdude.conf:11273]
t85 = ATtiny85 [/etc/avrdude.conf:9639]
t861 = ATtiny861 [/etc/avrdude.conf:7290]
t88 = ATtiny88 [/etc/avrdude.conf:8178]
t9 = ATtiny9 [/etc/avrdude.conf:14877]
x128a1 = ATxmega128A1 [/etc/avrdude.conf:13921]
x128a1d = ATxmega128A1revD [/etc/avrdude.conf:13937]
x128a1u = ATxmega128A1U [/etc/avrdude.conf:13947]
x128a3 = ATxmega128A3 [/etc/avrdude.conf:13958]
x128a3u = ATxmega128A3U [/etc/avrdude.conf:13968]
x128a4 = ATxmega128A4 [/etc/avrdude.conf:13979]
x128a4u = ATxmega128A4U [/etc/avrdude.conf:14037]
x128b1 = ATxmega128B1 [/etc/avrdude.conf:14090]
x128b3 = ATxmega128B3 [/etc/avrdude.conf:14149]
x128c3 = ATxmega128C3 [/etc/avrdude.conf:13848]
x128d3 = ATxmega128D3 [/etc/avrdude.conf:13901]
x128d4 = ATxmega128D4 [/etc/avrdude.conf:13911]
x16a4 = ATxmega16A4 [/etc/avrdude.conf:13579]
x16a4u = ATxmega16A4U [/etc/avrdude.conf:13506]
x16c4 = ATxmega16C4 [/etc/avrdude.conf:13559]
x16d4 = ATxmega16D4 [/etc/avrdude.conf:13569]
x16e5 = ATxmega16E5 [/etc/avrdude.conf:14496]
x192a1 = ATxmega192A1 [/etc/avrdude.conf:14223]
x192a3 = ATxmega192A3 [/etc/avrdude.conf:14239]
x192a3u = ATxmega192A3U [/etc/avrdude.conf:14249]
x192c3 = ATxmega192C3 [/etc/avrdude.conf:14160]
x192d3 = ATxmega192D3 [/etc/avrdude.conf:14213]
x256a1 = ATxmega256A1 [/etc/avrdude.conf:14323]
x256a3 = ATxmega256A3 [/etc/avrdude.conf:14339]
x256a3b = ATxmega256A3B [/etc/avrdude.conf:14360]
x256a3bu = ATxmega256A3BU [/etc/avrdude.conf:14370]
x256a3u = ATxmega256A3U [/etc/avrdude.conf:14349]
x256c3 = ATxmega256C3 [/etc/avrdude.conf:14260]
x256d3 = ATxmega256D3 [/etc/avrdude.conf:14313]
x32a4 = ATxmega32A4 [/etc/avrdude.conf:13668]
x32a4u = ATxmega32A4U [/etc/avrdude.conf:13595]
x32c4 = ATxmega32C4 [/etc/avrdude.conf:13648]
x32d4 = ATxmega32D4 [/etc/avrdude.conf:13658]
x32e5 = ATxmega32E5 [/etc/avrdude.conf:14548]
x384c3 = ATxmega384C3 [/etc/avrdude.conf:14381]
x384d3 = ATxmega384D3 [/etc/avrdude.conf:14434]
x64a1 = ATxmega64A1 [/etc/avrdude.conf:13768]
x64a1u = ATxmega64A1U [/etc/avrdude.conf:13784]
x64a3 = ATxmega64A3 [/etc/avrdude.conf:13795]
x64a3u = ATxmega64A3U [/etc/avrdude.conf:13805]
x64a4 = ATxmega64A4 [/etc/avrdude.conf:13816]
x64a4u = ATxmega64A4U [/etc/avrdude.conf:13684]
x64b1 = ATxmega64B1 [/etc/avrdude.conf:13826]
x64b3 = ATxmega64B3 [/etc/avrdude.conf:13837]
x64c3 = ATxmega64C3 [/etc/avrdude.conf:13737]
x64d3 = ATxmega64D3 [/etc/avrdude.conf:13748]
x64d4 = ATxmega64D4 [/etc/avrdude.conf:13758]
x8e5 = ATxmega8E5 [/etc/avrdude.conf:14444]
ucr2 = deprecated, use ‘uc3a0512’ [/etc/avrdude.conf:14617]

Regards