OSDN Git Service

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