forked from apache/nuttx-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnxscope_iser.c
268 lines (200 loc) · 6.66 KB
/
nxscope_iser.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/****************************************************************************
* apps/logging/nxscope/nxscope_iser.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <debug.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <logging/nxscope/nxscope.h>
/****************************************************************************
* Private Type Definition
****************************************************************************/
struct nxscope_intf_ser_s
{
FAR struct nxscope_ser_cfg_s *cfg;
int fd;
};
/****************************************************************************
* Private Function Protototypes
****************************************************************************/
static int nxscope_ser_send(FAR struct nxscope_intf_s *intf,
FAR uint8_t *buff, int len);
static int nxscope_ser_recv(FAR struct nxscope_intf_s *intf,
FAR uint8_t *buff, int len);
/****************************************************************************
* Private Data
****************************************************************************/
static struct nxscope_intf_ops_s g_nxscope_ser_ops =
{
nxscope_ser_send,
nxscope_ser_recv
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: nxscope_ser_send
****************************************************************************/
static int nxscope_ser_send(FAR struct nxscope_intf_s *intf,
FAR uint8_t *buff, int len)
{
FAR struct nxscope_intf_ser_s *priv = NULL;
DEBUGASSERT(intf);
DEBUGASSERT(intf->priv);
/* Get priv data */
priv = (FAR struct nxscope_intf_ser_s *)intf->priv;
/* Write data */
return write(priv->fd, buff, len);
}
/****************************************************************************
* Name: nxscope_ser_recv
****************************************************************************/
static int nxscope_ser_recv(FAR struct nxscope_intf_s *intf,
FAR uint8_t *buff, int len)
{
FAR struct nxscope_intf_ser_s *priv = NULL;
int ret = OK;
DEBUGASSERT(intf);
DEBUGASSERT(intf->priv);
/* Get priv data */
priv = (FAR struct nxscope_intf_ser_s *)intf->priv;
/* Read data */
ret = read(priv->fd, buff, len);
if (ret < 0)
{
if (priv->cfg->nonblock && (errno == EAGAIN))
{
ret = 0;
}
}
return ret;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: nxscope_ser_init
****************************************************************************/
int nxscope_ser_init(FAR struct nxscope_intf_s *intf,
FAR struct nxscope_ser_cfg_s *cfg)
{
FAR struct nxscope_intf_ser_s *priv = NULL;
struct termios tio;
int ret = OK;
int flags = 0;
DEBUGASSERT(intf);
DEBUGASSERT(cfg);
/* Allocate priv data */
intf->priv = zalloc(sizeof(struct nxscope_intf_ser_s));
if (intf->priv == NULL)
{
_err("ERROR: intf->priv alloc failed %d\n", errno);
ret = -errno;
goto errout;
}
/* Get priv data */
priv = (FAR struct nxscope_intf_ser_s *)intf->priv;
/* Connect configuration */
priv->cfg = (FAR struct nxscope_ser_cfg_s *)cfg;
/* Connect ops */
intf->ops = &g_nxscope_ser_ops;
/* Open serial port */
flags = O_RDWR;
if (priv->cfg->nonblock)
{
flags |= O_NONBLOCK;
}
#ifdef CONFIG_SERIAL_REMOVABLE
do
{
/* Try to open the console */
priv->fd = open(priv->cfg->path, flags);
if (priv->fd < 0)
{
/* ENOTCONN means that the USB device is not yet connected.
* Anything else is bad.
*/
if (errno != ENOTCONN)
{
break;
}
/* Sleep a bit and try again */
sleep(1);
}
}
while (priv->fd < 0);
#else
priv->fd = open(priv->cfg->path, flags);
#endif
if (priv->fd < 0)
{
_err("ERROR: failed to open %s %d\n", priv->cfg->path, errno);
ret = -errno;
goto errout;
}
/* Get the attributes */
tcgetattr(priv->fd, &tio);
#ifdef CONFIG_SERIAL_TERMIOS
if (priv->cfg->baud > 0)
{
/* Configure a baud rate */
ret = cfsetspeed(&tio, priv->cfg->baud);
if (ret < 0)
{
_err("ERROR: failed to set baud rate %d\n", errno);
goto errout;
}
}
#endif
/* Configure RAW mode */
cfmakeraw(&tio);
/* Change the attributes now */
tcsetattr(priv->fd, TCSANOW, &tio);
/* Initialized */
intf->initialized = true;
errout:
return ret;
}
/****************************************************************************
* Name: nxscope_ser_deinit
****************************************************************************/
void nxscope_ser_deinit(FAR struct nxscope_intf_s *intf)
{
FAR struct nxscope_intf_ser_s *priv = NULL;
DEBUGASSERT(intf);
/* Get priv data */
priv = (FAR struct nxscope_intf_ser_s *)intf->priv;
/* Close dev */
if (priv->fd != -1)
{
close(priv->fd);
}
if (priv != NULL)
{
free(priv);
}
/* Reset structure */
memset(intf, 0, sizeof(struct nxscope_intf_s));
}