1 /* Copyright (C) 2013 Czech Technical University in Prague
4 * - Carlos Jenkins <carlos@jenkins.co.cr>
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.
13 * Serial Communication Interface RPP API implementation file.
17 * RPP API documentation.
22 #include <stdio.h> // vsnprintf()
23 #include <stdarg.h> // va_start, va_end
25 #if rppCONFIG_DRV == 1
29 static boolean_t initialized = FALSE;
38 #if rppCONFIG_DRV == 1
40 drv_sci_set_crlf_conv_en(TRUE);
41 #elif defined(FREERTOS_POSIX)
49 boolean_t rpp_sci_setup(uint32_t baud)
51 boolean_t known = FALSE;
57 // FIXME This is a standard list of baud rates. This should include only
104 #if rppCONFIG_DRV == 1
105 drv_sci_set_baudrate(baud);
112 #ifndef FREERTOS_POSIX
113 uint16_t rpp_sci_available()
115 uint16_t available = 0;
117 #if rppCONFIG_DRV == 1
118 available = drv_sci_available();
125 int8_t rpp_sci_read(uint32_t amount, uint8_t* buffer)
127 #if rppCONFIG_DRV == 1
128 drv_sci_receive(amount, buffer, portMAX_DELAY);
135 int8_t rpp_sci_read_nb(uint32_t amount, uint8_t* buffer)
137 #if rppCONFIG_DRV == 1
138 if(drv_sci_receive(amount, buffer, 0) != SUCCESS) {
147 int8_t rpp_sci_write(uint32_t amount, uint8_t* data)
149 #if rppCONFIG_DRV == 1
150 drv_sci_send(amount, data, portMAX_DELAY);
157 int8_t rpp_sci_write_nb(uint32_t amount, uint8_t* data)
159 #if rppCONFIG_DRV == 1
160 if(drv_sci_send(amount, data, 0) != SUCCESS) {
169 int8_t rpp_sci_flush(boolean_t buff)
171 #if rppCONFIG_DRV == 1
172 return drv_sci_flush(buff);
178 int32_t rpp_sci_printk(const char* format, ...)
180 char str[MAX_BUFFER_LEN];
183 va_start(argList, format);
185 length = vsnprintf(str, sizeof(str), format, argList);
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);
200 drv_sci_send_imm((uint32_t) length, (uint8_t*) str);
210 int32_t rpp_sci_printkb(const char* format, ...)
212 char str[MAX_BUFFER_LEN];
215 va_start(argList, format);
217 length = vsnprintf(str, sizeof(str), format, argList);
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);
232 return drv_sci_send_try_append((uint32_t) length, (uint8_t*) str);
241 int32_t rpp_sci_printf(const char* format, ...)
245 va_start(argList, format);
246 length = rpp_sci_vprintf(format, argList);
252 int32_t rpp_sci_vprintf(const char* format, va_list argList)
254 char str[MAX_BUFFER_LEN];
257 length = vsnprintf(str, sizeof(str), format, argList);
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);
272 portMAX_DELAY) != SUCCESS) {
282 int8_t rpp_sci_putc(uint8_t byte)
284 #if rppCONFIG_DRV == 1
285 drv_sci_send(1, &byte, portMAX_DELAY);
292 int16_t rpp_sci_getc()
296 #if rppCONFIG_DRV == 1
297 if(drv_sci_receive(1, &result, portMAX_DELAY) == FAILURE) {
302 return (int16_t)result;
304 #endif /* !FREERTOS_POSIX */