Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bootloader project as subfolder #47

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
a3dd0e9
Initial commit
stefan-b-jakobsson Mar 13, 2023
7527452
Added readme
stefan-b-jakobsson Mar 13, 2023
074457e
Updated I2C setup
stefan-b-jakobsson Mar 14, 2023
4b47382
Testing to see if I2C comes to life
stefan-b-jakobsson Mar 15, 2023
6ee9c62
Added flash self-programming functions
stefan-b-jakobsson Mar 17, 2023
0b9e45b
Added API planning to README
stefan-b-jakobsson Mar 17, 2023
3b82677
Upload of partially working I2C code so I can revert if need be
stefan-b-jakobsson Mar 18, 2023
2a6b116
Further testing, not yet fully working (slave hangs line on transmit …
stefan-b-jakobsson Mar 19, 2023
1f0e3ae
USI hold clock on overflow
stefan-b-jakobsson Mar 20, 2023
21a33dc
Updated README
stefan-b-jakobsson Mar 20, 2023
89f9fdb
Added general project info to the README
stefan-b-jakobsson Mar 20, 2023
85e8f33
Implemented offset 0x80
stefan-b-jakobsson Mar 20, 2023
32da60f
Uploading untested implementation, a bit too large
stefan-b-jakobsson Mar 21, 2023
129d945
Command implementations, first test
stefan-b-jakobsson Mar 22, 2023
4cf4eb7
Clean-up and better, more compact implementation
stefan-b-jakobsson Mar 25, 2023
44e36ab
Working on clean-up after update, still problems...
stefan-b-jakobsson Mar 25, 2023
941d1bf
Further work to make it actually work
stefan-b-jakobsson Mar 25, 2023
8bf7ae5
Removed I2C command Flush
stefan-b-jakobsson Mar 25, 2023
75fb9e2
Updated readme info
stefan-b-jakobsson Mar 25, 2023
f55f045
Clean-up, and rework to make second write to first page work
stefan-b-jakobsson Mar 26, 2023
bc06cfd
Fixed typo
stefan-b-jakobsson Mar 26, 2023
8994dab
Fixed language
stefan-b-jakobsson Mar 26, 2023
a69821b
Added copyright notice
stefan-b-jakobsson Mar 27, 2023
4259ff8
Fixed typo in comment
stefan-b-jakobsson Mar 27, 2023
e479e9f
Reboot command not to hang I2C bus. Clear flash memory not set.
stefan-b-jakobsson Mar 30, 2023
3e2fbae
Bugfix: 16 bit address increment
stefan-b-jakobsson Apr 3, 2023
b02fb9c
Clean-up
stefan-b-jakobsson Apr 5, 2023
9056460
Updated info on fuse byte settings
stefan-b-jakobsson Apr 5, 2023
4f632b6
Changed return values for commit command
stefan-b-jakobsson Apr 11, 2023
6a4deeb
Update README
stefan-b-jakobsson Apr 11, 2023
24ab6a6
Updating readme
stefan-b-jakobsson Apr 11, 2023
ec97697
test table formatting
stefan-b-jakobsson Apr 11, 2023
6ec5922
Updating readme file
stefan-b-jakobsson Apr 11, 2023
76fa0d4
Improved README
stefan-b-jakobsson Apr 15, 2023
323c120
Improved README
stefan-b-jakobsson Apr 15, 2023
3fb1361
Improved README
stefan-b-jakobsson Apr 15, 2023
5dbc102
Improved README
stefan-b-jakobsson Apr 15, 2023
972ba0b
Added license file
stefan-b-jakobsson Apr 15, 2023
8b1b11e
Improved merge script
stefan-b-jakobsson Apr 15, 2023
28aec43
Fixed spelling error
stefan-b-jakobsson Apr 15, 2023
6df98be
Updated compiled version of bootloader
stefan-b-jakobsson Apr 15, 2023
1b5b5ed
Fixed license file
stefan-b-jakobsson Apr 19, 2023
d88571f
Add information on ISP programming with avrdude
stefan-b-jakobsson May 17, 2023
41a4f0d
Add information on ISP programming with avrdude
stefan-b-jakobsson May 17, 2023
302c6e0
Add information on ISP programming with avrdude
stefan-b-jakobsson May 17, 2023
8d574cf
Add information on ISP programming with avrdude
stefan-b-jakobsson May 17, 2023
0bef526
Add information on ISP programming with avrdude
stefan-b-jakobsson May 17, 2023
bd0688e
Removed spaces from avrdude command line options, not working on Windows
stefan-b-jakobsson May 17, 2023
52db238
Fixed typo
stefan-b-jakobsson May 17, 2023
17e2032
Improved readme language
stefan-b-jakobsson May 18, 2023
f1927b3
Improved readme language
stefan-b-jakobsson May 18, 2023
ade2823
Improved readme language
stefan-b-jakobsson May 18, 2023
99d6ecc
Update bootloader to reset SMC when done
stefan-b-jakobsson Sep 14, 2023
4c6f6ad
Changes to commands.asm file wasn't included in previous commit
stefan-b-jakobsson Sep 14, 2023
14b2f2c
Update README info about the reboot command
stefan-b-jakobsson Sep 14, 2023
b6d73cb
Improved post_reset function documentation
stefan-b-jakobsson Sep 17, 2023
378ba3e
Fixed typo
stefan-b-jakobsson Sep 17, 2023
a67282f
Makefile
stefan-b-jakobsson Oct 4, 2023
2226544
Updates for version 3
stefan-b-jakobsson Jul 11, 2024
89704aa
Added changelog
stefan-b-jakobsson Jul 11, 2024
4c190a5
Updated changelog
stefan-b-jakobsson Jul 11, 2024
e85656f
Fix errors in changelog
stefan-b-jakobsson Jul 11, 2024
8b70f50
Bootloader command to get version, build with make
stefan-b-jakobsson Jul 12, 2024
a9a75f6
Code optimization for size, rewrite of i2c without states, and format…
stefan-b-jakobsson Jul 15, 2024
06e3301
Avoid rcall in main function, stack pointer setup in update_firmware …
stefan-b-jakobsson Jul 16, 2024
08a724c
Add commands to enable flash verify (set address and read flash)
stefan-b-jakobsson Jul 16, 2024
9528787
Add set target address command
stefan-b-jakobsson Aug 25, 2024
bcf5222
Code size optimization, jumping to start vector
stefan-b-jakobsson Aug 25, 2024
3339dcc
Make and build scripts update, adding bin file output
stefan-b-jakobsson Aug 25, 2024
e77e69d
Update readme
stefan-b-jakobsson Aug 25, 2024
d36172a
Remove hex file from tracking
stefan-b-jakobsson Aug 25, 2024
e5d1392
Moving firmware hex file used by build scripts to resources folder
stefan-b-jakobsson Aug 25, 2024
4b0b78a
Command set target address page replaced by rewind target address
stefan-b-jakobsson Aug 25, 2024
842c4cc
Rename and move scripts to special folder
stefan-b-jakobsson Aug 27, 2024
4ca66e8
Delete unnecessary files
stefan-b-jakobsson Aug 27, 2024
4e5f7b2
Add scripts folder
stefan-b-jakobsson Aug 27, 2024
f0a852e
Improve info about rewind target address
stefan-b-jakobsson Aug 27, 2024
1c4ef40
Merge bootloader project
stefan-b-jakobsson Aug 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions bootloader/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
BUILD_DIR=build
SRC_FILES=$(wildcard *.asm *.inc)

# Bootloader
$(BUILD_DIR)/bootloader.hex: $(SRC_FILES)
@mkdir -p $(BUILD_DIR)
avra -o $@ -W NoRegDef main.asm
rm -f main.eep.hex
rm -f main.obj
python scripts/merge_firmware_and_bootloader.py
python scripts/convert_hex2bin.py

# Clean
clean:
rm -f $(BUILD_DIR)/*
206 changes: 206 additions & 0 deletions bootloader/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
# Purpose

This is a custom bootloader for the Commander X16 ATTiny861 based System Management Controller (SMC).

The purpose of the bootloader is to make it possible to update the SMC firmware from the Commander X16 without using an external programmer.

Firmware data is transmitted from the computer to the SMC over I2C.

# How it works

## SMC memory map

| Byte address | Size | Description |
|-------------- |-------------| ---------------------------|
| 0x0000-0x1DFF | 7,680 bytes | Firmware area |
| 0x0000 | 2 bytes | Reset vector |
| 0x0012 | 2 bytes | EE_RDY vector |
| 0x1E00-0x1FFF | 512 bytes | Bootloader area |
| 0x1E00 | 2 bytes | Bootloader main vector |
| 0x1E02 | 2 bytes | Start update vector |
| 0x1FFE | 2 bytes | Bootloader version |


## SMC reset procedure

On SMC reset the reset vector at address 0x0000 is always executed.
This happens both when mains power is connected to the computer
and when the SMC reset pin #10 is grounded.

When the bootloader is installed, the reset vector is
remapped and always jumps to the bootloader main vector (0x1E00).

The bootloader main function checks if the Reset button is being pressed.
If the button is pressed, the computer is powered on and the
bootloader's firmware update process is started.

If the Reset button was not pressed, the bootloader jumps to
the vector stored in EE_RDY (0x0012). The firmware's original
reset vector is always moved to this address when updating
the firmware.


## Firmware update procedure

The update procedure can be started by holding down the Reset
button as described [above](#smc-reset-procedure).

The procedure can also be started by requesting the
firmware to call the start update vector (0x1e02). This is
done through the I2C command 0x8F (Start bootloader).

While the update procedure is active, firmware data
is transmitted from the CPU to the SMC over I2C as
follows:

- 8 bytes plus 1 checksum byte are transmitted
using the 0x80 I2C command (Transmit).

- The transmitted bytes are committed with
the 0x81 I2C command (Commit).

- Transmitting and committing data is
repeated for the rest of the firmware.

- The flash memory is updated in pages of 64 bytes (pages). On every
8th commit, firmware data will actually be written to flash
memory.

- Just before writing the first flash memory page, all of
the firmware flash area is erased, starting from the last page.

- Finally, the update program is expected to call
the 0x82 I2C command (Reboot) to reset the SMC and power
off the system. This will also write
any remaining data to flash memory.

If the update procedure was started by holding down
the Reset button, the system has no keyboard or mouse
connection. SMCUPDATE-x.x.x.PRG needs to stored as AUTOBOOT.X16
on the SD card so that it is automatically loaded and run.


# Building the project

The firmware hex file (x16-smc.ino.hex) is expected to be found at x16-smc/build. You need
to build or download the firmware and store it there. The firmware is
typically built with the Arduino IDE.

Type ```make``` to build the bootloader.

Build dependencies:

- AVRA assembler https://github.com/Ro5bert/avra

- Python 3

- Python intelhex module, install with pip intelhex


# Fuse settings

The bootloader and the SMC firmware depend on several ATtiny fuse settings as set out below.

The recommended low fuse value is 0xF1. This will run the SMC at 16 MHz.

The recommended high fuse value is 0xD4. This enables Brown-out Detection at 4.3V, which is necessary to prevent flash memory corruption when self-programming is enabled. Serial Programming should be enabled (bit 5) and external reset disable should not be selected (bit 7). These settings are necessary for programming the SMC with an external programmer.

Finally, the extended fuse value must be 0xFE to enable self-programming of the flash memory. The bootloader cannot work without this setting.


# Initial programming of the SMC with avrdude

The initial programming of the SMC must be done with an
external programmer.

The command line utility avrdude is the recommended tool together
with a programmer that is compatible with avrdude.

Example 1. Set fuses
```
avrdude -cstk500v1 -Cavrdude.conf -pattiny861 -P<your port> -b19200 -Ulfuse:w:0xF1:m -Uhfuse:w:0xD4:m -Uefuse:w:0xFE:m
```

Example 2. Write to flash
```
avrdude -cstk500v1 -Cavrdude.conf -pattiny861 -P<your port> -b19200 -Uflash:w:build/firmware_with_bootloader.hex:i
```

The -c option selects programmer-id; stk500v1 is for using Arduino UNO as an In-System Programmer. If you have another ISP programmer, you may need to change this value accordingly.

The -p option selects the target device, always attiny861.

The -P option selects port name on the host computer.

The -b option sets transmission baudrate; 19200 is a good value.

The -U option performs a memory operation. "-U flash:w:filename:i" writes to flash memory. "-U lfuse:w:0xF1:m" writes the low fuse value.

Please note that some fuse settings may cause the ATtiny861 not to respond. Resetting might require equipment for high voltage programming. Be careful if you choose not to use the recommended values.

The Arduino IDE also uses avrdude in the background. If you have installed the IDE can use it to program the SMC, you may enable verbose output and see what parameters are used by the IDE when it starts avrdude.


# I2C API

## Command 0x80 = Transmit (master write)

The transmit command is used to send a data packet to the bootloader.

A packet consists of 8 bytes to be written to flash and 1 checksum byte.

The checksum is the two's complement of the least significant byte of the sum of the previous bytes in the packet. The least significant byte of the sum of all 9 bytes in a packet will consequently always be 0.

## Command 0x81 = Commit (master read)

After a data packet of 9 bytes has been transmitted it must be committed with this command.

The first commit will target flash memory address 0x0000. The target address is moved forward 8 bytes on each successful commit.

The command returns 1 byte. The possible return values are:

Value | Description
------|-------------
1 | OK
2 | Error, packet size not 9
3 | Checksum error
4 | Reserved
5 | Error, overwriting bootloader section

The firmware flash memory are will be erased on the 8th successful commit, just before writing the first 64 bytes to flash memory.

## Command 0x82 = Reboot (master write)

The reboot command must always be called after the last packet
has been committed. If not, the SMC may be left in an inoperable
state.

The command first writes any buffered data to flash.

The bootloader then resets the SMC. The SMC reset shuts down the computer. It
can be restarted by pressing the power button. It is not necessary to power cycle the system
after an update.

## Command 0x83 = Get bootloader version (master read)

This command returns the bootloader version.

Available since bootloader v3.

## Command 0x84 = Rewind target address (master write)

Rewinds the target address to 0.

Available since bootloader v3.

## Command 0x85 = Read flash memory

Reads one byte of flash memory at the current target address.

The target address is post-incremented one byte.

This function is primarily intended to be used for verifying the
content of the flash memory after an update.

Available since bootloader v3.

Loading