1
0
mirror of git://projects.qi-hardware.com/f32xbase.git synced 2024-11-27 16:06:17 +02:00

Switched from four-spaces to one-tab indentation.

This commit is contained in:
Werner Almesberger 2010-10-21 10:05:26 -03:00
parent de75051afa
commit 3bbf318536
6 changed files with 331 additions and 330 deletions

View File

@ -27,103 +27,104 @@
static uint8_t reg_read(uint8_t addr) static uint8_t reg_read(uint8_t addr)
{ {
c2_addr_write(addr); c2_addr_write(addr);
return c2_data_read(1); return c2_data_read(1);
} }
static void reg_write(uint8_t addr, uint8_t value) static void reg_write(uint8_t addr, uint8_t value)
{ {
c2_addr_write(addr); c2_addr_write(addr);
c2_data_write(value, 1); c2_data_write(value, 1);
} }
static const char *parse(const char *s, uint8_t *port, uint8_t *mode, int bits) static const char *parse(const char *s, uint8_t *port, uint8_t *mode, int bits)
{ {
int pos; int pos;
*port = *mode = 0; *port = *mode = 0;
pos = 0; pos = 0;
while (pos != bits) { while (pos != bits) {
switch (*s++) { switch (*s++) {
case '.': case '.':
continue; continue;
case '1': case '1':
*port |= 1 << pos; *port |= 1 << pos;
/* fall through */ /* fall through */
case '0': case '0':
*mode |= 1 << pos; *mode |= 1 << pos;
break; break;
case 'r': case 'r':
case 'R': case 'R':
*port |= 1 << pos; *port |= 1 << pos;
break; break;
case 0: case 0:
fprintf(stderr, "not enough pin settings\n"); fprintf(stderr, "not enough pin settings\n");
exit(1); exit(1);
default: default:
fprintf(stderr, "unrecognized pin setting \"%c\"\n", s[-1]); fprintf(stderr,
exit(1); "unrecognized pin setting \"%c\"\n", s[-1]);
} exit(1);
pos++; }
} pos++;
return s; }
return s;
} }
static void setup(const char *init) 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, &p0, &p0mdout, 8);
init = parse(init, &p2, &p2mdout, 6); init = parse(init, &p2, &p2mdout, 6);
if (*init) { if (*init) {
fprintf(stderr, "too many pin settings\n"); fprintf(stderr, "too many pin settings\n");
exit(1); exit(1);
} }
reg_write(P0, p0); reg_write(P0, p0);
reg_write(P0MDOUT, p0mdout); reg_write(P0MDOUT, p0mdout);
reg_write(P2, p2); reg_write(P2, p2);
reg_write(P2MDOUT, p2mdout); reg_write(P2MDOUT, p2mdout);
} }
static void print(uint8_t v, int bits) static void print(uint8_t v, int bits)
{ {
int pos; int pos;
for (pos = 0; pos != bits; pos++) for (pos = 0; pos != bits; pos++)
putchar(v & (1 << pos) ? '1' : '0'); putchar(v & (1 << pos) ? '1' : '0');
} }
static void scan(void) static void scan(void)
{ {
uint8_t p0, p2; uint8_t p0, p2;
p0 = reg_read(P0); p0 = reg_read(P0);
p2 = reg_read(P2); p2 = reg_read(P2);
print(p0, 8); print(p0, 8);
putchar('.'); putchar('.');
print(p2, 6); print(p2, 6);
putchar('\n'); putchar('\n');
} }
static void __attribute__((unused)) dump(void) static void __attribute__((unused)) dump(void)
{ {
fprintf(stderr, "GPIOCN %02x\n", reg_read(0xe2)); fprintf(stderr, "GPIOCN %02x\n", reg_read(0xe2));
fprintf(stderr, " P0 %02x\n", reg_read(P0)); fprintf(stderr, " P0 %02x\n", reg_read(P0));
fprintf(stderr, " P0MDOUT %02x\n", reg_read(P0MDOUT)); fprintf(stderr, " P0MDOUT %02x\n", reg_read(P0MDOUT));
fprintf(stderr, " P2 %02x\n", reg_read(P2)); fprintf(stderr, " P2 %02x\n", reg_read(P2));
fprintf(stderr, " P2MDOUT %02x\n", reg_read(P2MDOUT)); fprintf(stderr, " P2MDOUT %02x\n", reg_read(P2MDOUT));
} }
void boundary(const char *init) void boundary(const char *init)
{ {
//dump(); //dump();
setup(init); setup(init);
scan(); scan();
} }

