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