# MT30s Bootloader Mainboard
This is the bootloader for the `TM4C129XNCZADI3R` microcontroller.

## Dependency
As this bootloader uses a CRC check, it requires a firmware (see https://github.com/ZeraIndia/mt30s_firmware_mainboard) of version `FW.14.12` or higher that contains a CRC header.

Unfortunately the old bootloader version without CRC check didn't get added to Git, but could be easily restored by just commenting out the `#define`s `CHECK_CRC` and `ENFORCE_CRC` in `bl_config.h` and then rebuilding it.

## Source
### Choice of SDK
The proper SDK for the MT30s is `DK-TM4C129X` (most likely the one used for firmware after its planned update). Its version is 2.2.0.295.
But the UART bootloader example in that SDK is a ROM-based one and does not support CRC check (which is only supported by the Flash-based bootloader).
That's why we use the SDK for `EK-TM4C129XL` as it contains the flash bootloader example `boot_serial`.

### Modifications to SDK
This bootloader is based on the TI SDK described above. Following steps were performed for the initial version (which essentially moves the `boot_serial` folder from the `examples/boards` folder to the main folder):
* Reduce the SDK code to a minimum so that the bootloader still can get compiled. This prevents adding dozens of MB of code we don't need to Git. In case we need code we now removed, we can just copy the missing files from the original SDK code into this repository.
* Move `examples\boards\ek-tm4c1294xl\boot_serial` to the main folder (i.e. `..\..\..\`).
* In the project settings go through all tabs and adapt all path from `..\..\..\..\boot_loader` to `..\boot_loader` and from `..\..\..\..\` to `..\`.
* For each file (which therefore is part of that project) in the project explorer (Keil µVision) go to its settings and also adapt its path.

This created a version compiling, but not yet adapted to the used platform and settings (see above).

## Build
It gets build with following settings (in `bl_config.h`):
* Overwriting the bootloader is not possible (`ENABLE_BL_UPDATE` disabled).
* It performs a CRC check of the firmware before loading it (`CHECK_CRC` and `ENFORCE_CRC` enabled). This prevents bricking the device in case the firmware udpate failed and the resulting firmware is corrupted. Without a CRC check the bootloader starts the (non-working) firmware and the user has no chance to jump back to the bootloader to retry updating the firmware. So when the CRC check failed, but bootloader just doesn't start the firmware, but retains control, i.e. stays in its update loop.


### Issue building `bl_config.inc`
When building the bootloader one will see something like this in the output folder:
```
User command #1 armcc ... bl_config.inc ...
User command terminated, Exit-Code = 1
```
The reason for that is unknown.

A workaround is to just build it twice. But this issue starts all over again after closing and reopening the Keil µVision IDE.

## Update
Normally the bootloader doesn't get updated. But the devices shipped (only internally) with firmware version older than 14.12 had an old bootloader which got built with following configuration:
* `CHECK_CRC`: disabled
* `ENFORCE_CRC`: disabled
* `ENABLE_BL_UPDATE`: disabled

That means that this old bootloader cannot get updated (in fact there was never a bootloader version that could get updated without a J-Link programmer not even the new one). This old bootloader is not CRC-enabled but can anyway get used in combination with firmware containing a CRC-header. But when using the old bootloader with never CRC-enabled firmware versions this doesn't prevent from bricking the device in case the firmware update fails.