View File

@ -32,32 +32,32 @@
static void c2_pulse(void) static void c2_pulse(void)
{ {
gpio_low(C2CK); gpio_low(C2CK);
gpio_high(C2CK); gpio_high(C2CK);
} }
static void c2_send(uint32_t value, int bits) static void c2_send(uint32_t value, int bits)
{ {
int i; int i;
for (i = 0; i != bits; i++) { for (i = 0; i != bits; i++) {
gpio_set(C2D, (value >> i) & 1); gpio_set(C2D, (value >> i) & 1);
c2_pulse(); c2_pulse();
} }
} }
static uint32_t c2_recv(int bits) static uint32_t c2_recv(int bits)
{ {
uint32_t v = 0; uint32_t v = 0;
int i; int i;
for (i = 0; i != bits; i++) { for (i = 0; i != bits; i++) {
v |= gpio_get(C2D) << i; v |= gpio_get(C2D) << i;
c2_pulse(); c2_pulse();
} }
return v; return v;
} }
@ -66,49 +66,49 @@ static uint32_t c2_recv(int bits)
static void om_addr_write(uint8_t addr) static void om_addr_write(uint8_t addr)
{ {
c2_pulse(); c2_pulse();
gpio_output(C2D); gpio_output(C2D);
c2_send(C2_ADDR_WRITE, 2); c2_send(C2_ADDR_WRITE, 2);
c2_send(addr, 8); c2_send(addr, 8);
gpio_input(C2D); gpio_input(C2D);
c2_pulse(); c2_pulse();
} }
static uint8_t om_addr_read(void) static uint8_t om_addr_read(void)
{ {
c2_pulse(); c2_pulse();
gpio_output(C2D); gpio_output(C2D);
c2_send(C2_ADDR_READ, 2); c2_send(C2_ADDR_READ, 2);
gpio_input(C2D); gpio_input(C2D);
c2_pulse(); c2_pulse();
return c2_recv(8); return c2_recv(8);
} }
static void om_data_write(uint32_t data, int bytes) static void om_data_write(uint32_t data, int bytes)
{ {
c2_pulse(); c2_pulse();
gpio_output(C2D); gpio_output(C2D);
c2_send(C2_DATA_WRITE, 2); c2_send(C2_DATA_WRITE, 2);
c2_send(bytes-1, 2); c2_send(bytes-1, 2);
c2_send(data, 8*bytes); c2_send(data, 8*bytes);
gpio_input(C2D); gpio_input(C2D);
c2_pulse(); c2_pulse();
while (!c2_recv(1)); while (!c2_recv(1));
} }
static uint32_t om_data_read(int bytes) static uint32_t om_data_read(int bytes)
{ {
c2_pulse(); c2_pulse();
gpio_output(C2D); gpio_output(C2D);
c2_send(C2_DATA_READ, 2); c2_send(C2_DATA_READ, 2);
c2_send(bytes-1, 2); c2_send(bytes-1, 2);
gpio_input(C2D); gpio_input(C2D);
c2_pulse(); c2_pulse();
while (!c2_recv(1)); while (!c2_recv(1));
return c2_recv(8*bytes); return c2_recv(8*bytes);
} }
@ -117,24 +117,24 @@ static uint32_t om_data_read(int bytes)
static void om_init(void) static void om_init(void)
{ {
gpio_init(); gpio_init();
gpio_input(C2D); gpio_input(C2D);
gpio_output(C2CK); gpio_output(C2CK);
gpio_low(C2CK); gpio_low(C2CK);
usleep(20); usleep(20);
gpio_high(C2CK); gpio_high(C2CK);
usleep(2); usleep(2);
} }
static void om_reset(void) static void om_reset(void)
{ {
gpio_input(C2D); gpio_input(C2D);
gpio_low(C2CK); gpio_low(C2CK);
usleep(20); usleep(20);
// gpio_input(C2CK); // gpio_input(C2CK);
gpio_output(C2CK); gpio_output(C2CK);
gpio_high(C2CK); gpio_high(C2CK);
} }

View File

