Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:43

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  *  Console driver for Lattice Mico32 (lm32).
0005  */
0006 
0007 /*
0008  *  COPYRIGHT (c) 1989-1999.
0009  *  On-Line Applications Research Corporation (OAR).
0010  *
0011  * Redistribution and use in source and binary forms, with or without
0012  * modification, are permitted provided that the following conditions
0013  * are met:
0014  * 1. Redistributions of source code must retain the above copyright
0015  *    notice, this list of conditions and the following disclaimer.
0016  * 2. Redistributions in binary form must reproduce the above copyright
0017  *    notice, this list of conditions and the following disclaimer in the
0018  *    documentation and/or other materials provided with the distribution.
0019  *
0020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0021  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0022  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0023  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0024  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0025  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0026  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0027  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0028  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0029  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0030  * POSSIBILITY OF SUCH DAMAGE.
0031  *
0032  *  Jukka Pietarinen <jukka.pietarinen@mrf.fi>, 2008,
0033  *  Micro-Research Finland Oy
0034  */
0035 
0036 #define NO_BSP_INIT
0037 
0038 #include <bsp.h>
0039 #include <rtems/bspIo.h>
0040 #include <rtems/libio.h>
0041 #include <rtems/console.h>
0042 
0043 /*  console_initialize
0044  *
0045  *  This routine initializes the console IO driver.
0046  */
0047 rtems_device_driver console_initialize(
0048   rtems_device_major_number  major,
0049   rtems_device_minor_number  minor,
0050   void                      *arg
0051 )
0052 {
0053   rtems_status_code status;
0054 
0055   printk("console_initialize\n");
0056 
0057   status = rtems_io_register_name(
0058     "/dev/console",
0059     major,
0060     (rtems_device_minor_number) 0
0061   );
0062 
0063   if (status != RTEMS_SUCCESSFUL)
0064     rtems_fatal_error_occurred(status);
0065 
0066   return RTEMS_SUCCESSFUL;
0067 }
0068 
0069 /*  inbyte
0070  *
0071  *  This routine reads a character from the SOURCE.
0072  */
0073 static int inbyte( void )
0074 {
0075   /*
0076    *  If polling, wait until a character is available.
0077    */
0078   return BSP_uart_polled_read();
0079 }
0080 
0081 /*  outbyte
0082  *
0083  *  This routine transmits a character out the SOURCE.  It may support
0084  *  XON/XOFF flow control.
0085  */
0086 static void outbyte(
0087   char ch
0088 )
0089 {
0090   /*
0091    *  If polling, wait for the transmitter to be ready.
0092    *  Check for flow control requests and process.
0093    *  Then output the character.
0094    */
0095 
0096   BSP_uart_polled_write(ch);
0097 }
0098 
0099 /*
0100  *  Open entry point
0101  */
0102 rtems_device_driver console_open(
0103   rtems_device_major_number major,
0104   rtems_device_minor_number minor,
0105   void                    * arg
0106 )
0107 {
0108   return RTEMS_SUCCESSFUL;
0109 }
0110 
0111 /*
0112  *  Close entry point
0113  */
0114 rtems_device_driver console_close(
0115   rtems_device_major_number major,
0116   rtems_device_minor_number minor,
0117   void                    * arg
0118 )
0119 {
0120   return RTEMS_SUCCESSFUL;
0121 }
0122 
0123 /*
0124  * read bytes from the serial port. We only have stdin.
0125  */
0126 rtems_device_driver console_read(
0127   rtems_device_major_number major,
0128   rtems_device_minor_number minor,
0129   void                    * arg
0130 )
0131 {
0132   rtems_libio_rw_args_t *rw_args;
0133   char *buffer;
0134   int maximum;
0135   int count = 0;
0136 
0137   rw_args = (rtems_libio_rw_args_t *) arg;
0138 
0139   buffer = rw_args->buffer;
0140   maximum = rw_args->count;
0141 
0142   for (count = 0; count < maximum; count++) {
0143     buffer[ count ] = inbyte();
0144     if (buffer[ count ] == '\n' || buffer[ count ] == '\r') {
0145       buffer[ count++ ]  = '\n';
0146       break;
0147     }
0148   }
0149 
0150   rw_args->bytes_moved = count;
0151   return (count >= 0) ? RTEMS_SUCCESSFUL : RTEMS_UNSATISFIED;
0152 }
0153 
0154 /*
0155  * write bytes to the serial port. Stdout and stderr are the same.
0156  */
0157 rtems_device_driver console_write(
0158   rtems_device_major_number major,
0159   rtems_device_minor_number minor,
0160   void                    * arg
0161 )
0162 {
0163   int count;
0164   int maximum;
0165   rtems_libio_rw_args_t *rw_args;
0166   char *buffer;
0167 
0168   rw_args = (rtems_libio_rw_args_t *) arg;
0169 
0170   buffer = rw_args->buffer;
0171   maximum = rw_args->count;
0172 
0173   for (count = 0; count < maximum; count++) {
0174     if ( buffer[ count ] == '\n') {
0175       outbyte('\r');
0176     }
0177     outbyte( buffer[ count ] );
0178   }
0179 
0180   rw_args->bytes_moved = maximum;
0181   return 0;
0182 }
0183 
0184 /*
0185  *  IO Control entry point
0186  */
0187 rtems_device_driver console_control(
0188   rtems_device_major_number major,
0189   rtems_device_minor_number minor,
0190   void                    * arg
0191 )
0192 {
0193   return RTEMS_SUCCESSFUL;
0194 }
0195 
0196 BSP_output_char_function_type BSP_output_char = BSP_uart_polled_write;
0197 BSP_polling_getchar_function_type BSP_poll_char = BSP_uart_polled_read;