]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blob - rpp/src/rpp/sci.c
Merge branch 'master' of rtime.felk.cvut.cz:pes-rpp/rpp-lib
[pes-rpp/rpp-lib.git] / rpp / src / rpp / sci.c
1 /* Copyright (C) 2013 Czech Technical University in Prague
2  *
3  * Authors:
4  *     - Carlos Jenkins <carlos@jenkins.co.cr>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  * File : sci.c
20  * Abstract:
21  *     Serial Communication Interface RPP API implementation file.
22  *
23  * References:
24  *     sci.h
25  *     RPP API documentation.
26  */
27
28
29 #include "rpp/rpp.h"
30
31 #if rppCONFIG_INCLUDE_SCI == 1
32 #include <stdio.h>  // vsnprintf()
33 #include <stdarg.h> // va_start, va_end
34
35 #if rppCONFIG_DRV == 1
36 #include "drv/drv.h"
37 #endif
38
39 static boolean_t initialized = FALSE;
40
41 int8_t rpp_sci_init()
42 {
43     if(initialized) {
44         return FAILURE;
45     }
46     initialized = TRUE;
47
48     #if rppCONFIG_DRV == 1
49     drv_sci_init();
50     #elif defined(FREERTOS_POSIX)
51     rpp_sci_posix_init();
52     #endif
53
54     return SUCCESS;
55 }
56
57
58 boolean_t rpp_sci_setup(uint32_t baud)
59 {
60     boolean_t known = FALSE;
61
62     if(baud == 0) {
63         baud = 9600;
64     }
65
66     // FIXME This is a standard list of baud rates. This should include only
67     // tested baud rates.
68     switch(baud) {
69         case 110:
70             known = TRUE;
71             break;
72         case 300:
73             known = TRUE;
74             break;
75         case 600:
76             known = TRUE;
77             break;
78         case 1200:
79             known = TRUE;
80             break;
81         case 2400:
82             known = TRUE;
83             break;
84         case 4800:
85             known = TRUE;
86             break;
87         case 9600:
88             known = TRUE;
89             break;
90         case 14400:
91             known = TRUE;
92             break;
93         case 19200:
94             known = TRUE;
95             break;
96         case 28800:
97             known = TRUE;
98             break;
99         case 38400:
100             known = TRUE;
101             break;
102         case 56000:
103             known = TRUE;
104             break;
105         case 57600:
106             known = TRUE;
107             break;
108         case 115200:
109             known = TRUE;
110             break;
111     }
112
113     #if rppCONFIG_DRV == 1
114     drv_sci_set_baudrate(baud);
115     #endif
116
117     return known;
118 }
119
120
121 #ifndef FREERTOS_POSIX
122 uint16_t rpp_sci_available()
123 {
124     uint16_t available = 0;
125
126     #if rppCONFIG_DRV == 1
127     available = drv_sci_available();
128     #endif
129
130     return available;
131 }
132
133
134 int8_t rpp_sci_read(uint32_t amount, uint8_t* buffer)
135 {
136     #if rppCONFIG_DRV == 1
137     drv_sci_receive(amount, buffer, portMAX_DELAY);
138     #endif
139
140     return SUCCESS;
141 }
142
143
144 int8_t rpp_sci_read_nb(uint32_t amount, uint8_t* buffer)
145 {
146     #if rppCONFIG_DRV == 1
147     if(drv_sci_receive(amount, buffer, 0) != SUCCESS) {
148         return FAILURE;
149     }
150     #endif
151
152     return SUCCESS;
153 }
154
155
156 int8_t rpp_sci_write(uint32_t amount, uint8_t* data)
157 {
158     #if rppCONFIG_DRV == 1
159     drv_sci_send(amount, data, portMAX_DELAY);
160     #endif
161
162     return SUCCESS;
163 }
164
165
166 int8_t rpp_sci_write_nb(uint32_t amount, uint8_t* data)
167 {
168     #if rppCONFIG_DRV == 1
169     if(drv_sci_send(amount, data, 0) != SUCCESS) {
170         return FAILURE;
171     }
172     #endif
173
174     return SUCCESS;
175 }
176
177
178 int8_t rpp_sci_flush(boolean_t buff)
179 {
180     #if rppCONFIG_DRV == 1
181     return drv_sci_flush(buff);
182     #else
183     return SUCCESS;
184     #endif
185 }
186
187 int32_t rpp_sci_printk(const char* format, ...)
188 {
189     char str[MAX_BUFFER_LEN];
190     int length = -1;
191     va_list argList;
192     va_start(argList, format);
193
194     length = vsnprintf(str, sizeof(str), format, argList);
195
196     va_end(argList);
197
198     if(length > 0) {
199         #if rppCONFIG_DRV == 1
200         // According to the C stdlib about vsnprintf:
201         // If the resulting string would be longer than n-1 characters, the
202         // remaining characters are discarded and not stored, but counted
203         // for the value returned by the function.
204         // In consequence we need to trim the value if larger than buffer.
205         if(length > sizeof(str)) {
206             length = sizeof(str);
207         }
208
209         drv_sci_send_imm((uint32_t) length, (uint8_t*) str);
210
211         #else
212         length = -1;
213         #endif
214     }
215
216     return length;
217 }
218
219 int32_t rpp_sci_printkb(const char* format, ...)
220 {
221     char str[MAX_BUFFER_LEN];
222     int length = -1;
223     va_list argList;
224     va_start(argList, format);
225
226     length = vsnprintf(str, sizeof(str), format, argList);
227
228     va_end(argList);
229
230     if(length > 0) {
231         #if rppCONFIG_DRV == 1
232         // According to the C stdlib about vsnprintf:
233         // If the resulting string would be longer than n-1 characters, the
234         // remaining characters are discarded and not stored, but counted
235         // for the value returned by the function.
236         // In consequence we need to trim the value if larger than buffer.
237         if(length > sizeof(str)) {
238             length = sizeof(str);
239         }
240
241         return drv_sci_send_try_append((uint32_t) length, (uint8_t*) str);
242
243             #else
244         return -1;
245         #endif
246     }
247     return length;
248 }
249
250 int32_t rpp_sci_printf(const char* format, ...)
251 {
252         int length = -1;
253     va_list argList;
254     va_start(argList, format);
255     length = rpp_sci_vprintf(format, argList);
256     va_end(argList);
257
258     return length;
259 }
260
261 int32_t rpp_sci_vprintf(const char* format, va_list argList)
262 {
263     char str[MAX_BUFFER_LEN];
264     int length = -1;
265
266     length = vsnprintf(str, sizeof(str), format, argList);
267
268     if(length > 0) {
269         #if rppCONFIG_DRV == 1
270         // According to the C stdlib about vsnprintf:
271         // If the resulting string would be longer than n-1 characters, the
272         // remaining characters are discarded and not stored, but counted
273         // for the value returned by the function.
274         // In consequence we need to trim the value if larger than buffer.
275         if(length > sizeof(str)) {
276             length = sizeof(str);
277         }
278         if(drv_sci_send(
279                         (uint32_t)length,
280                         (uint8_t*)str,
281                         portMAX_DELAY) != SUCCESS) {
282             return -1;
283         }
284         #endif
285     }
286
287     return length;
288 }
289
290
291 int8_t rpp_sci_putc(uint8_t byte)
292 {
293     #if rppCONFIG_DRV == 1
294     drv_sci_send(1, &byte, portMAX_DELAY);
295     #endif
296
297     return SUCCESS;
298 }
299
300
301 int16_t rpp_sci_getc()
302 {
303     uint8_t result = 0;
304
305     #if rppCONFIG_DRV == 1
306     if(drv_sci_receive(1, &result, portMAX_DELAY) == FAILURE) {
307         return FAILURE;
308     }
309     #endif
310
311     return (int16_t)result;
312 }
313 #endif /* !FREERTOS_POSIX */
314
315 #endif /* rppCONFIG_INCLUDE_SCI */
316