-
Notifications
You must be signed in to change notification settings - Fork 1
/
legosoar.c
465 lines (386 loc) · 12.3 KB
/
legosoar.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "MPL"); you may not use this file except in
* compliance with the MPL. You may obtain a copy of the MPL at
* http://www.mozilla.org/MPL/
*
* Software distributed under the MPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the MPL
* for the specific language governing rights and limitations under the
* MPL.
*
* The Initial Developer of this code under the MPL is Christopher
* R. Waterson. Portions created by Christopher R. Waterson are
* Copyright (C) 2000 Christopher R. Waterson. All Rights Reserved.
*
* Contributor(s):
* Christopher R. Waterson <waterson@maubi.net>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
*/
/*
* An embedding harness for TinySoar that runs on the Lego Mindstorms
* RCX. Much of the code here was cribbed from LegOS
*
* <http://legos.sourceforge.net/>
*
* with minor adaptations.
*/
#include "alloc.h"
#include "soar.h"
#include "rcx.h"
#define DECL_RETE_NETWORK
#include "agent.inc"
/*
* The RCX wants to find this string to unlock the firmware.
*/
unsigned char *firmware_string = "Do you byte, when I knock?";
#define RUNSTATE_POWER_ON 0x1
#define RUNSTATE_RUNNING 0x2
volatile unsigned char runstate = 0;
/*static*/ unsigned char timer = 10;
static volatile unsigned char keystate = 0;
#define KEY_ON_OFF (1 << 0)
#define KEY_RUN (1 << 1)
#define KEY_VIEW (1 << 2)
#define KEY_PROGRAM (1 << 3)
#define KEY_ANY (KEY_ON_OFF | KEY_RUN | KEY_VIEW | KEY_PROGRAM)
/*
* Handler for the OCIA interrupt. Does key debouncing. Borrows
* heavily from LegOS's dkey.c, which is Copyright (C) 1999 Markus
* L. Noga <markus@noga.de>.
*/
static void
ocia_handler()
{
asm(/* Check the debouncing timer. Has it decreased to zero? */
"\tmov.b @_timer,r6l\n"
"\tbeq check\n"
/* Not yet, decrement it and bail. */
"\tdec r6l\n"
"\tmov.b r6l,@_timer\n"
"\tjmp done\n"
/* Load key status from PORT4 into bits zero and one of R6L. */
"check:\n"
"\tsub.b r6l,r6l\n"
"\tmov.b @_PORT4,r6h\n"
"\tbld #1,r6h\n" /* ON-OFF */
"\tbist #0,r6l\n"
"\tbld #2,r6h\n" /* RUN */
"\tbist #1,r6l\n"
/* Load key status from PORT7 into bits two and three of R6L. */
"\tmov.b @_PORT7,r6h\n"
"\tbld #6,r6h\n"
"\tbist #2,r6l\n" /* VIEW */
"\tbld #7,r6h\n"
"\tbist #3,r6l\n" /* PROGRAM */
/* Retrieve the keystate and see if anything has changed. */
"\tmov.b @_keystate,r6h\n"
"\txor.b r6l,r6h\n"
"\tbeq done\n"
/* Yup, store the new keystate. */
"\tmov.b r6l,@_keystate\n"
#if 0 /* XXX if we ever want to remember the last key pressed... */
/* Remeber the last single key pressed. */
"\tand.b r6h,r6l\n"
"\tmov.b r6l,@_key\n"
#endif
/* Was the run key pressed? */
"\tbtst #1,r6l\n"
"\tbeq power\n"
/* Yes: invert the RUNSTATE_RUNNING bit in the runstate. */
"\tmov.b @_runstate,r6h\n"
"\tbnot #1,r6h\n"
"\tmov.b r6h,@_runstate\n"
/* Was the power key pressed? */
"power:\n"
"\tbtst #0,r6l\n"
"\tbeq reset\n"
/* Yes: clear the RUNSTATE_POWER_ON bit in the runstate. */
"\tmov.b @_runstate,r6h\n"
"\tbclr #0,r6h\n"
"\tmov.b r6h,@_runstate\n"
/* Reset the debouncing timer. */
"reset:\n"
"\tmov.b #10,r6h\n"
"\tmov.b r6h,@_timer\n"
/* Clear the OCFA register so we keep getting interrupts. */
"done:\n"
"\tbclr #3,@_T_CSR:8\n"
"\trts\n");
}
void
debug_wait_button(int value)
{
rcx_lcd_show_int16(value);
while (! (keystate & KEY_VIEW))
asm("sleep");
while (keystate & KEY_VIEW)
asm("sleep");
}
void
panic()
{
debug_wait_button(3507);
}
/*
* Runtime heap setup.
*/
extern char mm_start;
char *addrs[] = {
(char *) &mm_start, /* 0x8000-0xef30 (usable) */
(char *) 0xef30, /* 0xef30-0xef50 lcd data */
(char *) 0xef50, /* 0xef50-0xf000 (usable) */
(char *) 0xf000, /* 0xf000-0xf010 motor */
(char *) 0xf010, /* 0xf010-0xfb80 (usable) */
(char *) 0xfb80, /* 0xfb80-0xfe00 bad memory and vectors */
(char *) 0xfe00, /* 0xfe00-0xff00 (usable) */
(char *) 0xff00 /* 0xff00- stack */
};
/*
* A structure for the sensor data.
*/
struct sensor {
unsigned symbol_value;
unsigned ad_csr_bits;
volatile unsigned *ad_register;
struct preference *preference;
};
struct sensor sensors[] = {
#ifdef SYM_SENSOR_1
{ SYM_SENSOR_1, 2, &AD_C, 0 },
#endif
#ifdef SYM_SENSOR_2
{ SYM_SENSOR_2, 1, &AD_B, 0 },
#endif
#ifdef SYM_SENSOR_3
{ SYM_SENSOR_3, 0, &AD_A, 0 },
#endif
};
#define NSENSORS (sizeof(sensors) / sizeof(sensors[0]))
/*
* Synchronize the preference value for (s1 ^io.input-link.sensor-<n>).
*/
static void
sync_sensor(struct sensor *sensor)
{
struct preference *pref = sensor->preference;
unsigned sensor_val;
symbol_t sym_input_link;
symbol_t sym_sensor_n;
symbol_t sym_value;
/* Power down the sensor so we can read the channel. */
PORT6 &= ~(1 << sensor->ad_csr_bits);
/* Request an A/D conversion on the sensor's channel. */
AD_CSR = sensor->ad_csr_bits;
AD_CSR |= ADST;
/* Wait for the conversion to complete. */
while (! (AD_CSR & ADF))
;
AD_CSR &= ~ADF;
/* Read the sensor's value. */
sensor_val = (*(sensor->ad_register) >> 6) & 0x03ff;
/* Power the sensor back up. */
PORT6 |= (1 << sensor->ad_csr_bits);
/* If the value hasn't changed since we last set the preference,
bail now. */
if (pref && GET_SYMBOL_VALUE(pref->value) == sensor_val)
return;
/* Remove the preference for the old value, if there is one. */
if (pref)
wmem_remove_preference(&agent, pref);
/* Create a preference for the new value. */
INIT_SYMBOL(sym_input_link, symbol_type_identifier, 3 /*XXX*/);
INIT_SYMBOL(sym_sensor_n, symbol_type_symbolic_constant, sensor->symbol_value);
INIT_SYMBOL(sym_value, symbol_type_integer_constant, sensor_val);
sensor->preference =
wmem_add_preference(&agent,
sym_input_link, sym_sensor_n, sym_value,
preference_type_acceptable,
support_type_architecture);
}
/*
* Synchronize the input-link with the sensors.
*/
static void
sync_input_link()
{
struct sensor *sensor = sensors;
struct sensor *limit = sensors + NSENSORS;
for ( ; sensor < limit; ++sensor)
sync_sensor(sensor);
}
/*
* The motor interface.
*/
extern volatile unsigned char motor_controller;
/*
* A structure for the motor data.
*/
struct motor {
unsigned symbol_value;
unsigned char pattern[4];
};
struct motor motors[] = {
#ifdef SYM_MOTOR_A
{ SYM_MOTOR_A, { 0x00, 0x80, 0x40, 0xc0 } },
#endif
#ifdef SYM_MOTOR_B
{ SYM_MOTOR_B, { 0x00, 0x08, 0x04, 0x0c } },
#endif
#ifdef SYM_MOTOR_C
{ SYM_MOTOR_C, { 0x00, 0x02, 0x01, 0x03 } },
#endif
};
#define NMOTORS (sizeof(motors) / sizeof(motors[0]))
typedef enum motor_direction {
motor_direction_off = 0,
motor_direction_forward = 1,
motor_direction_reverse = 2,
motor_direction_brake = 3
} motor_direction_t;
/*
* Synchronize the motor state with the value from
* (s1 ^io.output-link.motor-<n>)
*/
static void
sync_motor(struct motor *motor)
{
motor_direction_t direction = motor_direction_off;
unsigned char output;
struct wme *wme;
symbol_t sym_output_link;
symbol_t sym_motor_n;
/* See if there's a wme for the motor. */
INIT_SYMBOL(sym_output_link, symbol_type_identifier, 4 /*XXX*/);
INIT_SYMBOL(sym_motor_n, symbol_type_symbolic_constant, motor->symbol_value);
wme = wmem_get_wmes(&agent, sym_output_link, sym_motor_n);
/* If we find one, and it's got an integer value, then set the
direction appropriately. */
if (wme) {
switch (GET_SYMBOL_VALUE(wme->value)) {
#ifdef SYM_FORWARD
case SYM_FORWARD:
direction = motor_direction_forward;
break;
#endif
#ifdef SYM_REVERSE
case SYM_REVERSE:
direction = motor_direction_reverse;
break;
#endif
#ifdef SYM_BRAKE
case SYM_BRAKE:
direction = motor_direction_brake;
break;
#endif
default:
/* Either SYM_OFF or something random. Leave the default
value of motor_direction_off untouched. */
break;
}
}
/* Compute and output the waveform. */
output = motor_controller;
output &= ~(motor->pattern[motor_direction_brake]);
output |= motor->pattern[direction];
motor_controller = output;
}
/*
* Synchronize the motors with the output-link.
*/
static void
sync_output_link()
{
struct motor *motor = motors;
struct motor *limit = motors + NMOTORS;
for ( ; motor < limit; ++motor)
sync_motor(motor);
}
extern char __bss, __bss_end;
static void
bss_init()
{
char *p;
for (p = &__bss; p < &__bss_end; ++p)
*p = 0;
}
__attribute__((noreturn))
void
_start()
{
/* Do one-time startup when the firmware is first installed. */
bss_init();
heap_init(addrs, sizeof addrs / sizeof addrs[0]);
agent_init(&agent);
while (1) {
/* Power up and disable the software standby so we can safely
issue the `sleep' instruction when idling. */
asm("\tpush r6\n"
"\tjsr power_init\n"
"\tbclr #7,@_SYSCR:8\n"
"\tpop r6\n");
/* Make sure the motors are off. */
motor_controller = 0;
/* Configure the 16-bit timer compare A IRQ to occur every
10ms. */
T_CSR = TCSR_OCA | TCSR_RESET_ON_A;
T_CR = TCR_CLOCK_32;
T_OCR &= ~TOCR_OCRB;
T_OCRA = 5000;
ocia_vector = ocia_handler;
T_IER |= TIER_ENABLE_OCA;
/* Wait for the power key to be released. */
keystate = KEY_ANY;
while (keystate & KEY_ON_OFF)
;
/* Run until the power gets turned off. */
runstate |= RUNSTATE_POWER_ON;
while (runstate & RUNSTATE_POWER_ON) {
/* Standing man. */
rcx_lcd_show_icon(LCD_STANDING);
while ((runstate & RUNSTATE_POWER_ON) && !(runstate & RUNSTATE_RUNNING))
asm("sleep"); /* Idle. */
/* Running Man. */
rcx_lcd_show_icon(LCD_WALKING);
/* Power up active sensors. */
PORT6_DDR = rom_port6_ddr |= 0x7;
PORT6 |= 0x7;
while ((runstate & RUNSTATE_POWER_ON) && (runstate & RUNSTATE_RUNNING)) {
agent_elaborate(&agent);
sync_output_link();
sync_input_link();
}
/* Power down active sensors. */
PORT6_DDR = rom_port6_ddr &= ~0x7;
PORT6 &= ~0x7;
/* Shut off the motors. */
motor_controller = 0;
}
runstate &= ~RUNSTATE_RUNNING;
agent_reset(&agent);
/* Stop our timer interrupt. */
T_IER &= ~TIER_ENABLE_OCA;
/* Clear the display. */
rcx_lcd_clear();
/* Power down after re-enabling software stand-by mode. */
asm("\tpush r6\n"
"\tbset #7,@_SYSCR:8\n"
"\tjsr power_off\n"
"\tpop r6\n");
}
}