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