diff --git a/f32x/boundary.c b/f32x/boundary.c index a82ade2..646247b 100644 --- a/f32x/boundary.c +++ b/f32x/boundary.c @@ -27,103 +27,104 @@ static uint8_t reg_read(uint8_t addr) { - c2_addr_write(addr); - return c2_data_read(1); + c2_addr_write(addr); + return c2_data_read(1); } static void reg_write(uint8_t addr, uint8_t value) { - c2_addr_write(addr); - c2_data_write(value, 1); + c2_addr_write(addr); + c2_data_write(value, 1); } static const char *parse(const char *s, uint8_t *port, uint8_t *mode, int bits) { - int pos; + int pos; - *port = *mode = 0; - pos = 0; - while (pos != bits) { - switch (*s++) { - case '.': - continue; - case '1': - *port |= 1 << pos; - /* fall through */ - case '0': - *mode |= 1 << pos; - break; - case 'r': - case 'R': - *port |= 1 << pos; - break; - case 0: - fprintf(stderr, "not enough pin settings\n"); - exit(1); - default: - fprintf(stderr, "unrecognized pin setting \"%c\"\n", s[-1]); - exit(1); - } - pos++; - } - return s; + *port = *mode = 0; + pos = 0; + while (pos != bits) { + switch (*s++) { + case '.': + continue; + case '1': + *port |= 1 << pos; + /* fall through */ + case '0': + *mode |= 1 << pos; + break; + case 'r': + case 'R': + *port |= 1 << pos; + break; + case 0: + fprintf(stderr, "not enough pin settings\n"); + exit(1); + default: + fprintf(stderr, + "unrecognized pin setting \"%c\"\n", s[-1]); + exit(1); + } + pos++; + } + return s; } static void setup(const char *init) { - uint8_t p0, p0mdout, p2, p2mdout; + uint8_t p0, p0mdout, p2, p2mdout; - init = parse(init, &p0, &p0mdout, 8); - init = parse(init, &p2, &p2mdout, 6); - if (*init) { - fprintf(stderr, "too many pin settings\n"); - exit(1); - } - reg_write(P0, p0); - reg_write(P0MDOUT, p0mdout); - reg_write(P2, p2); - reg_write(P2MDOUT, p2mdout); + init = parse(init, &p0, &p0mdout, 8); + init = parse(init, &p2, &p2mdout, 6); + if (*init) { + fprintf(stderr, "too many pin settings\n"); + exit(1); + } + reg_write(P0, p0); + reg_write(P0MDOUT, p0mdout); + reg_write(P2, p2); + reg_write(P2MDOUT, p2mdout); } static void print(uint8_t v, int bits) { - int pos; + int pos; - for (pos = 0; pos != bits; pos++) - putchar(v & (1 << pos) ? '1' : '0'); + for (pos = 0; pos != bits; pos++) + putchar(v & (1 << pos) ? '1' : '0'); } static void scan(void) { - uint8_t p0, p2; + uint8_t p0, p2; - p0 = reg_read(P0); - p2 = reg_read(P2); - print(p0, 8); - putchar('.'); - print(p2, 6); - putchar('\n'); + p0 = reg_read(P0); + p2 = reg_read(P2); + print(p0, 8); + putchar('.'); + print(p2, 6); + putchar('\n'); } static void __attribute__((unused)) dump(void) { - fprintf(stderr, "GPIOCN %02x\n", reg_read(0xe2)); - fprintf(stderr, " P0 %02x\n", reg_read(P0)); - fprintf(stderr, " P0MDOUT %02x\n", reg_read(P0MDOUT)); - fprintf(stderr, " P2 %02x\n", reg_read(P2)); - fprintf(stderr, " P2MDOUT %02x\n", reg_read(P2MDOUT)); + fprintf(stderr, "GPIOCN %02x\n", reg_read(0xe2)); + fprintf(stderr, " P0 %02x\n", reg_read(P0)); + fprintf(stderr, " P0MDOUT %02x\n", reg_read(P0MDOUT)); + fprintf(stderr, " P2 %02x\n", reg_read(P2)); + fprintf(stderr, " P2MDOUT %02x\n", reg_read(P2MDOUT)); } void boundary(const char *init) { //dump(); - setup(init); - scan(); + setup(init); + scan(); } diff --git a/f32x/c2-om.c b/f32x/c2-om.c index 1852de1..3ff3f4f 100644 --- a/f32x/c2-om.c +++ b/f32x/c2-om.c @@ -32,32 +32,32 @@ static void c2_pulse(void) { - gpio_low(C2CK); - gpio_high(C2CK); + gpio_low(C2CK); + gpio_high(C2CK); } static void c2_send(uint32_t value, int bits) { - int i; + int i; - for (i = 0; i != bits; i++) { - gpio_set(C2D, (value >> i) & 1); - c2_pulse(); - } + for (i = 0; i != bits; i++) { + gpio_set(C2D, (value >> i) & 1); + c2_pulse(); + } } static uint32_t c2_recv(int bits) { - uint32_t v = 0; - int i; + uint32_t v = 0; + int i; - for (i = 0; i != bits; i++) { - v |= gpio_get(C2D) << i; - c2_pulse(); - } - return v; + for (i = 0; i != bits; i++) { + v |= gpio_get(C2D) << i; + c2_pulse(); + } + return v; } @@ -66,49 +66,49 @@ static uint32_t c2_recv(int bits) static void om_addr_write(uint8_t addr) { - c2_pulse(); - gpio_output(C2D); - c2_send(C2_ADDR_WRITE, 2); - c2_send(addr, 8); - gpio_input(C2D); - c2_pulse(); + c2_pulse(); + gpio_output(C2D); + c2_send(C2_ADDR_WRITE, 2); + c2_send(addr, 8); + gpio_input(C2D); + c2_pulse(); } static uint8_t om_addr_read(void) { - c2_pulse(); - gpio_output(C2D); - c2_send(C2_ADDR_READ, 2); - gpio_input(C2D); - c2_pulse(); - return c2_recv(8); + c2_pulse(); + gpio_output(C2D); + c2_send(C2_ADDR_READ, 2); + gpio_input(C2D); + c2_pulse(); + return c2_recv(8); } static void om_data_write(uint32_t data, int bytes) { - c2_pulse(); - gpio_output(C2D); - c2_send(C2_DATA_WRITE, 2); - c2_send(bytes-1, 2); - c2_send(data, 8*bytes); - gpio_input(C2D); - c2_pulse(); - while (!c2_recv(1)); + c2_pulse(); + gpio_output(C2D); + c2_send(C2_DATA_WRITE, 2); + c2_send(bytes-1, 2); + c2_send(data, 8*bytes); + gpio_input(C2D); + c2_pulse(); + while (!c2_recv(1)); } static uint32_t om_data_read(int bytes) { - c2_pulse(); - gpio_output(C2D); - c2_send(C2_DATA_READ, 2); - c2_send(bytes-1, 2); - gpio_input(C2D); - c2_pulse(); - while (!c2_recv(1)); - return c2_recv(8*bytes); + c2_pulse(); + gpio_output(C2D); + c2_send(C2_DATA_READ, 2); + c2_send(bytes-1, 2); + gpio_input(C2D); + c2_pulse(); + while (!c2_recv(1)); + return c2_recv(8*bytes); } @@ -117,24 +117,24 @@ static uint32_t om_data_read(int bytes) static void om_init(void) { - gpio_init(); - gpio_input(C2D); - gpio_output(C2CK); - gpio_low(C2CK); - usleep(20); - gpio_high(C2CK); - usleep(2); + gpio_init(); + gpio_input(C2D); + gpio_output(C2CK); + gpio_low(C2CK); + usleep(20); + gpio_high(C2CK); + usleep(2); } static void om_reset(void) { - gpio_input(C2D); - gpio_low(C2CK); - usleep(20); -// gpio_input(C2CK); - gpio_output(C2CK); - gpio_high(C2CK); + gpio_input(C2D); + gpio_low(C2CK); + usleep(20); +// gpio_input(C2CK); + gpio_output(C2CK); + gpio_high(C2CK); } diff --git a/f32x/f32x.c b/f32x/f32x.c index c631ce2..c3d541d 100644 --- a/f32x/f32x.c +++ b/f32x/f32x.c @@ -30,139 +30,140 @@ static size_t file_size; static void dump(const char *title, void *data, size_t size) { - int i, j; + int i, j; - fprintf(stderr, "%s:\n", title); - for (i = 0; i < size; i += 16) { - fprintf(stderr, " %04x", i); - for (j = 0; j != 16 && i+j < size; j++) - fprintf(stderr, " %02x", ((uint8_t *) data)[i+j]); - fprintf(stderr, "\n"); - } + fprintf(stderr, "%s:\n", title); + for (i = 0; i < size; i += 16) { + fprintf(stderr, " %04x", i); + for (j = 0; j != 16 && i+j < size; j++) + fprintf(stderr, " %02x", ((uint8_t *) data)[i+j]); + fprintf(stderr, "\n"); + } } static void flash_device(void *data, size_t size) { - int i; - size_t len; - uint8_t buf[256]; + int i; + size_t len; + uint8_t buf[256]; - for (i = 0; i < size; i += 256) - fputc('-', stderr); - fputc('\r', stderr); + for (i = 0; i < size; i += 256) + fputc('-', stderr); + fputc('\r', stderr); - flash_init(); - flash_device_erase(); + flash_init(); + flash_device_erase(); - for (i = 0; i < size; i += 256) { - fputc('*', stderr); - fflush(stderr); - len = size-i <= 256 ? size-i : 256; - flash_block_write(i, data+i, len); - } - fputc('\r', stderr); - - for (i = 0; i < size; i += 256) { - fputc('#', stderr); - fflush(stderr); - len = size-i <= 256 ? size-i : 256; - flash_block_read(i, buf, len); - if (memcmp(buf, data+i, len)) { - fprintf(stderr, "compare error at 0x%04x\n", i); - dump("Expected", data+i, len); - dump("Read", buf, len); - exit(1); + for (i = 0; i < size; i += 256) { + fputc('*', stderr); + fflush(stderr); + len = size-i <= 256 ? size-i : 256; + flash_block_write(i, data+i, len); } - } - fputc('\n', stderr); + fputc('\r', stderr); + + for (i = 0; i < size; i += 256) { + fputc('#', stderr); + fflush(stderr); + len = size-i <= 256 ? size-i : 256; + flash_block_read(i, buf, len); + if (memcmp(buf, data+i, len)) { + fprintf(stderr, "compare error at 0x%04x\n", i); + dump("Expected", data+i, len); + dump("Read", buf, len); + exit(1); + } + } + fputc('\n', stderr); } static void erase_flash(void) { - flash_init(); - flash_device_erase(); + flash_init(); + flash_device_erase(); } static void dump_flash(size_t size) { - int i, j; - size_t len; - uint8_t buf[256], last[256]; - int skipping = 0; + int i, j; + size_t len; + uint8_t buf[256], last[256]; + int skipping = 0; - flash_init(); - for (i = 0; i < size; i += 16) { - len = size-i <= 16 ? size-i : 16; - flash_block_read(i, buf, len); - if (i && !memcmp(last, buf, len)) { - printf("%04x: *%c", i, skipping ? '\r' : '\n'); - fflush(stdout); - skipping = 1; - continue; + flash_init(); + for (i = 0; i < size; i += 16) { + len = size-i <= 16 ? size-i : 16; + flash_block_read(i, buf, len); + if (i && !memcmp(last, buf, len)) { + printf("%04x: *%c", i, skipping ? '\r' : '\n'); + fflush(stdout); + skipping = 1; + continue; + } + skipping = 0; + memcpy(last, buf, len); + printf("%04x:", i); + for (j = 0; j != len; j++) + printf(" %02x", buf[j]); + printf(" "); + for (j = 0; j != len; j++) + printf("%c", + buf[j] >= ' ' && buf[j] <= '~' ? buf[j] : '.'); + putchar('\n'); + fflush(stdout); } - skipping = 0; - memcpy(last, buf, len); - printf("%04x:", i); - for (j = 0; j != len; j++) - printf(" %02x", buf[j]); - printf(" "); - for (j = 0; j != len; j++) - printf("%c", buf[j] >= ' ' && buf[j] <= '~' ? buf[j] : '.'); - putchar('\n'); - fflush(stdout); - } } static void identify(void) { - int i; + int i; - c2_addr_write(0); - printf("Dev"); - for (i = 0; i != 10; i++) - printf(" 0x%02x", c2_data_read(1)); - c2_addr_write(1); - printf("\nRev"); - for (i = 0; i != 10; i++) - printf(" 0x%02x", c2_data_read(1)); - printf("\n"); + c2_addr_write(0); + printf("Dev"); + for (i = 0; i != 10; i++) + printf(" 0x%02x", c2_data_read(1)); + c2_addr_write(1); + printf("\nRev"); + for (i = 0; i != 10; i++) + printf(" 0x%02x", c2_data_read(1)); + printf("\n"); } static void do_flash(const char *name) { - FILE *file; - uint8_t code[16384]; + FILE *file; + uint8_t code[16384]; - file = fopen(name, "r"); - if (!file) { - perror(name); - exit(1); - } - file_size = fread(code, 1, sizeof(code), file); - (void) fclose(file); - flash_device(code, file_size); + file = fopen(name, "r"); + if (!file) { + perror(name); + exit(1); + } + file_size = fread(code, 1, sizeof(code), file); + (void) fclose(file); + flash_device(code, file_size); } static void protect(void) { - uint8_t pages, lock_byte; + uint8_t pages, lock_byte; - pages = (file_size+511) >> 9; - printf("Protecting %d page%s\n", pages, pages == 1 ? "" : "s"); - lock_byte = ~pages; - flash_block_write(LOCK_BYTE, &lock_byte, 1); + pages = (file_size+511) >> 9; + printf("Protecting %d page%s\n", pages, pages == 1 ? "" : "s"); + lock_byte = ~pages; + flash_block_write(LOCK_BYTE, &lock_byte, 1); } static void usage(const char *name) { - fprintf(stderr, + fprintf(stderr, "usage: %s [-p] file\n" " %s -d\n" " %s -e\n" @@ -179,48 +180,48 @@ static void usage(const char *name) " -p Protect the data after writing\n" "Invocation without argument resets the F32x.\n" , name, name, name, name, name); - exit(1); + exit(1); } int main(int argc, char **argv) { - c2_init(); - identify(); + c2_init(); + identify(); - switch (argc) { - case 1: - /* just reset */ - break; - case 2: - if (!strcmp(argv[1], "-d")) - dump_flash(0x4000); - else if (!strcmp(argv[1], "-e")) - erase_flash(); - else if (*argv[1] == '-') - usage(*argv); - else { - do_flash(argv[1]); - identify(); - } - break; - case 3: - if (!strcmp(argv[1], "-p")) { - if (*argv[2] == '-') + switch (argc) { + case 1: + /* just reset */ + break; + case 2: + if (!strcmp(argv[1], "-d")) + dump_flash(0x4000); + else if (!strcmp(argv[1], "-e")) + erase_flash(); + else if (*argv[1] == '-') + usage(*argv); + else { + do_flash(argv[1]); + identify(); + } + break; + case 3: + if (!strcmp(argv[1], "-p")) { + if (*argv[2] == '-') + usage(*argv); + do_flash(argv[2]); + protect(); + identify(); + break; + } + if (strcmp(argv[1], "-b")) + usage(*argv); + boundary(argv[2]); + break; + default: usage(*argv); - do_flash(argv[2]); - protect(); - identify(); - break; } - if (strcmp(argv[1], "-b")) - usage(*argv); - boundary(argv[2]); - break; - default: - usage(*argv); - } - c2_reset(); + c2_reset(); - return 0; + return 0; } diff --git a/f32x/flash.c b/f32x/flash.c index 2a6c1d9..e129346 100644 --- a/f32x/flash.c +++ b/f32x/flash.c @@ -25,50 +25,50 @@ static uint8_t _c2_addr_read(void) { - uint8_t x; + uint8_t x; - usleep(1000); - x = c2_addr_read(); -// fprintf(stderr, "got 0x%02x\n", x); - return x; + usleep(1000); + x = c2_addr_read(); +// fprintf(stderr, "got 0x%02x\n", x); + return x; } static void wait_busy(void) { - while (_c2_addr_read() & InBusy); + while (_c2_addr_read() & InBusy); } static void wait_ready(void) { - while (!(_c2_addr_read() & OutReady)); + while (!(_c2_addr_read() & OutReady)); } static void fpdat_write(uint8_t value) { - c2_data_write(value, 1); - wait_busy(); + c2_data_write(value, 1); + wait_busy(); } static uint8_t fpdat_read(void) { - wait_ready(); - return c2_data_read(1); + wait_ready(); + return c2_data_read(1); } static void check_status(void) { - uint8_t status; + uint8_t status; - status = fpdat_read(); - if (status != FLASH_STATUS_OK) { - fprintf(stderr, "status 0x%02x\n", status); - exit(1); - } + status = fpdat_read(); + if (status != FLASH_STATUS_OK) { + fprintf(stderr, "status 0x%02x\n", status); + exit(1); + } } @@ -77,58 +77,58 @@ static void check_status(void) void flash_device_erase(void) { - c2_addr_write(FPDAT); - fpdat_write(FLASH_DEVICE_ERASE); - check_status(); - fpdat_write(FLASH_ERASE_MAGIC1); - fpdat_write(FLASH_ERASE_MAGIC2); - fpdat_write(FLASH_ERASE_MAGIC3); - check_status(); + c2_addr_write(FPDAT); + fpdat_write(FLASH_DEVICE_ERASE); + check_status(); + fpdat_write(FLASH_ERASE_MAGIC1); + fpdat_write(FLASH_ERASE_MAGIC2); + fpdat_write(FLASH_ERASE_MAGIC3); + check_status(); } void flash_block_write(uint16_t addr, const void *data, size_t size) { - int i; + int i; - if (!size) - return; - c2_addr_write(FPDAT); - fpdat_write(FLASH_BLOCK_WRITE); - check_status(); - fpdat_write(addr >> 8); - fpdat_write(addr); - fpdat_write(size); - check_status(); - for (i = 0; i != size; i++) - fpdat_write(((uint8_t *) data)[i]); + if (!size) + return; + c2_addr_write(FPDAT); + fpdat_write(FLASH_BLOCK_WRITE); + check_status(); + fpdat_write(addr >> 8); + fpdat_write(addr); + fpdat_write(size); + check_status(); + for (i = 0; i != size; i++) + fpdat_write(((uint8_t *) data)[i]); } void flash_block_read(uint16_t addr, void *data, size_t size) { - int i; + int i; - if (!size) - return; - c2_addr_write(FPDAT); - fpdat_write(FLASH_BLOCK_READ); - check_status(); - fpdat_write(addr >> 8); - fpdat_write(addr); - fpdat_write(size); - check_status(); - for (i = 0; i != size; i++) - ((uint8_t *) data)[i] = fpdat_read(); + if (!size) + return; + c2_addr_write(FPDAT); + fpdat_write(FLASH_BLOCK_READ); + check_status(); + fpdat_write(addr >> 8); + fpdat_write(addr); + fpdat_write(size); + check_status(); + for (i = 0; i != size; i++) + ((uint8_t *) data)[i] = fpdat_read(); } void flash_init(void) { - c2_addr_write(FPCTL); - c2_data_write(FLASH_INIT_MAGIC1, 1); - c2_data_write(FLASH_INIT_MAGIC2, 1); - usleep(20000); + c2_addr_write(FPCTL); + c2_data_write(FLASH_INIT_MAGIC1, 1); + c2_data_write(FLASH_INIT_MAGIC2, 1); + usleep(20000); } @@ -136,22 +136,21 @@ void flash_init(void) uint8_t fp_reg_read(uint8_t addr) { - c2_addr_write(FPDAT); - fpdat_write(REG_READ); - check_status(); - fpdat_write(addr); - fpdat_write(1); - return fpdat_read(); + c2_addr_write(FPDAT); + fpdat_write(REG_READ); + check_status(); + fpdat_write(addr); + fpdat_write(1); + return fpdat_read(); } void fp_reg_write(uint8_t addr, uint8_t value) { - c2_addr_write(FPDAT); - fpdat_write(REG_WRITE); - check_status(); - fpdat_write(addr); - fpdat_write(1); - fpdat_write(value); + c2_addr_write(FPDAT); + fpdat_write(REG_WRITE); + check_status(); + fpdat_write(addr); + fpdat_write(1); + fpdat_write(value); } - diff --git a/f32x/gpio.h b/f32x/gpio.h index 9afeb21..a1465a7 100644 --- a/f32x/gpio.h +++ b/f32x/gpio.h @@ -31,40 +31,40 @@ volatile uint32_t *mem; static inline void gpio_high(unsigned port, unsigned bit) { - port_dat(port) |= 1 << bit; + port_dat(port) |= 1 << bit; } static inline void gpio_low(unsigned port, unsigned bit) { - port_dat(port) &= ~(1 << bit); + port_dat(port) &= ~(1 << bit); } static inline void gpio_set(unsigned port, unsigned bit, int value) { - if (value) - gpio_high(port, bit); - else - gpio_low(port, bit); + if (value) + gpio_high(port, bit); + else + gpio_low(port, bit); } static inline int gpio_get(unsigned port, unsigned bit) { - return (port_dat(port) >> bit) & 1; + return (port_dat(port) >> bit) & 1; } static inline void gpio_output(unsigned port, unsigned bit) { - port_con(port) = (port_con(port) & ~(3 << bit*2)) | (1 << bit*2); + port_con(port) = (port_con(port) & ~(3 << bit*2)) | (1 << bit*2); } static inline void gpio_input(unsigned port, unsigned bit) { - port_con(port) &= ~(3 << bit*2); + port_con(port) &= ~(3 << bit*2); } diff --git a/f32x/rt.c b/f32x/rt.c index cb3ba06..2d5f68d 100644 --- a/f32x/rt.c +++ b/f32x/rt.c @@ -22,35 +22,35 @@ static void realtimize(void) { - struct sched_param prm; + struct sched_param prm; - prm.sched_priority = sched_get_priority_max(SCHED_FIFO); - if (prm.sched_priority < 0) { - perror("sched_get_priority_max SCHED_FIFO"); - exit(1); - } - if (sched_setscheduler(0, SCHED_FIFO, &prm) < 0) { - perror("sched_setscheduler SCHED_FIFO"); - exit(1); - } + prm.sched_priority = sched_get_priority_max(SCHED_FIFO); + if (prm.sched_priority < 0) { + perror("sched_get_priority_max SCHED_FIFO"); + exit(1); + } + if (sched_setscheduler(0, SCHED_FIFO, &prm) < 0) { + perror("sched_setscheduler SCHED_FIFO"); + exit(1); + } } static void unrealtime(void) { - struct sched_param prm = { .sched_priority = 0 }; + struct sched_param prm = { .sched_priority = 0 }; - if (sched_setscheduler(0, SCHED_OTHER, &prm) < 0) { - perror("sched_setscheduler SCHED_OTHER"); - exit(1); - } + if (sched_setscheduler(0, SCHED_OTHER, &prm) < 0) { + perror("sched_setscheduler SCHED_OTHER"); + exit(1); + } } void rt(int on) { - if (on) - realtimize(); - else - unrealtime(); + if (on) + realtimize(); + else + unrealtime(); }