brickOS Kernel Developer v0.9.0
kmain.c
Go to the documentation of this file.
1
6/*
7 * The contents of this file are subject to the Mozilla Public License
8 * Version 1.0 (the "License"); you may not use this file except in
9 * compliance with the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
11 *
12 * Software distributed under the License is distributed on an "AS IS"
13 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
14 * License for the specific language governing rights and limitations
15 * under the License.
16 *
17 * The Original Code is legOS code, released October 17, 1999.
18 *
19 * The Initial Developer of the Original Code is Markus L. Noga.
20 * Portions created by Markus L. Noga are Copyright (C) 1999
21 * Markus L. Noga. All Rights Reserved.
22 *
23 * Contributor(s): Markus L. Noga <markus@noga.de>
24 * Kekoa Proudfoot <kekoa@graphics.stanford.edu>
25 * Lou Sortman <lou (at) sunsite (dot) unc (dot) edu>
26 */
27
28#include <sys/mm.h>
29#include <sys/time.h>
30#include <sys/tm.h>
31#include <sys/irq.h>
32#include <sys/lcd.h>
33#include <sys/dsensor.h>
34#include <sys/dmotor.h>
35#include <sys/dsound.h>
36#include <sys/lnp.h>
37#include <sys/lnp-logical.h>
38#include <sys/program.h>
39#ifdef CONF_AUTOSHUTOFF
40#include <sys/timeout.h>
41#endif
42#include <rom/system.h>
43
44#include <dbutton.h>
45
46#include <string.h>
47#include <conio.h>
48#include <unistd.h>
50//
51// Variables
52//
54
56
58unsigned char *firmware_string = "Do you byte, when I knock?";
59
60extern char __bss;
61extern char __bss_end;
62
64extern char __text_hi, __etext_hi;
65
66#if defined(CONF_DSOUND) && defined(CONF_ON_OFF_SOUND)
67static const note_t on_sound[]={{PITCH_G4, 1}, {PITCH_G5, 1}, {PITCH_END, 0}};
68static const note_t off_sound[]={{PITCH_C4, 1}, {PITCH_C3, 1}, {PITCH_END, 0}};
69#endif
70
72//
73// Functions
74//
76
77extern int main(int argc, char **argv);
78
79#if defined(CONF_DSOUND) && defined(CONF_ON_OFF_SOUND)
80static void onOffSound(const note_t *snd) {
82 dsound_play(snd);
83}
84#endif
85
87extern inline void show_on(void) {
88 cls();
89
90#ifdef CONF_ASCII
91 cputs("ON");
92#else
93#ifdef CONF_CONIO
94 cputc_native_4(0x38);
95 cputc_native_3(0x3d);
96 cputc_native_2(0x7c);
97 cputc_native_1(0x7e);
98 cputc_native_0(0x6d);
99#else
100 lcd_digit(1);
101#endif
102#endif
103#ifndef CONF_LCD_REFRESH
104 lcd_refresh();
105#endif
106#if defined(CONF_DSOUND) && defined(CONF_ON_OFF_SOUND)
107 onOffSound(on_sound);
108#endif
109 delay(250);
110#if defined(CONF_DSOUND) && defined(CONF_ON_OFF_SOUND)
112#endif
113}
114
116extern inline void show_off(void) {
117 cls();
118
119#ifdef CONF_ASCII
120 cputs("OFF");
121#else
122#ifdef CONF_CONIO
123 cputc_native_4(0x7e);
124 cputc_native_3(0x1d);
125 cputc_native_2(0x1d);
126#else
127 lcd_digit(0);
128#endif
129#endif
130#ifndef CONF_LCD_REFRESH
131 lcd_refresh();
132#endif
133#if defined(CONF_DSOUND) && defined(CONF_ON_OFF_SOUND)
134 onOffSound(off_sound);
135#endif
136}
137
138
140
142void kmain(void) __attribute__((noreturn));
143void kmain(void)
144{
145 int reset_after_shutdown=0;
146#ifdef CONF_DKEY
147 int c;
148#endif
149
150 /* Install the text.hi segment in the correct place. The
151 * firmware loader puts it in the bss segment, we copy it
152 * to it's final location.
153 */
155
157
158 /* Turn off motor, since writing to hitext manipulates motors */
160
161 memset(&__bss, 0, &__bss_end - &__bss);
162
163#ifdef CONF_MM
164 mm_init();
165#endif
166
167 while (1) {
168 power_init();
169
170#ifdef CONF_AUTOSHUTOFF
171 shutoff_init();
172#endif
173
174 lcd_init();
175
176#ifdef CONF_DSOUND
177 dsound_init();
178#endif
179#ifdef CONF_TIME
180 systime_init();
181#endif
182#ifdef CONF_DSENSOR
183 ds_init();
184#endif
185#ifdef CONF_DMOTOR
186 dm_init();
187#endif
188#ifdef CONF_LNP
189 lnp_init();
191#endif
192#ifdef CONF_TM
193 tm_init();
194#endif
195#ifdef CONF_PROGRAM
196 program_init();
197#endif
198
199 show_on();
200
201 // wait till power key released
202 //
203#ifdef CONF_DKEY
205 while((c=dkey_multi) & KEY_ONOFF);
206#else
207 while (PRESSED(dbutton(), BUTTON_ONOFF));
208 delay(100);
209#endif
210
211 cls();
212#ifndef CONF_PROGRAM
214#ifndef CONF_LCD_REFRESH
215 lcd_refresh();
216#endif
217#endif
218
219 // run app
220 //
221#ifdef CONF_TM
222# ifndef CONF_PROGRAM
224# endif
225 tm_start();
226#else
227 main(0, (void*)0);
228#endif
229
230 show_off();
231
232 // ON/OFF + PROGRAM -> erase firmware
233#ifdef CONF_DKEY
234 while((c=dkey_multi) & KEY_ONOFF)
235 if(c&KEY_PRGM)
236 reset_after_shutdown=1;
237#else
238 while (PRESSED(dbutton(), BUTTON_ONOFF))
240 reset_after_shutdown=1;
241#endif
242
243#ifdef CONF_PROGRAM
245#endif
246#ifdef CONF_LNP
248#endif
249#ifdef CONF_DMOTOR
250 dm_shutdown();
251#endif
252#ifdef CONF_DSENSOR
253 ds_shutdown();
254#endif
255#ifdef CONF_TIME
257#endif
258
259 if (reset_after_shutdown)
260 rom_reset();
261
262 lcd_clear();
264
265 power_off();
266 }
267}
void delay(unsigned ms)
uncalibrated delay loop
Definition conio.c:204
Interface: console input / output.
void cls()
clear user portion of LCD
void cputc_native_1(char mask)
write bit-pattern for segments at position 1 of LCD
void cputc_native_0(char mask)
write bit-pattern for segments at position 0 of LCD
void cputc_native_3(char mask)
write bit-pattern for segments at position 3 of LCD
void cputc_native_4(char mask)
write bit-pattern for segments at position 4 of LCD
void cputc_native_2(char mask)
write bit-pattern for segments at position 2 of LCD
void cputs(char *s)
Write string s to LCD (Only first 5 chars)
Interface: query raw button states.
#define BUTTON_ONOFF
the on/off button
Definition dbutton.h:42
int dbutton(void)
get button states
Definition dbutton.h:67
#define PRESSED(state, button)
true if all of the specified buttons are pressed
Definition dbutton.h:56
#define BUTTON_PROGRAM
the program button
Definition dbutton.h:45
#define KEY_PRGM
the program key is pressed
Definition dkey.h:47
#define KEY_ANY
any of the keys
Definition dkey.h:49
volatile unsigned char dkey_multi
the currently active keys
#define KEY_ONOFF
the on/off key is pressed
Definition dkey.h:44
#define PITCH_G4
Definition dsound.h:132
#define PITCH_C4
Definition dsound.h:125
#define DSOUND_DEFAULT_16th_ms
default duration of 1/16th note in ms
Definition dsound.h:204
#define PITCH_END
mark the end of a list of note_t entries
Definition dsound.h:193
#define PITCH_G5
Definition dsound.h:144
#define PITCH_C3
Definition dsound.h:113
static void dsound_play(const note_t *notes)
play a sequence of notes
Definition dsound.h:234
static unsigned dsound_set_duration(unsigned duration)
set duration of a 16th note in ms; return the previous duration.
Definition dsound.h:246
Internal LNP Interface: RCX redirected IRQ vectors.
void * reset_vector
reset vector
void * rom_reset_vector
ROM reset vector.
char __text_hi
the high memory segment
char __bss
the start of the uninitialized data segment
int main(int argc, char **argv)
the user main()
void kmain(void)
the beginning of everything
Definition kmain.c:142
void show_on(void)
show ON string
Definition kmain.c:87
void show_off(void)
show OFF string
Definition kmain.c:116
unsigned char * firmware_string
firmware recognition string
Definition kmain.c:58
char __bss_end
the end of the uninitialized data segment
char __etext_hi
Definition kmain.c:64
void lcd_refresh(void)
refresh the entire LCD display
Definition lcd.c:254
void lcd_init(void)
initialize the LCD display driver
Definition lcd.c:295
void lcd_power_off(void)
power off the LCD controller
Definition lcd.c:279
Internal Interface: memory management.
void mm_init()
initialize memory management
Internal Interface: program data structures and functions.
void program_shutdown()
shutdown program support
void program_init()
initialize program support
#define lcd_digit(d)
display a single digit right of the man symbol
Definition lcd.h:134
@ man_run
Definition lcd.h:55
void lcd_clear(void)
clear LCD display
Definition lcd.h:191
void lcd_show(lcd_segment segment)
show LCD segment
Definition lcd.h:156
Interface: string functions.
void * memcpy(void *dest, const void *src, size_t size)
copy memory block from src to dest.
void * memset(void *s, int c, size_t n)
fill memory block with a byte value.
the note structure describing a single note.
Definition dsound.h:56
Internal Interface: direct motor control.
void dm_init(void)
initialize motors
void dm_shutdown(void)
shutdown motors
unsigned char motor_controller
RCX Motor Controller port.
Internal Interface: direct sensor access.
void ds_shutdown(void)
shutdown sensor a/d conversion
void ds_init(void)
initialize sensor a/d conversion
Internal Interface: direct sound control.
void dsound_init()
initialize sound driver
Internal Interface: LCD control and constants.
Internal LNP Interface: link networking protocol logical layer.
void lnp_logical_shutdown(void)
Shutdown the logical layer (IR port)
void lnp_logical_init(void)
Initialize the logical layer (IR port)
Internal LNP Interface: link networking protocol.
void lnp_init(void)
Initialise protocol handlers.
Internal Interface: system time functions.
Internal Interface: task management.
void tm_init(void)
init task management
void tm_start(void)
start task management
ROM Interface: RCX system control functions.
void power_init(void)
disables software standby mode so tm_idle_task() can use the sleep
Definition system.h:54
void rom_reset(void) __attribute__((noreturn))
turns off interrupts, then issues reset.
Definition system.h:68
void power_off(void)
enters software standby mode.
Definition system.h:42
void systime_init(void)
initialize system timer
Definition systime.c:270
void systime_shutdown(void)
shutdown system timer
Definition systime.c:317
Internal Interface: Powerdown Timer Routines.
void shutoff_init(void)
#define PRIO_NORMAL
The priority of most tasks.
Definition tm.h:53
#define DEFAULT_STACK_SIZE
that's enough.
Definition tm.h:81
Interface: reduced UNIX standard library.