]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blob - rpp/lib/rpp/src/rpp/sci.c
944d9cc3bf410015d4a3d72aa1eff6549050b509
[pes-rpp/rpp-test-sw.git] / rpp / lib / 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
188 int32_t rpp_sci_printf(const char* format, ...)
189 {
190     char str[MAX_BUFFER_LEN];
191     int length = -1;
192
193     va_list argList;
194     va_start(argList, format);
195
196     length = vsnprintf(str, sizeof(str), format, argList);
197
198     va_end(argList);
199
200     if(length > 0) {
201         #if rppCONFIG_DRV == 1
202         // According to the C stdlib about vsnprintf:
203         // If the resulting string would be longer than n-1 characters, the
204         // remaining characters are discarded and not stored, but counted
205         // for the value returned by the function.
206         // In consequence we need to trim the value if larger than buffer.
207         if(length > sizeof(str)) {
208             length = sizeof(str);
209         }
210         if(drv_sci_send(
211                         (uint32_t)length,
212                         (uint8_t*)str,
213                         portMAX_DELAY) != SUCCESS) {
214             return -1;
215         }
216         #endif
217     }
218
219     return length;
220 }
221
222
223 int8_t rpp_sci_putc(uint8_t byte)
224 {
225     #if rppCONFIG_DRV == 1
226     drv_sci_send(1, &byte, portMAX_DELAY);
227     #endif
228
229     return SUCCESS;
230 }
231
232
233 int16_t rpp_sci_getc()
234 {
235     uint8_t result = 0;
236
237     #if rppCONFIG_DRV == 1
238     if(drv_sci_receive(1, &result, portMAX_DELAY) == FAILURE) {
239         return FAILURE;
240     }
241     #endif
242
243     return (int16_t)result;
244 }
245 #endif /* !FREERTOS_POSIX */
246
247 #endif /* rppCONFIG_INCLUDE_SCI */
248