Skip to content

Instantly share code, notes, and snippets.

@sshaplygin
Last active July 17, 2022 17:09
Show Gist options
  • Save sshaplygin/b28aecd4254249e9bdbb0d6e8c52f27b to your computer and use it in GitHub Desktop.
Save sshaplygin/b28aecd4254249e9bdbb0d6e8c52f27b to your computer and use it in GitHub Desktop.
Build and upload c/cpp code to Arduino microcontroller by avr-gcc, avr-objedump, avrdude

Scheme build and upload cpp code into Arduino:

Example by docs

Снимок экрана 2022-07-17 в 17 27 46

Usage

  • Install gcc, avr-gcc, avr-objcopy, avrdude
  • Use updaload.sh

Docs

  • main file from link
  • basic led scratch
  • If you use mac os look up this repo
  • More details in official docs link
#include <Arduino.h>
const int delayTimeout = 100;
const int pinLed = 13;
void setup() {
pinMode(pinLed, OUTPUT);
}
void loop() {
digitalWrite(pinLed, HIGH);
delay(delayTimeout);
digitalWrite(pinLed, LOW);
delay(delayTimeout);
}
int main(void) {
init();
#if defined(USBCON)
USBDEVICE.attach();
#endif
setup();
for (;;) {
loop();
if (serialEventRun) serialEventRun();
}
return 0;
}
#!bin/sh
SOURCE=main.cpp
OBJ=main.o
OUTPUT=main
LOAD=main.hex
PORT=/dev/ttyACM0
SPEED=115200
avr-gcc -Os -DF_CPU=16000000UL -mmcu=atmega328p -c -o $OBJ $SOURCE
avr-gcc -mmcu=atmega328p $OBJ -o $OUTPUT
avr-objcopy -O ihex -R .eeprom $OUTPUT $LOAD
avrdude -F -V -c arduino -p ATMEGA328P -P $PORT -b $SPEED -U flash:w:$LOAD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment