1 /* Copyright (C) 2013 Czech Technical University in Prague
4 * - Carlos Jenkins <carlos@jenkins.co.cr>
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.
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.
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/>.
21 * Serial Communication Interface RPP API implementation file.
25 * RPP API documentation.
31 #if rppCONFIG_INCLUDE_SCI == 1
32 #include <stdio.h> // vsnprintf()
33 #include <stdarg.h> // va_start, va_end
35 #if rppCONFIG_DRV == 1
39 static boolean_t initialized = FALSE;
48 #if rppCONFIG_DRV == 1
50 #elif defined(FREERTOS_POSIX)
58 boolean_t rpp_sci_setup(uint32_t baud)
60 boolean_t known = FALSE;
66 // FIXME This is a standard list of baud rates. This should include only
113 #if rppCONFIG_DRV == 1
114 drv_sci_set_baudrate(baud);
121 #ifndef FREERTOS_POSIX
122 uint16_t rpp_sci_available()
124 uint16_t available = 0;
126 #if rppCONFIG_DRV == 1
127 available = drv_sci_available();
134 int8_t rpp_sci_read(uint32_t amount, uint8_t* buffer)
136 #if rppCONFIG_DRV == 1
137 drv_sci_receive(amount, buffer, portMAX_DELAY);
144 int8_t rpp_sci_read_nb(uint32_t amount, uint8_t* buffer)
146 #if rppCONFIG_DRV == 1
147 if(drv_sci_receive(amount, buffer, 0) != SUCCESS) {
156 int8_t rpp_sci_write(uint32_t amount, uint8_t* data)
158 #if rppCONFIG_DRV == 1
159 drv_sci_send(amount, data, portMAX_DELAY);
166 int8_t rpp_sci_write_nb(uint32_t amount, uint8_t* data)
168 #if rppCONFIG_DRV == 1
169 if(drv_sci_send(amount, data, 0) != SUCCESS) {
178 int8_t rpp_sci_flush(boolean_t buff)
180 #if rppCONFIG_DRV == 1
181 return drv_sci_flush(buff);
188 int32_t rpp_sci_printf(const char* format, ...)
190 char str[MAX_BUFFER_LEN];
194 va_start(argList, format);
196 length = vsnprintf(str, sizeof(str), format, argList);
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);
213 portMAX_DELAY) != SUCCESS) {
223 int8_t rpp_sci_putc(uint8_t byte)
225 #if rppCONFIG_DRV == 1
226 drv_sci_send(1, &byte, portMAX_DELAY);
233 int16_t rpp_sci_getc()
237 #if rppCONFIG_DRV == 1
238 if(drv_sci_receive(1, &result, portMAX_DELAY) == FAILURE) {
243 return (int16_t)result;
245 #endif /* !FREERTOS_POSIX */
247 #endif /* rppCONFIG_INCLUDE_SCI */