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

Subversion Repositories layer2

[/] [layer2/] [trunk/] [sw/] [void/] [main.c] - Blame information for rev 5

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 idiolatrie
/******************************************************************************
2 5 idiolatrie
 * void - Bootloader Version 0.2.2                                            *
3 2 idiolatrie
 ******************************************************************************
4
 * Copyright (C)2011  Mathias Hörtnagl <mathias.hoertnagl@gmail.com>          *
5
 *                                                                            *
6
 * This program is free software: you can redistribute it and/or modify       *
7
 * it under the terms of the GNU General Public License as published by       *
8
 * the Free Software Foundation, either version 3 of the License, or          *
9
 * (at your option) any later version.                                        *
10
 *                                                                            *
11
 * This program is distributed in the hope that it will be useful,            *
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
14
 * GNU General Public License for more details.                               *
15
 *                                                                            *
16
 * You should have received a copy of the GNU General Public License          *
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.      *
18
 ******************************************************************************/
19
#include "stdio.h"
20
#include "stdlib.h"
21
#include "flash.h"
22
#include "ui.h"
23
#include "view.h"
24
 
25
#define DDR_ADDRESS ((volatile uint *) 0x20000000)
26
 
27
/******************************************************************************
28
 * Upload View                                                                *
29
 ******************************************************************************/
30 5 idiolatrie
/* Wait until a flash write is completed and check for errors. */
31
void checkFlashWrite() {
32
 
33
   uchar state;         // Flash state.
34
 
35
   // Error checking.
36
   state = flash_wait();
37
   if(state & FLASH_BLOCK_LOCKED) {
38
      drawErrorWindow(&errErrorFlashLocked);
39
      return 0;
40
   }
41
   if(state & FLASH_PROGRAM_ERROR) {
42
      drawErrorWindow(&errErrorFlashWrite);
43
      return 0;
44
   }
45
}
46
 
47 2 idiolatrie
/* NOTE: Automatic deduction of the number of blocks, that need to be erased
48
         has not been tested extensive. */
49
void upload() {
50
 
51
   uint size;           // Image size.
52
   uint step;           // Progress bar step size.
53 4 idiolatrie
   uint cval;           // Current progress value.
54 2 idiolatrie
 
55
   // Clear screen.
56
   cls();
57
 
58
   // User Upload Menu.
59
   drawWindow(&wUpload);
60
 
61
   // Upload Initialization.
62
   drawMessage(&wUpload, &msgUploadWait);
63
   pbUpload.val = 0;
64
   drawProgressBar(&wUpload, &pbUpload);
65
 
66
   // Receive 4 bytes of size data.
67
   for(uchar i=0; i < 4; i++) {
68
      size <<= 8;
69
      size += rs232_receive();
70
   }
71
 
72
   // Check for image size to fit into flash.
73
   if(size > FLASH_BLOCK_SIZE * FLASH_BLOCKS) {
74
      drawErrorWindow(&errErrorFlashSize);
75
      return 0;
76
   }
77
 
78
   // Flash Clean Up.
79
   drawMessage(&wUpload, &msgUploadErase);
80
   pbUpload.val = 0;                         // Reset progress bar.
81
   drawProgressBar(&wUpload, &pbUpload);
82
 
83
   // Erase affected flash blocks.
84
   for(uint i=0; i < (size / FLASH_BLOCK_SIZE) + 1; i++) {
85
      flash_block_erase(i * FLASH_BLOCK_SIZE);
86
 
87
      // Update the Progress Bar.
88
      pbUpload.val = (i >> 1);
89
      drawProgressBar(&wUpload, &pbUpload);
90
 
91
      // Check for errors while erasing.
92
      if(flash_wait() & FLASH_ERASE_ERROR) {
93
         drawErrorWindow(&errErrorFlashErase);
94
         return 0;
95
      }
96
   }
97 5 idiolatrie
 
98
   // Write image size at flash address 0x0.
99
   for(uchar i=0; i<4; i++) {
100
      flash_write(i, size >> ((3-i) * 8) );
101
      checkFlashWrite();
102
   }
103 2 idiolatrie
 
104
   // Upload data.
105
   drawMessage(&wUpload, &msgUploadWrite);
106
   pbUpload.val = 0;                         // Reset progress bar.
107
   step = size / 64;                         // Calculate progress step size.
108
   cval = step;
109
 
110 5 idiolatrie
   // Echoing received image size.
111
   for(uchar i=0; i<4; i++) {
112
      rs232_transmit( size >> ((3-i) * 8) );
113
   }
114
 
115 2 idiolatrie
   // Write each single byte to Flash.
116
   for(uint i=0; i < size; i++) {
117 5 idiolatrie
      flash_write(i + 4, rs232_receive());
118 2 idiolatrie
 
119
      // Update status bar.
120
      if(i == cval) {
121
         pbUpload.val++;
122
         drawProgressBar(&wUpload, &pbUpload);
123
         cval += step;
124
      }
125
 
126 5 idiolatrie
      checkFlashWrite();
127 2 idiolatrie
   }
128
 
129
   // Go back to main menu.
130
   boot();
131
}
132
 
133
 
134
/******************************************************************************
135 4 idiolatrie
 * DDR Load View                                                              *
136
 ******************************************************************************/
137 5 idiolatrie
/* Load Flash contents into DDR.
138
 
139
   Input:
140
      start    Image start address on flash.
141
      size     Image size.
142
 */
