User | Revision | Line number | New contents of line |
mbed714 |
0:d616ece2d859
|
1
|
|
mbed714 |
0:d616ece2d859
|
2
|
/*
|
mbed714 |
0:d616ece2d859
|
3
|
Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
|
mbed714 |
0:d616ece2d859
|
4
|
|
mbed714 |
0:d616ece2d859
|
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
mbed714 |
0:d616ece2d859
|
6
|
of this software and associated documentation files (the "Software"), to deal
|
mbed714 |
0:d616ece2d859
|
7
|
in the Software without restriction, including without limitation the rights
|
mbed714 |
0:d616ece2d859
|
8
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
mbed714 |
0:d616ece2d859
|
9
|
copies of the Software, and to permit persons to whom the Software is
|
mbed714 |
0:d616ece2d859
|
10
|
furnished to do so, subject to the following conditions:
|
mbed714 |
0:d616ece2d859
|
11
|
|
mbed714 |
0:d616ece2d859
|
12
|
The above copyright notice and this permission notice shall be included in
|
mbed714 |
0:d616ece2d859
|
13
|
all copies or substantial portions of the Software.
|
mbed714 |
0:d616ece2d859
|
14
|
|
mbed714 |
0:d616ece2d859
|
15
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
mbed714 |
0:d616ece2d859
|
16
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
mbed714 |
0:d616ece2d859
|
17
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
mbed714 |
0:d616ece2d859
|
18
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
mbed714 |
0:d616ece2d859
|
19
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
mbed714 |
0:d616ece2d859
|
20
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
mbed714 |
0:d616ece2d859
|
21
|
THE SOFTWARE.
|
mbed714 |
0:d616ece2d859
|
22
|
*/
|
mbed714 |
0:d616ece2d859
|
23
|
|
mbed714 |
0:d616ece2d859
|
24
|
/*
|
mbed714 |
0:d616ece2d859
|
25
|
|
mbed714 |
0:d616ece2d859
|
26
|
This is a wrapper around Serial if for lwIP (acts as a serial driver)
|
mbed714 |
0:d616ece2d859
|
27
|
|
mbed714 |
0:d616ece2d859
|
28
|
See sio.h for functions to be implemented
|
mbed714 |
0:d616ece2d859
|
29
|
|
mbed714 |
0:d616ece2d859
|
30
|
sio_fd_t is a void* defined type, we use it as a SerialBuf ptr
|
mbed714 |
0:d616ece2d859
|
31
|
|
mbed714 |
0:d616ece2d859
|
32
|
|
mbed714 |
0:d616ece2d859
|
33
|
|
mbed714 |
0:d616ece2d859
|
34
|
*/
|
mbed714 |
0:d616ece2d859
|
35
|
|
mbed714 |
0:d616ece2d859
|
36
|
|
mbed714 |
0:d616ece2d859
|
37
|
#include "netCfg.h"
|
mbed714 |
0:d616ece2d859
|
38
|
#if NET_PPP
|
mbed714 |
0:d616ece2d859
|
39
|
|
mbed714 |
0:d616ece2d859
|
40
|
//#define MAX_SERIAL_PORTS 8
|
mbed714 |
0:d616ece2d859
|
41
|
|
mbed714 |
0:d616ece2d859
|
42
|
#include "lwip/sio.h"
|
mbed714 |
0:d616ece2d859
|
43
|
#include "mbed.h"
|
mbed714 |
0:d616ece2d859
|
44
|
//#include "sioMgr.h"
|
mbed714 |
0:d616ece2d859
|
45
|
#include "drv/serial/buf/SerialBuf.h"
|
mbed714 |
0:d616ece2d859
|
46
|
|
mbed714 |
0:d616ece2d859
|
47
|
//#define __DEBUG
|
mbed714 |
0:d616ece2d859
|
48
|
#include "dbg/dbg.h"
|
mbed714 |
0:d616ece2d859
|
49
|
|
mbed714 |
0:d616ece2d859
|
50
|
//extern "C" {
|
mbed714 |
0:d616ece2d859
|
51
|
|
mbed714 |
0:d616ece2d859
|
52
|
/**
|
mbed714 |
0:d616ece2d859
|
53
|
* Opens a serial device for communication.
|
mbed714 |
0:d616ece2d859
|
54
|
*
|
mbed714 |
0:d616ece2d859
|
55
|
* @param devnum device number
|
mbed714 |
0:d616ece2d859
|
56
|
* @return handle to serial device if successful, NULL otherwise
|
mbed714 |
0:d616ece2d859
|
57
|
*/
|
mbed714 |
0:d616ece2d859
|
58
|
sio_fd_t sio_open(u8_t devnum)
|
mbed714 |
0:d616ece2d859
|
59
|
{
|
mbed714 |
0:d616ece2d859
|
60
|
#if 0
|
mbed714 |
0:d616ece2d859
|
61
|
SerialBuf* pIf = SioMgr::getIf(devnum);
|
mbed714 |
0:d616ece2d859
|
62
|
if(pIf == NULL)
|
mbed714 |
0:d616ece2d859
|
63
|
return NULL;
|
mbed714 |
0:d616ece2d859
|
64
|
|
mbed714 |
0:d616ece2d859
|
65
|
//Got a SerialBuf* object
|
mbed714 |
0:d616ece2d859
|
66
|
//WARN: It HAS to be initialised (instanciated + attached to a Serial obj)
|
mbed714 |
0:d616ece2d859
|
67
|
|
mbed714 |
0:d616ece2d859
|
68
|
return (sio_fd_t) pIf;
|
mbed714 |
0:d616ece2d859
|
69
|
#endif
|
mbed714 |
0:d616ece2d859
|
70
|
return NULL;
|
mbed714 |
0:d616ece2d859
|
71
|
}
|
mbed714 |
0:d616ece2d859
|
72
|
|
mbed714 |
0:d616ece2d859
|
73
|
/**
|
mbed714 |
0:d616ece2d859
|
74
|
* Sends a single character to the serial device.
|
mbed714 |
0:d616ece2d859
|
75
|
*
|
mbed714 |
0:d616ece2d859
|
76
|
* @param c character to send
|
mbed714 |
0:d616ece2d859
|
77
|
* @param fd serial device handle
|
mbed714 |
0:d616ece2d859
|
78
|
*
|
mbed714 |
0:d616ece2d859
|
79
|
* @note This function will block until the character can be sent.
|
mbed714 |
0:d616ece2d859
|
80
|
*/
|
mbed714 |
0:d616ece2d859
|
81
|
void sio_send(u8_t c, sio_fd_t fd)
|
mbed714 |
0:d616ece2d859
|
82
|
{
|
mbed714 |
0:d616ece2d859
|
83
|
SerialBuf* pIf = (SerialBuf*) fd;
|
mbed714 |
0:d616ece2d859
|
84
|
//while(!pIf->writeable());
|
mbed714 |
0:d616ece2d859
|
85
|
pIf->putc( (char) c );
|
mbed714 |
0:d616ece2d859
|
86
|
}
|
mbed714 |
0:d616ece2d859
|
87
|
|
mbed714 |
0:d616ece2d859
|
88
|
/**
|
mbed714 |
0:d616ece2d859
|
89
|
* Receives a single character from the serial device.
|
mbed714 |
0:d616ece2d859
|
90
|
*
|
mbed714 |
0:d616ece2d859
|
91
|
* @param fd serial device handle
|
mbed714 |
0:d616ece2d859
|
92
|
*
|
mbed714 |
0:d616ece2d859
|
93
|
* @note This function will block until a character is received.
|
mbed714 |
0:d616ece2d859
|
94
|
*/
|
mbed714 |
0:d616ece2d859
|
95
|
u8_t sio_recv(sio_fd_t fd)
|
mbed714 |
0:d616ece2d859
|
96
|
{
|
mbed714 |
0:d616ece2d859
|
97
|
SerialBuf* pIf = (SerialBuf*) fd;
|
mbed714 |
0:d616ece2d859
|
98
|
pIf->setReadMode(false);
|
mbed714 |
0:d616ece2d859
|
99
|
while(!pIf->readable());
|
mbed714 |
0:d616ece2d859
|
100
|
return (u8_t) pIf->getc();
|
mbed714 |
0:d616ece2d859
|
101
|
}
|
mbed714 |
0:d616ece2d859
|
102
|
|
mbed714 |
0:d616ece2d859
|
103
|
/**
|
mbed714 |
0:d616ece2d859
|
104
|
* Reads from the serial device.
|
mbed714 |
0:d616ece2d859
|
105
|
*
|
mbed714 |
0:d616ece2d859
|
106
|
* @param fd serial device handle
|
mbed714 |
0:d616ece2d859
|
107
|
* @param data pointer to data buffer for receiving
|
mbed714 |
0:d616ece2d859
|
108
|
* @param len maximum length (in bytes) of data to receive
|
mbed714 |
0:d616ece2d859
|
109
|
* @return number of bytes actually received - may be 0 if aborted by sio_read_abort
|
mbed714 |
0:d616ece2d859
|
110
|
*
|
mbed714 |
0:d616ece2d859
|
111
|
* @note This function will block until data can be received. The blocking
|
mbed714 |
0:d616ece2d859
|
112
|
* can be cancelled by calling sio_read_abort().
|
mbed714 |
0:d616ece2d859
|
113
|
*/
|
mbed714 |
0:d616ece2d859
|
114
|
static volatile bool m_abort = false;
|
mbed714 |
0:d616ece2d859
|
115
|
u32_t sio_read(sio_fd_t fd, u8_t *data, u32_t len)
|
mbed714 |
0:d616ece2d859
|
116
|
{
|
mbed714 |
0:d616ece2d859
|
117
|
u32_t recvd = 0; //bytes received
|
mbed714 |
0:d616ece2d859
|
118
|
SerialBuf* pIf = (SerialBuf*) fd;
|
mbed714 |
0:d616ece2d859
|
119
|
pIf->setReadMode(false);
|
mbed714 |
0:d616ece2d859
|
120
|
while(!m_abort && len)
|
mbed714 |
0:d616ece2d859
|
121
|
{
|
mbed714 |
0:d616ece2d859
|
122
|
while(!pIf->readable());
|
mbed714 |
0:d616ece2d859
|
123
|
*data = (u8_t) pIf->getc();
|
mbed714 |
0:d616ece2d859
|
124
|
data++;
|
mbed714 |
0:d616ece2d859
|
125
|
len--;
|
mbed714 |
0:d616ece2d859
|
126
|
recvd++;
|
mbed714 |
0:d616ece2d859
|
127
|
}
|
mbed714 |
0:d616ece2d859
|
128
|
m_abort = false;
|
mbed714 |
0:d616ece2d859
|
129
|
return recvd;
|
mbed714 |
0:d616ece2d859
|
130
|
}
|
mbed714 |
0:d616ece2d859
|
131
|
|
mbed714 |
0:d616ece2d859
|
132
|
/**
|
mbed714 |
0:d616ece2d859
|
133
|
* Tries to read from the serial device. Same as sio_read but returns
|
mbed714 |
0:d616ece2d859
|
134
|
* immediately if no data is available and never blocks.
|
mbed714 |
0:d616ece2d859
|
135
|
*
|
mbed714 |
0:d616ece2d859
|
136
|
* @param fd serial device handle
|
mbed714 |
0:d616ece2d859
|
137
|
* @param data pointer to data buffer for receiving
|
mbed714 |
0:d616ece2d859
|
138
|
* @param len maximum length (in bytes) of data to receive
|
mbed714 |
0:d616ece2d859
|
139
|
* @return number of bytes actually received
|
mbed714 |
0:d616ece2d859
|
140
|
*/
|
mbed714 |
0:d616ece2d859
|
141
|
u32_t sio_tryread(sio_fd_t fd, u8_t *data, u32_t len)
|
mbed714 |
0:d616ece2d859
|
142
|
{
|
mbed714 |
0:d616ece2d859
|
143
|
u32_t recvd = 0; //bytes received
|
mbed714 |
0:d616ece2d859
|
144
|
SerialBuf* pIf = (SerialBuf*) fd;
|
mbed714 |
0:d616ece2d859
|
145
|
pIf->setReadMode(false);
|
mbed714 |
0:d616ece2d859
|
146
|
while(len)
|
mbed714 |
0:d616ece2d859
|
147
|
{
|
mbed714 |
0:d616ece2d859
|
148
|
/* if(!pIf->readable())
|
mbed714 |
0:d616ece2d859
|
149
|
{
|
mbed714 |
0:d616ece2d859
|
150
|
wait_ms(4);
|
mbed714 |
0:d616ece2d859
|
151
|
}*/
|
mbed714 |
0:d616ece2d859
|
152
|
if(!pIf->readable())
|
mbed714 |
0:d616ece2d859
|
153
|
{
|
mbed714 |
0:d616ece2d859
|
154
|
return recvd;
|
mbed714 |
0:d616ece2d859
|
155
|
}
|
mbed714 |
0:d616ece2d859
|
156
|
*data = (u8_t) pIf->getc();
|
mbed714 |
0:d616ece2d859
|
157
|
data++;
|
mbed714 |
0:d616ece2d859
|
158
|
len--;
|
mbed714 |
0:d616ece2d859
|
159
|
recvd++;
|
mbed714 |
0:d616ece2d859
|
160
|
}
|
mbed714 |
0:d616ece2d859
|
161
|
return recvd;
|
mbed714 |
0:d616ece2d859
|
162
|
}
|
mbed714 |
0:d616ece2d859
|
163
|
|
mbed714 |
0:d616ece2d859
|
164
|
/**
|
mbed714 |
0:d616ece2d859
|
165
|
* Writes to the serial device.
|
mbed714 |
0:d616ece2d859
|
166
|
*
|
mbed714 |
0:d616ece2d859
|
167
|
* @param fd serial device handle
|
mbed714 |
0:d616ece2d859
|
168
|
* @param data pointer to data to send
|
mbed714 |
0:d616ece2d859
|
169
|
* @param len length (in bytes) of data to send
|
mbed714 |
0:d616ece2d859
|
170
|
* @return number of bytes actually sent
|
mbed714 |
0:d616ece2d859
|
171
|
*
|
mbed714 |
0:d616ece2d859
|
172
|
* @note This function will block until all data can be sent.
|
mbed714 |
0:d616ece2d859
|
173
|
*/
|
mbed714 |
0:d616ece2d859
|
174
|
u32_t sio_write(sio_fd_t fd, u8_t *data, u32_t len)
|
mbed714 |
0:d616ece2d859
|
175
|
{
|
mbed714 |
0:d616ece2d859
|
176
|
u32_t sent = 0; //bytes sent
|
mbed714 |
0:d616ece2d859
|
177
|
SerialBuf* pIf = (SerialBuf*) fd;
|
mbed714 |
0:d616ece2d859
|
178
|
while(len)
|
mbed714 |
0:d616ece2d859
|
179
|
{
|
mbed714 |
0:d616ece2d859
|
180
|
while(!pIf->writeable());
|
mbed714 |
0:d616ece2d859
|
181
|
pIf->putc(*data);
|
mbed714 |
0:d616ece2d859
|
182
|
data++;
|
mbed714 |
0:d616ece2d859
|
183
|
len--;
|
mbed714 |
0:d616ece2d859
|
184
|
sent++;
|
mbed714 |
0:d616ece2d859
|
185
|
}
|
mbed714 |
0:d616ece2d859
|
186
|
return sent; //Well, this is bound to be len if no interrupt mechanism
|
mbed714 |
0:d616ece2d859
|
187
|
}
|
mbed714 |
0:d616ece2d859
|
188
|
|
mbed714 |
0:d616ece2d859
|
189
|
/**
|
mbed714 |
0:d616ece2d859
|
190
|
* Aborts a blocking sio_read() call.
|
mbed714 |
0:d616ece2d859
|
191
|
*
|
mbed714 |
0:d616ece2d859
|
192
|
* @param fd serial device handle
|
mbed714 |
0:d616ece2d859
|
193
|
*/
|
mbed714 |
0:d616ece2d859
|
194
|
void sio_read_abort(sio_fd_t fd)
|
mbed714 |
0:d616ece2d859
|
195
|
{
|
mbed714 |
0:d616ece2d859
|
196
|
m_abort = true;
|
mbed714 |
0:d616ece2d859
|
197
|
}
|
mbed714 |
0:d616ece2d859
|
198
|
|
mbed714 |
0:d616ece2d859
|
199
|
//}
|
mbed714 |
0:d616ece2d859
|
200
|
|
mbed714 |
0:d616ece2d859
|
201
|
#endif
|
mbed714 |
0:d616ece2d859
|
202
|
|