2 * Copyright © 2011 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
18 package org.altusmetrum.AltosLib;
21 import java.util.concurrent.*;
23 public class AltosEepromChunk {
25 public static final int chunk_size = 256;
26 public static final int per_line = 8;
30 public ParseException parse_exception = null;
32 int[] ParseHex(String line) {
33 String[] tokens = line.split("\\s+");
34 int[] array = new int[tokens.length];
36 for (int i = 0; i < tokens.length; i++)
38 array[i] = Integer.parseInt(tokens[i], 16);
39 } catch (NumberFormatException ne) {
45 public int data(int offset) {
49 public int data16(int offset) {
50 return data[offset] | (data[offset + 1] << 8);
53 public int data32(int offset) {
54 return data[offset] | (data[offset + 1] << 8) |
55 (data[offset+2] << 16) | (data[offset+3] << 24);
58 public boolean erased(int start, int len) {
59 for (int i = 0; i < len; i++)
60 if (data[start+i] != 0xff)
65 public AltosEepromChunk(AltosLink link, int block, boolean flush)
66 throws TimeoutException, InterruptedException {
70 data = new int[chunk_size];
71 address = block * chunk_size;
74 link.printf("e %x\n", block);
76 for (offset = 0; offset < chunk_size; offset += per_line) {
78 String line = link.get_reply(5000);
81 throw new TimeoutException();
83 int[] values = ParseHex(line);
85 if (values == null || values.length != per_line + 1)
86 throw new ParseException(String.format("invalid line %s", line), 0);
87 if (values[0] != offset)
88 throw new ParseException(String.format("data address out of sync at 0x%x",
89 address + offset), 0);
90 for (int i = 0; i < per_line; i++)
91 data[offset + i] = values[1 + i];
92 } catch (ParseException pe) {
93 for (int i = 0; i < per_line; i++)
94 data[offset + i] = 0xff;
95 if (parse_exception == null)