From 031380141d9d000ceab505845ed2b723cb597774 Mon Sep 17 00:00:00 2001 From: jaseg Date: Mon, 24 Feb 2020 20:19:40 +0100 Subject: controller-fw: initial jtag programming draft --- .gitmodules | 6 ++ controller/fw/libopencm3 | 1 + controller/fw/mspdebug | 1 + controller/fw/mspdebug_wrapper.c | 121 ++++++++++++++++++++++++++++++++++----- controller/fw/mspdebug_wrapper.h | 6 ++ controller/fw/sr_global.h | 8 +++ 6 files changed, 130 insertions(+), 13 deletions(-) create mode 100644 .gitmodules create mode 160000 controller/fw/libopencm3 create mode 160000 controller/fw/mspdebug create mode 100644 controller/fw/mspdebug_wrapper.h create mode 100644 controller/fw/sr_global.h diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..dee64b1 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,6 @@ +[submodule "controller/fw/libopencm3"] + path = controller/fw/libopencm3 + url = https://github.com/libopencm3/libopencm3 +[submodule "controller/fw/mspdebug"] + path = controller/fw/mspdebug + url = https://github.com/dlbeer/mspdebug diff --git a/controller/fw/libopencm3 b/controller/fw/libopencm3 new file mode 160000 index 0000000..cb0661f --- /dev/null +++ b/controller/fw/libopencm3 @@ -0,0 +1 @@ +Subproject commit cb0661f81de5b1cae52ca99c7b5985b678176db7 diff --git a/controller/fw/mspdebug b/controller/fw/mspdebug new file mode 160000 index 0000000..b506542 --- /dev/null +++ b/controller/fw/mspdebug @@ -0,0 +1 @@ +Subproject commit b506542094de19a0a11e638a7e34e0bc4adf8d7c diff --git a/controller/fw/mspdebug_wrapper.c b/controller/fw/mspdebug_wrapper.c index 3d8daf5..68d3b1d 100644 --- a/controller/fw/mspdebug_wrapper.c +++ b/controller/fw/mspdebug_wrapper.c @@ -1,9 +1,78 @@ -#include "sr_global.h" +#include +#include + +#include #include "output.h" #include "jtaglib.h" +#include "sr_global.h" +#include "mspdebug_wrapper.h" + +#define BLOCK_SIZE 512 /* bytes */ + + +static struct jtdev sr_jtdev; + + +int flash_and_reset(size_t img_start, size_t img_len, ssize_t (*read_block)(int addr, size_t len, uint8_t *out)) +{ + union { + uint8_t bytes[BLOCK_SIZE]; + uint16_t words[BLOCK_SIZE/2]; + } block; + + /* Initialize JTAG connection */ + unsigned int jtag_id = jtag_init(&sr_jtdev); + + if (sr_jtdev.failed) + return -EPIPE; + + if (jtag_id != 0x89 && jtag_id != 0x91) + return -EINVAL; + + /* Clear flash */ + jtag_erase_flash(&sr_jtdev, JTAG_ERASE_MAIN, 0); + if (sr_jtdev.failed) + return -EPIPE; + + /* Write flash */ + for (size_t p = img_start; p < img_start + img_len; p += BLOCK_SIZE) { + ssize_t nin = read_block(p, BLOCK_SIZE, block.bytes); + + if (nin < 0) + return nin; + + if (nin & 1) { /* pad unaligned */ + block.bytes[nin] = 0; + nin ++; + } + + /* Convert to little-endian */ + for (ssize_t i=0; i