1 | /* |
---|
2 | * M68340/349 UART management tools |
---|
3 | */ |
---|
4 | |
---|
5 | /* |
---|
6 | * Author: |
---|
7 | * Geoffroy Montel |
---|
8 | * France Telecom - CNET/DSM/TAM/CAT |
---|
9 | * 4, rue du Clos Courtel |
---|
10 | * 35512 CESSON-SEVIGNE |
---|
11 | * FRANCE |
---|
12 | * |
---|
13 | * e-mail: g_montel@yahoo.com |
---|
14 | * |
---|
15 | * COPYRIGHT (c) 1989-1999. |
---|
16 | * On-Line Applications Research Corporation (OAR). |
---|
17 | * |
---|
18 | * The license and distribution terms for this file may be |
---|
19 | * found in the file LICENSE in this distribution or at |
---|
20 | * http://www.rtems.org/license/LICENSE. |
---|
21 | */ |
---|
22 | |
---|
23 | #include <termios.h> |
---|
24 | #include <bsp.h> |
---|
25 | #include <rtems/libio.h> |
---|
26 | #include <m68340.h> |
---|
27 | #include <m340uart.h> |
---|
28 | #include <stdarg.h> |
---|
29 | #include <string.h> |
---|
30 | |
---|
31 | /* this table shows compatible speed configurations for the MC68340: |
---|
32 | the first row shows baud rates for baud speed set 1 |
---|
33 | the second row shows baud rates for baud speed set 2 |
---|
34 | look at Motorola's MC68340 Integrated Processor User's Manual |
---|
35 | page 7-30 for more infos */ |
---|
36 | |
---|
37 | float m340_Baud_Rates_Table[16][2] = { |
---|
38 | { 50, 75 }, |
---|
39 | { 110, 110 }, |
---|
40 | { 134.5, 134.5 }, |
---|
41 | { 200, 150 }, |
---|
42 | { 300, 300 }, |
---|
43 | { 600, 600 }, |
---|
44 | { 1200, 1200 }, |
---|
45 | { 1050, 2000 }, |
---|
46 | { 2400, 2400 }, |
---|
47 | { 4800, 4800 }, |
---|
48 | { 7200, 1800 }, |
---|
49 | { 9600, 9600 }, |
---|
50 | { 38400, 19200 }, |
---|
51 | { 76800, 38400 }, |
---|
52 | { SCLK/16, SCLK/16}, |
---|
53 | { SCLK, SCLK }, |
---|
54 | }; |
---|
55 | |
---|
56 | /* config on both 340 channels */ |
---|
57 | uart_channel_config m340_uart_config[UART_NUMBER_OF_CHANNELS]; |
---|
58 | |
---|
59 | /* |
---|
60 | * Init UART table |
---|
61 | */ |
---|
62 | |
---|
63 | #define NOT_IMPLEMENTED_YET 0 |
---|
64 | |
---|
65 | /****************************************************** |
---|
66 | Name: Init_UART_Table |
---|
67 | Input parameters: - |
---|
68 | Output parameters: - |
---|
69 | Description: Init the m340_uart_config |
---|
70 | THIS SHOULD NOT BE HERE! |
---|
71 | Its aim was to let the user configure |
---|
72 | UARTs for each application. |
---|
73 | As we can't pass args to the console |
---|
74 | driver initialisation routine at the |
---|
75 | moment, this was not done. |
---|
76 | ATTENTION: TERMIOS init presupposes that the channel |
---|
77 | baud rates is 9600/9600. |
---|
78 | -> risks when using IOCTL |
---|
79 | *****************************************************/ |
---|
80 | void Init_UART_Table(void) |
---|
81 | { |
---|
82 | m340_uart_config[UART_CHANNEL_A].enable = TRUE; |
---|
83 | strcpy(m340_uart_config[UART_CHANNEL_A].name, UART_CONSOLE_NAME); |
---|
84 | m340_uart_config[UART_CHANNEL_A].parity_mode = m340_No_Parity; |
---|
85 | m340_uart_config[UART_CHANNEL_A].bits_per_char = m340_8bpc; |
---|
86 | m340_uart_config[UART_CHANNEL_A].rx_baudrate = 9600; |
---|
87 | m340_uart_config[UART_CHANNEL_A].tx_baudrate = 9600; |
---|
88 | m340_uart_config[UART_CHANNEL_A].rx_mode = UART_CRR; |
---|
89 | m340_uart_config[UART_CHANNEL_A].mode = UART_POLLING; |
---|
90 | |
---|
91 | m340_uart_config[UART_CHANNEL_A].termios.enable = TRUE; |
---|
92 | m340_uart_config[UART_CHANNEL_A].termios.rx_buffer_size = NOT_IMPLEMENTED_YET; |
---|
93 | m340_uart_config[UART_CHANNEL_A].termios.tx_buffer_size = NOT_IMPLEMENTED_YET; |
---|
94 | |
---|
95 | m340_uart_config[UART_CHANNEL_B].enable = FALSE; |
---|
96 | strcpy(m340_uart_config[UART_CHANNEL_B].name, UART_RAW_IO_NAME); |
---|
97 | m340_uart_config[UART_CHANNEL_B].parity_mode = m340_No_Parity; |
---|
98 | m340_uart_config[UART_CHANNEL_B].bits_per_char = m340_8bpc; |
---|
99 | m340_uart_config[UART_CHANNEL_B].rx_baudrate = 38400; |
---|
100 | m340_uart_config[UART_CHANNEL_B].tx_baudrate = 38400; |
---|
101 | m340_uart_config[UART_CHANNEL_B].rx_mode = UART_CRR; |
---|
102 | m340_uart_config[UART_CHANNEL_B].mode = UART_INTERRUPTS; |
---|
103 | |
---|
104 | m340_uart_config[UART_CHANNEL_B].termios.enable = TRUE; |
---|
105 | m340_uart_config[UART_CHANNEL_B].termios.rx_buffer_size = NOT_IMPLEMENTED_YET; |
---|
106 | m340_uart_config[UART_CHANNEL_B].termios.tx_buffer_size = NOT_IMPLEMENTED_YET; |
---|
107 | } |
---|
108 | |
---|
109 | /****************************************************** |
---|
110 | Name: Find_Right_m340_UART_Channel_Config |
---|
111 | Input parameters: Send/Receive baud rates for a |
---|
112 | given channel |
---|
113 | Output parameters: UART compatible configs for this |
---|
114 | channel |
---|
115 | Description: returns which uart configurations fit |
---|
116 | Receiver Baud Rate and Transmitter Baud |
---|
117 | Rate for a given channel |
---|
118 | For instance, according to the |
---|
119 | m340_Baud_Rates_Table: |
---|
120 | - Output Speed = 50, Input Speed = 75 |
---|
121 | is not a correct config, because |
---|
122 | 50 bauds implies set 1 and 75 bauds |
---|
123 | implies set 2 |
---|
124 | - Output Speed = 9600, Input Speed = 9600 |
---|
125 | two correct configs for this: |
---|
126 | RCS=11, TCS=11, Set=1 or 2 |
---|
127 | *****************************************************/ |
---|
128 | static t_baud_speed_table |
---|
129 | Find_Right_m340_UART_Channel_Config( |
---|
130 | float ReceiverBaudRate, |
---|
131 | float TransmitterBaudRate |
---|
132 | ) |
---|
133 | { |
---|
134 | t_baud_speed_table return_value; |
---|
135 | int i,j; |
---|
136 | |
---|
137 | struct { |
---|
138 | int cs; |
---|
139 | int set; |
---|
140 | } Receiver[2], Transmitter[2]; |
---|
141 | |
---|
142 | int Receiver_nb_of_config = 0; |
---|
143 | int Transmitter_nb_of_config = 0; |
---|
144 | |
---|
145 | /* Receiver and Transmitter baud rates must be compatible, ie in the |
---|
146 | * same set. |
---|
147 | */ |
---|
148 | |
---|
149 | /* search for configurations for ReceiverBaudRate |
---|
150 | * there can't be more than two (only two sets). |
---|
151 | */ |
---|
152 | for (i=0;i<16;i++) { |
---|
153 | for (j=0;j<2;j++) { |
---|
154 | if (m340_Baud_Rates_Table[i][j]==ReceiverBaudRate) { |
---|
155 | Receiver[Receiver_nb_of_config].cs=i; |
---|
156 | Receiver[Receiver_nb_of_config].set=j; |
---|
157 | Receiver_nb_of_config++; |
---|
158 | } |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | /* search for configurations for TransmitterBaudRate |
---|
163 | * there can't be more than two (only two sets) |
---|
164 | */ |
---|
165 | for (i=0;i<16;i++) { |
---|
166 | for (j=0;j<2;j++) { |
---|
167 | if (m340_Baud_Rates_Table[i][j]==TransmitterBaudRate) { |
---|
168 | Transmitter[Transmitter_nb_of_config].cs=i; |
---|
169 | Transmitter[Transmitter_nb_of_config].set=j; |
---|
170 | Transmitter_nb_of_config++; |
---|
171 | } |
---|
172 | } |
---|
173 | } |
---|
174 | |
---|
175 | /* now check if there's a compatible config */ |
---|
176 | return_value.nb=0; |
---|
177 | |
---|
178 | for (i=0; i<Receiver_nb_of_config; i++) { |
---|
179 | for (j=0;j<Transmitter_nb_of_config;j++) { |
---|
180 | if (Receiver[i].set == Transmitter[j].set) { |
---|
181 | return_value.baud_speed_table[return_value.nb].set = Receiver[i].set + 1; |
---|
182 | /* we want set 1 or set 2, not 0 or 1 */ |
---|
183 | return_value.baud_speed_table[return_value.nb].rcs = Receiver[i].cs; |
---|
184 | return_value.baud_speed_table[return_value.nb].tcs = Transmitter[j].cs; |
---|
185 | return_value.nb++; |
---|
186 | } |
---|
187 | } |
---|
188 | } |
---|
189 | |
---|
190 | return return_value; |
---|
191 | } |
---|
192 | |
---|
193 | /****************************************************** |
---|
194 | Name: Find_Right_m340_UART_Config |
---|
195 | Input parameters: Send/Receive baud rates for both |
---|
196 | channels |
---|
197 | Output parameters: UART compatible configs for |
---|
198 | BOTH channels |
---|
199 | Description: returns which uart configurations fit |
---|
200 | Receiver Baud Rate and Transmitter Baud |
---|
201 | Rate for both channels |
---|
202 | For instance, if we want 9600/38400 on |
---|
203 | channel A and 9600/19200 on channel B, |
---|
204 | this is not a good m340 uart config |
---|
205 | (channel A needs set 1 and channel B |
---|
206 | needs set 2) |
---|
207 | *****************************************************/ |
---|
208 | t_baud_speed_table |
---|
209 | Find_Right_m340_UART_Config( |
---|
210 | float ChannelA_ReceiverBaudRate, |
---|
211 | float ChannelA_TransmitterBaudRate, |
---|
212 | uint8_t enableA, |
---|
213 | float ChannelB_ReceiverBaudRate, |
---|
214 | float ChannelB_TransmitterBaudRate, |
---|
215 | uint8_t enableB |
---|
216 | ) |
---|
217 | { |
---|
218 | t_baud_speed_table tableA, tableB; |
---|
219 | t_baud_speed_table return_value, tmp; |
---|
220 | int i,j; |
---|
221 | |
---|
222 | memset( &return_value, '\0', sizeof(return_value) ); |
---|
223 | return_value.nb=0; |
---|
224 | |
---|
225 | if (enableA && enableB) { |
---|
226 | tableA = Find_Right_m340_UART_Channel_Config( |
---|
227 | ChannelA_ReceiverBaudRate, ChannelA_TransmitterBaudRate); |
---|
228 | tableB = Find_Right_m340_UART_Channel_Config( |
---|
229 | ChannelB_ReceiverBaudRate, ChannelB_TransmitterBaudRate); |
---|
230 | |
---|
231 | for (i=0;i<tableA.nb;i++) { |
---|
232 | for (j=0;j<tableB.nb;j++) { |
---|
233 | if (tableA.baud_speed_table[i].set==tableB.baud_speed_table[j].set) { |
---|
234 | return_value.baud_speed_table[UART_CHANNEL_A].set = |
---|
235 | tableA.baud_speed_table[i].set; |
---|
236 | return_value.baud_speed_table[UART_CHANNEL_A].rcs = |
---|
237 | tableA.baud_speed_table[i].rcs; |
---|
238 | return_value.baud_speed_table[UART_CHANNEL_A].tcs = |
---|
239 | tableA.baud_speed_table[i].tcs; |
---|
240 | return_value.baud_speed_table[UART_CHANNEL_B].set = |
---|
241 | tableB.baud_speed_table[j].set; |
---|
242 | return_value.baud_speed_table[UART_CHANNEL_B].rcs = |
---|
243 | tableB.baud_speed_table[j].rcs; |
---|
244 | return_value.baud_speed_table[UART_CHANNEL_B].tcs = |
---|
245 | tableB.baud_speed_table[j].tcs; |
---|
246 | return_value.nb=2; |
---|
247 | break; |
---|
248 | } |
---|
249 | } |
---|
250 | } |
---|
251 | return return_value; |
---|
252 | } |
---|
253 | |
---|
254 | if (enableA) { |
---|
255 | return_value = Find_Right_m340_UART_Channel_Config( |
---|
256 | ChannelA_ReceiverBaudRate, ChannelA_TransmitterBaudRate); |
---|
257 | return return_value; |
---|
258 | } |
---|
259 | |
---|
260 | if (enableB) { |
---|
261 | tmp = Find_Right_m340_UART_Channel_Config( |
---|
262 | ChannelB_ReceiverBaudRate, ChannelB_TransmitterBaudRate); |
---|
263 | if (tmp.nb!=0) { |
---|
264 | return_value.nb = 2; |
---|
265 | return_value.baud_speed_table[1].set = tmp.baud_speed_table[0].set; |
---|
266 | return_value.baud_speed_table[1].rcs = tmp.baud_speed_table[0].rcs; |
---|
267 | return_value.baud_speed_table[1].tcs = tmp.baud_speed_table[0].tcs; |
---|
268 | } |
---|
269 | } |
---|
270 | return return_value; |
---|
271 | } |
---|
272 | |
---|
273 | |
---|
274 | /* |
---|
275 | * very low level fmted output |
---|
276 | */ |
---|
277 | extern void dbug_out_char( int minor, int ch ); |
---|
278 | extern int dbug_in_char( int minor ); |
---|
279 | extern int dbug_char_present( int minor ); |
---|
280 | |
---|
281 | /****************************************************** |
---|
282 | Name: dbugRead |
---|
283 | Input parameters: channel |
---|
284 | Output parameters: char read |
---|
285 | Description: polled read |
---|
286 | *****************************************************/ |
---|
287 | int dbugRead (int minor) |
---|
288 | { |
---|
289 | if (dbug_char_present(minor) == 0) |
---|
290 | return -1; |
---|
291 | return dbug_in_char(minor); |
---|
292 | } |
---|
293 | |
---|
294 | /****************************************************** |
---|
295 | Name: dbugWrite |
---|
296 | Input parameters: channel, buffer and its length |
---|
297 | Output parameters: always successfull |
---|
298 | Description: polled write |
---|
299 | *****************************************************/ |
---|
300 | ssize_t dbugWrite (int minor, const char *buf, size_t len) |
---|
301 | { |
---|
302 | static char txBuf; |
---|
303 | size_t retval = len; |
---|
304 | |
---|
305 | while (len--) { |
---|
306 | txBuf = *buf++; |
---|
307 | dbug_out_char( minor, (int)txBuf ); |
---|
308 | } |
---|
309 | return retval; |
---|
310 | } |
---|
311 | |
---|