@ -30,139 +30,140 @@ static size_t file_size;
static void dump(const char *title, void *data, size_t size) static void dump(const char *title, void *data, size_t size)
{ {
int i, j; int i, j;
fprintf(stderr, "%s:\n", title); fprintf(stderr, "%s:\n", title);
for (i = 0; i < size; i += 16) { for (i = 0; i < size; i += 16) {
fprintf(stderr, " %04x", i); fprintf(stderr, " %04x", i);
for (j = 0; j != 16 && i+j < size; j++) for (j = 0; j != 16 && i+j < size; j++)
fprintf(stderr, " %02x", ((uint8_t *) data)[i+j]); fprintf(stderr, " %02x", ((uint8_t *) data)[i+j]);
fprintf(stderr, "\n"); fprintf(stderr, "\n");
} }
} }
static void flash_device(void *data, size_t size) static void flash_device(void *data, size_t size)
{ {
int i; int i;
size_t len; size_t len;
uint8_t buf[256]; uint8_t buf[256];
for (i = 0; i < size; i += 256) for (i = 0; i < size; i += 256)
fputc('-', stderr); fputc('-', stderr);
fputc('\r', stderr); fputc('\r', stderr);
flash_init(); flash_init();
flash_device_erase(); flash_device_erase();
for (i = 0; i < size; i += 256) { for (i = 0; i < size; i += 256) {
fputc('*', stderr); fputc('*', stderr);
fflush(stderr); fflush(stderr);
len = size-i <= 256 ? size-i : 256; len = size-i <= 256 ? size-i : 256;
flash_block_write(i, data+i, len); 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);
} }
} fputc('\r', stderr);
fputc('\n', 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) static void erase_flash(void)
{ {
flash_init(); flash_init();
flash_device_erase(); flash_device_erase();
} }
static void dump_flash(size_t size) static void dump_flash(size_t size)
{ {
int i, j; int i, j;
size_t len; size_t len;
uint8_t buf[256], last[256]; uint8_t buf[256], last[256];
int skipping = 0; int skipping = 0;
flash_init(); flash_init();
for (i = 0; i < size; i += 16) { for (i = 0; i < size; i += 16) {
len = size-i <= 16 ? size-i : 16; len = size-i <= 16 ? size-i : 16;
flash_block_read(i, buf, len); flash_block_read(i, buf, len);
if (i && !memcmp(last, buf, len)) { if (i && !memcmp(last, buf, len)) {
printf("%04x: *%c", i, skipping ? '\r' : '\n'); printf("%04x: *%c", i, skipping ? '\r' : '\n');
fflush(stdout); fflush(stdout);
skipping = 1; skipping = 1;
continue; 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) static void identify(void)
{ {
int i; int i;
c2_addr_write(0); c2_addr_write(0);
printf("Dev"); printf("Dev");
for (i = 0; i != 10; i++) for (i = 0; i != 10; i++)
printf(" 0x%02x", c2_data_read(1)); printf(" 0x%02x", c2_data_read(1));
c2_addr_write(1); c2_addr_write(1);
printf("\nRev"); printf("\nRev");
for (i = 0; i != 10; i++) for (i = 0; i != 10; i++)
printf(" 0x%02x", c2_data_read(1)); printf(" 0x%02x", c2_data_read(1));
printf("\n"); printf("\n");
} }
static void do_flash(const char *name) static void do_flash(const char *name)
{ {
FILE *file; FILE *file;
uint8_t code[16384]; uint8_t code[16384];
file = fopen(name, "r"); file = fopen(name, "r");
if (!file) { if (!file) {
perror(name); perror(name);
exit(1); exit(1);
} }
file_size = fread(code, 1, sizeof(code), file); file_size = fread(code, 1, sizeof(code), file);
(void) fclose(file); (void) fclose(file);
flash_device(code, file_size); flash_device(code, file_size);
} }
static void protect(void) static void protect(void)
{ {
uint8_t pages, lock_byte; uint8_t pages, lock_byte;
pages = (file_size+511) >> 9; pages = (file_size+511) >> 9;
printf("Protecting %d page%s\n", pages, pages == 1 ? "" : "s"); printf("Protecting %d page%s\n", pages, pages == 1 ? "" : "s");
lock_byte = ~pages; lock_byte = ~pages;
flash_block_write(LOCK_BYTE, &lock_byte, 1); flash_block_write(LOCK_BYTE, &lock_byte, 1);
} }
static void usage(const char *name) static void usage(const char *name)
{ {
fprintf(stderr, fprintf(stderr,
"usage: %s [-p] file\n" "usage: %s [-p] file\n"
" %s -d\n" " %s -d\n"
" %s -e\n" " %s -e\n"
@ -179,48 +180,48 @@ static void usage(const char *name)
" -p Protect the data after writing\n" " -p Protect the data after writing\n"
"Invocation without argument resets the F32x.\n" "Invocation without argument resets the F32x.\n"
, name, name, name, name, name); , name, name, name, name, name);
exit(1); exit(1);
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
c2_init(); c2_init();
identify(); identify();
switch (argc) { switch (argc) {
case 1: case 1:
/* just reset */ /* just reset */
break; break;
case 2: case 2:
if (!strcmp(argv[1], "-d")) if (!strcmp(argv[1], "-d"))
dump_flash(0x4000); dump_flash(0x4000);
else if (!strcmp(argv[1], "-e")) else if (!strcmp(argv[1], "-e"))
erase_flash(); erase_flash();
else if (*argv[1] == '-') else if (*argv[1] == '-')
usage(*argv); usage(*argv);
else { else {
do_flash(argv[1]); do_flash(argv[1]);
identify(); identify();
} }
break; break;
case 3: case 3:
if (!strcmp(argv[1], "-p")) { if (!strcmp(argv[1], "-p")) {
if (*argv[2] == '-') 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); usage(*argv);
do_flash(argv[2]);
protect();
identify();
break;
} }
if (strcmp(argv[1], "-b")) c2_reset();
usage(*argv);
boundary(argv[2]);
break;
default:
usage(*argv);
}
c2_reset();
return 0; return 0;
} }

View File

@ -25,50 +25,50 @@
static uint8_t _c2_addr_read(void) static uint8_t _c2_addr_read(void)
{ {
uint8_t x; uint8_t x;
usleep(1000); usleep(1000);
x = c2_addr_read(); x = c2_addr_read();
// fprintf(stderr, "got 0x%02x\n", x); // fprintf(stderr, "got 0x%02x\n", x);
return x; return x;
} }
static void wait_busy(void) static void wait_busy(void)
{ {
while (_c2_addr_read() & InBusy); while (_c2_addr_read() & InBusy);
} }
static void wait_ready(void) static void wait_ready(void)
{ {
while (!(_c2_addr_read() & OutReady)); while (!(_c2_addr_read() & OutReady));
} }
static void fpdat_write(uint8_t value) static void fpdat_write(uint8_t value)
{ {
c2_data_write(value, 1); c2_data_write(value, 1);
wait_busy(); wait_busy();
} }
static uint8_t fpdat_read(void) static uint8_t fpdat_read(void)
{ {
wait_ready(); wait_ready();
return c2_data_read(1); return c2_data_read(1);
} }
static void check_status(void) static void check_status(void)
{ {
uint8_t status; uint8_t status;
status = fpdat_read(); status = fpdat_read();
if (status != FLASH_STATUS_OK) { if (status != FLASH_STATUS_OK) {
fprintf(stderr, "status 0x%02x\n", status); fprintf(stderr, "status 0x%02x\n", status);
exit(1); exit(1);
} }
} }
@ -77,58 +77,58 @@ static void check_status(void)
void flash_device_erase(void) void flash_device_erase(void)
{ {
c2_addr_write(FPDAT); c2_addr_write(FPDAT);
fpdat_write(FLASH_DEVICE_ERASE); fpdat_write(FLASH_DEVICE_ERASE);
check_status(); check_status();
fpdat_write(FLASH_ERASE_MAGIC1); fpdat_write(FLASH_ERASE_MAGIC1);
fpdat_write(FLASH_ERASE_MAGIC2); fpdat_write(FLASH_ERASE_MAGIC2);
fpdat_write(FLASH_ERASE_MAGIC3); fpdat_write(FLASH_ERASE_MAGIC3);
check_status(); check_status();
} }
void flash_block_write(uint16_t addr, const void *data, size_t size) void flash_block_write(uint16_t addr, const void *data, size_t size)
{ {
int i; int i;
if (!size) if (!size)
return; return;
c2_addr_write(FPDAT); c2_addr_write(FPDAT);
fpdat_write(FLASH_BLOCK_WRITE); fpdat_write(FLASH_BLOCK_WRITE);
check_status(); check_status();
fpdat_write(addr >> 8); fpdat_write(addr >> 8);
fpdat_write(addr); fpdat_write(addr);
fpdat_write(size); fpdat_write(size);
check_status(); check_status();
for (i = 0; i != size; i++) for (i = 0; i != size; i++)
fpdat_write(((uint8_t *) data)[i]); fpdat_write(((uint8_t *) data)[i]);
} }
void flash_block_read(uint16_t addr, void *data, size_t size) void flash_block_read(uint16_t addr, void *data, size_t size)
{ {
int i; int i;
if (!size) if (!size)
return; return;
c2_addr_write(FPDAT); c2_addr_write(FPDAT);
fpdat_write(FLASH_BLOCK_READ); fpdat_write(FLASH_BLOCK_READ);
check_status(); check_status();
fpdat_write(addr >> 8); fpdat_write(addr >> 8);
fpdat_write(addr); fpdat_write(addr);
fpdat_write(size); fpdat_write(size);
check_status(); check_status();
for (i = 0; i != size; i++) for (i = 0; i != size; i++)
((uint8_t *) data)[i] = fpdat_read(); ((uint8_t *) data)[i] = fpdat_read();
} }
void flash_init(void) void flash_init(void)
{ {
c2_addr_write(FPCTL); c2_addr_write(FPCTL);
c2_data_write(FLASH_INIT_MAGIC1, 1); c2_data_write(FLASH_INIT_MAGIC1, 1);
c2_data_write(FLASH_INIT_MAGIC2, 1); c2_data_write(FLASH_INIT_MAGIC2, 1);
usleep(20000); usleep(20000);
} }
@ -136,22 +136,21 @@ void flash_init(void)
uint8_t fp_reg_read(uint8_t addr) uint8_t fp_reg_read(uint8_t addr)
{ {
c2_addr_write(FPDAT); c2_addr_write(FPDAT);
fpdat_write(REG_READ); fpdat_write(REG_READ);
check_status(); check_status();
fpdat_write(addr); fpdat_write(addr);
fpdat_write(1); fpdat_write(1);
return fpdat_read(); return fpdat_read();
} }
void fp_reg_write(uint8_t addr, uint8_t value) void fp_reg_write(uint8_t addr, uint8_t value)
{ {
c2_addr_write(FPDAT); c2_addr_write(FPDAT);
fpdat_write(REG_WRITE); fpdat_write(REG_WRITE);
check_status(); check_status();
fpdat_write(addr); fpdat_write(addr);
fpdat_write(1); fpdat_write(1);
fpdat_write(value); fpdat_write(value);
} }

View File

@ -31,40 +31,40 @@ volatile uint32_t *mem;
static inline void gpio_high(unsigned port, unsigned bit) 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) 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) static inline void gpio_set(unsigned port, unsigned bit, int value)
{ {
if (value) if (value)
gpio_high(port, bit); gpio_high(port, bit);
else else
gpio_low(port, bit); gpio_low(port, bit);
} }
static inline int gpio_get(unsigned port, unsigned 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) 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) static inline void gpio_input(unsigned port, unsigned bit)
{ {
port_con(port) &= ~(3 << bit*2); port_con(port) &= ~(3 << bit*2);
} }

View File

@ -22,35 +22,35 @@
static void realtimize(void) static void realtimize(void)
{ {
struct sched_param prm; struct sched_param prm;
prm.sched_priority = sched_get_priority_max(SCHED_FIFO); prm.sched_priority = sched_get_priority_max(SCHED_FIFO);
if (prm.sched_priority < 0) { if (prm.sched_priority < 0) {
perror("sched_get_priority_max SCHED_FIFO"); perror("sched_get_priority_max SCHED_FIFO");
exit(1); exit(1);
} }
if (sched_setscheduler(0, SCHED_FIFO, &prm) < 0) { if (sched_setscheduler(0, SCHED_FIFO, &prm) < 0) {
perror("sched_setscheduler SCHED_FIFO"); perror("sched_setscheduler SCHED_FIFO");
exit(1); exit(1);
} }
} }
static void unrealtime(void) 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) { if (sched_setscheduler(0, SCHED_OTHER, &prm) < 0) {
perror("sched_setscheduler SCHED_OTHER"); perror("sched_setscheduler SCHED_OTHER");
exit(1); exit(1);
} }
} }
void rt(int on) void rt(int on)
{ {
if (on) if (on)
realtimize(); realtimize();
else else
unrealtime(); unrealtime();
} }