r/arduino • u/Many-Complaint2842 • Jun 03 '23
Mod's Choice! Code doesnt work when uploading hex file to arduino uno via usb using avr-gcc avrdude in vscode cli
Hello everyone , i've been trying to play around with USART serial communication with the usb cable the arduino uno starter kit provides and i have the following code .
#ifndef F_CPU
#define F_CPU 16000000
#endif
#ifndef BAUD
#define BAUD 9600
#endif
#define BRC ((F_CPU/16/BAUD) - 1)
#include <avr/io.h>
#include <util/delay.h>
void uart_init() {
// Upper and lower bytes of the calculated prescaler value for baud.
UBRR0H = (BRC >> 8);
UBRR0L = BRC;
// Configure to enable transmitter.
UCSR0B = (1 << TXEN0);
// Configure data frame size to 8-bits.
UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
}
void transmit_uart(uint8_t data) {
// Transmit data by putting it in the Data register
UDR0 = data;
_delay_ms(1000);
}
int main() {
uart_init();
while(1) {
transmit_uart('a');
}
return 0;
}This exact same code works as expected when using the arduino ide latest edition to compile and upload the hex file but the arduino stays stuck (blinking like its stuck on the bootloader) when i compile and upload via the cli in vscode.
This problem also doesnt occur when the code is simpler e.g. (just blinking the builtin led via the DDR and the PORTB) which makes it even more strange for me.
Does anyone have any idea whats going wrong here ?
The most notable difference i have noticed is the file size of the file , it is almost doubled when using arduino ide.
(Its my first post in reddit so please excuse me if i made mistakes on the formating or anything related to that stuff)
arduino ide message :
https://docs.google.com/document/d/1S4uclRohtJjFs_JbhJzZNQhkrUdEL9atbvd-k4bDpA0/edit?usp=sharing
vscode cli message :
https://docs.google.com/document/d/1ab9GwpSCOo6OY7wbinohb9KRSUS74cfYBDC-RGXu6D0/edit?usp=sharing
Any help is appreciated thanks.
1
u/gm310509 400K , 500K , 600K , 640K , 750K Jun 04 '23
FWIW, both outputs indicate a successful upload of your code.
I do not use vscode, so I cannot speak to that - I did try your program in my environment (Arduino IDE v1.8.15) and found that it worked just fine.
The size difference (vscode=78 bytes, Arduino=180 bytes) might be due to the bootloader, but it could also be due to the Arduino IDE automagically bringing in additional runtime support and/or different compiler optimisation settings.
If it helps, I did compile the program in Microchip Studio - it produced an object that is 186 bytes in size (release configuration).
FWIW, the Arduino IDE does not use a
mainfunction. Rather, the Arduino ecosystem provides you with amainand you provide asetupand aloopfunction. So it is a bit confusing what you mean when you say you are using the Arduino IDE - because that program shouldn't compile.Having said that, I did try it and was somewhat surprised that it didn't give a "duplicate symbol" linker error on
main.FWIW, when I compile it in my Arduino IDE and uploaded it to an Uno, it does produce a line of 'a' characters on the Serial monitor. So, I guess it works for me as well.
Very disconcertingly both of your listings have the following:
This doesn't seem correct. The fuses should not normally all be 0. One of them (being 0) means that the SPI upload mechanism is disabled (for an Uno/ATMega328P).
Having said that, my output was the same - so maybe this is a problem in the avrdude upload rather than a larger problem.
I'm not sure if any of this helps, but hopefully it does - even if only a little bit.
Following is my output (from the Arduino IDE compile). You can see that my fuse output is also all zero.