OSDN Git Service

lejos_NXJ_win32_0_5_0beta.zip
[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   sound_freq(100,500); // buzz
76   display_clear(0);
77   display_goto_xy(0, 0);
78   display_string("Java Exception:");
79   display_goto_xy(0, 1);
80   display_string("Class:");
81   display_goto_xy(7, 1);
82   display_int(get_class_index(exception), 0);
83   display_goto_xy(0, 2);
84   display_string("Method:");
85   display_goto_xy(8, 2);
86   display_int(methodRecord->signatureId, 0);
87   display_update();
88   wait_for_power_down_signal();
89 }
90
91 void
92 switch_thread_hook()
93 {
94   int b = buttons_get();
95
96   // Check for ENTER and ESCAPE pressed
97   if (b == 9) {
98     // Shut down power immediately
99     while (1) {
100       nxt_avr_power_down();
101     }
102   }
103 }
104
105 void
106 assert_hook(boolean aCond, int aCode)
107 {
108   if (aCond) return;
109   display_clear(0);
110   display_goto_xy(0,0);
111   display_string("Assert failed");
112   display_goto_xy(0,1);
113   display_string("Code: ");
114   display_goto_xy(6,1);
115   display_int(aCode,0);
116   display_update();
117   while(1); // Hang
118 }
119
120
121 void
122 run(int jsize)
123 {
124   byte *ram_end = (byte *) (&__free_ram_end__);
125   byte *ram_start = (byte *) (&__free_ram_start__);
126
127   init_poller();
128
129   //printf("Initializing Binary\n");
130
131 #if EXECUTE_FROM_FLASH
132   {
133     MasterRecord *mrec = get_master_record();
134     int staticSize = mrec->staticStateLength;
135     int statusSize = (mrec->lastClass + 1) * sizeof( classStatusBase[0]);
136
137     staticSize = (staticSize + 1) & ~(1);
138     statusSize = (statusSize + 3) & ~(3);
139
140     ram_end -= staticSize * 2;
141     classStaticStateBase = ram_end;
142
143     ram_end -= statusSize;
144     classStatusBase = ram_end;
145
146     memset( classStatusBase, 0, statusSize);
147   }
148 #endif
149
150   // Initialize binary image state
151   initialize_binary();
152
153   //printf("Initializing memory\n");
154
155   // Initialize memory
156   {
157     //int size;
158
159 #if ! EXECUTE_FROM_FLASH
160     // Skip java binary if it is an top of ram
161     if (jsize > 0)
162       ram_end -= (jsize + 4);
163 #endif
164
165     //size = ((unsigned) ram_end) - ((unsigned) ram_start);
166
167     memory_init();
168
169     region = ram_start;
170     memory_add_region(region, (byte *) ram_end);
171
172     /*Add extra RAM if available */
173     //ram_end = (byte *) (&__extra_ram_end__);
174     //ram_start = (byte *) (&__extra_ram_start__);
175     //size = ((unsigned) ram_end) - ((unsigned) ram_start);
176
177     //if(size > 0)
178     //  memory_add_region(ram_start, ram_end);
179   }
180
181   //printf("Initializing exceptions\n");
182
183   // Initialize exceptions
184   init_exceptions();
185
186   // Create the boot thread (bootThread is a special global)
187   bootThread = (Thread *) new_object_for_class(JAVA_LANG_THREAD);
188
189   init_threads();
190   if (!init_thread(bootThread)) {
191     return;
192   }
193   //printf("Executing Interpreter\n");
194
195   // Execute the bytecode interpreter
196   set_program_number(0);
197
198   engine();
199   // Engine returns when all non-daemon threads are dead
200 }
201
202 /***************************************************************************
203  * int nxt_main *--------------------------------------------------------------------------
204  ***************************************************************************/
205 //int main (int argc, char *argv[])
206 int
207 nxt_main(int bin, int size)
208 {
209   int jsize = 0;
210   const char *binary; 
211   unsigned *temp;
212
213 #if EXECUTE_FROM_FLASH
214   if (bin > 0) {
215     size = (size + 3) & ~3;
216         binary = (char *) bin;
217         jsize = size - 4;
218   } else {
219     // Execute flash menu
220
221     bin = (unsigned *) 0x00108000;
222     size = *((unsigned *) 0x00113ffc);
223     size = (size + 3) & ~3;
224         binary = (char *) bin;
225     jsize = size - 4;
226   }
227 #else
228   if (bin > 0) {
229     size = (size + 3) & ~3;
230         temp = ((unsigned *) (&__free_ram_end__)) - (size >> 2);
231         memcpy(temp,bin,size);
232         binary = (char *) temp;
233         jsize = size - 4;
234   } else {
235     // Execute flash menu
236
237     bin = (unsigned *) 0x00108000;
238     size = *((unsigned *) 0x00113ffc);
239     size = (size + 3) & ~3;
240     temp = ((unsigned *) (&__free_ram_end__)) - (size >> 2);   
241     memcpy(temp,bin,size);
242     binary = ((char *) temp);
243     jsize = size - 4;
244   }
245 #endif
246   
247   // reset all motors, sensors and devices
248
249   init_sensors();
250   nxt_motor_reset_all();
251   bt_reset(); 
252   display_clear(1);
253   display_set_auto_update(1);
254   //       printf("Installing Binary\n");
255
256   install_binary(binary);
257
258   //      printf("Running\n");
259
260   run(jsize);
261
262   return 0;
263 }
264
265 const U8 splash_data[4 * 26] = {
266   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
267   0x00, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFC,
268   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA0, 0x40, 0xA0, 0x40,
269   0xA0, 0x40, 0xA0, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
270   0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0x00, 0x00, 0x00, 0x0A, 0x05, 0x0A, 0x05,
271   0x0A, 0x05, 0x0A, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
272   0x0F, 0x1F, 0x3F, 0x3F, 0x7F, 0x7F, 0xFC, 0xF8, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
273   0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF8, 0xFC, 0x7F, 0x7F, 0x3F, 0x3F, 0x1F, 0x0F,
274 };
275
276 void
277 show_splash(U32 milliseconds)
278 {
279   display_clear(0);
280   display_bitmap_copy(splash_data, 26, 4, 37, 1);
281
282   display_goto_xy(6, 6);
283   display_string("LEJOS");
284   display_update();
285
286   systick_wait_ms(milliseconds);
287 }
288
289
290
291 U32
292 free_stack(void)
293 {
294   extern U32 __system_stack_bottom__;
295   extern U32 __system_stack_top__;
296   U32 *sp = &__system_stack_bottom__;
297   U32 space = 0;
298
299   while ((sp < &__system_stack_top__) && *sp == 0x6b617453) {
300     sp++;
301     space += 4;
302   }
303   return space;
304 }
305
306 void
307 xx_show(void)
308 {
309   int iterator = 0;
310   U32 buttons;
311   U32 motor_mode = 0;
312   int result;
313   U8 distance;
314
315   show_splash(3000);
316
317   /* set up power for ultrasonic sensor on port 1 (port 0 here )*/  
318   nxt_avr_set_input_power(0,1);
319   i2c_enable(0);
320                         
321   while (1) {
322     display_clear(0);
323
324     if ((iterator & 15) == 0) {
325       motor_mode = (iterator >> 4) & 3;
326       switch (motor_mode) {
327
328       case 0:
329         nxt_motor_set_speed(2, -100, 0);
330         break;
331       case 1:
332         nxt_motor_set_speed(2, 100, 0);
333         break;
334       case 2:
335         nxt_motor_set_speed(2, 0, 0);
336         break;
337       case 3:
338         nxt_motor_set_speed(2, 0, 1);
339         break;
340       }
341     }
342     iterator++;
343
344     if ((iterator %10) < 5) {
345       buttons = buttons_get();
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
354       display_goto_xy(0, 2);
355       display_string("BATTERY ");
356       display_unsigned(battery_voltage(), 0);
357
358       display_goto_xy(0, 3);
359       display_string("BUTTONS ");
360       if (buttons & 1)
361         display_string("0 ");
362       if (buttons & 2)
363         display_string("1 ");
364       if (buttons & 4)
365         display_string("2 ");
366       if (buttons & 8)
367         display_string("3 ");
368
369       display_goto_xy(0, 4);
370       display_string("ADC ");
371       display_unsigned(sensor_adc(0), 5);
372       display_unsigned(sensor_adc(1), 5);
373       display_goto_xy(0, 5);
374       display_string("    ");
375       display_unsigned(sensor_adc(2), 5);
376       display_unsigned(sensor_adc(3), 5);
377       
378       i2c_start_transaction(0,1,0x42,1,&distance,1,0);
379       systick_wait_ms(200);
380       result = i2c_busy(0);
381
382       display_goto_xy(0,6);
383       display_string("DIST ");
384       display_unsigned(distance,3);
385
386       display_update();
387       systick_wait_ms(500);
388       
389     } else {
390
391       display_goto_xy(iterator & 7, 0);
392       display_string("LEJOS NXT");
393
394       display_goto_xy(0, 1);
395       display_string("TIME ");
396       display_unsigned(systick_get_ms(), 0);
397       display_goto_xy(0, 2);
398       display_string("Stack ");
399       display_unsigned(free_stack(), 0);
400       display_goto_xy(0, 3);
401       switch (motor_mode) {
402       case 0:
403         display_string("MOTORS REV");
404         break;
405       case 1:
406         display_string("MOTORS FWD");
407         break;
408       case 2:
409         display_string("MOTORS COAST");
410         break;
411       case 3:
412         display_string("MOTORS BRAKE");
413         break;
414       }
415
416       display_goto_xy(1, 4);
417       display_int(nxt_motor_get_count(0), 0);
418       display_goto_xy(1, 5);
419       display_int(nxt_motor_get_count(1), 0);
420       display_goto_xy(1, 6);
421       display_int(nxt_motor_get_count(2), 0);
422
423       display_update();
424       systick_wait_ms(500);
425     }
426   }
427 }
428
429 void
430 main(void)
431 {
432   /* When we get here:
433    * PLL and flash have been initialised and
434    * interrupts are off, but the AIC has not been initialised.
435    */
436   aic_initialise();
437   interrupts_enable();
438   systick_init();
439   sound_init();
440   nxt_avr_init();
441   nxt_motor_init();
442   i2c_init();
443   bt_init();
444   systick_wait_ms(1000); // wait for LCD to stabilize
445   display_init();
446   
447   //flash_set_mode(*((unsigned *) 0x0010fff8));
448     
449   //xx_show();
450   
451   show_splash(3000);    
452  
453   gNextProgram = 0;
454   do 
455   {
456         int next = gNextProgram;
457         gNextProgram = 0;
458         nxt_main(next, gNextProgramSize);
459   }
460   while (true);
461
462   while (1) {
463     nxt_avr_power_down();
464   }
465   
466 }