1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * This file contains a test fixture termios device driver |
---|
5 | */ |
---|
6 | |
---|
7 | /* |
---|
8 | * COPYRIGHT (c) 1989-2012. |
---|
9 | * On-Line Applications Research Corporation (OAR). |
---|
10 | * |
---|
11 | * The license and distribution terms for this file may be |
---|
12 | * found in the file LICENSE in this distribution or at |
---|
13 | * http://www.rtems.com/license/LICENSE. |
---|
14 | */ |
---|
15 | |
---|
16 | #ifdef HAVE_CONFIG_H |
---|
17 | #include "config.h" |
---|
18 | #endif |
---|
19 | |
---|
20 | #include "tmacros.h" |
---|
21 | #include <rtems/libio.h> |
---|
22 | #include <stdlib.h> |
---|
23 | #include <termios.h> |
---|
24 | #include <rtems/termiostypes.h> |
---|
25 | #include <rtems/dumpbuf.h> |
---|
26 | #include "termios_testdriver_intr.h" |
---|
27 | |
---|
28 | /* forward declarations to avoid warnings */ |
---|
29 | void termios_test_driver_wait_for_tx_to_complete(void); |
---|
30 | rtems_timer_service_routine Rx_ISR( |
---|
31 | rtems_id ignored_id, |
---|
32 | void *ignored_address |
---|
33 | ); |
---|
34 | rtems_timer_service_routine Tx_ISR( |
---|
35 | rtems_id ignored_id, |
---|
36 | void *ignored_address |
---|
37 | ); |
---|
38 | ssize_t termios_test_driver_write_helper( |
---|
39 | int port, |
---|
40 | const char *buf, |
---|
41 | size_t len |
---|
42 | ); |
---|
43 | int termios_test_driver_set_attributes( |
---|
44 | int minor, |
---|
45 | const struct termios *t |
---|
46 | ); |
---|
47 | #if defined(TASK_DRIVEN) |
---|
48 | int termios_test_driver_inbyte_nonblocking(int port); |
---|
49 | #endif |
---|
50 | |
---|
51 | /* global variables */ |
---|
52 | rtems_id Rx_Timer; |
---|
53 | rtems_id Tx_Timer; |
---|
54 | |
---|
55 | #define TX_MAX 1024 |
---|
56 | uint8_t Tx_Buffer[TX_MAX]; |
---|
57 | int Tx_Index = 0; |
---|
58 | struct rtems_termios_tty *Ttyp; |
---|
59 | |
---|
60 | void termios_test_driver_wait_for_tx_to_complete(void) |
---|
61 | { |
---|
62 | rtems_task_wake_after( 2 * rtems_clock_get_ticks_per_second() ); |
---|
63 | } |
---|
64 | |
---|
65 | void termios_test_driver_dump_tx(const char *c) |
---|
66 | { |
---|
67 | termios_test_driver_wait_for_tx_to_complete(); |
---|
68 | |
---|
69 | printf( "%s %d characters\n", c, Tx_Index ); |
---|
70 | rtems_print_buffer( Tx_Buffer, Tx_Index ); |
---|
71 | Tx_Index = 0; |
---|
72 | } |
---|
73 | |
---|
74 | const uint8_t *Rx_Buffer; |
---|
75 | int Rx_Index = 0; |
---|
76 | int Rx_Length = 0; |
---|
77 | bool Rx_FirstTime = true; |
---|
78 | bool Rx_EnqueueNow = false; |
---|
79 | |
---|
80 | #if defined(TASK_DRIVEN) |
---|
81 | int termios_test_driver_inbyte_nonblocking( int port ) |
---|
82 | { |
---|
83 | if ( Rx_FirstTime == true ) { |
---|
84 | Rx_FirstTime = false; |
---|
85 | return -1; |
---|
86 | } |
---|
87 | if ( Rx_Index >= Rx_Length ) |
---|
88 | return -1; |
---|
89 | return Rx_Buffer[ Rx_Index++ ]; |
---|
90 | } |
---|
91 | #endif |
---|
92 | |
---|
93 | rtems_timer_service_routine Rx_ISR( |
---|
94 | rtems_id ignored_id, |
---|
95 | void *ignored_address |
---|
96 | ) |
---|
97 | { |
---|
98 | uint8_t ch; |
---|
99 | |
---|
100 | if ( Rx_Index >= Rx_Length ) |
---|
101 | return; |
---|
102 | |
---|
103 | ch = Rx_Buffer[ Rx_Index++ ]; |
---|
104 | rtems_termios_enqueue_raw_characters (Ttyp, (char *)&ch, 1); |
---|
105 | #if defined(TASK_DRIVEN) |
---|
106 | rtems_termios_rxirq_occured(Ttyp); |
---|
107 | #endif |
---|
108 | |
---|
109 | (void) rtems_timer_fire_after( Rx_Timer, 10, Rx_ISR, NULL ); |
---|
110 | } |
---|
111 | |
---|
112 | rtems_timer_service_routine Tx_ISR( |
---|
113 | rtems_id ignored_id, |
---|
114 | void *ignored_address |
---|
115 | ) |
---|
116 | { |
---|
117 | rtems_termios_dequeue_characters (Ttyp, 1); |
---|
118 | |
---|
119 | (void) rtems_timer_fire_after( Tx_Timer, 10, Tx_ISR, NULL ); |
---|
120 | } |
---|
121 | |
---|
122 | void termios_test_driver_set_rx_enqueue_now( |
---|
123 | bool value |
---|
124 | ) |
---|
125 | { |
---|
126 | Rx_EnqueueNow = value; |
---|
127 | } |
---|
128 | |
---|
129 | void termios_test_driver_set_rx( |
---|
130 | const void *p, |
---|
131 | size_t len |
---|
132 | ) |
---|
133 | { |
---|
134 | Rx_Buffer = p; |
---|
135 | Rx_Length = len; |
---|
136 | Rx_Index = 0; |
---|
137 | |
---|
138 | if ( Rx_EnqueueNow == false) { |
---|
139 | (void) rtems_timer_fire_after( Rx_Timer, 10, Rx_ISR, NULL ); |
---|
140 | return; |
---|
141 | } |
---|
142 | |
---|
143 | do { |
---|
144 | uint8_t ch; |
---|
145 | ch = Rx_Buffer[ Rx_Index++ ]; |
---|
146 | rtems_termios_enqueue_raw_characters (Ttyp, (char *)&ch, 1); |
---|
147 | } while (Rx_Index < Rx_Length ); |
---|
148 | } |
---|
149 | |
---|
150 | ssize_t termios_test_driver_write_helper( |
---|
151 | int port, |
---|
152 | const char *buf, |
---|
153 | size_t len |
---|
154 | ) |
---|
155 | { |
---|
156 | Tx_Buffer[Tx_Index++] = buf[0]; |
---|
157 | (void) rtems_timer_fire_after( Tx_Timer, 10, Tx_ISR, NULL ); |
---|
158 | return 1; |
---|
159 | } |
---|
160 | |
---|
161 | /* |
---|
162 | * Set Attributes Handler |
---|
163 | */ |
---|
164 | int termios_test_driver_set_attributes( |
---|
165 | int minor, |
---|
166 | const struct termios *t |
---|
167 | ) |
---|
168 | { |
---|
169 | return 0; |
---|
170 | } |
---|
171 | |
---|
172 | /* |
---|
173 | * Test Device Driver Entry Points |
---|
174 | */ |
---|
175 | rtems_device_driver termios_test_driver_initialize( |
---|
176 | rtems_device_major_number major, |
---|
177 | rtems_device_minor_number minor, |
---|
178 | void *arg |
---|
179 | ) |
---|
180 | { |
---|
181 | rtems_status_code status; |
---|
182 | |
---|
183 | rtems_termios_initialize(); |
---|
184 | |
---|
185 | /* |
---|
186 | * Register Device Names |
---|
187 | */ |
---|
188 | (void) rtems_io_register_name( TERMIOS_TEST_DRIVER_DEVICE_NAME, major, 0 ); |
---|
189 | |
---|
190 | status = rtems_timer_create(rtems_build_name('T', 'M', 'R', 'X'), &Rx_Timer); |
---|
191 | if ( status ) |
---|
192 | rtems_fatal_error_occurred(1);; |
---|
193 | |
---|
194 | status = rtems_timer_create(rtems_build_name('T', 'M', 'T', 'X'), &Tx_Timer); |
---|
195 | if ( status ) |
---|
196 | rtems_fatal_error_occurred(1);; |
---|
197 | |
---|
198 | return RTEMS_SUCCESSFUL; |
---|
199 | } |
---|
200 | |
---|
201 | rtems_device_driver termios_test_driver_open( |
---|
202 | rtems_device_major_number major, |
---|
203 | rtems_device_minor_number minor, |
---|
204 | void * arg |
---|
205 | ) |
---|
206 | { |
---|
207 | rtems_status_code sc; |
---|
208 | rtems_libio_open_close_args_t *args = arg; |
---|
209 | static const rtems_termios_callbacks Callbacks = { |
---|
210 | NULL, /* firstOpen */ |
---|
211 | NULL, /* lastClose */ |
---|
212 | #if defined(TASK_DRIVEN) |
---|
213 | termios_test_driver_inbyte_nonblocking,/* pollRead */ |
---|
214 | #else |
---|
215 | NULL, /* pollRead */ |
---|
216 | #endif |
---|
217 | termios_test_driver_write_helper, /* write */ |
---|
218 | termios_test_driver_set_attributes, /* setAttributes */ |
---|
219 | NULL, /* stopRemoteTx */ |
---|
220 | NULL, /* startRemoteTx */ |
---|
221 | #if defined(TASK_DRIVEN) |
---|
222 | TERMIOS_TASK_DRIVEN /* outputUsesInterrupts */ |
---|
223 | #else |
---|
224 | 0 /* outputUsesInterrupts */ |
---|
225 | #endif |
---|
226 | }; |
---|
227 | |
---|
228 | if ( minor > 2 ) { |
---|
229 | puts( "ERROR - Termios_testdriver - only 1 minor supported" ); |
---|
230 | rtems_test_exit(0); |
---|
231 | } |
---|
232 | |
---|
233 | sc = rtems_termios_open (major, minor, arg, &Callbacks); |
---|
234 | directive_failed( sc, "termios open" ); |
---|
235 | |
---|
236 | Ttyp = args->iop->data1; /* Keep cookie returned by termios_open */ |
---|
237 | |
---|
238 | return RTEMS_SUCCESSFUL; |
---|
239 | } |
---|
240 | |
---|
241 | rtems_device_driver termios_test_driver_close( |
---|
242 | rtems_device_major_number major, |
---|
243 | rtems_device_minor_number minor, |
---|
244 | void * arg |
---|
245 | ) |
---|
246 | { |
---|
247 | return rtems_termios_close (arg); |
---|
248 | } |
---|
249 | |
---|
250 | rtems_device_driver termios_test_driver_read( |
---|
251 | rtems_device_major_number major, |
---|
252 | rtems_device_minor_number minor, |
---|
253 | void * arg |
---|
254 | ) |
---|
255 | { |
---|
256 | return rtems_termios_read (arg); |
---|
257 | } |
---|
258 | |
---|
259 | rtems_device_driver termios_test_driver_write( |
---|
260 | rtems_device_major_number major, |
---|
261 | rtems_device_minor_number minor, |
---|
262 | void * arg |
---|
263 | ) |
---|
264 | { |
---|
265 | return rtems_termios_write (arg); |
---|
266 | } |
---|
267 | |
---|
268 | rtems_device_driver termios_test_driver_control( |
---|
269 | rtems_device_major_number major, |
---|
270 | rtems_device_minor_number minor, |
---|
271 | void * arg |
---|
272 | ) |
---|
273 | { |
---|
274 | return rtems_termios_ioctl (arg); |
---|
275 | } |
---|