From eea0484a41978b957d897271f5365e3a1f237785 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Fri, 28 Dec 2012 21:00:28 -0300 Subject: [PATCH] lpc111x-isp/: Ben-based in-system programmer (ISP) for NXP LPC111x chips (WIP) Uses SWUART. This is just a "first contact" skeleton. --- lpc111x-isp/Makefile | 19 ++++++++ lpc111x-isp/lpc111x.c | 105 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 lpc111x-isp/Makefile create mode 100644 lpc111x-isp/lpc111x.c diff --git a/lpc111x-isp/Makefile b/lpc111x-isp/Makefile new file mode 100644 index 0000000..10a751e --- /dev/null +++ b/lpc111x-isp/Makefile @@ -0,0 +1,19 @@ +CC = mipsel-openwrt-linux-gcc +CFLAGS = -g -Wall -O9 -I../libubb/include +LDFLAGS = -static +LDLIBS = -L../libubb -lubb + +MAIN = lpc111x +OBJS = lpc111x.o + +.PHONY: all clean spotless + +all: $(MAIN) + +$(MAIN): $(OBJS) + +clean: + rm -f $(OBJS) + +spotless: clean + rm -f $(MAIN) diff --git a/lpc111x-isp/lpc111x.c b/lpc111x-isp/lpc111x.c new file mode 100644 index 0000000..4680bd8 --- /dev/null +++ b/lpc111x-isp/lpc111x.c @@ -0,0 +1,105 @@ +/* + * lpc111x-isp/lpc111x.c - LPC111x/LPC11Cxx ISP programmer + * + * Written 2012 by Werner Almesberger + * Copyright 2012 Werner Almesberger + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + + +#define TGT_nRESET UBB_CMD +#define TGT_nISP UBB_DAT1 +#define TGT_TX UBB_DAT3 +#define TGT_RX UBB_DAT2 + +#define HOST_RX TGT_TX +#define HOST_TX TGT_RX + + +static void dump(uint8_t *s, int len) +{ + const uint8_t *end = s+len; + + for (end = s+len; s != end; s++) { + if (*s >= ' ' && *s <= '~') + printf("%c", *s); + else if (*s == 10) + printf(s+1 == end ? "\n" : "\\n"); + else if (*s == 13) + ; + else + printf("\\%02o", *s); + } + if (len && end[-1] != '\n') + printf("...\n"); +} + + +static void at_exit(void) +{ + ubb_close(0); +} + + +int main(int argc, char **argv) +{ + uint8_t reply[1000]; + int got; + + if (ubb_open(0) < 0) { + perror("ubb_open"); + exit(1); + } + atexit(at_exit); + + ubb_power(1); + + usleep(100*1000); + + SET(TGT_nRESET); + OUT(TGT_nRESET); + + CLR(TGT_nISP); + OUT(TGT_nISP); + + swuart_open(HOST_TX, HOST_RX, 115200); + + CLR(TGT_nRESET); + usleep(10); /* DS Table 9 pg 29 says min 50 ns */ + SET(TGT_nRESET); + + usleep(5*1000); /* UM 26.3.1 pg 408 says max 3 ms */ + + got = swuart_trx("?", 1, reply, sizeof(reply), 1000, 100); + dump(reply, got); + + got = swuart_trx("Synchronized\r\n", 14, + reply, sizeof(reply), 1000, 100); + dump(reply, got); + + got = swuart_trx("12000\r\n", 7, + reply, sizeof(reply), 1000, 100); + dump(reply, got); + + got = swuart_trx("J\r\n", 7, + reply, sizeof(reply), 1000, 100); + dump(reply, got); + + return 0; +}