Arduino IDE is great, however, some times I enjoy a simple C program and a simple Makefile. Here is a simple application skeleton allowing to blink a LED of course.
All information and files below are freely inspired from the following readings :
- https://github.com/micronucleus/micronucleus
- http://www.instructables.com/id/Honey-I-Shrunk-the-Arduino-Moving-from-Arduino-t/
Requirements :
- AVR gcc (Already installed if you have installed Arduino IDE from packages)
sudo apt-get install gcc-avr
- Micronucleus (I used v1.06 as suggested. Use make && sudo make install)
- UDEV rules to avoid sudo on each flash (copy to /etc/udev/rules.d/49-micronucleus.rules)
Source code :
#include <avr/io.h> #include <util/delay.h> // Digispark Internal LED #define PIN_LED PB1 #define DELAY_MS 500 int main(void) { // Init LED pin as output DDRB |= (1 << PIN_LED); // Light up LED PORTB |= (1 << PIN_LED); // Blink ! for (;;) { PORTB ^= (1 << PIN_LED); _delay_ms(DELAY_MS); } return 0; }
Makefile
DEVICE = attiny85 # See avr-help for all possible devices CLOCK = 16000000 # 16Mhz OBJECTS = main.o # Add more objects for each .c file here UPLOAD = micronucleus --run COMPILE = avr-gcc -Wall -Os -DF_CPU=$(CLOCK) -mmcu=$(DEVICE) # symbolic targets: all: main.hex .c.o: $(COMPILE) -c $< -o $@ flash: all $(UPLOAD) main.hex clean: rm -f main.hex main.elf $(OBJECTS) main.elf: $(OBJECTS) $(COMPILE) -o main.elf $(OBJECTS) main.hex: main.elf rm -f main.hex avr-objcopy -j .text -j .data -O ihex main.elf main.hex avr-size --format=avr --mcu=$(DEVICE) main.elf
Awesome, thanks 🙂
One thing to add: your dependency install should read “sudo apt-get install gcc-avr avr-libc” for the example to compile successfully.
Thanks! I actually used these steps in raspberry pi and they work.
Just a note: Before firmware install (i.e. running micronucleus) switch off power to attiny85. Run the program and then switch on the power.
Also, micronucleus should be run as root (“sudo micronucleus…”) unless steps have been followed to make it work without root. I point this out specifically because otherwise nothing happens and one keeps wondering whether code is wrong.