MCS Digistump Digispark Attiny 85 is an inexpensive (five-euro) board that has a 16MHz CPU, 8KB RAM (really 6K due to the pre-loaded bootloader-programmer), 8-IO pin ATmega microcontroller with the USB connector, see the image below.
The problem is that its programming (flashing) is not as trivial as it should be. Most tutorials involve the Arduino IDE, but those IDE-centered workflows depend on the IDE version number and are not flexible and portable enough. Here are some notes about programming this chip directly from the Ubuntu 20.04 command line terminal.
The default setting is F_CPU=16500000 and all the pins of the port B are set to the output pins, this even includes the reset pin PB5!
- Install gcc-avr tool chain:
sudo apt-get install gcc build-essential gcc-avr binutils-avr avr-libc gdb-avr avrdude libusb-dev
- Install micronucleus:
git clone https://github.com/micronucleus/micronucleus.git
cd micronucleus/commandline
make
Add this line to .bashrc (note your path to micronucleus, mine is /home/tokyo/):
export PATH=$PATH:/home/tokyo/micronucleus/commandline
- Modify Makefile (see the sample file) or simply execute the following to flash the code:
sudo -E env "PATH=$PATH" micronucleus --run main.hex
- The main workflow is to run these three commands after plugging in the board to USB:
make clean
make
make install
In the included Makefile, make install
runs sudo with the environment PATH borrowed from the user space
so that the micronucleus program becomes visible.
The promt issues
> Please plug in the device ...
and one then needs to unplug and plug the board to USB port again before succeeding:
> Device is found!
connecting: 33% complete
> Device has firmware version 1.6
> Available space for user applications: 6012 bytes
> Suggested sleep time between sending pages: 8ms
> Whole page count: 94 page size: 64
> Erase function sleep duration: 752ms
parsing: 50% complete
> Erasing the memory ...
erasing: 66% complete
> Starting to upload ...
writing: 83% complete
> Starting the user app ...
running: 100% complete
>> Micronucleus done. Thank you!
Sometimes it is also useful to run lsusb
command to see if the device is seen on the USB port.
A sample code is included in this folder, see also this minimal complete example.
- Updating Micronucleus
One problem with this board is that in order to update the Micronucleus bootloader, one still needs to wire an external ISP programmer such as USBasp to the corresponding pins MISO, MOSI, SCK and RESET. In order to update it:
cd micronucleus-master/firmware/releases
avrdude -P usb -c usbasp -p t85 -U flash:w:t85_default.hex
avrdude -P usb -c usbasp -p t85 -U lfuse:w:0x62:m -U hfuse:w:0xdf:m -U efuse:w:0xfe:m
This particular low fuse bit setting uses an internal 8 MHz oscillator with a division by 8 yielding F_CPU = 1000000u.
One should be extremely careful with the fuse bits. There are many ways you can "brick" the device:
- Changing the lock bits. Only the omitted (shown above) or the default -U lock:w:0xFF:m is non-locking!
- High DWEN or RSTDISBL overrides RESET and will thus irreversibly break SPI and the ability to program!
- SPIEN should be 1, Enable Serial Program and Data Downloading, otherwise no SPI anymore!
- SELFPRGEN should be 1 if you want to use micronucleus.
I wish I read the articles fuse-settings-general-1 and fuse-settings-general-2 earlier.