1 /* Copyright (C) 2013, 2015 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 #ifndef FREERTOS_POSIX
29 static boolean_t initialized = FALSE;
37 #ifndef FREERTOS_POSIX
39 drv_sci_set_crlf_conv_en(TRUE);
45 boolean_t rpp_sci_setup(uint32_t baud)
47 boolean_t known = FALSE;
48 // FIXME This is a standard list of baud rates. This should include only
50 static const uint32_t baud_list[] = {110, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 56000, 57600, 115200};
55 for (i = 0; i < ARRAY_SIZE(baud_list); i++) {
56 if (baud == baud_list[i]) {
62 #ifndef FREERTOS_POSIX
63 drv_sci_set_baudrate(baud);
69 #ifndef FREERTOS_POSIX
70 uint16_t rpp_sci_available()
72 uint16_t available = 0;
74 available = drv_sci_available();
80 int8_t rpp_sci_read(uint32_t amount, uint8_t *buffer)
82 drv_sci_receive(amount, buffer, portMAX_DELAY);
88 int8_t rpp_sci_read_nb(uint32_t amount, uint8_t *buffer)
90 if (drv_sci_receive(amount, buffer, 0) != SUCCESS)
97 int8_t rpp_sci_write(uint32_t amount, uint8_t *data)
99 drv_sci_send(amount, data, portMAX_DELAY);
105 int8_t rpp_sci_write_nb(uint32_t amount, uint8_t *data)
107 if (drv_sci_send(amount, data, 0) != SUCCESS)
114 int8_t rpp_sci_flush(boolean_t buff)
116 return drv_sci_flush(buff);
119 int32_t rpp_sci_printk(const char *format, ...)
121 char str[MAX_BUFFER_LEN];
125 va_start(argList, format);
127 length = vsnprintf(str, sizeof(str), format, argList);
132 // According to the C stdlib about vsnprintf:
133 // If the resulting string would be longer than n-1 characters, the
134 // remaining characters are discarded and not stored, but counted
135 // for the value returned by the function.
136 // In consequence we need to trim the value if larger than buffer.
137 if (length > sizeof(str))
138 length = sizeof(str);
140 drv_sci_send_imm((uint32_t)length, (uint8_t *)str);
146 int32_t rpp_sci_printkb(const char *format, ...)
148 char str[MAX_BUFFER_LEN];
152 va_start(argList, format);
154 length = vsnprintf(str, sizeof(str), format, argList);
159 // According to the C stdlib about vsnprintf:
160 // If the resulting string would be longer than n-1 characters, the
161 // remaining characters are discarded and not stored, but counted
162 // for the value returned by the function.
163 // In consequence we need to trim the value if larger than buffer.
164 if (length > sizeof(str))
165 length = sizeof(str);
167 return drv_sci_send_try_append((uint32_t)length, (uint8_t *)str);
173 int32_t rpp_sci_printf(const char *format, ...)
178 va_start(argList, format);
179 length = rpp_sci_vprintf(format, argList);
185 int32_t rpp_sci_vprintf(const char *format, va_list argList)
187 char str[MAX_BUFFER_LEN];
190 length = vsnprintf(str, sizeof(str), format, argList);
193 // According to the C stdlib about vsnprintf:
194 // If the resulting string would be longer than n-1 characters, the
195 // remaining characters are discarded and not stored, but counted
196 // for the value returned by the function.
197 // In consequence we need to trim the value if larger than buffer.
198 if (length > sizeof(str))
199 length = sizeof(str);
203 portMAX_DELAY) != SUCCESS)
211 int8_t rpp_sci_putc(uint8_t byte)
213 drv_sci_send(1, &byte, portMAX_DELAY);
219 int16_t rpp_sci_getc()
223 if (drv_sci_receive(1, &result, portMAX_DELAY) == FAILURE)
226 return (int16_t)result;
228 #endif /* !FREERTOS_POSIX */