mirror of
git://projects.qi-hardware.com/ben-blinkenlights.git
synced 2024-11-23 22:42:49 +02:00
lpc111x-isp/lpc111x.c: restructure code (WIP)
This commit is contained in:
parent
eea0484a41
commit
5487578ee7
@ -15,9 +15,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <termios.h>
|
#include <alloca.h>
|
||||||
#include <fcntl.h>
|
|
||||||
#include <errno.h>
|
|
||||||
|
|
||||||
#include <ubb/ubb.h>
|
#include <ubb/ubb.h>
|
||||||
#include <ubb/swuart.h>
|
#include <ubb/swuart.h>
|
||||||
@ -31,11 +29,19 @@
|
|||||||
#define HOST_RX TGT_TX
|
#define HOST_RX TGT_TX
|
||||||
#define HOST_TX TGT_RX
|
#define HOST_TX TGT_RX
|
||||||
|
|
||||||
|
#define AUTOBAUD_TRIES 10
|
||||||
|
#define SYNC "Synchronized"
|
||||||
|
|
||||||
static void dump(uint8_t *s, int len)
|
|
||||||
|
/* ----- Debugging and tracing --------------------------------------------- */
|
||||||
|
|
||||||
|
|
||||||
|
static void trace(const char *label, const uint8_t *s, int len)
|
||||||
{
|
{
|
||||||
const uint8_t *end = s+len;
|
const uint8_t *end = s+len;
|
||||||
|
|
||||||
|
if (label)
|
||||||
|
printf("%s ", label);
|
||||||
for (end = s+len; s != end; s++) {
|
for (end = s+len; s != end; s++) {
|
||||||
if (*s >= ' ' && *s <= '~')
|
if (*s >= ' ' && *s <= '~')
|
||||||
printf("%c", *s);
|
printf("%c", *s);
|
||||||
@ -51,13 +57,94 @@ static void dump(uint8_t *s, int len)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void trace_out(const void *s, int len)
|
||||||
|
{
|
||||||
|
trace(">>>", s, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void trace_in(const uint8_t *s, int len)
|
||||||
|
{
|
||||||
|
trace("<<<", s, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* ----- Dialog functions -------------------------------------------------- */
|
||||||
|
|
||||||
|
|
||||||
|
static int dialog(const char *cmd, void *buf, int buf_len, int idle)
|
||||||
|
{
|
||||||
|
int cmd_len = strlen(cmd);
|
||||||
|
char *tx_buf = alloca(cmd_len+3);
|
||||||
|
uint8_t *rx_buf = alloca(cmd_len+2+buf_len);
|
||||||
|
int got;
|
||||||
|
|
||||||
|
memcpy(tx_buf, cmd, cmd_len);
|
||||||
|
memcpy(tx_buf+cmd_len, "\r\n", 2);
|
||||||
|
|
||||||
|
trace_out(tx_buf, cmd_len+2);
|
||||||
|
got = swuart_trx(tx_buf, cmd_len+2, rx_buf, cmd_len+2+buf_len,
|
||||||
|
idle, idle);
|
||||||
|
|
||||||
|
if (got < cmd_len+2) {
|
||||||
|
trace_in(rx_buf, got);
|
||||||
|
fprintf(stderr, "response too short for echo\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
if (memcmp(rx_buf, cmd, cmd_len) ||
|
||||||
|
rx_buf[cmd_len] != '\r' || rx_buf[cmd_len+1] != '\n') {
|
||||||
|
trace_in(rx_buf, got);
|
||||||
|
fprintf(stderr, "echo mismatch\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
trace_in(rx_buf, cmd_len+2);
|
||||||
|
|
||||||
|
got -= cmd_len+2;
|
||||||
|
memcpy(buf, rx_buf+cmd_len+2, got);
|
||||||
|
|
||||||
|
return got;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int autobaud(void)
|
||||||
|
{
|
||||||
|
uint8_t reply[100];
|
||||||
|
int i, got;
|
||||||
|
|
||||||
|
for (i = 0; i != AUTOBAUD_TRIES; i++) {
|
||||||
|
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);
|
||||||
|
trace_in(reply, got);
|
||||||
|
|
||||||
|
if (got != strlen(SYNC)+2 || memcmp(reply, SYNC "\r\n", got))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
got = dialog(SYNC, reply, sizeof(reply), 100);
|
||||||
|
trace_in(reply, got);
|
||||||
|
|
||||||
|
if (got == 4 && !memcmp(reply, "OK\r\n", 4))
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* ----- ISP session ------------------------------------------------------- */
|
||||||
|
|
||||||
|
|
||||||
static void at_exit(void)
|
static void at_exit(void)
|
||||||
{
|
{
|
||||||
ubb_close(0);
|
ubb_close(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
static void start_isp(void)
|
||||||
{
|
{
|
||||||
uint8_t reply[1000];
|
uint8_t reply[1000];
|
||||||
int got;
|
int got;
|
||||||
@ -80,26 +167,32 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
swuart_open(HOST_TX, HOST_RX, 115200);
|
swuart_open(HOST_TX, HOST_RX, 115200);
|
||||||
|
|
||||||
CLR(TGT_nRESET);
|
if (!autobaud()) {
|
||||||
usleep(10); /* DS Table 9 pg 29 says min 50 ns */
|
fprintf(stderr, "target is not responding\n");
|
||||||
SET(TGT_nRESET);
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
usleep(5*1000); /* UM 26.3.1 pg 408 says max 3 ms */
|
got = dialog("12000", reply, sizeof(reply), 100);
|
||||||
|
trace_in(reply, got);
|
||||||
|
if (got != 4 || memcmp(reply, "OK\r\n", 4)) {
|
||||||
|
fprintf(stderr, "cannot set clock rate\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
got = swuart_trx("?", 1, reply, sizeof(reply), 1000, 100);
|
|
||||||
dump(reply, got);
|
|
||||||
|
|
||||||
got = swuart_trx("Synchronized\r\n", 14,
|
/* ----- Command-line processing ------------------------------------------- */
|
||||||
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,
|
int main(int argc, char **argv)
|
||||||
reply, sizeof(reply), 1000, 100);
|
{
|
||||||
dump(reply, got);
|
uint8_t reply[1000];
|
||||||
|
int got;
|
||||||
|
|
||||||
|
start_isp();
|
||||||
|
|
||||||
|
got = dialog("J", reply, sizeof(reply), 100);
|
||||||
|
trace_in(reply, got);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user