-
Notifications
You must be signed in to change notification settings - Fork 2
/
ca0132-8051-write-exram-from-file.c
80 lines (65 loc) · 1.75 KB
/
ca0132-8051-write-exram-from-file.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* ca0132-8051-write-exram-from-file:
* Writes a range of 8051 exram data from a file.
*/
#include "ca0132_defs.h"
static void usage(char *pname)
{
fprintf(stderr, "usage: %s <hwdep-device> <start-addr> <file-to-read>\n", pname);
}
static uint32_t get_file_size(FILE *file)
{
uint32_t tmp;
fseek(file, 0, SEEK_END);
tmp = ftell(file);
rewind(file);
return tmp;
}
int main(int argc, char **argv)
{
uint32_t data_size, start_addr, i;
FILE *data_file;
uint8_t *buf;
int fd, ret;
if (argc < 3) {
usage(argv[0]);
return 1;
}
ret = open_hwdep(argv[1], &fd);
if (ret)
return ret;
data_file = fopen(argv[3], "r");
if (!data_file) {
fprintf(stderr, "Failed to open file to read from.\n");
return 1;
}
/* Get size of the file, and the starting address to write it to. */
start_addr = strtol(argv[2], NULL, 16);
data_size = get_file_size(data_file);
if ((data_size + start_addr) > 0xffff) {
printf("File data too big to fit into exram!\n");
fclose(data_file);
close(fd);
return 1;
}
/* Allocate a buffer to read the file into, double it for readback. */
buf = calloc(data_size * 2, sizeof(uint8_t));
if (fread(buf, sizeof(uint8_t), data_size, data_file) != data_size) {
printf("Failed to read file to write.\n");
goto exit;
}
chipio_8051_write_exram_data_range(fd, start_addr, data_size, buf);
chipio_8051_read_exram_data_range(fd, start_addr, data_size, &buf[data_size]);
/* Confirm that the file was written. */
for (i = 0; i < data_size; i++) {
if (buf[i] != buf[data_size + i]) {
printf("Addr 0x%04x: expected 0x%02x, got 0x%02x.\n",
start_addr + i, buf[i], buf[data_size + i]);
}
}
exit:
free(buf);
fclose(data_file);
close(fd);
return 0;
}