ESOS32
ESOSOn32-bitProcessors
esos_pc_stdio.c
Go to the documentation of this file.
1 /*
2  * "Copyright (c) 2019 J. W. Bruce ("AUTHOR(S)")"
3  * All rights reserved.
4  * (J. W. Bruce, jwbruce_AT_tntech.edu, Tennessee Tech University)
5  *
6  * Permission to use, copy, modify, and distribute this software and its
7  * documentation for any purpose, without fee, and without written agreement is
8  * hereby granted, provided that the above copyright notice, the following
9  * two paragraphs and the authors appear in all copies of this software.
10  *
11  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
12  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
13  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
14  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15  *
16  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
17  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
19  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
20  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
21  *
22  * Please maintain this header in its entirety when copying/modifying
23  * these files.
24  *
25  *
26  */
27 
28 
34 // Documentation for this file. If the \file tag isn't present,
35 // this file won't be documented.
45 /*** I N C L U D E S ****
46 *********************************************/
47 #include "esos_pc_stdio.h"
48 #ifdef USE_NCURSES
49 #include <ncurses.h>
50 #else
51 #include <stdio.h>
52 #include <sys/select.h>
53 #include <termios.h>
54 #include <unistd.h>
55 #endif
56 
57 
58 
59 //#define USE_NCURSES
60 
61 /*** G L O B A L S *************************************************/
62 struct termios stored_settings;
63 
64 /*** T H E C O D E *************************************************/
65 
66 /*********************************************************
67  * Public functions intended to be called by other files *
68  *********************************************************/
69 void __esos_hw_signal_start_tx(void) {
70  while (__st_TxBuffer.u16_Head != __st_TxBuffer.u16_Tail) {
71  __st_TxBuffer.u16_Tail++; //increment tail pointer
72  if (__st_TxBuffer.u16_Tail == ESOS_SERIAL_IN_EP_SIZE)
73  __st_TxBuffer.u16_Tail = 0; //wrap if needed
74  //transfer character from software buffer to transmit buffer
75 #ifdef USE_NCURSES
76  waddch( __st_TxBuffer.pau8_Data[__st_TxBuffer.u16_Tail] );
77 #else
78  printf("%c", __st_TxBuffer.pau8_Data[__st_TxBuffer.u16_Tail]);
79  // make the stdout do its thing right away
80  fflush(stdout);
81 #endif
82  }
83 }
84 
85 void __esos_hw_signal_stop_tx(void) {
86 
87 }
88 
89 ESOS_USER_TASK( __Linux_check_keyboard ) {
90  static uint8_t u8_c;
91 
93  set_keypress();
94  while (TRUE) {
95  ESOS_TASK_WAIT_UNTIL( kbhit() );
96  u8_c = getchar(); //read character
97  __st_RxBuffer.u16_Head++; //increment head pointer
98  if (__st_RxBuffer.u16_Head == ESOS_SERIAL_OUT_EP_SIZE)
99  __st_RxBuffer.u16_Head = 0; //wrap if needed
100 
101  __st_RxBuffer.pau8_Data[__st_RxBuffer.u16_Head] = u8_c; //place in buffer
102  } // endof while(TRUE)
103  ESOS_TASK_END();
104 } // endof TASK
105 
106 
107 /*
108 ** POSIX systems do NOT have a kbhit() function in their stdio
109 ** libraries. So we will write our own here. This function
110 ** returns TRUE when there is some keypress, else FALSE. So,
111 ** we can poll or wait on kbhit(). When it is true, we can
112 ** call the normal getch()
113 */
114 uint8_t kbhit(void) {
115  struct timeval tv;
116  fd_set read_fd;
117 
118  tv.tv_sec = 0;
119  tv.tv_usec = 0;
120  FD_ZERO(&read_fd);
121  FD_SET(0,&read_fd);
122 
123  if (select(1, &read_fd, NULL, NULL, &tv) == -1)
124  return FALSE;
125 
126  if (FD_ISSET(0,&read_fd))
127  return TRUE;
128 
129  return FALSE;
130 } //end kbhit())
131 
132 
133 void set_keypress(void) {
134  struct termios new_settings;
135 
136  tcgetattr(0,&stored_settings);
137  new_settings = stored_settings;
138 
139  /* Disable canonical mode, set buffer size to 1 byte, turn OFF echo */
140  new_settings.c_lflag &= (~ICANON);
141  new_settings.c_cc[VTIME] = 0;
142  new_settings.c_cc[VMIN] = 1;
143  new_settings.c_lflag &= (~ECHO);
144  tcsetattr(0,TCSANOW,&new_settings);
145  return;
146 }
147 
148 void reset_keypress(void) {
149  tcsetattr(0,TCSANOW,&stored_settings);
150  return;
151 }
152 
153 
154 
155 /* ########################################################################### */
156 
157 
158 /******************************************************************************
159  * Function: void _esos_hw_InitSerialUart( void )
160  *
161  * PreCondition: None
162  *
163  * Input: None
164  *
165  * Output: ptr to ESOS_COMM_BUFF_DSC structure with initialized ptrs
166  *
167  * Side Effects: Turns on USART hardware
168  *
169  *****************************************************************************/
170 void __esos_hw_InitCommSystem(void) {
171  // Init the NCURSES system instead of the serial port....
172  uint8_t u8_char;
173 
174 #if 0
175  initscr(); /* Start curses mode */
176  raw(); /* Line buffering disabled */
177  keypad(stdscr, TRUE); /* We get F1, F2 etc.. */
178  noecho(); /* Don't echo() while we do getch */
179 
180  printw("Hello from ESOS -- the PC/Linux version w/ NCURSES\n");
181  refresh(); /* Print it on to the real screen */
182  getch(); /* Wait for user input */
183 #else
184  printf ("Hello from ESOS -- the PC/Linux version\n");
185 #endif
186 
187  // Since the PC does not support background interrupts (easily),
188  // we will just mimic that by creating an ESOS task to read the
189  // from the keyboard. The transmits will be done with printf
190  // and occur immediately.
191  //
192  // NOTE: When this task runs, the KDE debugger (kdbg) does not
193  // run very well. When stepping, try to structure the application
194  // to run without input from stdin and COMMENT OUT THE FOLLOWING LINE!
195  esos_RegisterTask( __Linux_check_keyboard );
196 
197 } // end __esos_hw_InitCommSystem()
198 
199 
200 /******************************************************************************
201  * Function: uint8_t esos_GetCommSystemMaxInDataLen(void)
202  *
203  * PreCondition: None.
204  *
205  * Input: None
206  *
207  * Output: the maximum number of uint8_ts that the comm system will
208  * receive in a single buffer transfer from the host -- OR --
209  * in the case of single uint8_t xfers (like RS232), the maximum
210  * number of uint8_ts that can be RX-ed before the buffers
211  * overflow
212  *
213  * Side Effects: None
214  *
215  * Overview: A way for a run-time determination of the maximum buffer
216  * size that the user can can expect. This number is
217  * actually hard-coded in the USB CDC header file, but this
218  * method will allow the user code to be more generic, if
219  * it chooses to be.
220  *
221  *****************************************************************************/
222 uint8_t esos_GetCommSystemMaxInDataLen(void) {
223  return ESOS_SERIAL_OUT_EP_SIZE;
224 } //end esos_GetCommSystemMaxInDataLen()
225 
226 /******************************************************************************
227  * Function: uint8_t esos_GetCommSystemMaxOutDataLen(void)
228  *
229  * PreCondition: None.
230  *
231  * Input: None
232  *
233  * Output: the maximum number of uint8_ts that the comm system will
234  * transfer back to the host in a single buffer -- OR --
235  * in the case of singe uint8_t xfers (like RS232), the maximum
236  * number of uint8_ts in the output buffer before overflow
237  *
238  * Side Effects: None
239  *
240  * Overview: A way for a run-time determination of the maximum buffer
241  * size that the user can can send efficiently. The USB system
242  * will send a bigger buffer than getUSBCdcTxMax() size, but
243  * will do so in several smaller getUSBCdcTxMax()-sized chunks.
244  *
245  * This number is actually hard-coded in the USB CDC header file,
246  * but this method will allow the user code to be more generic,
247  * if it chooses to be.
248  *
249  *****************************************************************************/
250 uint8_t esos_GetCommSystemMaxOutDataLen(void) {
251  return ESOS_SERIAL_IN_EP_SIZE;
252 } //end esos_GetCommSystemMaxOutDataLen()
253 
254 /******************************************************************************
255  * Function: uint8_t _esos_hw_GetUartVersion(void)
256  *
257  * PreCondition: None.
258  *
259  * Input: None
260  *
261  * Output: Return the version number of the MSU Bulk CDC driver firmware
262  * currently running.
263  * The most-significant bit denotes we're running USB
264  * The most-significant nibble is the major revision number
265  * The least-significant nibble is the minor revision number
266  *
267  * Side Effects: None
268  *
269  *****************************************************************************/
270 uint8_t _esos_hw_GetSerialUartVersion(void) {
271  return ESOS_COMM_SYS_SERIAL_REV;
272 } //end _esos_hw_GetUartVersion()
ESOS_TASK_WAIT_UNTIL
#define ESOS_TASK_WAIT_UNTIL(condition)
Definition: esos_task.h:335
ESOS_TASK_END
#define ESOS_TASK_END()
Definition: esos_task.h:271
esos_GetCommSystemMaxOutDataLen
uint8_t esos_GetCommSystemMaxOutDataLen(void)
Definition: esos_hwxxxx_rs232.c:222
TRUE
@ TRUE
Definition: all_generic.h:410
FALSE
@ FALSE
Definition: all_generic.h:408
ESOS_USER_TASK
#define ESOS_USER_TASK(taskname)
Definition: esos_task.h:227
ESOS_TASK_BEGIN
#define ESOS_TASK_BEGIN()
Definition: esos_task.h:259
esos_RegisterTask
ESOS_TASK_HANDLE esos_RegisterTask(uint8_t(*taskname)(ESOS_TASK_HANDLE pstTask))
Definition: esos.c:126
esos_GetCommSystemMaxInDataLen
uint8_t esos_GetCommSystemMaxInDataLen(void)
Definition: esos_hwxxxx_rs232.c:194