comparison src/cs/services/atp/atp_uart.c @ 0:92470e5d0b9e

src: partial import from FC Selenite
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 15 May 2020 01:28:16 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:92470e5d0b9e
1 /********************************************************************************/
2 /* */
3 /* File Name: atp_uart.c */
4 /* */
5 /* Purpose: This file contains the internal functions related to */
6 /* the ATP-UART interface and used to utilize a COM port */
7 /* under WIN32. */
8 /* */
9 /* Note: None. */
10 /* */
11 /* Revision History: */
12 /* 10/04/01 Pascal Pompei */
13 /* - Create. */
14 /* */
15 /* (C) Copyright 2001 by Texas Instruments Incorporated, All Rights Reserved. */
16 /* */
17 /********************************************************************************/
18 #include "atp/atp_uart_i.h"
19
20
21 /********************************************************************************/
22 /* */
23 /* Function Name: atp_uart_create_com_port */
24 /* */
25 /* Purpose: This function creates a COM port. */
26 /* */
27 /* Input Parameters: */
28 /* com_port - Contains the COM port number. */
29 /* baud_rate - Contains the baud rate (in bits per second). */
30 /* */
31 /* Output Parameters: None. */
32 /* */
33 /* Global Parameters: None. */
34 /* */
35 /* Note: None. */
36 /* */
37 /* Revision History: */
38 /* 02/05/01 David Lamy-Charrier */
39 /* - Create. */
40 /* */
41 /********************************************************************************/
42 T_ATP_UART_ERROR_CODES atp_uart_create_com_port (T_ATP_UART_COM_PORT com_port,
43 T_ATP_UART_BAUD_RATE baud_rate)
44 {
45 /* Declare local variables. */
46 DCB control_settings;
47 COMMTIMEOUTS timeouts = {MAXDWORD, \
48 MAXDWORD, \
49 0x0000000A, \
50 0x00000000, \
51 0x00000000};
52
53 /******************* atp_uart_create_com_port function begins *******************/
54
55 /* First, update the name associated with the COM port. */
56 (gbl_atp_uart_ctrl_blk_p->conn_ctrl_blk).com_port_name[3] = (char) ('0' + com_port);
57
58 /* Then, create the COM port with the following parameters: */
59 /* - Read/Write access, */
60 /* - The COM port can not be shared and can not be opened again until the */
61 /* handle is closed, */
62 /* - The handle can not be inherited, */
63 /* - The COM port is just opened (failed if does not exist), */
64 /* - No specific file attributes, */
65 /* - No template used. */
66 (gbl_atp_uart_ctrl_blk_p->conn_ctrl_blk).com_port_handle = CreateFile ((gbl_atp_uart_ctrl_blk_p->conn_ctrl_blk).com_port_name,
67 (GENERIC_READ | GENERIC_WRITE),
68 0x00000000,
69 NULL,
70 OPEN_EXISTING,
71 0x00000000,
72 NULL);
73
74 /* If any error occurred, then abort. */
75 if ((gbl_atp_uart_ctrl_blk_p->conn_ctrl_blk).com_port_handle == INVALID_HANDLE_VALUE)
76 {
77 return (RV_INVALID_PARAMETER);
78 }
79
80 /* Otherwise, having created the COM port, get the default setting */
81 /* information. However, if any error occurred, then remove the COM port */
82 /* and abort. */
83 control_settings.DCBlength = sizeof (DCB);
84 if (GetCommState ((gbl_atp_uart_ctrl_blk_p->conn_ctrl_blk).com_port_handle, \
85 &control_settings) == FALSE)
86 {
87 (void) atp_uart_remove_com_port ();
88 return (RV_INVALID_PARAMETER);
89 }
90
91 /* Define the baud rate, as well as the DCB structure settings. */
92 control_settings.BaudRate = baud_rate;
93 control_settings.fOutxCtsFlow = TRUE;
94 control_settings.fOutxDsrFlow = FALSE;
95 control_settings.fDtrControl = DTR_CONTROL_ENABLE;
96 control_settings.fDsrSensitivity = FALSE;
97 control_settings.fRtsControl = RTS_CONTROL_HANDSHAKE;
98
99 /* Then, configure the COM port according to the specifications of the DCB */
100 /* structure, set the timeout parameters for all read and write operations */
101 /* on the COM port and discard all characters from the output and input */
102 /* buffers. However, if any error occurred, then remove the COM port and */
103 /* abort. */
104 if ((SetCommState ((gbl_atp_uart_ctrl_blk_p->conn_ctrl_blk).com_port_handle, \
105 &control_settings) == FALSE) || \
106 (SetCommTimeouts ((gbl_atp_uart_ctrl_blk_p->conn_ctrl_blk).com_port_handle, \
107 &timeouts) == FALSE) || \
108 (PurgeComm ((gbl_atp_uart_ctrl_blk_p->conn_ctrl_blk).com_port_handle, \
109 (PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT)) == FALSE))
110 {
111 (void) atp_uart_remove_com_port ();
112 return (RV_INTERNAL_ERR);
113 }
114 return (RV_OK);
115
116 } /****************** End of atp_uart_create_com_port function ******************/
117
118
119 /********************************************************************************/
120 /* */
121 /* Function Name: atp_uart_write_com_port */
122 /* */
123 /* Purpose: This function writes data on the COM port. */
124 /* */
125 /* Input Parameters: */
126 /* data_buffer_p - Pointer on the data to be written. */
127 /* data_size - Contains the total byte count to be written. */
128 /* */
129 /* Output Parameters: None. */
130 /* */
131 /* Global Parameters: None. */
132 /* */
133 /* Note: Buffer de-allocation is outside the scope of this */
134 /* function. */
135 /* */
136 /* Revision History: */
137 /* 02/05/01 David Lamy-Charrier */
138 /* - Create. */
139 /* */
140 /********************************************************************************/
141 T_ATP_UART_ERROR_CODES atp_uart_write_com_port (UINT8 *data_buffer_p,
142 UINT32 data_size)
143 {
144 /* Declare a local variable. */
145 UINT32 data_size_sent = 0x00000000;
146
147 /******************** atp_uart_write_com_port function begins *******************/
148
149 /* Attempt writing data. If any error occurred, then abort. */
150 if (((gbl_atp_uart_ctrl_blk_p->conn_ctrl_blk).com_port_handle == INVALID_HANDLE_VALUE) || \
151 (WriteFile ((gbl_atp_uart_ctrl_blk_p->conn_ctrl_blk).com_port_handle, \
152 data_buffer_p, \
153 data_size, \
154 (unsigned long *) (&data_size_sent), \
155 NULL) == FALSE) || \
156 (data_size_sent != data_size))
157 {
158 return (RV_INVALID_PARAMETER);
159 }
160 return (RV_OK);
161
162 } /******************* End of atp_uart_write_com_port function ******************/
163
164
165 /********************************************************************************/
166 /* */
167 /* Function Name: atp_uart_read_com_port */
168 /* */
169 /* Purpose: This function reads data from the COM port. */
170 /* */
171 /* Input Parameters: */
172 /* data_buffer_p - Pointer on the destination buffer. */
173 /* data_size_p - Pointer on the total byte count to be read. */
174 /* */
175 /* Output Parameters: None. */
176 /* data_size_p - Contains the total byte count read. */
177 /* */
178 /* Global Parameters: None. */
179 /* */
180 /* Notes: Buffer allocation is outside the scope of this */
181 /* function Moreover, this function returns the total */
182 /* byte count read, up to the expected value (from */
183 /* 0x00000000 to 'data_size_p'). */
184 /* */
185 /* Revision History: */
186 /* 02/05/01 David Lamy-Charrier */
187 /* - Create. */
188 /* */
189 /********************************************************************************/
190 T_ATP_UART_ERROR_CODES atp_uart_read_com_port (UINT8 *data_buffer_p,
191 UINT32 *data_size_p)
192 {
193 /* Declare a local variable. */
194 UINT32 data_size_read = 0x00000000;
195 UINT32 total_data_size_read = 0x00000000;
196
197 /******************** atp_uart_read_com_port function begins ********************/
198
199 /* First, check whether the COM port is in use and whether the total byte */
200 /* count to be read is valid. Otherwise, abort. */
201 if (((gbl_atp_uart_ctrl_blk_p->conn_ctrl_blk).com_port_handle == INVALID_HANDLE_VALUE) || \
202 (data_size_p == NULL))
203 {
204 return (RV_INVALID_PARAMETER);
205 }
206
207 /* Then, attempt reading data, up to the expected value. However, abort if */
208 /* no data available from the COM port or if any error occurred. */
209 do
210 {
211 if (ReadFile ((gbl_atp_uart_ctrl_blk_p->conn_ctrl_blk).com_port_handle, \
212 (data_buffer_p + total_data_size_read), \
213 (*data_size_p - total_data_size_read), \
214 (unsigned long *) (&data_size_read), \
215 NULL) == FALSE)
216 {
217 return (RV_INVALID_PARAMETER);
218 }
219 total_data_size_read += data_size_read;
220 }
221 while (data_size_read != 0x00000000);
222
223 /* Update the total byte count read. */
224 *data_size_p = total_data_size_read;
225 return(RV_OK);
226
227 } /******************* End of atp_uart_read_com_port function *******************/
228
229
230 /********************************************************************************/
231 /* */
232 /* Function Name: atp_uart_remove_com_port */
233 /* */
234 /* Purpose: This function removes the COM port. */
235 /* */
236 /* Input Parameters: None. */
237 /* */
238 /* Output Parameters: None. */
239 /* */
240 /* Global Parameters: None. */
241 /* */
242 /* Note: None. */
243 /* */
244 /* Revision History: */
245 /* 02/05/01 David Lamy-Charrier */
246 /* - Create. */
247 /* */
248 /********************************************************************************/
249 T_ATP_UART_ERROR_CODES atp_uart_remove_com_port (void)
250 {
251
252 /******************* atp_uart_remove_com_port function begins *******************/
253
254 /* Remove the COM port. If any error occurred, then abort. */
255 if (((gbl_atp_uart_ctrl_blk_p->conn_ctrl_blk).com_port_handle == INVALID_HANDLE_VALUE) || \
256 (DeleteFile ((gbl_atp_uart_ctrl_blk_p->conn_ctrl_blk).com_port_name) == FALSE))
257 {
258 return (RV_INVALID_PARAMETER);
259 }
260
261 /* Set the default handle assigned to the COM port. */
262 (gbl_atp_uart_ctrl_blk_p->conn_ctrl_blk).com_port_handle = INVALID_HANDLE_VALUE;
263 return (RV_OK);
264
265 } /****************** End of atp_uart_remove_com_port function ******************/
266