VScode flashing Atmega328PB using ft232 not flashing

I wonder is there something important missing from my attempts to flash a program with make flash using ft232. I have ubuntu 22.04 and Atmega328PB board which I can successfully flash using arduino ide but I’d like to use VS code instead.

This is my make flash output:

avrdude -v -c arduino -p m328pb -P /dev/ttyUSB0 -U flash:w:atmega328pb_test.hex

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/tuomas/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : /dev/ttyUSB0
         Using Programmer              : arduino
         AVR Part                      : ATmega328PB
         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: 3
         Firmware Version: 4.4
         Vtarget         : 0.3 V
         Varef           : 0.3 V
         Oscillator      : 28.800 kHz
         SCK period      : 3.3 us

avrdude: AVR device initialized and ready to accept instructions

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

avrdude: Device signature = 0x1e9516 (probably m328pb)
avrdude: safemode: lfuse reads as 0
avrdude: safemode: hfuse reads as 0
avrdude: safemode: efuse reads as 0
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "atmega328pb_test.hex"
avrdude: input file atmega328pb_test.hex auto detected as Intel Hex
avrdude: writing flash (0 bytes):

Writing | ################################################## | 100% 0.00s

avrdude: 0 bytes of flash written
avrdude: verifying flash memory against atmega328pb_test.hex:
avrdude: load data flash data from input file atmega328pb_test.hex:
avrdude: input file atmega328pb_test.hex auto detected as Intel Hex
avrdude: input file atmega328pb_test.hex contains 0 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.00s

avrdude: verifying ...
avrdude: 0 bytes of flash verified

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.

ft232 rx and tx leds flash like it is sending data but the code is not run on the 328pb. Here is also my makefile:

MCU   = atmega328pb
F_CPU = 16000000UL
BAUD  = 9600UL
## Also try BAUD = 19200 or 38400 if you're feeling lucky.

## A directory for common include files and the simple USART library.
## If you move either the current folder or the Library folder, you'll 
##  need to change this path to match.
LIBDIR = ../../AVR-Programming-Library

##########------------------------------------------------------##########
##########                 Programmer Defaults                  ##########
##########          Set up once, then forget about it           ##########
##########        (Can override.  See bottom of file.)          ##########
##########------------------------------------------------------##########

PROGRAMMER_TYPE = arduino
# extra arguments to avrdude: baud rate, chip type, -F flag, etc.
PROGRAMMER_ARGS =

##########------------------------------------------------------##########
##########                  Program Locations                   ##########
##########     Won't need to change if they're in your PATH     ##########
##########------------------------------------------------------##########

CC = avr-gcc
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
AVRSIZE = avr-size
AVRDUDE = avrdude

##########------------------------------------------------------##########
##########                   Makefile Magic!                    ##########
##########         Summary:                                     ##########
##########             We want a .hex file                      ##########
##########        Compile source files into .elf                ##########
##########        Convert .elf file into .hex                   ##########
##########        You shouldn't need to edit below.             ##########
##########------------------------------------------------------##########

## The name of your project (without the .c)
# TARGET = blinkLED
## Or name it automatically after the enclosing directory
TARGET = $(lastword $(subst /, ,$(CURDIR)))

# Object files: will find all .c/.h files in current directory
#  and in LIBDIR.  If you have any other (sub-)directories with code,
#  you can add them in to SOURCES below in the wildcard statement.
SOURCES=$(wildcard *.c $(LIBDIR)/*.c)
OBJECTS=$(SOURCES:.c=.o)
HEADERS=$(SOURCES:.c=.h)

## Compilation options, type man avr-gcc if you're curious.
CPPFLAGS = -DF_CPU=$(F_CPU) -DBAUD=$(BAUD) -I. -I$(LIBDIR)
CFLAGS = -Os -g -std=gnu99 -Wall
## Use short (8-bit) data types 
CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums 
## Splits up object files per function
CFLAGS += -ffunction-sections -fdata-sections 
LDFLAGS = -Wl,-Map,$(TARGET).map -L/snap/arduino/85/hardware/tools/avr/avr/lib/avr5
LIBS = -latmega328pb

$(TARGET).elf: $(OBJECTS)
	$(CC) $(LDFLAGS) $(TARGET_ARCH) $^ -o $@ $(LIBS)

all: $(TARGET).hex
clean:
	rm -f $(TARGET).elf $(TARGET).hex $(OBJECTS) $(TARGET).map

$(TARGET).hex: $(TARGET).elf
	$(OBJCOPY) -j .text -j .data -O ihex $< $@

.PHONY: all clean

# programmer for Pololu atmega 328pb is "arduino" and partno "m328p"
flash:
	avrdude -v -c arduino -p m328pb -P /dev/ttyUSB0 -U flash:w:$(TARGET).hex

What am I missing?

Here’s also the small test application but I don’t think the flash is going through:

#include <avr/io.h>
#include <stdio.h>
#include <util/delay.h>

#define F_CPU 16000000UL // Set CPU frequency to 16MHz
#define BAUD 115200
#define UBRR_VALUE ((F_CPU/16/BAUD)-1)

void uart_init(void) {
    // Set baud rate
    UBRR0H = (uint8_t)(UBRR_VALUE >> 8);
    UBRR0L = (uint8_t)UBRR_VALUE;

    // Enable transmitter
    UCSR0B = (1 << TXEN0);

    // Set frame format: 8 data bits, no parity, 1 stop bit
    UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
}

int uart_putchar(char c, FILE *stream) {
    if (c == '\n') {
        uart_putchar('\r', stream);
    }

    // Wait for transmit buffer to be empty
    while (!(UCSR0A & (1 << UDRE0)));

    // Write character to transmit buffer
    UDR0 = c;

    return 0;
}

FILE uart_output = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);

int main(void)
{
    // Initialize UART
    uart_init();

    // Redirect stdout to UART
    stdout = &uart_output;

    // Set PB5 as output
    DDRB |= (1 << PB5);

    while (1) {
        // Toggle PB5
        PORTB ^= (1 << PB5);

        printf("LED state: %d\n", (PORTB >> PB5) & 1);

        // Wait for 2 seconds
        _delay_ms(1000);
    }

    return 0;
}

Are you sure that your FT232 module/IC is working? Did you test that separately?

I use the same ft232 with arduino ide and it works correctly. Arduino ide’s programmer is AVRISP MKII, but with avrdude it didn’t work, I use “arduino” with avrdude and it at least doesn’t give errors and communicates something but I have no idea if anything is going through.

Is there any need to modify the EFUSE, HFUSE and LFUSE values? They’re values seem to be 00. Wouldn’t want to brick the board by trying values in random.

It sounds like you have an FT232 and a Pololu A-Star 328PB board, and you are able to upload programs to the A-Star correctly through the Arduino IDE. Then when you try to upload programs using all the same hardware with make flash you are getting strange output from AVRDUDE and the program doesn’t run. Is that correct?

AVRDUDE is giving you this error:

avrdude: input file atmega328pb_test.hex contains 0 bytes

I think you should review how you are compiling that file, because if it really has 0 bytes in it then you definitely won’t get good results from trying to program it. You might consider running make clean and then running make && ls -l *.hex and posting the full output from the latter command here so we can check the commands you are running to compile your code and the resulting HEX file sizes.

–David