1 | /* |
---|
2 | * console.c -- console I/O package |
---|
3 | * |
---|
4 | * Copyright (C) 1999 Eric Valette. valette@crf.canon.fr |
---|
5 | * |
---|
6 | * This code is based on the pc386 BSP console.c so the following |
---|
7 | * copyright also applies : |
---|
8 | * |
---|
9 | * (C) Copyright 1997 - |
---|
10 | * - NavIST Group - Real-Time Distributed Systems and Industrial Automation |
---|
11 | * |
---|
12 | * http://pandora.ist.utl.pt |
---|
13 | * |
---|
14 | * Instituto Superior Tecnico * Lisboa * PORTUGAL |
---|
15 | * The license and distribution terms for this file may be |
---|
16 | * found in found in the file LICENSE in this distribution or at |
---|
17 | * http://www.OARcorp.com/rtems/license.html. |
---|
18 | * |
---|
19 | * $Id$ |
---|
20 | */ |
---|
21 | |
---|
22 | #include <stdlib.h> |
---|
23 | #include <assert.h> |
---|
24 | #include <stdlib.h> |
---|
25 | |
---|
26 | #undef __assert |
---|
27 | void __assert (const char *file, int line, const char *msg); |
---|
28 | extern int close(int fd); |
---|
29 | |
---|
30 | #include <bsp.h> |
---|
31 | #include <bsp/irq.h> |
---|
32 | #include <rtems/libio.h> |
---|
33 | #include <termios.h> |
---|
34 | #include <bsp/uart.h> |
---|
35 | #include <bsp/consoleIo.h> |
---|
36 | |
---|
37 | /* Definitions for BSPConsolePort */ |
---|
38 | #define BSP_CONSOLE_PORT_CONSOLE (-1) |
---|
39 | #define BSP_CONSOLE_PORT_COM1 (BSP_UART_COM1) |
---|
40 | #define BSP_CONSOLE_PORT_COM2 (BSP_UART_COM2) |
---|
41 | /* |
---|
42 | * Possible value for console input/output : |
---|
43 | * BSP_CONSOLE_PORT_CONSOLE |
---|
44 | * BSP_UART_COM1 |
---|
45 | * BSP_UART_COM2 |
---|
46 | */ |
---|
47 | |
---|
48 | int BSPConsolePort = BSP_UART_COM1; |
---|
49 | |
---|
50 | /* int BSPConsolePort = BSP_UART_COM2; */ |
---|
51 | int BSPBaseBaud = 115200; |
---|
52 | |
---|
53 | /*-------------------------------------------------------------------------+ |
---|
54 | | External Prototypes |
---|
55 | +--------------------------------------------------------------------------*/ |
---|
56 | |
---|
57 | static int conSetAttr(int minor, const struct termios *); |
---|
58 | static void isr_on(const rtems_irq_connect_data *); |
---|
59 | static void isr_off(const rtems_irq_connect_data *); |
---|
60 | static int isr_is_on(const rtems_irq_connect_data *); |
---|
61 | |
---|
62 | |
---|
63 | static rtems_irq_connect_data console_isr_data = {BSP_ISA_UART_COM1_IRQ, |
---|
64 | BSP_uart_termios_isr_com1, |
---|
65 | isr_on, |
---|
66 | isr_off, |
---|
67 | isr_is_on}; |
---|
68 | |
---|
69 | static void |
---|
70 | isr_on(const rtems_irq_connect_data *unused) |
---|
71 | { |
---|
72 | return; |
---|
73 | } |
---|
74 | |
---|
75 | static void |
---|
76 | isr_off(const rtems_irq_connect_data *unused) |
---|
77 | { |
---|
78 | return; |
---|
79 | } |
---|
80 | |
---|
81 | static int |
---|
82 | isr_is_on(const rtems_irq_connect_data *irq) |
---|
83 | { |
---|
84 | return BSP_irq_enabled_at_i8259s(irq->name); |
---|
85 | } |
---|
86 | |
---|
87 | void console_reserve_resources(rtems_configuration_table *conf) |
---|
88 | { |
---|
89 | if(BSPConsolePort != BSP_CONSOLE_PORT_CONSOLE) |
---|
90 | { |
---|
91 | rtems_termios_reserve_resources(conf, 1); |
---|
92 | } |
---|
93 | |
---|
94 | return; |
---|
95 | } |
---|
96 | |
---|
97 | void __assert (const char *file, int line, const char *msg) |
---|
98 | { |
---|
99 | static char exit_msg[] = "EXECUTIVE SHUTDOWN! Any key to reboot..."; |
---|
100 | unsigned char ch; |
---|
101 | |
---|
102 | /* |
---|
103 | * Note we cannot call exit or printf from here, |
---|
104 | * assert can fail inside ISR too |
---|
105 | */ |
---|
106 | |
---|
107 | /* |
---|
108 | * Close console |
---|
109 | */ |
---|
110 | close(2); |
---|
111 | close(1); |
---|
112 | close(0); |
---|
113 | |
---|
114 | printk("\nassert failed: %s: ", file); |
---|
115 | printk("%d: ", line); |
---|
116 | printk("%s\n\n", msg); |
---|
117 | printk(exit_msg); |
---|
118 | ch = debug_getc(); |
---|
119 | printk("\n\n"); |
---|
120 | rtemsReboot(); |
---|
121 | |
---|
122 | } |
---|
123 | |
---|
124 | |
---|
125 | /*-------------------------------------------------------------------------+ |
---|
126 | | Console device driver INITIALIZE entry point. |
---|
127 | +--------------------------------------------------------------------------+ |
---|
128 | | Initilizes the I/O console (keyboard + VGA display) driver. |
---|
129 | +--------------------------------------------------------------------------*/ |
---|
130 | rtems_device_driver |
---|
131 | console_initialize(rtems_device_major_number major, |
---|
132 | rtems_device_minor_number minor, |
---|
133 | void *arg) |
---|
134 | { |
---|
135 | rtems_status_code status; |
---|
136 | |
---|
137 | /* |
---|
138 | * The video was initialized in the start.s code and does not need |
---|
139 | * to be reinitialized. |
---|
140 | */ |
---|
141 | |
---|
142 | |
---|
143 | /* |
---|
144 | * Set up TERMIOS |
---|
145 | */ |
---|
146 | rtems_termios_initialize (); |
---|
147 | |
---|
148 | /* |
---|
149 | * Do device-specific initialization |
---|
150 | */ |
---|
151 | |
---|
152 | /* 9600-8-N-1 */ |
---|
153 | BSP_uart_init(BSPConsolePort, 9600, 0); |
---|
154 | |
---|
155 | |
---|
156 | /* Set interrupt handler */ |
---|
157 | if(BSPConsolePort == BSP_UART_COM1) |
---|
158 | { |
---|
159 | console_isr_data.name = BSP_ISA_UART_COM1_IRQ; |
---|
160 | console_isr_data.hdl = BSP_uart_termios_isr_com1; |
---|
161 | |
---|
162 | } |
---|
163 | else |
---|
164 | { |
---|
165 | assert(BSPConsolePort == BSP_UART_COM2); |
---|
166 | console_isr_data.name = BSP_ISA_UART_COM2_IRQ; |
---|
167 | console_isr_data.hdl = BSP_uart_termios_isr_com2; |
---|
168 | } |
---|
169 | |
---|
170 | status = BSP_install_rtems_irq_handler(&console_isr_data); |
---|
171 | |
---|
172 | if (!status){ |
---|
173 | printk("Error installing serial console interrupt handler!\n"); |
---|
174 | rtems_fatal_error_occurred(status); |
---|
175 | } |
---|
176 | /* |
---|
177 | * Register the device |
---|
178 | */ |
---|
179 | status = rtems_io_register_name ("/dev/console", major, 0); |
---|
180 | if (status != RTEMS_SUCCESSFUL) |
---|
181 | { |
---|
182 | printk("Error registering console device!\n"); |
---|
183 | rtems_fatal_error_occurred (status); |
---|
184 | } |
---|
185 | |
---|
186 | if(BSPConsolePort == BSP_UART_COM1) |
---|
187 | { |
---|
188 | printk("Initialized console on port COM1 9600-8-N-1\n\n"); |
---|
189 | } |
---|
190 | else |
---|
191 | { |
---|
192 | printk("Initialized console on port COM2 9600-8-N-1\n\n"); |
---|
193 | } |
---|
194 | return RTEMS_SUCCESSFUL; |
---|
195 | } /* console_initialize */ |
---|
196 | |
---|
197 | |
---|
198 | static int console_last_close(int major, int minor, void *arg) |
---|
199 | { |
---|
200 | BSP_remove_rtems_irq_handler (&console_isr_data); |
---|
201 | |
---|
202 | return 0; |
---|
203 | } |
---|
204 | |
---|
205 | /*-------------------------------------------------------------------------+ |
---|
206 | | Console device driver OPEN entry point |
---|
207 | +--------------------------------------------------------------------------*/ |
---|
208 | rtems_device_driver |
---|
209 | console_open(rtems_device_major_number major, |
---|
210 | rtems_device_minor_number minor, |
---|
211 | void *arg) |
---|
212 | { |
---|
213 | rtems_status_code status; |
---|
214 | static rtems_termios_callbacks cb = |
---|
215 | { |
---|
216 | NULL, /* firstOpen */ |
---|
217 | console_last_close, /* lastClose */ |
---|
218 | NULL, /* pollRead */ |
---|
219 | BSP_uart_termios_write_com1, /* write */ |
---|
220 | conSetAttr, /* setAttributes */ |
---|
221 | NULL, /* stopRemoteTx */ |
---|
222 | NULL, /* startRemoteTx */ |
---|
223 | 1 /* outputUsesInterrupts */ |
---|
224 | }; |
---|
225 | |
---|
226 | if(BSPConsolePort == BSP_UART_COM2) |
---|
227 | { |
---|
228 | cb.write = BSP_uart_termios_write_com2; |
---|
229 | } |
---|
230 | |
---|
231 | status = rtems_termios_open (major, minor, arg, &cb); |
---|
232 | |
---|
233 | if(status != RTEMS_SUCCESSFUL) |
---|
234 | { |
---|
235 | printk("Error openning console device\n"); |
---|
236 | return status; |
---|
237 | } |
---|
238 | |
---|
239 | /* |
---|
240 | * Pass data area info down to driver |
---|
241 | */ |
---|
242 | BSP_uart_termios_set(BSPConsolePort, |
---|
243 | ((rtems_libio_open_close_args_t *)arg)->iop->data1); |
---|
244 | /* Enable interrupts on channel */ |
---|
245 | BSP_uart_intr_ctrl(BSPConsolePort, BSP_UART_INTR_CTRL_TERMIOS); |
---|
246 | |
---|
247 | return RTEMS_SUCCESSFUL; |
---|
248 | } |
---|
249 | |
---|
250 | /*-------------------------------------------------------------------------+ |
---|
251 | | Console device driver CLOSE entry point |
---|
252 | +--------------------------------------------------------------------------*/ |
---|
253 | rtems_device_driver |
---|
254 | console_close(rtems_device_major_number major, |
---|
255 | rtems_device_minor_number minor, |
---|
256 | void *arg) |
---|
257 | { |
---|
258 | rtems_device_driver res = RTEMS_SUCCESSFUL; |
---|
259 | |
---|
260 | res = rtems_termios_close (arg); |
---|
261 | |
---|
262 | return res; |
---|
263 | } /* console_close */ |
---|
264 | |
---|
265 | |
---|
266 | /*-------------------------------------------------------------------------+ |
---|
267 | | Console device driver READ entry point. |
---|
268 | +--------------------------------------------------------------------------+ |
---|
269 | | Read characters from the I/O console. We only have stdin. |
---|
270 | +--------------------------------------------------------------------------*/ |
---|
271 | rtems_device_driver |
---|
272 | console_read(rtems_device_major_number major, |
---|
273 | rtems_device_minor_number minor, |
---|
274 | void *arg) |
---|
275 | { |
---|
276 | |
---|
277 | return rtems_termios_read (arg); |
---|
278 | } /* console_read */ |
---|
279 | |
---|
280 | |
---|
281 | /*-------------------------------------------------------------------------+ |
---|
282 | | Console device driver WRITE entry point. |
---|
283 | +--------------------------------------------------------------------------+ |
---|
284 | | Write characters to the I/O console. Stderr and stdout are the same. |
---|
285 | +--------------------------------------------------------------------------*/ |
---|
286 | rtems_device_driver |
---|
287 | console_write(rtems_device_major_number major, |
---|
288 | rtems_device_minor_number minor, |
---|
289 | void * arg) |
---|
290 | { |
---|
291 | |
---|
292 | return rtems_termios_write (arg); |
---|
293 | |
---|
294 | } /* console_write */ |
---|
295 | |
---|
296 | |
---|
297 | |
---|
298 | /* |
---|
299 | * Handle ioctl request. |
---|
300 | */ |
---|
301 | rtems_device_driver |
---|
302 | console_control(rtems_device_major_number major, |
---|
303 | rtems_device_minor_number minor, |
---|
304 | void * arg |
---|
305 | ) |
---|
306 | { |
---|
307 | return rtems_termios_ioctl (arg); |
---|
308 | } |
---|
309 | |
---|
310 | static int |
---|
311 | conSetAttr(int minor, const struct termios *t) |
---|
312 | { |
---|
313 | int baud; |
---|
314 | |
---|
315 | switch (t->c_cflag & CBAUD) |
---|
316 | { |
---|
317 | case B50: |
---|
318 | baud = 50; |
---|
319 | break; |
---|
320 | case B75: |
---|
321 | baud = 75; |
---|
322 | break; |
---|
323 | case B110: |
---|
324 | baud = 110; |
---|
325 | break; |
---|
326 | case B134: |
---|
327 | baud = 134; |
---|
328 | break; |
---|
329 | case B150: |
---|
330 | baud = 150; |
---|
331 | break; |
---|
332 | case B200: |
---|
333 | baud = 200; |
---|
334 | break; |
---|
335 | case B300: |
---|
336 | baud = 300; |
---|
337 | break; |
---|
338 | case B600: |
---|
339 | baud = 600; |
---|
340 | break; |
---|
341 | case B1200: |
---|
342 | baud = 1200; |
---|
343 | break; |
---|
344 | case B1800: |
---|
345 | baud = 1800; |
---|
346 | break; |
---|
347 | case B2400: |
---|
348 | baud = 2400; |
---|
349 | break; |
---|
350 | case B4800: |
---|
351 | baud = 4800; |
---|
352 | break; |
---|
353 | case B9600: |
---|
354 | baud = 9600; |
---|
355 | break; |
---|
356 | case B19200: |
---|
357 | baud = 19200; |
---|
358 | break; |
---|
359 | case B38400: |
---|
360 | baud = 38400; |
---|
361 | break; |
---|
362 | case B57600: |
---|
363 | baud = 57600; |
---|
364 | break; |
---|
365 | case B115200: |
---|
366 | baud = 115200; |
---|
367 | break; |
---|
368 | default: |
---|
369 | baud = 0; |
---|
370 | rtems_fatal_error_occurred (RTEMS_INTERNAL_ERROR); |
---|
371 | return 0; |
---|
372 | } |
---|
373 | |
---|
374 | BSP_uart_set_baud(BSPConsolePort, baud); |
---|
375 | |
---|
376 | return 0; |
---|
377 | } |
---|
378 | |
---|
379 | |
---|
380 | |
---|