ftdi_transfer: Windows compatibility

The primary issue was the "r" vs "rb" argument given to fopen.
This commit is contained in:
Zack Buhman 2024-10-31 10:12:59 -05:00
parent 17c1418574
commit a3794d8718
2 changed files with 28 additions and 14 deletions

View File

@ -22,7 +22,7 @@ ftdi_%.o: ftdi_%.cpp
$(CXX) $(CFLAGS) $(CXXFLAGS) $(FTDI_CFLAGS) -c $< -o $@ $(CXX) $(CFLAGS) $(CXXFLAGS) $(FTDI_CFLAGS) -c $< -o $@
ftdi_%: ftdi_%.o crc32.o ftdi_%: ftdi_%.o crc32.o
$(CXX) $(LDFLAGS) $(FTDI_LDFLAGS) $^ -o $@ $(CXX) $^ -o $@ $(FTDI_LDFLAGS)
ttf_outline: ttf_outline.o 2d_pack.o ttf_outline: ttf_outline.o 2d_pack.o

View File

@ -96,11 +96,13 @@ int init_ftdi_context(struct ftdi_context * ftdi)
return -1; return -1;
} }
/*
res = ftdi_set_latency_timer(ftdi, 1); res = ftdi_set_latency_timer(ftdi, 1);
if (res < 0) { if (res < 0) {
fprintf(stderr, "ftdi_set_latency_timer\n"); fprintf(stderr, "ftdi_set_latency_timer\n");
return -1; return -1;
} }
*/
res = ftdi_tciflush(ftdi); res = ftdi_tciflush(ftdi);
if (res < 0) { if (res < 0) {
@ -156,8 +158,6 @@ long read_with_timeout(struct ftdi_context * ftdi, uint8_t * read_buf, const lon
return read_length; return read_length;
} }
const int chunk_size = 1024;
long min(long a, long b) long min(long a, long b)
{ {
return a > b ? b : a; return a > b ? b : a;
@ -237,6 +237,7 @@ int do_write(struct ftdi_context * ftdi, const uint8_t * buf, const uint32_t siz
} }
fprintf(stderr, "remote: dest: %08x size: %08x\n", reply.arg[0], reply.arg[1]); fprintf(stderr, "remote: dest: %08x size: %08x\n", reply.arg[0], reply.arg[1]);
if (reply.arg[0] != dest || reply.arg[1] != size) { if (reply.arg[0] != dest || reply.arg[1] != size) {
fprintf(stderr, "dest or size mismatch\n");
return -1; return -1;
} }
@ -289,7 +290,7 @@ int do_jump(struct ftdi_context * ftdi)
int read_file(const char * filename, uint8_t ** buf, uint32_t * size) int read_file(const char * filename, uint8_t ** buf, uint32_t * size)
{ {
FILE * file = fopen(filename, "r"); FILE * file = fopen(filename, "rb");
if (file == NULL) { if (file == NULL) {
fprintf(stderr, "fopen\n"); fprintf(stderr, "fopen\n");
return -1; return -1;
@ -314,7 +315,11 @@ int read_file(const char * filename, uint8_t ** buf, uint32_t * size)
*buf = (uint8_t *)malloc(off); *buf = (uint8_t *)malloc(off);
ssize_t fread_size = fread(*buf, 1, off, file); ssize_t fread_size = fread(*buf, 1, off, file);
if (fread_size < 0) { if (fread_size < 0) {
fprintf(stderr, "fread"); fprintf(stderr, "fread error");
return -1;
}
if (fread_size != off) {
fprintf(stderr, "fread short read: %ld ; expected: %ld\n", fread_size, off);
return -1; return -1;
} }
@ -329,14 +334,13 @@ int read_file(const char * filename, uint8_t ** buf, uint32_t * size)
return 0; return 0;
} }
int do_read(struct ftdi_context * ftdi) int do_read(struct ftdi_context * ftdi, uint8_t * buf, const uint32_t size)
{ {
fprintf(stderr, "do_read\n"); fprintf(stderr, "do_read\n");
int res; int res;
const uint32_t src = 0xac010000; const uint32_t src = 0xac010000;
const uint32_t size = 51584;
union serial_load::command_reply command = serial_load::read_command(src, size); union serial_load::command_reply command = serial_load::read_command(src, size);
res = ftdi_write_data(ftdi, command.u8, (sizeof (command))); res = ftdi_write_data(ftdi, command.u8, (sizeof (command)));
assert(res == (sizeof (command))); assert(res == (sizeof (command)));
@ -350,10 +354,14 @@ int do_read(struct ftdi_context * ftdi)
return -1; return -1;
} }
uint32_t * buf = (uint32_t *)malloc(size); uint32_t read_length = 0;
res = ftdi_read_data(ftdi, (uint8_t *)buf, size); while (read_length < size) {
assert(res >= 0); res = ftdi_read_data(ftdi, (uint8_t *)buf, size - read_length);
assert((uint32_t)res == size); assert(res >= 0);
read_length += res;
if (read_length < size)
fprintf(stderr, "short read; want %x out of %x\n", size - read_length, size);
}
uint32_t buf_crc = crc32((uint8_t*)buf, size); uint32_t buf_crc = crc32((uint8_t*)buf, size);
@ -414,18 +422,24 @@ int main(int argc, char * argv[])
return EXIT_FAILURE; return EXIT_FAILURE;
} }
uint8_t discard[1024];
res = ftdi_read_data(ftdi, discard, (sizeof (discard)));
assert(res >= 0);
(void)discard;
struct timespec start; struct timespec start;
struct timespec end; struct timespec end;
res = clock_gettime(CLOCK_MONOTONIC, &start); res = clock_gettime(CLOCK_MONOTONIC, &start);
assert(res >= 0); assert(res >= 0);
int do_write_ret = do_write(ftdi, buf, size); int do_write_ret = do_write(ftdi, buf, size);
res = clock_gettime(CLOCK_MONOTONIC, &end); res = clock_gettime(CLOCK_MONOTONIC, &end);
assert(res >= 0); assert(res >= 0);
fprintf(stderr, "do_write time: %.03f\n", timespec_difference(&end, &start)); fprintf(stderr, "do_write time: %.03f\n", timespec_difference(&end, &start));
if (do_write_ret == 0) { if (do_write_ret == 0) {
//do_read(ftdi); //do_read(ftdi, buf2, size);
do_jump(ftdi); //do_jump(ftdi);
console(ftdi); //console(ftdi);
} else { } else {
return_code = EXIT_FAILURE; return_code = EXIT_FAILURE;
} }