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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.altoslib_13;
22 import java.util.concurrent.*;
24 public class AltosEepromChunk {
26 public static final int chunk_size = 256;
27 public static final int per_line = 8;
31 public ParseException parse_exception = null;
33 int[] ParseHex(String line) {
34 String[] tokens = line.split("\\s+");
35 int[] array = new int[tokens.length];
37 for (int i = 0; i < tokens.length; i++)
39 array[i] = Integer.parseInt(tokens[i], 16);
40 } catch (NumberFormatException ne) {
46 public int data(int offset) {
50 public int data16(int offset) {
51 return data[offset] | (data[offset + 1] << 8);
54 public int data32(int offset) {
55 return data[offset] | (data[offset + 1] << 8) |
56 (data[offset+2] << 16) | (data[offset+3] << 24);
59 public boolean erased(int start, int len) {
60 for (int i = 0; i < len; i++)
61 if (data[start+i] != 0xff)
66 public boolean erased() {
67 return erased(0, chunk_size);
70 public AltosEepromChunk(AltosLink link, int block, boolean flush)
71 throws TimeoutException, InterruptedException {
75 data = new int[chunk_size];
76 address = block * chunk_size;
79 link.printf("e %x\n", block);
81 for (offset = 0; offset < chunk_size; offset += per_line) {
83 String line = link.get_reply(5000);
86 throw new TimeoutException();
88 int[] values = ParseHex(line);
90 if (values == null || values.length != per_line + 1)
91 throw new ParseException(String.format("invalid line %s", line), 0);
92 if (values[0] != offset)
93 throw new ParseException(String.format("data address out of sync at 0x%x",
94 address + offset), 0);
95 for (int i = 0; i < per_line; i++)
96 data[offset + i] = values[1 + i];
97 } catch (ParseException pe) {
98 for (int i = 0; i < per_line; i++)
99 data[offset + i] = 0xff;
100 if (parse_exception == null)
101 parse_exception = pe;