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

Subversion Repositories layer2

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

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
   uint cval;           //
39
 
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
 
118
   // Copy flash data to DDR2 memory.
119
   // NOTE: Missing bytes, if binary file is not 4 bytes aligned.
120
   for(uint i=0; i < (size / 4) /* + 1 */; i++) {
121
      DDR_ADDRESS[i] = flash_read(i);
122
   }
123
 
124
   // Go back to main menu.
125
   boot();
126
}
127
 
128
 
129
/******************************************************************************
130
 * Memory View                                                                *
131
 ******************************************************************************/
132
/* TODO: Cleaner generic version.
133
   Quick and dirty implementation of an memory matrix view. Shows the next
134
   'NUM_OF_WORDS' starting at location 'adr' of the Flash and the DDR memory
135
   device. */
136
void show_memory_contents(uint adr) {
137
 
138
   uchar b;
139
   uchar t;
140
 
141
   b = 0; t = 0;
142
   for(uint i=adr; i < adr + NUM_OF_WORDS; i++) {
143
 
144
      if(b == 0) {
145
         gotoxy(6, 4 + t++);
146
         printf("$y%x:$w ", FLASH_MEMORY + (i << 2));
147
      }
148
      printf("%x ", flash_read(i));
149
      if(b++ == 6) b = 0;
150
   }
151
 
152
   b = 0; t = 0;
153
   for(uint i=adr; i < adr + NUM_OF_WORDS; i++) {
154
 
155
      if(b == 0) {
156
         gotoxy(6, 20 + t++);
157
         printf("$y%x:$w ", DDR_ADDRESS + i);
158
      }
159
      printf("%x ", DDR_ADDRESS[i]);
160
      if(b++ == 6) b = 0;
161
   }
162
}
163
 
164
/* View the memory contents of the Flash and DDR devices. Navigate through the
165
   address space with ARROW UP and DOWN keys. Returns to the boot loader on
166
   ESC key pressed. */
167
void view_memories() {
168
 
169
   uint adr = 0;
170
 
171
   cls();
172
 
173
   drawWindow(&wFlashMemory);
174
   drawWindow(&wDDRMemory);
175
 
176
   // Show contetnts starting at address 0 at the beginning.
177
   show_memory_contents(0);
178
 
179
   while(TRUE) {
180
      switch(getc()->chr) {
181
         case KEY_ARROWD:
182
            adr += NUM_OF_WORDS;
183
            show_memory_contents(adr);
184
            break;
185
 
186
         case KEY_ARROWU:
187
            if(adr >= NUM_OF_WORDS) adr -= NUM_OF_WORDS;
188
            show_memory_contents(adr);
189
            break;
190
 
191
         case KEY_ESC:
192
            boot();
193
            break;
194
 
195
         default:
196
            break;
197
      }
198
   }
199
}
200
 
201
 
202
/******************************************************************************
203
 * Boot View                                                                  *
204
 ******************************************************************************/
205
/* Wait for completed flash initialization. Set up main menu box. */
206
int main() {
207
 
208
   // Clear screen.
209
   cls();
210
 
211
   // Wait for flash hardware initialization end.
212
   uchar s = flash_wait();
213
 
214
   // Flash not ready.
215
   if( !(s & FLASH_READY) ) {
216
      drawErrorWindow(&errFlashNotReady);
217
      return 0;
218
   }
219
 
220
   // Flash command error.
221
   if(s & FLASH_CMD_ERROR) {
222
      drawErrorWindow(&errFlashState);
223
      flash_clear_sr();
224
      //boot();
225
      return 0;
226
   }
227
 
228
   // User Main Menu.
229
   drawWindow(&wBoot);
230
 
231
   while(TRUE) {
232
      switch(getc()->chr) {
233
         case KEY_ARROWD:
234
            menuKeyDown(&wBoot, &menu);
235
            break;
236
 
237
         case KEY_ARROWU:
238
            menuKeyUp(&wBoot, &menu);
239
            break;
240
 
241
         case KEY_ENTER:
242
            switch(menu.index) {
243
               case OPTION_UPLOAD:
244
                  upload();
245
                  break;
246
 
247
               case OPTION_MEMORY:
248
                  view_memories();
249
                  break;
250
 
251
               case OPTION_START:
252
                  start();
253
                  break;
254
 
255
               default:
256
                  break;
257
            }
258
            break;
259
 
260
         default:
261
            break;
262
      }
263
   }
264
}

powered by: WebSVN 2.1.0

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