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

Subversion Repositories layer2

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

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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