143
void load(uint start, uint size) {
144 4 idiolatrie
 
145
   uint step;           // Progress bar step size.
146
   uint cval;           // Current progress value.
147
 
148
   cls();
149
 
150
   // User Upload Menu.
151
   drawWindow(&wDDRUpload);
152
 
153
   // Upload Initialization.
154
   pbUpload.val = 0;
155
   drawProgressBar(&wDDRUpload, &pbUpload);
156
 
157 5 idiolatrie
   step = size / 64;
158 4 idiolatrie
   cval = step;
159
 
160
   // Copy flash data to DDR2 memory.
161 5 idiolatrie
   // NOTE: Missing bytes, if binary file is not 4 bytes aligned.
162
   for(uint i=0; i < (size / 4); i++) {
163 4 idiolatrie
 
164 5 idiolatrie
      DDR_ADDRESS[i] = flash_read(i + start);
165 4 idiolatrie
 
166
      // Update status bar.
167
      if(i == cval) {
168
         pbUpload.val++;
169
         drawProgressBar(&wUpload, &pbUpload);
170
         cval += step;
171
      }
172
   }
173
}
174
 
175
 
176
/******************************************************************************
177 2 idiolatrie
 * Memory View                                                                *
178
 ******************************************************************************/
179 5 idiolatrie
#define NUM_OF_WORDS 77
180 2 idiolatrie
/* TODO: Cleaner generic version.
181
   Quick and dirty implementation of an memory matrix view. Shows the next
182
   'NUM_OF_WORDS' starting at location 'adr' of the Flash and the DDR memory
183
   device. */
184
void show_memory_contents(uint adr) {
185
 
186
   uchar b;
187
   uchar t;
188
 
189
   b = 0; t = 0;
190
   for(uint i=adr; i < adr + NUM_OF_WORDS; i++) {
191
 
192
      if(b == 0) {
193
         gotoxy(6, 4 + t++);
194
         printf("$y%x:$w ", FLASH_MEMORY + (i << 2));
195
      }
196
      printf("%x ", flash_read(i));
197
      if(b++ == 6) b = 0;
198
   }
199
 
200
   b = 0; t = 0;
201
   for(uint i=adr; i < adr + NUM_OF_WORDS; i++) {
202
 
203
      if(b == 0) {
204
         gotoxy(6, 20 + t++);
205
         printf("$y%x:$w ", DDR_ADDRESS + i);
206
      }
207
      printf("%x ", DDR_ADDRESS[i]);
208
      if(b++ == 6) b = 0;
209
   }
210
}
211
 
212
/* View the memory contents of the Flash and DDR devices. Navigate through the
213
   address space with ARROW UP and DOWN keys. Returns to the boot loader on
214
   ESC key pressed. */
215
void view_memories() {
216
 
217
   uint adr = 0;
218
 
219
   cls();
220
 
221
   drawWindow(&wFlashMemory);
222
   drawWindow(&wDDRMemory);
223
 
224
   // Show contetnts starting at address 0 at the beginning.
225
   show_memory_contents(0);
226
 
227
   while(TRUE) {
228
      switch(getc()->chr) {
229
         case KEY_ARROWD:
230
            adr += NUM_OF_WORDS;
231
            show_memory_contents(adr);
232
            break;
233
 
234
         case KEY_ARROWU:
235
            if(adr >= NUM_OF_WORDS) adr -= NUM_OF_WORDS;
236
            show_memory_contents(adr);
237
            break;
238
 
239
         case KEY_ESC:
240
            boot();
241
            break;
242
 
243
         default:
244
            break;
245
      }
246
   }
247
}
248
 
249
 
250
/******************************************************************************
251
 * Boot View                                                                  *
252
 ******************************************************************************/
253
/* Wait for completed flash initialization. Set up main menu box. */
254
int main() {
255 5 idiolatrie
 
256
   uchar s;
257
 
258 2 idiolatrie
   // Clear screen.
259
   cls();
260
 
261
   // Wait for flash hardware initialization end.
262 5 idiolatrie
   s = flash_wait();
263 2 idiolatrie
 
264
   // Flash not ready.
265
   if( !(s & FLASH_READY) ) {
266
      drawErrorWindow(&errFlashNotReady);
267
      return 0;
268
   }
269
 
270
   // Flash command error.
271
   if(s & FLASH_CMD_ERROR) {
272
      drawErrorWindow(&errFlashState);
273
      flash_clear_sr();
274
      //boot();
275
      return 0;
276
   }
277
 
278
   // User Main Menu.
279
   drawWindow(&wBoot);
280
 
281
   while(TRUE) {
282
      switch(getc()->chr) {
283
         case KEY_ARROWD:
284
            menuKeyDown(&wBoot, &menu);
285
            break;
286
 
287
         case KEY_ARROWU:
288
            menuKeyUp(&wBoot, &menu);
289
            break;
290
 
291
         case KEY_ENTER:
292
            switch(menu.index) {
293
               case OPTION_UPLOAD:
294
                  upload();
295
                  break;
296
 
297
               case OPTION_MEMORY:
298
                  view_memories();
299
                  break;
300
 
301
               case OPTION_START:
302 5 idiolatrie
                  load(1, flash_read(0));
303 2 idiolatrie
                  start();
304
                  break;
305
 
306
               default:
307
                  break;
308
            }
309
            break;
310
 
311
         default:
312
            break;
313
      }
314
   }
315
}

powered by: WebSVN 2.1.0

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