telegps: use new eeprom reading code
[fw/altos] / ao-bringup / test-flash
1 #!/usr/bin/nickle
2
3 import File;
4
5 string timed_read(file f, int timeout) {
6         thread reader = fork func() {
7                 try {
8                         return fgets(f);
9                 } catch Thread::signal(int i) {
10                         return "";
11                 }
12         }();
13
14         thread killer = fork func() {
15                 try {
16                         sleep (timeout);
17                         Thread::send_signal(reader, 1);
18                 } catch Thread::signal(int i) {
19                         return;
20                 }
21         }();
22
23         poly v = Thread::join(reader);
24         Thread::send_signal(killer, 1);
25         Thread::join(killer);
26         if (is_string(v))
27                 return v;
28         return "";
29 }
30
31 void flush_input(file f) {
32         for (;;) {
33                 string s = timed_read(f, 200);
34                 if (s == "")
35                         break;
36         }
37 }
38
39 string[*] flash(file f) {
40         string[...] x = {};
41
42         flush_input(f);
43         fprintf (f, "f\nv\n");
44         flush(f);
45         for (;;) {
46                 string l = timed_read(f, 1000);
47                 if (l == "") {
48                         File::fprintf(stderr, "Read timedout\n");
49                         exit(1);
50                 }
51                 x[dim(x)] = l;
52                 if (String::index(l, "software-version") == 0)
53                         break;
54         }
55         return x;
56 }
57
58 string[*] find_flash(string[*] s, string match) {
59         for (int i = 0; i < dim(s); i++)
60                 if (String::index(s[i], match) >= 0)
61                         return String::wordsplit(s[i], " ");
62         return (string[*]) {};
63 }
64
65 bool
66 do_flash(file f, int expected_size) {
67         string[*] i = flash(f);
68         string[*] size = find_flash(i, "Storage size:");
69         string[*] erase = find_flash(i, "Storage erase unit:");
70
71         int actual_size = string_to_integer(size[2]);
72
73         if (actual_size != expected_size) {
74                 printf ("weird flash size %d != %d\n", actual_size, expected_size);
75                 return false;
76         }
77
78         int actual_erase = string_to_integer(erase[3]);
79
80         if (actual_erase != 65536) {
81                 printf ("weird erase size %d\n", actual_erase);
82                 return false;
83         }
84
85         printf ("flash size %d erase block %d\n", actual_size, actual_erase);
86
87         return true;
88 }
89
90 void main () {
91         string  name = argv[1];
92         string  size = argv[2];
93         file    f = open(name, "r+");
94         bool ret = true;
95
96         if (!do_flash(f, string_to_integer(size)))
97                 ret = false;
98         exit (ret? 0 : 1);
99 }
100
101 main();