diff options
Diffstat (limited to 'controller/fw/src/mspdebug_wrapper.c')
-rw-r--r-- | controller/fw/src/mspdebug_wrapper.c | 120 |
1 files changed, 84 insertions, 36 deletions
diff --git a/controller/fw/src/mspdebug_wrapper.c b/controller/fw/src/mspdebug_wrapper.c index eda91cc..c1f6061 100644 --- a/controller/fw/src/mspdebug_wrapper.c +++ b/controller/fw/src/mspdebug_wrapper.c @@ -6,32 +6,86 @@ #include "jtaglib.h" #include "sr_global.h" +#include "gpio_helpers.h" #include "mspdebug_wrapper.h" +#include "con_usart.h" #include <stm32f407xx.h> #define BLOCK_SIZE 512 /* bytes */ +static void sr_delay_inst(void); + static struct jtdev sr_jtdev; +static struct jtdev sr_jtdev_default; + +enum sr_gpio_types { + SR_GPIO_TCK, + SR_GPIO_TMS, + SR_GPIO_TDI, + SR_GPIO_RST, + SR_GPIO_TST, + SR_GPIO_TDO, + SR_NUM_GPIOS +}; + +struct { + GPIO_TypeDef *gpio; + int pin; + int mode; +} gpios[SR_NUM_GPIOS] = { + [SR_GPIO_TCK] = {GPIOE, 10, 1}, + [SR_GPIO_TMS] = {GPIOE, 11, 1}, + [SR_GPIO_TDI] = {GPIOE, 12, 1}, + [SR_GPIO_RST] = {GPIOE, 9, 1}, + [SR_GPIO_TST] = {GPIOE, 14, 1}, + [SR_GPIO_TDO] = {GPIOE, 13, 0}, +}; + +void sr_delay_inst() { + for (int i=0; i<10; i++) + asm volatile("nop"); +} +void mspd_jtag_init() { + for (int i=0; i<SR_NUM_GPIOS; i++) + gpio_pin_setup(gpios[i].gpio, gpios[i].pin, gpios[i].mode, 3, 0, 0); +} -int flash_and_reset(size_t img_start, size_t img_len, ssize_t (*read_block)(int addr, size_t len, uint8_t *out)) +int mspd_jtag_flash_and_reset(size_t img_start, size_t img_len, ssize_t (*read_block)(void *usr, int addr, size_t len, uint8_t *out), void *usr) { union { uint8_t bytes[BLOCK_SIZE]; uint16_t words[BLOCK_SIZE/2]; } block; + memcpy(&sr_jtdev, &sr_jtdev_default, sizeof(sr_jtdev)); /* Initialize JTAG connection */ unsigned int jtag_id = jtag_init(&sr_jtdev); - if (sr_jtdev.failed) + if (sr_jtdev.failed) { + con_printf("Couldn't initialize device\r\n"); return -EPIPE; + } + con_printf("JTAG device ID: 0x%02x\r\n", jtag_id); if (jtag_id != 0x89 && jtag_id != 0x91) return -EINVAL; + /* FIXME DEBUG */ + + con_printf("Memory dump:\r\n"); + for (size_t i=0; i<256;) { + con_printf("%04x: ", i); + for (size_t j=0; j<16; i+=2, j+=2) { + con_printf("%04x ", jtag_read_mem(&sr_jtdev, 16, 0xc000 + i)); + } + con_printf("\r\n"); + } + return 0; + /* END DEBUG */ + /* Clear flash */ jtag_erase_flash(&sr_jtdev, JTAG_ERASE_MAIN, 0); if (sr_jtdev.failed) @@ -39,7 +93,7 @@ int flash_and_reset(size_t img_start, size_t img_len, ssize_t (*read_block)(int /* 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); + ssize_t nin = read_block(usr, p, BLOCK_SIZE, block.bytes); if (nin < 0) return nin; @@ -74,9 +128,18 @@ int flash_and_reset(size_t img_start, size_t img_len, ssize_t (*read_block)(int /* mspdebug HAL shim */ int printc_err(const char *fmt, ...) { - UNUSED(fmt); - /* ignore */ - return 0; + va_list va; + va_start(va, fmt); + int rc = usart_printf_blocking_va(&con_usart, fmt, va); + if (rc) + return rc; + + size_t i; + for (i=0; fmt[i]; i++) + ; + if (i > 0 && fmt[i-1] == '\n') + usart_putc_nonblocking(&con_usart, '\r'); + return rc; } @@ -90,33 +153,11 @@ static void sr_jtdev_connect(struct jtdev *p) { /* ignore */ } -enum sr_gpio_types { - SR_GPIO_TCK, - SR_GPIO_TMS, - SR_GPIO_TDI, - SR_GPIO_RST, - SR_GPIO_TST, - SR_GPIO_TDO, - SR_NUM_GPIOS -}; - -struct { - GPIO_TypeDef *gpio; - uint16_t num; -} gpios[SR_NUM_GPIOS] = { - [SR_GPIO_TCK] = {GPIOD, 8}, - [SR_GPIO_TMS] = {GPIOD, 9}, - [SR_GPIO_TDI] = {GPIOD, 10}, - [SR_GPIO_RST] = {GPIOD, 11}, - [SR_GPIO_TST] = {GPIOD, 12}, - [SR_GPIO_TDO] = {GPIOD, 13}, -}; - static void sr_gpio_write(int num, int out) { if (out) - gpios[num].gpio->BSRR = 1<<gpios[num].num; + gpios[num].gpio->BSRR = 1<<gpios[num].pin; else - gpios[num].gpio->BSRR = 1<<gpios[num].num<<16; + gpios[num].gpio->BSRR = 1<<gpios[num].pin<<16; } static void sr_jtdev_tck(struct jtdev *p, int out) { @@ -136,7 +177,7 @@ static void sr_jtdev_tdi(struct jtdev *p, int out) { static void sr_jtdev_rst(struct jtdev *p, int out) { UNUSED(p); - sr_gpio_write(SR_GPIO_RST, out); + sr_gpio_write(SR_GPIO_RST, !out); } static void sr_jtdev_tst(struct jtdev *p, int out) { @@ -146,24 +187,25 @@ static void sr_jtdev_tst(struct jtdev *p, int out) { static int sr_jtdev_tdo_get(struct jtdev *p) { UNUSED(p); - return !!(gpios[SR_GPIO_TST].gpio->IDR & (1<<gpios[SR_GPIO_TST].num)); + return !!(gpios[SR_GPIO_TDO].gpio->IDR & (1<<gpios[SR_GPIO_TDO].pin)); } static void sr_jtdev_tclk(struct jtdev *p, int out) { UNUSED(p); - sr_gpio_write(SR_GPIO_TST, out); + sr_gpio_write(SR_GPIO_TDI, out); } static int sr_jtdev_tclk_get(struct jtdev *p) { UNUSED(p); - return !!(gpios[SR_GPIO_TDI].gpio->IDR & (1<<gpios[SR_GPIO_TDI].num)); + return !!(gpios[SR_GPIO_TDI].gpio->ODR & (1<<gpios[SR_GPIO_TDI].pin)); } static void sr_jtdev_tclk_strobe(struct jtdev *p, unsigned int count) { UNUSED(p); while (count--) { - gpios[SR_GPIO_TDI].gpio->BSRR = 1<<gpios[SR_GPIO_TDI].num; - gpios[SR_GPIO_TDI].gpio->BSRR = 1<<gpios[SR_GPIO_TDI].num<<16; + gpios[SR_GPIO_TDI].gpio->BSRR = 1<<gpios[SR_GPIO_TDI].pin; + sr_delay_inst(); + gpios[SR_GPIO_TDI].gpio->BSRR = 1<<gpios[SR_GPIO_TDI].pin<<16; } } @@ -211,3 +253,9 @@ static struct jtdev sr_jtdev = { .f = &sr_jtdev_vtable }; +static struct jtdev sr_jtdev_default = { + 0, + .f = &sr_jtdev_vtable +}; + + |