2022-01-24

Real Programmers

1. Minimum assembly

Integrated Development Environments (IDEs), compilers, and even assemblers are NOT NECESSARY for creating code for a microcontroller, or any other CPU. All that is needed is a description of the CPU’s architecture, its memory map, and the machine instructions' binary encoding scheme.

  • The relevant datasheets

  • A text editor

  • Alt-Tab

  • ( and that’s it! )

Next, use an assembler to perform the translation from the name of instructions and their specific arguments into the machine code.

1.1. Modify example from videos

Create a new project in avr_sim and paste the following assembly:

; bare minimum code to set a pin high
; ATtiny85

; what follows is to be placed in program memory
.cseg

; starting at this address
.org 0x0000
; not enabling any interrupts, so don't need to jump
; past the (15) interrupt vectors
Setup:
; set PORTB[0] to output
;    ldi r16, 0x01   ; 1 cycle
;    out 0x17, r16   ; 1 cycle

; alternate method
; set PORTB[0] to output using DDRB
    sbi 0x17, 0     ; 2 cycles, same result

Main:
; set pin high
    sbi 0x18, 0     ; set PORTB[0] to 1
Again:
    rjmp Again      ; infinite loop of 1 instruction

Include the following at the top of your code, which tells the assembler which platform you intend to assemble for:

.nolist
.include "tn85def.inc" ; Define device ATtiny85
.list

The video example used PB0 for the output pin. Inconveniently, this pin is in use for the ISP programming, so choose a different pin --- choose PB3 (package pin 2).

Modify this assembly code:

  1. Switch the output pin to PB3.

  2. Make the code infinitely repeat the entire program.

Did you also change the comments?

1.2. Program with avrdude

Table 1. Arduino as ISP connections
Nano ATtiny85

D10

pin 1

D11

pin 5

D12

pin6

D13

pin 7

Modify the COM port number as necessary.

avrdude -c stk500v1 -p attiny85 -P COMx -b 19200 -U flash:w:first.hex

1.3. Tips

Keep all of your work inside the WinAVR portable folder.

1.4. Loop to make a blinky

cbi is the complement to sbi to set a bit low.

How do you delay for a time before changing the pin’s state?

2. More reading

2.1. Assembly tutorials

These are highly recommended:

2.2. HEX files

A programmer is a device which accepts a description of machine code for a particular MCU and takes the actions necessary to put that code into the MCU’s program memory space. There is no particular need for a programmer to be the same software as an assembler or compiler and therefore it makes sense to have a file format specific for its input.

2.2.1. Intel HEX

A common format for these files is called Intel HEX, which dates back to the 1970’s, along with the first microprocessors and need to describe and transfer this type of information.

There are many tools to read and write this format, but its simplicity also makes it easy to write your own tool, such as rcf2ihex.py demonstrated in the recent video.

2.2.2. Universal Hex Format

The micro:bit project is on its second generation of hardware, with a different MCU. Therefore, code compiled for micro:bit V1 is not compatible with micro:bit V2. To still maintain the project’s objective of easy code upload, they needed to design a solution to have one "code file" that is compatible with both boards.

micro:bit Universal Hex Format Specification describes an extension of the Intel HEX file format that allows a V1 board to read the new file without problems while also containing the code for running on a V2 board.

While creating a new or variation of an existing file format is possible, such a decision also therefore means that the developer needs to create or modify all of the tools which interact with the file. Perhaps that time and effort would be better spent achieving the main objectives!

Please read through that document, as it does a good job of describing the requirements, legacy constraints, and testing and experimentation to verify the solution. It lays out the case for creating such a custom format and the good reasons that the solution is better than some of the alternatives.

2.3. Memory

The class videos showed the simple memory layout of the ATtiny85 from its datasheet. Since the examples were directly specifying the machine code bits and the memory location directly, there was no need for a more abstract mapping of entities to their address locations.

We will cover linkers and memory maps later, but you should first watch this video as an introduction to the subject: