An open API service indexing awesome lists of open source software.

https://github.com/aabbtree77/digispark-attiny85-command-line

Digispark ATtiny85 setup with command line interface. Simple LED blinking example to test frequency and timing.
https://github.com/aabbtree77/digispark-attiny85-command-line

attiny85 cli digispark fuse isp micronucleus non-arduino usb usbasp

Last synced: 10 months ago
JSON representation

Digispark ATtiny85 setup with command line interface. Simple LED blinking example to test frequency and timing.

Awesome Lists containing this project

README

          

## Introduction

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.

![gThumb](attiny85-dostojevskij.jpg "Digispark ATtiny85")

The problem is that its programming (flashing) [is not as trivial as it should be][why-im-not-using]. Most tutorials involve the [Arduino][bootloader] 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.

## Flashing Code to MicroController

The [default setting][default-fuse-bits] 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][datasheet]!

1. Install gcc-avr tool chain:

```console
sudo apt-get install gcc build-essential gcc-avr binutils-avr avr-libc gdb-avr avrdude libusb-dev
```

2. Install micronucleus:

```console
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/):

```console
export PATH=$PATH:/home/tokyo/micronucleus/commandline
```

3. Modify Makefile (see the sample file) or simply execute the following to flash the code:

```console
sudo -E env "PATH=$PATH" micronucleus --run main.hex
```

4. The main workflow is to run these three commands after plugging in the board to USB:

```console
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

```console
> Please plug in the device ...
```

and one then needs to unplug and plug the board to USB port again before succeeding:

```console
> 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][minimal-code].

![gThumb](attiny85-blinking.jpg "Digispark ATtiny85")

5. 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:

```console
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][fuse-settings-general-1] and [fuse-settings-general-2][fuse-settings-general-2] earlier.

## References

- [why-im-not-using]
- [bootloader]
- [minimal-code]
- [default-fuse-bits]
- [datasheet]
- [fuse-settings-general-1]
- [fuse-settings-general-2]
- [fuse-bit-calculator]

[why-im-not-using]: https://blog.mousetech.com/why-im-not-using-digisparks-attiny85-in-almost-everything/

[bootloader]: https://www.youtube.com/watch?v=RlscDz5JCcI

[minimal-code]: https://github.com/nabilbendafi/Digispark

[default-fuse-bits]: https://github.com/micronucleus/micronucleus/blob/master/firmware/configuration/t85_default/Makefile.inc

[datasheet]: https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-2586-AVR-8-bit-Microcontroller-ATtiny25-ATtiny45-ATtiny85_Datasheet.pdf

[fuse-settings-general-1]: http://www.martyncurrey.com/arduino-atmega-328p-fuse-settings/

[fuse-settings-general-2]: http://web.engr.oregonstate.edu/~traylor/ece473/lectures/fuses.pdf

[fuse-bit-calculator]: http://eleccelerator.com/fusecalc/fusecalc.php?chip=attiny85