OpenCores
URL https://opencores.org/ocsvn/mips32r1/mips32r1/trunk

Subversion Repositories mips32r1

[/] [mips32r1/] [trunk/] [Software/] [xum_bootloader/] [programmer/] [win32_source/] [XumBootloader_GUI/] [Form1.cs] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ayersg
/* Implements the XUM bootloader protocol.
2
 *
3
 * Designed by Grant Ayers (ayers@cs.utah.edu) for
4
 * XUM project of the Gauss group at the University of Utah.
5
 *
6
 * You may reuse this code with proper attribution.
7
 *
8
 * Summer 2010
9
*/
10
using System;
11
using System.Collections.Generic;
12
using System.ComponentModel;
13
using System.Data;
14
using System.Drawing;
15
using System.Linq;
16
using System.Text;
17
using System.Windows.Forms;
18
using System.IO;
19
using System.Security.Permissions;
20
using System.IO.Ports;
21
using System.Diagnostics;
22
 
23
 
24
namespace XumBootloader_GUI
25
{
26
    public partial class Form1 : Form
27
    {
28
        public Form1()
29
        {
30
            InitializeComponent();
31
 
32
            /* Initialize some form components */
33
            foreach (string str in Program.getComPortNames())
34
            {
35
                try
36
                {
37
                    serialPortSelect.Items.Add(str);
38
                }
39
                catch (System.ArgumentNullException ex)
40
                {
41
                    MessageBox.Show(this, ex.Message + "\nYour system doesn't have any serial ports!");
42
                    Environment.Exit(0);
43
                }
44
            }
45
            try
46
            {
47
                serialPortSelect.SelectedIndex = 0;
48
                baudRateSelect.SelectedItem = "115200";
49
                serialPort1.Parity = System.IO.Ports.Parity.None;
50
                serialPort1.DataBits = 8;
51
                serialPort1.StopBits = System.IO.Ports.StopBits.One;
52
                serialPort1.Handshake = System.IO.Ports.Handshake.None;
53
                //serialPort1.WriteBufferSize = 1048576;  // 1MB
54
                //serialPort1.ReadBufferSize = 1048576;   // 1MB
55
                serialPort1.ReadTimeout = 1400; // 1400ms more than enough for 1KB even at 9600 baud.
56
                serialPort1.WriteTimeout = 1400;
57
                labelSuccess.Text = "";
58
                labelSize.Text = "";
59
                progressBar1.Visible = false;
60
            }
61
            catch (Exception ex)
62
            {
63
                MessageBox.Show(this, ex.Message);
64
                Environment.Exit(1);
65
            }
66
 
67
        }
68
 
69
        private void selectFile_Click(object sender, EventArgs e)
70
        {
71
            labelSuccess.Text = "";
72
            progressBar1.Visible = false;
73
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
74
            {
75
                string filename = openFileDialog1.FileName;
76
                labelFilename.Text = "Selected: " + filename;
77
                try
78
                {
79
                    System.IO.FileInfo fi = new System.IO.FileInfo(filename);
80
                    long filesize = fi.Length;
81
                    if (filesize > 1048576) // currently just 1MB
82
                    {
83
                        MessageBox.Show(this, "Maxiumum supported file size is 1MB");
84
                        labelFilename.Text = "";
85
                        labelSize.Text = "";
86
                        openFileDialog1.FileName = "";
87
                        return;
88
                    }
89
                    labelSize.Text = "Size: " + filesize + " bytes.";
90
                    if ((filesize % 4) != 0)
91
                        labelSize.Text = labelSize.Text + " (Not aligned to 32 bits!)";
92
                }
93
                catch (Exception ex)
94
                {
95
                    MessageBox.Show(this, ex.Message);
96
                    return;
97
                }
98
            }
99
            if (openFileDialog1.FileName == "")
100
            {
101
                labelFilename.Text = "No File Selected.";
102
                labelSize.Text = "";
103
            }
104
        }
105
 
106
        private void sendButton_Click(object sender, EventArgs e)
107
        {
108
            try
109
            {
110
                labelSuccess.Text = "";
111
                if (openFileDialog1.FileName == "")
112
                {
113
                    MessageBox.Show(this, "You must first select a file.");
114
                    return;
115
                }
116
                object portName = serialPortSelect.SelectedItem;
117
                if (portName == null)
118
                {
119
                    MessageBox.Show(this, "You must first select a serial port.");
120
                    return;
121
                }
122
                serialPort1.PortName = portName.ToString();
123
                object baudRate = baudRateSelect.SelectedItem;
124
                if (baudRate == null)
125
                {
126
                    MessageBox.Show(this, "You must first select the baud rate.");
127
                    return;
128
                }
129
                serialPort1.BaudRate = Convert.ToInt32(baudRate.ToString());
130
                if (serialPort1.IsOpen) // this doesn't work...
131
                {
132
                    MessageBox.Show(this, "Serial port " + portName + " could not be opened and may be in use");
133
                    return;
134
                }
135
                string filename = openFileDialog1.FileName;
136
                int fileSizeBytes = 0;
137
                System.IO.FileInfo fi = new System.IO.FileInfo(filename);
138
                fileSizeBytes = (int)fi.Length; // already verified <= 1MB
139
                labelSize.Text = "Size: " + fileSizeBytes + " bytes.";
140
                int offsetWords;
141
                string offsetStr = setOffset.Text;
142
                if (offsetStr == "")
143
                {
144
                    MessageBox.Show(this, "You must enter an offset between 0 and 262143");
145
                    return;
146
                }
147
                try
148
                {
149
                    offsetWords = Convert.ToInt32(offsetStr, 10);
150
                }
151
                catch (Exception)
152
                {
153
                    MessageBox.Show(this, "\"" + offsetStr + "\" is not a valid number between 0 and 262143");
154
                    return;
155
                }
156
                if ((offsetWords < 0) || (offsetWords > (1048576 - fileSizeBytes)))
157
                {
158
                    MessageBox.Show(this, "Offset must be between 0 and (262144 - number of words).");
159
                    return;
160
                }
161
                sendButton.Enabled = false;
162
                serialPort1.Open();
163
                /* Send the data */
164
                fileSizeBytes--;
165
                UInt32 size_head = (UInt32)((fileSizeBytes / 4));
166
                UInt32 offs_head = (UInt32)offsetWords;
167
                byte size1 = (byte)((size_head << 14) >> 30);
168
                byte size2 = (byte)((size_head << 16) >> 24);
169
                byte size3 = (byte)((size_head << 24) >> 24);
170
                byte offs1 = (byte)((offs_head << 14) >> 30);
171
                byte offs2 = (byte)((offs_head << 16) >> 24);
172
                byte offs3 = (byte)((offs_head << 24) >> 24);
173
                byte[] header = { 0x58, 0x55, 0x4d, size1, size2, size3, offs1, offs2, offs3 }; // 'X''U''M' followed by size, offset.
174
                serialPort1.Write(header, 0, header.Length);
175
                fileSizeBytes++;
176
 
177
                /* Check that the copy of size3 came back */
178
                serialPort1.ReadTimeout = 500;  // 500 ms to respond is more than enough.
179
                byte[] response = new byte[1];
180
                try
181
                {
182
                    serialPort1.Read(response, 0, 1);
183
                }
184
                catch (System.TimeoutException)
185
                {
186
                    MessageBox.Show(this, "The device did not respond.\n\nIt may be off, disconnected, set at a different baud rate," +
187
                        "\nor not listening for the XUM boot protocol.");
188
                    serialPort1.Close();
189
                    sendButton.Enabled = true;
190
                    return;
191
                }
192
                if (response[0] != size3)
193
                {
194
                    MessageBox.Show(this, "An unexpected response was received from the device.\n" +
195
                        "(Expected " + size3 + ", Received " + response[0] + ")\n\n" +
196
                        "Make sure it is configured for the XUM boot protocol.");
197
                    serialPort1.Close();
198
                    sendButton.Enabled = true;
199
                    return;
200
                }
201
 
202
                /* Send and verify the data */
203
                progressBar1.Visible = true;
204
                progressBar1.Minimum = 0;
205
                progressBar1.Maximum = fileSizeBytes;
206
                progressBar1.Value = 0;
207
                byte[] fileData = new byte[fileSizeBytes];
208
                byte[] echoData = new byte[1024];
209
                System.IO.BinaryReader file = new System.IO.BinaryReader(File.Open(filename, FileMode.Open));
210
                file.Read(fileData, 0, fileSizeBytes);
211
                file.Close();
212
                int sentByteCount = 0;
213
                int currentSendSize = 0;
214
                Stopwatch stopwatch = new Stopwatch();
215
                stopwatch.Start();
216
                while (sentByteCount < fileSizeBytes)
217
                {
218
                    if ((fileSizeBytes - sentByteCount) >= 1024)
219
                        currentSendSize = 1024;
220
                    else
221
                        currentSendSize = (fileSizeBytes - sentByteCount);
222
 
223
                    serialPort1.Write(fileData, sentByteCount, currentSendSize);
224
                    long waitStart = stopwatch.ElapsedMilliseconds;
225
                    while (serialPort1.BytesToRead < currentSendSize) {
226
                        if ((stopwatch.ElapsedMilliseconds - waitStart) > 4000)
227
                        {
228
                            MessageBox.Show(this, "The device stopped responding. Giving up." + "\nDevice Ack'd : " + serialPort1.BytesToRead + " of " + currentSendSize);
229
                            serialPort1.Close();
230
                            sendButton.Enabled = true;
231
                            progressBar1.Value = 0;
232
                            progressBar1.Visible = false;
233
                            return;
234
                        }
235
                    }
236
                    if (serialPort1.BytesToRead > currentSendSize)
237
                    {
238
                        MessageBox.Show(this, "The device sent back an extra " + (serialPort1.BytesToRead - currentSendSize) + " byte(s), which was unexpected.");
239
                    }
240
                    serialPort1.Read(echoData, 0, currentSendSize);
241
                    for (int i = 0; i < currentSendSize; i++)
242
                    {
243
                        if (fileData[sentByteCount + i] != echoData[i])
244
                        {
245
                            MessageBox.Show(this, "The device sent back non-matching data. There was a transmission error." +
246
                            "\n\n" + (sentByteCount + i) + " bytes were transmitted successfully.");
247
                            serialPort1.Close();
248
                            sendButton.Enabled = true;
249
                            return;
250
                        }
251
                    }
252
                    sentByteCount += currentSendSize;
253
                    progressBar1.Value = sentByteCount;
254
                }
255
                stopwatch.Stop();
256
                labelSuccess.Text = "Success (" + (stopwatch.ElapsedMilliseconds / 1000.0) + " seconds).";
257
                serialPort1.Close();
258
                sendButton.Enabled = true;
259
            }
260
            catch (Exception ex)
261
            {
262
                MessageBox.Show(this, ex.Message);
263
                if (serialPort1.IsOpen)
264
                    serialPort1.Close();
265
                sendButton.Enabled = true;
266
                progressBar1.Value = 0;
267
                return;
268
            }
269
 
270
        }
271
 
272
 
273
    }
274
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.