OSDN Git Service

9476d8bc4f4a07ed1cda0f4d7d8a1ed25983321a
[nxt-jsp/lejos_nxj.git] / nxtOSEK / lejos_nxj / src / nxtvm / platform / nxt / main.c
1
2 #include "mytypes.h"
3 #include "interrupts.h"
4 #include "aic.h"
5 #include "AT91SAM7.h"
6 #include "uart.h"
7 #include "systick.h"
8 #include "stdio.h"
9 #include "flashprog.h"
10 #include "nxt_avr.h"
11 #include "twi.h"
12 #include "types.h"
13 #include "constants.h"
14 #include "classes.h"
15 #include "threads.h"
16 #include "stack.h"
17 #include "specialclasses.h"
18 #include "specialsignatures.h"
19 #include "language.h"
20 #include "memory.h"
21 #include "interpreter.h"
22 #include "exceptions.h"
23 #include "trace.h"
24 #include "poll.h"
25 #include "sensors.h"
26 #include "platform_hooks.h"
27 //#include "java_binary.h"
28
29 #include "nxt_avr.h"
30 #include "nxt_lcd.h"
31 #include "i2c.h"
32 #include "nxt_motors.h"
33
34 #include "lejos_nxt.h"
35
36 #include "display.h"
37 #include "sound.h"
38 #include "bt.h"
39 #include "udp.h"
40 #include "flashprog.h"
41
42 #include <string.h>
43
44 extern U32 __free_ram_start__;
45 extern U32 __free_ram_end__;
46 extern U32 __extra_ram_start__;
47 extern U32 __extra_ram_end__;
48
49
50 byte *region;
51 Thread *bootThread;
52
53 void
54 wait_for_power_down_signal()
55 {
56   for (;;) {
57     int b = buttons_get();
58
59     // Check for ENTER and ESCAPE pressed
60     if (b == 9) {
61       // Shut down power immediately
62       while (1) {
63         nxt_avr_power_down();
64       }
65     }
66   }
67 }
68
69 void
70 handle_uncaught_exception(Object * exception,
71                           const Thread * thread,
72                           const MethodRecord * methodRecord,
73                           const MethodRecord * rootMethod, byte * pc)
74 {
75   display_clear(0);
76   display_goto_xy(0, 0);
77   display_string("Java Exception:");
78   display_goto_xy(0, 1);
79   display_string("Class:");
80   display_goto_xy(7, 1);
81   display_int(get_class_index(exception), 0);
82   display_goto_xy(0, 2);
83   display_string("Method:");
84   display_goto_xy(8, 2);
85   display_int(methodRecord->signatureId, 0);
86   display_update();
87   wait_for_power_down_signal();
88 }
89
90 void
91 switch_thread_hook()
92 {
93   int b = buttons_get();
94
95   // Check for ENTER and ESCAPE pressed
96   if (b == 9) {
97     // Shut down power immediately
98     while (1) {
99       nxt_avr_power_down();
100     }
101   }
102 }
103
104 void
105 assert_hook(boolean aCond, int aCode)
106 {
107   if (aCond) return;
108   display_clear(0);
109   display_goto_xy(0,0);
110   display_string("Assert failed");
111   display_goto_xy(0,1);
112   display_string("Code: ");
113   display_goto_xy(6,1);
114   display_int(aCode,0);
115   display_update();
116   while(1); // Hang
117 }
118
119
120 void
121 run(int jsize)
122 {
123   init_poller();
124
125   //printf("Initializing Binary\n");
126
127   // Initialize binary image state
128   initialize_binary();
129
130   //printf("Initializing memory\n");
131
132   // Initialize memory
133   {
134     byte *ram_end = (byte *) (&__free_ram_end__);
135     byte *ram_start = (byte *) (&__free_ram_start__);
136     int size, i;
137
138     // Skip java binary if it is an top of ram
139
140     if (jsize > 0)
141       ram_end -= (jsize + 4);
142     size = ((unsigned) ram_end) - ((unsigned) ram_start);
143
144     memory_init();
145
146     region = ram_start;
147     memory_add_region(region, (byte *) ram_end);
148
149     /*Add extra RAM if available */
150     ram_end = (byte *) (&__extra_ram_end__);
151     ram_start = (byte *) (&__extra_ram_start__);
152     size = ((unsigned) ram_end) - ((unsigned) ram_start);
153
154     //if(size > 0)
155     //  memory_add_region(ram_start, ram_end);
156   }
157
158   //printf("Initializing exceptions\n");
159
160   // Initialize exceptions
161   init_exceptions();
162
163   // Create the boot thread (bootThread is a special global)
164   bootThread = (Thread *) new_object_for_class(JAVA_LANG_THREAD);
165
166   init_threads();
167   if (!init_thread(bootThread)) {
168     return;
169   }
170   //printf("Executing Interpreter\n");
171
172   // Execute the bytecode interpreter
173   set_program_number(0);
174
175   engine();
176   // Engine returns when all non-daemon threads are dead
177 }
178
179 /***************************************************************************
180  * int nxt_main *--------------------------------------------------------------------------
181  ***************************************************************************/
182 //int main (int argc, char *argv[])
183 int
184 nxt_main(int bin, int size)
185 {
186   int jsize = 0;
187   const char *binary; 
188   unsigned *temp;
189
190   if (bin > 0) {
191     size = (size + 3) & ~3;
192         temp = ((unsigned *) (&__free_ram_end__)) - (size >> 2);
193         memcpy(temp,bin,size);
194         binary = (char *) temp;
195         jsize = size - 4;
196   } else {
197     // Execute flash menu
198
199     bin = (unsigned *) 0x00108000;
200     size = *((unsigned *) 0x0010fffc);
201     size = (size + 3) & ~3;
202     temp = ((unsigned *) (&__free_ram_end__)) - (size >> 2);   
203     memcpy(temp,bin,size);
204     binary = ((char *) temp);
205     jsize = size - 4;
206   }
207
208   init_sensors();
209
210   //       printf("Installing Binary\n");
211
212   install_binary(binary);
213
214   //      printf("Running\n");
215
216   run(jsize);
217
218   return 0;
219 }
220
221 const U8 splash_data[4 * 26] = {
222   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
223   0x00, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFC,
224   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA0, 0x40, 0xA0, 0x40,
225   0xA0, 0x40, 0xA0, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
226   0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0x00, 0x00, 0x00, 0x0A, 0x05, 0x0A, 0x05,
227   0x0A, 0x05, 0x0A, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
228   0x0F, 0x1F, 0x3F, 0x3F, 0x7F, 0x7F, 0xFC, 0xF8, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
229   0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF8, 0xFC, 0x7F, 0x7F, 0x3F, 0x3F, 0x1F, 0x0F,
230 };
231
232 void
233 show_splash(U32 milliseconds)
234 {
235   display_clear(0);
236   display_bitmap_copy(splash_data, 26, 4, 37, 1);
237
238   display_goto_xy(6, 6);
239   display_string("LEJOS");
240   display_update();
241
242   systick_wait_ms(milliseconds);
243 }
244
245
246
247 U32
248 free_stack(void)
249 {
250   extern U32 __system_stack_bottom__;
251   extern U32 __system_stack_top__;
252   U32 *sp = &__system_stack_bottom__;
253   U32 space = 0;
254
255   while ((sp < &__system_stack_top__) && *sp == 0x6b617453) {
256     sp++;
257     space += 4;
258   }
259   return space;
260 }
261
262 void
263 xx_show(void)
264 {
265   int iterator = 0;
266   U32 buttons;
267   U32 motor_mode = 0;
268   int result;
269   U8 distance;
270
271   show_splash(3000);
272
273   /* set up power for ultrasonic sensor on port 1 (port 0 here )*/  
274   nxt_avr_set_input_power(0,1);
275   i2c_enable(0);
276                         
277   while (1) {
278     display_clear(0);
279
280     if ((iterator & 15) == 0) {
281       motor_mode = (iterator >> 4) & 3;
282       switch (motor_mode) {
283
284       case 0:
285         nxt_motor_set_speed(2, -100, 0);
286         break;
287       case 1:
288         nxt_motor_set_speed(2, 100, 0);
289         break;
290       case 2:
291         nxt_motor_set_speed(2, 0, 0);
292         break;
293       case 3:
294         nxt_motor_set_speed(2, 0, 1);
295         break;
296       }
297     }
298     iterator++;
299
300     if ((iterator %10) < 5) {
301       buttons = buttons_get();
302
303       display_goto_xy(iterator & 7, 0);
304       display_string("LEJOS NXT");
305
306       display_goto_xy(0, 1);
307       display_string("TIME ");
308       display_unsigned(systick_get_ms(), 0);
309
310       display_goto_xy(0, 2);
311       display_string("BATTERY ");
312       display_unsigned(battery_voltage(), 0);
313
314       display_goto_xy(0, 3);
315       display_string("BUTTONS ");
316       if (buttons & 1)
317         display_string("0 ");
318       if (buttons & 2)
319         display_string("1 ");
320       if (buttons & 4)
321         display_string("2 ");
322       if (buttons & 8)
323         display_string("3 ");
324
325       display_goto_xy(0, 4);
326       display_string("ADC ");
327       display_unsigned(sensor_adc(0), 5);
328       display_unsigned(sensor_adc(1), 5);
329       display_goto_xy(0, 5);
330       display_string("    ");
331       display_unsigned(sensor_adc(2), 5);
332       display_unsigned(sensor_adc(3), 5);
333       
334       i2c_start_transaction(0,1,0x42,1,&distance,1,0);
335       systick_wait_ms(200);
336       result = i2c_busy(0);
337
338       display_goto_xy(0,6);
339       display_string("DIST ");
340       display_unsigned(distance,3);
341
342       display_update();
343       systick_wait_ms(500);
344       
345     } else {
346
347       display_goto_xy(iterator & 7, 0);
348       display_string("LEJOS NXT");
349
350       display_goto_xy(0, 1);
351       display_string("TIME ");
352       display_unsigned(systick_get_ms(), 0);
353       display_goto_xy(0, 2);
354       display_string("Stack ");
355       display_unsigned(free_stack(), 0);
356       display_goto_xy(0, 3);
357       switch (motor_mode) {
358       case 0:
359         display_string("MOTORS REV");
360         break;
361       case 1:
362         display_string("MOTORS FWD");
363         break;
364       case 2:
365         display_string("MOTORS COAST");
366         break;
367       case 3:
368         display_string("MOTORS BRAKE");
369         break;
370       }
371
372       display_goto_xy(1, 4);
373       display_int(nxt_motor_get_count(0), 0);
374       display_goto_xy(1, 5);
375       display_int(nxt_motor_get_count(1), 0);
376       display_goto_xy(1, 6);
377       display_int(nxt_motor_get_count(2), 0);
378
379       display_update();
380       systick_wait_ms(500);
381     }
382   }
383 }
384
385 void
386 main(void)
387 {
388   /* When we get here:
389    * PLL and flash have been initialised and
390    * interrupts are off, but the AIC has not been initialised.
391    */
392   aic_initialise();
393   interrupts_enable();
394   systick_init();
395   sound_init();
396   nxt_avr_init();
397   nxt_motor_init();
398   i2c_init();
399   bt_init();
400   display_init();
401   
402   //flash_set_mode(*((unsigned *) 0x0010fff8));
403     
404   //xx_show();
405   
406   show_splash(3000);    
407  
408   gNextProgram = 0;
409   do 
410   {
411         int next = gNextProgram;
412         gNextProgram = 0;
413          
414     display_clear(1);
415         nxt_main(next, gNextProgramSize);
416   }
417   while (gNextProgram != 0);
418   
419   systick_wait_ms(5000);
420
421   while (1) {
422     nxt_avr_power_down();
423   }
424   
425 }