source: rtems/c/src/lib/libbsp/m68k/gen68360/console/console.c @ 8f35817

4.104.114.84.95
Last change on this file since 8f35817 was 8f35817, checked in by Joel Sherrill <joel.sherrill@…>, on Sep 15, 1997 at 6:52:26 PM

eliminated potential for overfilling buffer on read

  • Property mode set to 100644
File size: 6.2 KB
Line 
1/*
2 * Initialize SMC1 for console IO.
3 *
4 * Based on the `gen68302' board support package, and covered by the
5 * original distribution terms.
6 *
7 * W. Eric Norum
8 * Saskatchewan Accelerator Laboratory
9 * University of Saskatchewan
10 * Saskatoon, Saskatchewan, CANADA
11 * eric@skatter.usask.ca
12 *
13 *  $Id$
14 */
15
16/*
17 *
18 *  COPYRIGHT (c) 1989-1997.
19 *  On-Line Applications Research Corporation (OAR).
20 *  Copyright assigned to U.S. Government, 1994.
21 *
22 *  The license and distribution terms for this file may in
23 *  the file LICENSE in this distribution or at
24 *  http://www.OARcorp.com/rtems/license.html.
25 */
26
27#define GEN68360_INIT
28
29#include <bsp.h>
30#include <rtems/libio.h>
31#include "m68360.h"
32
33/*  console_initialize
34 *
35 *  This routine initializes the console IO driver.
36 *
37 *  Input parameters: NONE
38 *
39 *  Output parameters:  NONE
40 *
41 *  Return values:
42 */
43
44/*
45 * I/O buffers can be in ordindary RAM
46 */
47static volatile char rxBuf, txBuf;
48static volatile m360BufferDescriptor_t *consoleRxBd, *consoleTxBd;
49
50rtems_device_driver console_initialize(
51  rtems_device_major_number  major,
52  rtems_device_minor_number  minor,
53  void                      *arg
54)
55{
56        rtems_status_code status;
57
58        /*
59         * Allocate buffer descriptors
60         */
61        consoleRxBd = M360AllocateBufferDescriptors (1);
62        consoleTxBd = M360AllocateBufferDescriptors (1);
63
64        /*
65         * Configure port B pins to enable SMTXD1 and SMRXD1 pins
66         */
67        m360.pbpar |=  0xC0;
68        m360.pbdir &= ~0xC0;
69        m360.pbodr &= ~0xC0;
70
71        /*
72         * Set up BRG1 (9,600 baud)
73         */
74        m360.brgc1 = M360_BRG_RST;
75        m360.brgc1 = M360_BRG_EN | M360_BRG_EXTC_BRGCLK | M360_BRG_9600;
76
77        /*
78         * Put SMC1 in NMSI mode, connect SMC1 to BRG1
79         */
80        m360.simode |= M360_SI_SMC1_BRG1;
81         
82        /*
83         * Set up SMC1 parameter RAM common to all protocols
84         */
85        m360.smc1p.rbase = (char *)consoleRxBd - (char *)&m360;
86        m360.smc1p.tbase = (char *)consoleTxBd - (char *)&m360;
87        m360.smc1p.rfcr = M360_RFCR_MOT | M360_RFCR_DMA_SPACE;
88        m360.smc1p.tfcr = M360_TFCR_MOT | M360_TFCR_DMA_SPACE;
89        m360.smc1p.mrblr = 1;
90         
91        /*
92         * Set up SMC1 parameter RAM UART-specific parameters
93         */
94        m360.smc1p.un.uart.max_idl = 0;
95        m360.smc1p.un.uart.brklen = 0;
96        m360.smc1p.un.uart.brkec = 0;
97        m360.smc1p.un.uart.brkcr = 0;
98         
99        /*
100         * Set up the Receive Buffer Descriptor
101         */
102        consoleRxBd->status = M360_BD_EMPTY | M360_BD_WRAP;
103        consoleRxBd->length = 0;
104        consoleRxBd->buffer = &rxBuf;
105         
106        /*
107         * Setup the Transmit Buffer Descriptor
108         */
109        consoleTxBd->length = 1;
110        consoleTxBd->status = M360_BD_WRAP;
111        consoleTxBd->buffer = &txBuf;
112         
113        /*
114         * Set up SMC1 general and protocol-specific mode registers
115         */
116        m360.smc1.smce = ~0;    /* Clear any pending events */
117        m360.smc1.smcm = 0;     /* Mask all interrupt/event sources */
118        m360.smc1.smcmr = M360_SMCMR_CLEN(9) | M360_SMCMR_SM_UART;
119
120        /*
121         * Send "Init parameters" command
122         */
123        M360ExecuteRISC (M360_CR_OP_INIT_RX_TX | M360_CR_CHAN_SMC1);
124
125        /*
126         * Enable receiver and transmitter
127         */
128        m360.smc1.smcmr |= M360_SMCMR_TEN | M360_SMCMR_REN;
129
130        status = rtems_io_register_name(
131                                "/dev/console",
132                                    major,
133                                    (rtems_device_minor_number)0);
134        if (status != RTEMS_SUCCESSFUL)
135                rtems_fatal_error_occurred(status);
136        return RTEMS_SUCCESSFUL;
137}
138
139/*  is_character_ready
140 *
141 *  Check to see if a character is available on the console port.  If so,
142 *  then return a TRUE (along with the character).  Otherwise return FALSE.
143 *
144 *  Input parameters:   pointer to location in which to return character
145 *
146 *  Output parameters:  character (if available)
147 *
148 *  Return values:      TRUE - character available
149 *                      FALSE - no character available
150 */
151
152rtems_boolean is_character_ready(
153  char *ch                              /* -> character  */
154)
155{
156        if (consoleRxBd->status & M360_BD_EMPTY)
157                return FALSE;
158        *ch = rxBuf;
159        consoleRxBd->status = M360_BD_EMPTY | M360_BD_WRAP;
160        return TRUE;
161}
162
163
164/*  inbyte
165 *
166 *  Receive a character from the console port
167 *
168 *  Input parameters:   NONE
169 *
170 *  Output parameters:  NONE
171 *
172 *  Return values:      character read
173 */
174
175char inbyte( void )
176{
177    char ch;
178
179    while (is_character_ready (&ch) == FALSE)
180        continue;
181    return ch;
182}
183
184
185/*  outbyte
186 *
187 *  Transmit a character to the console serial port
188 *
189 *  Input parameters:
190 *    ch  - character to be transmitted
191 *
192 *  Output parameters:  NONE
193 */
194
195void outbyte(
196  char ch
197)
198{
199        if (ch == '\n')
200                outbyte('\r');
201        while (consoleTxBd->status & M360_BD_READY)
202                continue;
203        txBuf = ch;
204        consoleTxBd->status = M360_BD_READY | M360_BD_WRAP;
205}
206
207/*
208 *  Open entry point
209 */
210
211rtems_device_driver console_open(
212  rtems_device_major_number major,
213  rtems_device_minor_number minor,
214  void                    * arg
215)
216{
217  return RTEMS_SUCCESSFUL;
218}
219 
220/*
221 *  Close entry point
222 */
223
224rtems_device_driver console_close(
225  rtems_device_major_number major,
226  rtems_device_minor_number minor,
227  void                    * arg
228)
229{
230  return RTEMS_SUCCESSFUL;
231}
232
233/*
234 * read bytes from the serial port. We only have stdin.
235 */
236
237rtems_device_driver console_read(
238  rtems_device_major_number major,
239  rtems_device_minor_number minor,
240  void                    * arg
241)
242{
243  rtems_libio_rw_args_t *rw_args;
244  char *buffer;
245  int maximum;
246  int count = 0;
247 
248  rw_args = (rtems_libio_rw_args_t *) arg;
249
250  buffer = rw_args->buffer;
251  maximum = rw_args->count;
252
253  for (count = 0; count < maximum; count++) {
254    buffer[ count ] = inbyte();
255    if (buffer[ count ] == '\n' || buffer[ count ] == '\r') {
256      buffer[ count++ ]  = '\n';
257      break;
258    }
259  }
260
261  rw_args->bytes_moved = count;
262  return (count >= 0) ? RTEMS_SUCCESSFUL : RTEMS_UNSATISFIED;
263}
264
265/*
266 * write bytes to the serial port. Stdout and stderr are the same.
267 */
268
269rtems_device_driver console_write(
270  rtems_device_major_number major,
271  rtems_device_minor_number minor,
272  void                    * arg
273)
274{
275  int count;
276  int maximum;
277  rtems_libio_rw_args_t *rw_args;
278  char *buffer;
279
280  rw_args = (rtems_libio_rw_args_t *) arg;
281
282  buffer = rw_args->buffer;
283  maximum = rw_args->count;
284
285  for (count = 0; count < maximum; count++) {
286    if ( buffer[ count ] == '\n') {
287      outbyte('\r');
288    }
289    outbyte( buffer[ count ] );
290  }
291
292  rw_args->bytes_moved = maximum;
293  return 0;
294}
295
296/*
297 *  IO Control entry point
298 */
299
300rtems_device_driver console_control(
301  rtems_device_major_number major,
302  rtems_device_minor_number minor,
303  void                    * arg
304)
305{
306  return RTEMS_SUCCESSFUL;
307}
Note: See TracBrowser for help on using the repository browser.