Root/plasma/bootldr/bootldr.c

1/*--------------------------------------------------------------------
2 * TITLE: Plasma Bootloader
3 * AUTHOR: Steve Rhoads (rhoadss@yahoo.com)
4 * DATE CREATED: 12/17/05
5 * FILENAME: bootldr.c
6 * PROJECT: Plasma CPU core
7 * COPYRIGHT: Software placed into the public domain by the author.
8 * Software 'as is' without warranty. Author liable for nothing.
9 * DESCRIPTION:
10 * Plasma bootloader.
11 *--------------------------------------------------------------------*/
12#include "plasma.h"
13
14#define MemoryRead(A) (*(volatile unsigned long*)(A))
15#define MemoryWrite(A,V) *(volatile unsigned long*)(A)=(V)
16
17extern int putchar(int ch);
18extern int puts(const char *string);
19extern int getch(void);
20extern int kbhit(void);
21extern int DdrInit(void);
22
23typedef void (*FuncPtr)(void);
24typedef unsigned long uint32;
25typedef unsigned short uint16;
26
27
28void FlashRead(uint16 *dst, uint32 byteOffset, int bytes)
29{
30   volatile uint32 *ptr=(uint32*)(FLASH_BASE + (byteOffset << 1));
31   *ptr = 0xff; //read mode
32   while(bytes > 0)
33   {
34      *dst++ = (uint16)*ptr++;
35      bytes -= 2;
36   }
37}
38
39
40void FlashWrite(uint16 *src, uint32 byteOffset, int bytes)
41{
42   volatile uint32 *ptr=(uint32*)(FLASH_BASE + (byteOffset << 1));
43   while(bytes > 0)
44   {
45      *ptr = 0x40; //write mode
46      *ptr++ = *src++; //write data
47      while((*ptr & 0x80) == 0) //check status
48         ;
49      bytes -= 2;
50   }
51}
52
53
54void FlashErase(uint32 byteOffset)
55{
56   volatile uint32 *ptr=(uint32*)(FLASH_BASE + (byteOffset << 1));
57   *ptr = 0x20; //erase block
58   *ptr = 0xd0; //confirm
59   while((*ptr & 0x80) == 0) //check status
60      ;
61}
62
63
64char *xtoa(unsigned long num)
65{
66   static char buf[12];
67   int i, digit;
68   buf[8] = 0;
69   for (i = 7; i >= 0; --i)
70   {
71      digit = num & 0xf;
72      buf[i] = digit + (digit < 10 ? '0' : 'A' - 10);
73      num >>= 4;
74   }
75   return buf;
76}
77
78
79unsigned long getnum(void)
80{
81   int i;
82   unsigned long ch, ch2, value=0;
83   for(i = 0; i < 16; )
84   {
85      ch = ch2 = getch();
86      if(ch == '\n' || ch == '\r')
87         break;
88      if('0' <= ch && ch <= '9')
89         ch -= '0';
90      else if('A' <= ch && ch <= 'Z')
91         ch = ch - 'A' + 10;
92      else if('a' <= ch && ch <= 'z')
93         ch = ch - 'a' + 10;
94      else if(ch == 8)
95      {
96         if(i > 0)
97         {
98            --i;
99            putchar(ch);
100            putchar(' ');
101            putchar(ch);
102         }
103         value >>= 4;
104         continue;
105      }
106      putchar(ch2);
107      value = (value << 4) + ch;
108      ++i;
109   }
110   putchar('\r');
111   putchar('\n');
112   return value;
113}
114
115
116int main(void)
117{
118   int i, j, ch;
119   unsigned long address, value, count;
120   FuncPtr funcPtr;
121   unsigned char *ptr1;
122
123   DdrInit(); //Harmless if SDRAM instead of DDR
124
125   puts("\nSAKC bootloader ");
126   puts(":\n");
127   MemoryWrite(FLASH_BASE, 0xff); //read mode
128   if((MemoryRead(GPIOA_IN) & 1) && (MemoryRead(FLASH_BASE) & 0xffff) == 0x3c1c)
129   {
130      puts("Boot from flash\n");
131      FlashRead((uint16*)RAM_EXTERNAL_BASE, 0, 1024*128);
132      funcPtr = (FuncPtr)RAM_EXTERNAL_BASE;
133      funcPtr();
134   }
135   for(;;)
136   {
137      puts("\nWaiting for binary image linked at 0x10000000\n");
138      puts("Other Menu Options:\n");
139      puts("1. Memory read word\n");
140      puts("2. Memory write word\n");
141      puts("3. Memory read byte\n");
142      puts("4. Memory write byte\n");
143      puts("5. Jump to address\n");
144      puts("6. Raw memory read\n");
145      puts("7. Raw memory write\n");
146      puts("8. Checksum\n");
147      puts("9. Dump\n");
148      puts("F. Copy 128KB from DDR to flash\n");
149      puts("> ");
150      ch = getch();
151      address = 0;
152      if('0' <= ch && ch <= '9')
153      {
154         putchar(ch);
155         puts("\nAddress in hex> ");
156         address = getnum();
157         puts("Address = ");
158         puts(xtoa(address));
159         puts("\n");
160      }
161      switch(ch)
162      {
163      case '1':
164         value = MemoryRead(address);
165         puts(xtoa(value));
166         puts("\n");
167         break;
168      case '2':
169         puts("\nValue in hex> ");
170         value = getnum();
171         puts(xtoa(value));
172         MemoryWrite(address, value);
173         break;
174      case '3':
175         value = *(unsigned char*)address;
176         puts(xtoa(value));
177         puts("\n");
178         break;
179      case '4':
180         puts("\nValue in hex> ");
181         value = getnum();
182         puts(xtoa(value));
183         *(unsigned char*)address = value;
184         break;
185      case '5':
186         funcPtr = (FuncPtr)address;
187         funcPtr();
188         break;
189      case '6':
190         puts("\nCount in hex> ");
191         count = getnum();
192         for(i = 0; i < count; ++i)
193         {
194            ch = *(unsigned char*)(address + i);
195            putchar(ch);
196         }
197         break;
198      case '7':
199         puts("\nCount in hex> ");
200         count = getnum();
201         for(i = 0; i < count; ++i)
202         {
203            ch = getch();
204            *(unsigned char*)(address+i) = ch;
205         }
206         break;
207      case '8':
208         puts("\nCount in hex> ");
209         count = getnum();
210         value = 0;
211         for(i = 0; i < count; ++i)
212         {
213            value += *(unsigned char*)(address+i);
214         }
215         puts(xtoa(value));
216         putchar('\n');
217         break;
218      case '9':
219         puts("\nCount in hex> ");
220         count = getnum();
221         value = 0;
222         for(i = 0; i < count; i += 4)
223         {
224            if((i & 15) == 0)
225               puts("\r\n");
226            value = *(unsigned long*)(address+i);
227            puts(xtoa(value));
228            putchar(' ');
229         }
230         puts("\r\n");
231         break;
232      case 'F':
233         puts("\nConfirm with 12345678> ");
234         value = getnum();
235         if(value == 0x12345678)
236         {
237            FlashErase(0);
238            FlashWrite((uint16*)RAM_EXTERNAL_BASE, 0, 1024*128);
239         }
240         break;
241      case 0x3c: //raw test.bin file
242         ptr1 = (unsigned char*)0xE00;
243         for(i = 0; i < 1024*1024; ++i)
244         {
245            ptr1[i] = (unsigned char)ch;
246            for(j = 0; j < 10000; ++j)
247            {
248               if(kbhit())
249                  break;
250            }
251            if(j >= 10000)
252               break; //assume end of file
253            ch = getch();
254         }
255         funcPtr = (FuncPtr)0xE00;
256         funcPtr();
257         break;
258      }
259   }
260   return 0;
261}
262
263

Archive Download this file

Branches:
master



interactive