Back to home page

LXR

 
 

    


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

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  * Copyright (C) 2012, 2015 embedded brains GmbH & Co. KG
0005  *
0006  * Redistribution and use in source and binary forms, with or without
0007  * modification, are permitted provided that the following conditions
0008  * are met:
0009  * 1. Redistributions of source code must retain the above copyright
0010  *    notice, this list of conditions and the following disclaimer.
0011  * 2. Redistributions in binary form must reproduce the above copyright
0012  *    notice, this list of conditions and the following disclaimer in the
0013  *    documentation and/or other materials provided with the distribution.
0014  *
0015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0018  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0025  * POSSIBILITY OF SUCH DAMAGE.
0026  */
0027 
0028 /*
0029  * Console driver for Lauterbach Trace32 Simulator.  The implementation is
0030  * based on the example in "demo/powerpc/etc/terminal/terminal_mpc85xx.cmm" in
0031  * the Trace32 system directory.
0032  */
0033 
0034 #include <rtems/bspIo.h>
0035 #include <rtems/console.h>
0036 #include <rtems/termiostypes.h>
0037 
0038 RTEMS_SECTION(".rtemsrwset.t32") volatile unsigned char messagebufferin[256];
0039 
0040 RTEMS_SECTION(".rtemsrwset.t32") volatile unsigned char messagebufferout[256];
0041 
0042 typedef struct {
0043   rtems_termios_device_context base;
0044   int input_size;
0045   int input_index;
0046 } t32_console_context;
0047 
0048 static t32_console_context t32_console_instance;
0049 
0050 static bool t32_console_first_open(
0051   rtems_termios_tty *tty,
0052   rtems_termios_device_context *base,
0053   struct termios *term,
0054   rtems_libio_open_close_args_t *args
0055 )
0056 {
0057   rtems_termios_set_initial_baud(tty, 115200);
0058 
0059   return true;
0060 }
0061 
0062 static int t32_console_read_polled(rtems_termios_device_context *base)
0063 {
0064   t32_console_context *ctx = (t32_console_context *) base;
0065   int c;
0066 
0067   if (ctx->input_size == 0) {
0068     int new_bufsize = messagebufferin[0];
0069 
0070     if (new_bufsize != 0) {
0071       ctx->input_size = new_bufsize;
0072       ctx->input_index = 0;
0073     } else {
0074       return -1;
0075     }
0076   }
0077 
0078   c = messagebufferin[4 + ctx->input_index];
0079 
0080   ++ctx->input_index;
0081   if (ctx->input_index >= ctx->input_size) {
0082     messagebufferin[0] = 0;
0083     ctx->input_size = 0;
0084   }
0085 
0086   return c;
0087 }
0088 
0089 static void t32_console_write_char_polled(char c)
0090 {
0091   while (messagebufferout[0] != 0) {
0092     /* Wait for ready */
0093   }
0094 
0095   messagebufferout[4] = (unsigned char) c;
0096   messagebufferout[0] = 1;
0097 }
0098 
0099 static void t32_console_write_polled(
0100   rtems_termios_device_context *base,
0101   const char *s,
0102   size_t n
0103 )
0104 {
0105   size_t i;
0106 
0107   for (i = 0; i < n; ++i) {
0108     t32_console_write_char_polled(s[i]);
0109   }
0110 }
0111 
0112 const rtems_termios_device_handler t32_console_handler = {
0113   .first_open = t32_console_first_open,
0114   .poll_read = t32_console_read_polled,
0115   .write = t32_console_write_polled,
0116   .mode = TERMIOS_POLLED
0117 };
0118 
0119 rtems_device_driver console_initialize(
0120   rtems_device_major_number major,
0121   rtems_device_minor_number minor,
0122   void *arg
0123 )
0124 {
0125   t32_console_context *ctx = &t32_console_instance;
0126 
0127   rtems_termios_initialize();
0128   rtems_termios_device_context_initialize(&ctx->base, "T32 Console");
0129   rtems_termios_device_install(
0130     CONSOLE_DEVICE_NAME,
0131     &t32_console_handler,
0132     NULL,
0133     &ctx->base
0134   );
0135 
0136   return RTEMS_SUCCESSFUL;
0137 }
0138 
0139 BSP_output_char_function_type BSP_output_char = t32_console_write_char_polled;
0140 
0141 BSP_polling_getchar_function_type BSP_poll_char = NULL;