Defining Chips

Before you can really do anything you need to tell AVRprog library what the chip is. We'll use a python dict for that. Define name (that's for your information and printing errors), sig - a list of the three-byte signature, flash_size - the size of the flash memory in bytes, page_size - the size of each flash memory page in bytes, and fuse_mask - a list of the four fuses in a list [low, high, ext, lock]

Fuse mask is the oddest one, but basically it defines which bits are actually used in each fuse. For example, the ext fuse is often only the bottom three bits, so its 0x07. If you're not sure, you can set all four to 0xFF and then when you burn fuses, set all the high bits to 1.

Here are some chip examples:

attiny85 = {'name': "ATtiny85"}
attiny85['sig'] = [0x1E, 0x93, 0x0B]
attiny85['flash_size'] = 8192
attiny85['page_size'] = 64
attiny85['fuse_mask'] = (0xFF, 0xFF, 0x07, 0x3F)
atmega328p = {'name': "ATmega328P"}
atmega328p['sig'] = [0x1E, 0x95, 0x0F]
atmega328p['flash_size'] = 32768
atmega328p['page_size'] = 128
atmega328p['fuse_mask'] = (0xFF, 0xFF, 0x07, 0x3F)
atmega2560 = {'name': "ATmega2560"}
atmega2560['sig'] = [0x1E, 0x98, 0x01]
atmega2560['flash_size'] = 262144
atmega2560['page_size'] = 256
atmega2560['fuse_mask'] = (0xFF, 0xFF, 0x07, 0x3F)

Verify Signature

avrprog.verify_sig(chip_dict, verbose=True)

We suggest calling this first, you can call it whenever you like, and it will return True/False. chip_dict is that dictionary you made above

Erasing Chip

This one is easy, just call avrprog.erase_chip() - the chip erase command is the same for all chips. It may take a second on bigger chips. You must do this before programming new firmware!

Also, if your chip has the lock-firmware-fuse set, you may have to erase the flash before you can change the lock fuse.

Fuses

You can read, write and verify fuses.

Read fuses with

avrprog.read_fuses(chip_dict)

Which will return a list of the four fuses [low, high, ext, lock]

Write fuses with

avrprog.write_fuses(chip_dict, low=0xll, high=0xhh, ext=0xee, lock=0xkk)

Only arguments that are passed in will be written, so you can choose to write one fuse, or all 4.

Verify fuses with

avrprog.verify_fuses(chip_dict, low=0xll, high=0xhh, ext=0xee, lock=0xkk)

Only arguments that are passed in will be verified, so you can choose to verify one fuse, or all 4.

Flash

OK this is the good part, here's how you can write and verify flash memory. Reading memory to disk is not supported yet!

avrprog.program_file(chip_dict, "filename.hex", verbose=True, verify=True)

This function does all the work really, give it the chip information dictionary, and the name of a file (full path is OK). If verify is True, it will verify each page manually after writing. This is way faster than writing the whole file and then verifying the whole file so we recommend it.

If you really want, you can also verify against a file with:

verify_file(chip_dict, "filename.hex", verbose=True)

But it will check every single byte of the flash chip, so for example, if its a sparse hex file, like most bootloaders are where only a small portion of flash is data and the rest is empty, the empty parts are still checked. So it's very slow!

EEPROM

Not supported at this time!

This guide was first published on Jan 11, 2018. It was last updated on Mar 17, 2024.

This page (AVRprog API) was last updated on Mar 08, 2024.

Text editor powered by tinymce.