import libaltosJNI.*;
-public class AltosConfigUI extends JDialog implements ActionListener, ItemListener, DocumentListener {
+public class AltosConfigUI
+ extends JDialog
+ implements ActionListener, ItemListener, DocumentListener
+{
Container pane;
Box box;
--- /dev/null
+/*
+ * Copyright © 2010 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+package altosui;
+
+import java.lang.*;
+import java.io.*;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.LinkedList;
+import java.util.Iterator;
+import altosui.AltosSerial;
+import altosui.AltosRomconfig;
+
+public class AltosDebug extends AltosSerial {
+
+ static final byte WR_CONFIG = 0x1d;
+ static final byte RD_CONFIG = 0x24;
+ static final byte CONFIG_TIMERS_OFF = (1 << 3);
+ static final byte CONFIG_DMA_PAUSE = (1 << 2);
+ static final byte CONFIG_TIMER_SUSPEND = (1 << 1);
+ static final byte SET_FLASH_INFO_PAGE = (1 << 0);
+
+ static final byte GET_PC = 0x28;
+ static final byte READ_STATUS = 0x34;
+ static final byte STATUS_CHIP_ERASE_DONE = (byte) (1 << 7);
+ static final byte STATUS_PCON_IDLE = (1 << 6);
+ static final byte STATUS_CPU_HALTED = (1 << 5);
+ static final byte STATUS_POWER_MODE_0 = (1 << 4);
+ static final byte STATUS_HALT_STATUS = (1 << 3);
+ static final byte STATUS_DEBUG_LOCKED = (1 << 2);
+ static final byte STATUS_OSCILLATOR_STABLE = (1 << 1);
+ static final byte STATUS_STACK_OVERFLOW = (1 << 0);
+
+ static final byte SET_HW_BRKPNT = 0x3b;
+ static byte HW_BRKPNT_N(byte n) { return (byte) ((n) << 3); }
+ static final byte HW_BRKPNT_N_MASK = (0x3 << 3);
+ static final byte HW_BRKPNT_ENABLE = (1 << 2);
+
+ static final byte HALT = 0x44;
+ static final byte RESUME = 0x4c;
+ static byte DEBUG_INSTR(byte n) { return (byte) (0x54|(n)); }
+ static final byte STEP_INSTR = 0x5c;
+ static byte STEP_REPLACE(byte n) { return (byte) (0x64|(n)); }
+ static final byte GET_CHIP_ID = 0x68;
+
+
+ static boolean ishex(int c) {
+ if ('0' <= c && c <= '9')
+ return true;
+ if ('a' <= c && c <= 'f')
+ return true;
+ if ('A' <= c && c <= 'F')
+ return true;
+ return false;
+ }
+
+ static boolean ishex(String s) {
+ for (int i = 0; i < s.length(); i++)
+ if (!ishex(s.charAt(i)))
+ return false;
+ return true;
+ }
+ static boolean isspace(int c) {
+ switch (c) {
+ case ' ':
+ case '\t':
+ return true;
+ }
+ return false;
+ }
+
+ static int fromhex(int c) {
+ if ('0' <= c && c <= '9')
+ return c - '0';
+ if ('a' <= c && c <= 'f')
+ return c - 'a' + 10;
+ if ('A' <= c && c <= 'F')
+ return c - 'A' + 10;
+ return -1;
+ }
+
+ boolean debug_mode;
+
+ void ensure_debug_mode() {
+ if (!debug_mode) {
+ printf("D\n");
+ debug_mode = true;
+ }
+ }
+
+ /*
+ * Write target memory
+ */
+ public void write_memory(int address, byte[] bytes) {
+ ensure_debug_mode();
+ printf("O %x %x\n", bytes.length, address);
+ for (int i = 0; i < bytes.length; i++)
+ printf("%02x", bytes[i]);
+ }
+
+ /*
+ * Read target memory
+ */
+ public byte[] read_memory(int address, int length)
+ throws IOException, InterruptedException {
+ byte[] data = new byte[length];
+
+ flush_reply();
+ ensure_debug_mode();
+ printf("I %x %x\n", length, address);
+ int i = 0;
+ while (i < length) {
+ String line = get_reply().trim();
+ if (!ishex(line) || line.length() % 2 != 0)
+ throw new IOException(
+ String.format
+ ("Invalid reply \"%s\"", line));
+ int this_time = line.length() / 2;
+ for (int j = 0; j < this_time; j++)
+ data[j] = (byte) ((fromhex(line.charAt(j*2)) << 4) +
+ fromhex(line.charAt(j*2+1)));
+ i += this_time;
+ }
+
+ return data;
+ }
+
+ /*
+ * Write raw bytes to the debug link using the 'P' command
+ */
+ public void write_bytes(byte[] bytes) throws IOException {
+ int i = 0;
+ ensure_debug_mode();
+ while (i < bytes.length) {
+ int this_time = bytes.length - i;
+ if (this_time > 8)
+ this_time = 0;
+ printf("P");
+ for (int j = 0; j < this_time; j++)
+ printf(" %02x", bytes[i+j]);
+ printf("\n");
+ i += this_time;
+ }
+ }
+
+ public void write_byte(byte b) throws IOException {
+ byte[] bytes = { b };
+ write_bytes(bytes);
+ }
+
+ /*
+ * Read raw bytes from the debug link using the 'G' command
+ */
+ public byte[] read_bytes(int length)
+ throws IOException, InterruptedException {
+
+ flush_reply();
+ ensure_debug_mode();
+ printf("G %x\n", length);
+ int i = 0;
+ byte[] data = new byte[length];
+ while (i < length) {
+ String line = get_reply().trim();
+ String tokens[] = line.split("\\s+");
+ for (int j = 0; j < tokens.length; j++) {
+ if (!ishex(tokens[j]) ||
+ tokens[j].length() != 2)
+ throw new IOException(
+ String.format
+ ("Invalid read_bytes reply \"%s\"", line));
+ try {
+ data[i + j] = (byte) Integer.parseInt(tokens[j], 16);
+ } catch (NumberFormatException ne) {
+ throw new IOException(
+ String.format
+ ("Invalid read_bytes reply \"%s\"", line));
+ }
+ }
+ i += tokens.length;
+ }
+ return data;
+ }
+
+ public byte read_status() throws IOException, InterruptedException {
+ write_byte(READ_STATUS);
+ return read_bytes(2)[0];
+ }
+
+ public boolean check_connection() throws IOException, InterruptedException {
+ byte reply = read_status();
+ System.out.printf("status %x\n", reply);
+ if ((reply & STATUS_CHIP_ERASE_DONE) == 0)
+ return false;
+ if ((reply & STATUS_PCON_IDLE) != 0)
+ return false;
+ if ((reply & STATUS_POWER_MODE_0) == 0)
+ return false;
+ return true;
+ }
+
+ public AltosRomconfig romconfig() {
+ try {
+ byte[] bytes = read_memory(0xa0, 10);
+ return new AltosRomconfig(bytes, 0);
+ } catch (IOException ie) {
+ } catch (InterruptedException ie) {
+ }
+ return new AltosRomconfig();
+ }
+
+ /*
+ * Reset target
+ */
+ public void reset() {
+ printf ("R\n");
+ }
+}
\ No newline at end of file
import altosui.AltosHexfile;
-public class AltosFlash implements Runnable {
+public class AltosFlash {
File file;
FileInputStream input;
Thread thread;
AltosHexfile image;
JFrame frame;
+ AltosDevice debug_dongle;
+ AltosDebug debug;
+ AltosRomconfig rom_config;
- public void run() {
- try {
- image = new AltosHexfile(input);
- System.out.printf("read file start %d length %d\n",
- image.address, image.data.length);
- } catch (IOException e) {
- JOptionPane.showMessageDialog(frame,
- file,
- e.getMessage(),
- JOptionPane.ERROR_MESSAGE);
- }
+ public void flash() throws IOException, FileNotFoundException, InterruptedException {
+ if (!check_rom_config())
+ throw new IOException("Invalid rom config settings");
+ rom_config.write(image);
}
- public AltosFlash(JFrame in_frame) {
- frame = in_frame;
+ public boolean check_rom_config() {
+ if (rom_config == null)
+ rom_config = debug.romconfig();
+ return rom_config != null && rom_config.valid();
+ }
- JFileChooser hexfile_chooser = new JFileChooser();
+ public void set_romconfig (AltosRomconfig romconfig) {
+ rom_config = romconfig;
+ }
- hexfile_chooser.setDialogTitle("Select Flash Image");
- hexfile_chooser.setFileFilter(new FileNameExtensionFilter("Flash Image", "ihx"));
- int returnVal = hexfile_chooser.showOpenDialog(frame);
+ public void open() throws IOException, FileNotFoundException, InterruptedException {
+ input = new FileInputStream(file);
+ image = new AltosHexfile(input);
+ debug.open(debug_dongle);
+ if (!debug.check_connection())
+ throw new IOException("Debug port not connected");
+ }
- if (returnVal == JFileChooser.APPROVE_OPTION) {
- file = hexfile_chooser.getSelectedFile();
- if (file != null) {
- try {
- input = new FileInputStream(file);
- thread = new Thread(this);
- thread.start();
- } catch (FileNotFoundException ee) {
- JOptionPane.showMessageDialog(frame,
- file,
- "Cannot open flash file",
- JOptionPane.ERROR_MESSAGE);
- }
- }
- }
+ public AltosFlash(File in_file, AltosDevice in_debug_dongle) {
+ file = in_file;
+ debug_dongle = in_debug_dongle;
+ debug = new AltosDebug();
}
}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright © 2010 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+package altosui;
+
+import java.awt.*;
+import java.awt.event.*;
+import javax.swing.*;
+import javax.swing.filechooser.FileNameExtensionFilter;
+import javax.swing.table.*;
+import java.io.*;
+import java.util.*;
+import java.text.*;
+import java.util.prefs.*;
+import java.util.concurrent.LinkedBlockingQueue;
+
+import altosui.AltosHexfile;
+import altosui.AltosFlash;
+
+public class AltosFlashUI implements Runnable {
+ File file;
+ Thread thread;
+ JFrame frame;
+ AltosDevice debug_dongle;
+ AltosFlash flash;
+
+ public void run() {
+ flash = new AltosFlash(file, debug_dongle);
+ try {
+ flash.open();
+ if (!flash.check_rom_config()) {
+ AltosRomconfigUI romconfig_ui = new AltosRomconfigUI (frame);
+ romconfig_ui.showDialog();
+ AltosRomconfig romconfig = romconfig_ui.romconfig();
+ if (romconfig == null)
+ return;
+ flash.set_romconfig(romconfig);
+ }
+ flash.flash();
+ } catch (FileNotFoundException ee) {
+ JOptionPane.showMessageDialog(frame,
+ "Cannot open image",
+ file.toString(),
+ JOptionPane.ERROR_MESSAGE);
+ return;
+ } catch (IOException e) {
+ JOptionPane.showMessageDialog(frame,
+ e.getMessage(),
+ file.toString(),
+ JOptionPane.ERROR_MESSAGE);
+ return;
+ } catch (InterruptedException ie) {
+ }
+ }
+
+ public AltosFlashUI(JFrame in_frame) {
+ frame = in_frame;
+
+ debug_dongle = AltosDeviceDialog.show(frame, AltosDevice.Any);
+
+ if (debug_dongle == null)
+ return;
+
+ JFileChooser hexfile_chooser = new JFileChooser();
+
+ hexfile_chooser.setDialogTitle("Select Flash Image");
+ hexfile_chooser.setFileFilter(new FileNameExtensionFilter("Flash Image", "ihx"));
+ int returnVal = hexfile_chooser.showOpenDialog(frame);
+
+ if (returnVal != JFileChooser.APPROVE_OPTION)
+ return;
+
+ file = hexfile_chooser.getSelectedFile();
+
+ thread = new Thread(this);
+ thread.start();
+ }
+}
\ No newline at end of file
public int address;
public byte[] data;
+ public byte get_byte(int a) {
+ return data[a - address];
+ }
+
public AltosHexfile(FileInputStream file) throws IOException {
HexFileInputStream input = new HexFileInputStream(file);
LinkedList<HexRecord> record_list = new LinkedList<HexRecord>();
data[records[i].address - base + j] = records[i].data[j];
}
}
+ for (int i = 0xa0; i < 0xaa; i++)
+ System.out.printf ("%04x: %02x\n", i, get_byte(i));
}
}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright © 2010 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+package altosui;
+import java.io.*;
+import altosui.AltosHexfile;
+
+public class AltosRomconfig {
+ public boolean valid;
+ public int version;
+ public int check;
+ public int serial_number;
+ public int radio_calibration;
+
+ static int get_int(byte[] bytes, int start, int len) {
+ int v = 0;
+ int o = 0;
+ while (len > 0) {
+ v = v | ((((int) bytes[start]) & 0xff) << o);
+ start++;
+ len--;
+ o += 8;
+ }
+ return v;
+ }
+
+ static void put_int(int value, byte[] bytes, int start, int len) {
+ while (len > 0) {
+ bytes[start] = (byte) (value & 0xff);
+ start++;
+ len--;
+ value >>= 8;
+ }
+ }
+
+ public AltosRomconfig(byte[] bytes, int offset) {
+ version = get_int(bytes, offset + 0, 2);
+ check = get_int(bytes, offset + 2, 2);
+ if (check == (~version & 0xffff)) {
+ switch (version) {
+ case 1:
+ serial_number = get_int(bytes, offset + 4, 2);
+ radio_calibration = get_int(bytes, offset + 6, 4);
+ valid = true;
+ break;
+ }
+ }
+ System.out.printf("version 0x%x check 0x%x valid %s serial %d cal %d\n",
+ version, check, valid ? "true" : "false",
+ serial_number, radio_calibration);
+ }
+
+ public AltosRomconfig(AltosHexfile hexfile) {
+ this(hexfile.data, 0xa0 - hexfile.address);
+ }
+
+ public void write(byte[] bytes, int offset) throws IOException {
+ if (!valid)
+ throw new IOException("rom configuration invalid");
+
+ if (offset < 0 || bytes.length < offset + 10)
+ throw new IOException("image cannot contain rom config");
+
+ AltosRomconfig existing = new AltosRomconfig(bytes, offset);
+ if (!existing.valid)
+ throw new IOException("image does not contain existing rom config");
+
+ switch (existing.version) {
+ case 1:
+ put_int(serial_number, bytes, offset + 4, 2);
+ put_int(radio_calibration, bytes, offset + 6, 4);
+ break;
+ }
+ }
+
+ public void write (AltosHexfile hexfile) throws IOException {
+ write(hexfile.data, 0xa0 - hexfile.address);
+ new AltosRomconfig(hexfile);
+ }
+
+ public AltosRomconfig(int in_serial_number, int in_radio_calibration) {
+ valid = true;
+ version = 1;
+ check = (~version & 0xffff);
+ serial_number = in_serial_number;
+ radio_calibration = in_radio_calibration;
+ }
+
+ public boolean valid() {
+ return valid && serial_number != 0;
+ }
+
+ public AltosRomconfig() {
+ valid = false;
+ }
+}
--- /dev/null
+/*
+ * Copyright © 2010 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+package altosui;
+
+import java.awt.*;
+import java.awt.event.*;
+import javax.swing.*;
+import javax.swing.filechooser.FileNameExtensionFilter;
+import javax.swing.table.*;
+import javax.swing.event.*;
+import java.io.*;
+import java.util.*;
+import java.text.*;
+import java.util.prefs.*;
+
+import altosui.AltosRomconfig;
+
+public class AltosRomconfigUI
+ extends JDialog
+ implements ActionListener
+{
+ Container pane;
+ Box box;
+ JLabel serial_label;
+ JLabel radio_calibration_label;
+
+ JFrame owner;
+ JTextField serial_value;
+ JTextField radio_calibration_value;
+
+ JButton ok;
+ JButton cancel;
+
+ /* Build the UI using a grid bag */
+ public AltosRomconfigUI(JFrame in_owner) {
+ super (in_owner, "Configure TeleMetrum Rom Values", true);
+
+ owner = in_owner;
+ GridBagConstraints c;
+
+ Insets il = new Insets(4,4,4,4);
+ Insets ir = new Insets(4,4,4,4);
+
+ pane = getContentPane();
+ pane.setLayout(new GridBagLayout());
+
+ /* Serial */
+ c = new GridBagConstraints();
+ c.gridx = 0; c.gridy = 0;
+ c.gridwidth = 3;
+ c.fill = GridBagConstraints.NONE;
+ c.anchor = GridBagConstraints.LINE_START;
+ c.insets = il;
+ serial_label = new JLabel("Serial:");
+ pane.add(serial_label, c);
+
+ c = new GridBagConstraints();
+ c.gridx = 3; c.gridy = 0;
+ c.gridwidth = 3;
+ c.fill = GridBagConstraints.HORIZONTAL;
+ c.weightx = 1;
+ c.anchor = GridBagConstraints.LINE_START;
+ c.insets = ir;
+ serial_value = new JTextField("0");
+ pane.add(serial_value, c);
+
+ /* Radio calibration value */
+ c = new GridBagConstraints();
+ c.gridx = 0; c.gridy = 1;
+ c.gridwidth = 3;
+ c.fill = GridBagConstraints.NONE;
+ c.anchor = GridBagConstraints.LINE_START;
+ c.insets = il;
+ c.ipady = 5;
+ radio_calibration_label = new JLabel("Radio Calibration:");
+ pane.add(radio_calibration_label, c);
+
+ c = new GridBagConstraints();
+ c.gridx = 3; c.gridy = 1;
+ c.gridwidth = 3;
+ c.fill = GridBagConstraints.HORIZONTAL;
+ c.weightx = 1;
+ c.anchor = GridBagConstraints.LINE_START;
+ c.insets = ir;
+ c.ipady = 5;
+ radio_calibration_value = new JTextField("1186611");
+ pane.add(radio_calibration_value, c);
+
+ /* Buttons */
+ c = new GridBagConstraints();
+ c.gridx = 0; c.gridy = 2;
+ c.gridwidth = 3;
+ c.fill = GridBagConstraints.NONE;
+ c.anchor = GridBagConstraints.CENTER;
+ c.insets = il;
+ ok = new JButton("OK");
+ pane.add(ok, c);
+ ok.addActionListener(this);
+ ok.setActionCommand("ok");
+
+ c = new GridBagConstraints();
+ c.gridx = 3; c.gridy = 2;
+ c.gridwidth = 3;
+ c.fill = GridBagConstraints.NONE;
+ c.anchor = GridBagConstraints.CENTER;
+ c.insets = il;
+ cancel = new JButton("Cancel");
+ pane.add(cancel, c);
+ cancel.addActionListener(this);
+ cancel.setActionCommand("cancel");
+
+ pack();
+ setLocationRelativeTo(owner);
+ }
+
+ boolean selected;
+
+ /* Listen for events from our buttons */
+ public void actionPerformed(ActionEvent e) {
+ String cmd = e.getActionCommand();
+
+ if (cmd.equals("ok"))
+ selected = true;
+ setVisible(false);
+ }
+
+ int serial() {
+ return Integer.parseInt(serial_value.getText());
+ }
+
+ int radio_calibration() {
+ return Integer.parseInt(radio_calibration_value.getText());
+ }
+
+ public AltosRomconfig romconfig() {
+ return new AltosRomconfig(serial(), radio_calibration());
+ }
+
+ public AltosRomconfig showDialog() {
+ setVisible(true);
+ if (selected)
+ return romconfig();
+ return null;
+ }
+}
}
}
+ public void flush_reply() {
+ reply_queue.clear();
+ }
+
public String get_reply() throws InterruptedException {
return reply_queue.take();
}
import altosui.AltosFlightStatusTableModel;
import altosui.AltosFlightInfoTableModel;
import altosui.AltosChannelMenu;
-import altosui.AltosFlash;
+import altosui.AltosFlashUI;
import libaltosJNI.*;
}
void FlashImage() {
- new AltosFlash(AltosUI.this);
+ new AltosFlashUI(AltosUI.this);
}
/*
AltosConfigUI.class \
AltosConvert.class \
AltosCSV.class \
+ AltosDebug.class \
AltosEepromDownload.class \
AltosEepromMonitor.class \
AltosEepromReader.class \
AltosEepromRecord.class \
AltosFile.class \
AltosFlash.class \
+ AltosFlashUI.class \
AltosFlightInfoTableModel.class \
AltosFlightStatusTableModel.class \
AltosGPS.class \
AltosUI.class \
AltosDevice.class \
AltosDeviceDialog.class \
+ AltosRomconfig.class \
+ AltosRomconfigUI.class \
AltosVoice.class
FREETTSSRC=/home/keithp/src/freetts/freetts-1.2.2