-/* Copyright (C) 2013 Czech Technical University in Prague
+/* Copyright (C) 2013, 2015 Czech Technical University in Prague
*
* Authors:
* - Carlos Jenkins <carlos@jenkins.co.cr>
#include <stdio.h> // vsnprintf()
#include <stdarg.h> // va_start, va_end
-#if rppCONFIG_DRV == 1
+#ifndef FREERTOS_POSIX
#include "drv/drv.h"
#endif
int8_t rpp_sci_init()
{
- if(initialized) {
- return FAILURE;
- }
- initialized = TRUE;
-
- #if rppCONFIG_DRV == 1
- drv_sci_init();
- drv_sci_set_crlf_conv_en(TRUE);
- #elif defined(FREERTOS_POSIX)
- rpp_sci_posix_init();
- #endif
-
- return SUCCESS;
+ if (initialized)
+ return FAILURE;
+ initialized = TRUE;
+
+#ifndef FREERTOS_POSIX
+ drv_sci_init();
+ drv_sci_set_crlf_conv_en(TRUE);
+#endif
+ return SUCCESS;
}
boolean_t rpp_sci_setup(uint32_t baud)
{
- boolean_t known = FALSE;
-
- if(baud == 0) {
- baud = 9600;
- }
-
- // FIXME This is a standard list of baud rates. This should include only
- // tested baud rates.
- switch(baud) {
- case 110:
- known = TRUE;
- break;
- case 300:
- known = TRUE;
- break;
- case 600:
- known = TRUE;
- break;
- case 1200:
- known = TRUE;
- break;
- case 2400:
- known = TRUE;
- break;
- case 4800:
- known = TRUE;
- break;
- case 9600:
- known = TRUE;
- break;
- case 14400:
- known = TRUE;
- break;
- case 19200:
- known = TRUE;
- break;
- case 28800:
- known = TRUE;
- break;
- case 38400:
- known = TRUE;
- break;
- case 56000:
- known = TRUE;
- break;
- case 57600:
- known = TRUE;
- break;
- case 115200:
- known = TRUE;
- break;
- }
-
- #if rppCONFIG_DRV == 1
- drv_sci_set_baudrate(baud);
- #endif
-
- return known;
+ boolean_t known = FALSE;
+ // FIXME This is a standard list of baud rates. This should include only
+ // tested baud rates.
+ static const uint32_t baud_list[] = {110, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 56000, 57600, 115200};
+
+ if (baud == 0)
+ baud = 9600;
+ uint32_t i;
+ for (i = 0; i < ARRAY_SIZE(baud_list); i++) {
+ if (baud == baud_list[i]) {
+ known = TRUE;
+ break;
+ }
+ }
+
+#ifndef FREERTOS_POSIX
+ drv_sci_set_baudrate(baud);
+#endif
+ return known;
}
#ifndef FREERTOS_POSIX
uint16_t rpp_sci_available()
{
- uint16_t available = 0;
+ uint16_t available = 0;
- #if rppCONFIG_DRV == 1
- available = drv_sci_available();
- #endif
+ available = drv_sci_available();
- return available;
+ return available;
}
-int8_t rpp_sci_read(uint32_t amount, uint8_t* buffer)
+int8_t rpp_sci_read(uint32_t amount, uint8_t *buffer)
{
- #if rppCONFIG_DRV == 1
- drv_sci_receive(amount, buffer, portMAX_DELAY);
- #endif
+ drv_sci_receive(amount, buffer, portMAX_DELAY);
- return SUCCESS;
+ return SUCCESS;
}
-int8_t rpp_sci_read_nb(uint32_t amount, uint8_t* buffer)
+int8_t rpp_sci_read_nb(uint32_t amount, uint8_t *buffer)
{
- #if rppCONFIG_DRV == 1
- if(drv_sci_receive(amount, buffer, 0) != SUCCESS) {
- return FAILURE;
- }
- #endif
+ if (drv_sci_receive(amount, buffer, 0) != SUCCESS)
+ return FAILURE;
- return SUCCESS;
+ return SUCCESS;
}
-int8_t rpp_sci_write(uint32_t amount, uint8_t* data)
+int8_t rpp_sci_write(uint32_t amount, uint8_t *data)
{
- #if rppCONFIG_DRV == 1
- drv_sci_send(amount, data, portMAX_DELAY);
- #endif
+ drv_sci_send(amount, data, portMAX_DELAY);
- return SUCCESS;
+ return SUCCESS;
}
-int8_t rpp_sci_write_nb(uint32_t amount, uint8_t* data)
+int8_t rpp_sci_write_nb(uint32_t amount, uint8_t *data)
{
- #if rppCONFIG_DRV == 1
- if(drv_sci_send(amount, data, 0) != SUCCESS) {
- return FAILURE;
- }
- #endif
+ if (drv_sci_send(amount, data, 0) != SUCCESS)
+ return FAILURE;
- return SUCCESS;
+ return SUCCESS;
}
int8_t rpp_sci_flush(boolean_t buff)
{
- #if rppCONFIG_DRV == 1
- return drv_sci_flush(buff);
- #else
- return SUCCESS;
- #endif
+ return drv_sci_flush(buff);
}
-int32_t rpp_sci_printk(const char* format, ...)
+int32_t rpp_sci_printk(const char *format, ...)
{
- char str[MAX_BUFFER_LEN];
- int length = -1;
- va_list argList;
- va_start(argList, format);
-
- length = vsnprintf(str, sizeof(str), format, argList);
-
- va_end(argList);
-
- if(length > 0) {
- #if rppCONFIG_DRV == 1
- // According to the C stdlib about vsnprintf:
- // If the resulting string would be longer than n-1 characters, the
- // remaining characters are discarded and not stored, but counted
- // for the value returned by the function.
- // In consequence we need to trim the value if larger than buffer.
- if(length > sizeof(str)) {
- length = sizeof(str);
- }
-
- drv_sci_send_imm((uint32_t) length, (uint8_t*) str);
-
- #else
- length = -1;
- #endif
- }
-
- return length;
+ char str[MAX_BUFFER_LEN];
+ int length = -1;
+ va_list argList;
+
+ va_start(argList, format);
+
+ length = vsnprintf(str, sizeof(str), format, argList);
+
+ va_end(argList);
+
+ if (length > 0) {
+ // According to the C stdlib about vsnprintf:
+ // If the resulting string would be longer than n-1 characters, the
+ // remaining characters are discarded and not stored, but counted
+ // for the value returned by the function.
+ // In consequence we need to trim the value if larger than buffer.
+ if (length > sizeof(str))
+ length = sizeof(str);
+
+ drv_sci_send_imm((uint32_t)length, (uint8_t *)str);
+ }
+
+ return length;
}
-int32_t rpp_sci_printkb(const char* format, ...)
+int32_t rpp_sci_printkb(const char *format, ...)
{
- char str[MAX_BUFFER_LEN];
- int length = -1;
- va_list argList;
- va_start(argList, format);
-
- length = vsnprintf(str, sizeof(str), format, argList);
-
- va_end(argList);
-
- if(length > 0) {
- #if rppCONFIG_DRV == 1
- // According to the C stdlib about vsnprintf:
- // If the resulting string would be longer than n-1 characters, the
- // remaining characters are discarded and not stored, but counted
- // for the value returned by the function.
- // In consequence we need to trim the value if larger than buffer.
- if(length > sizeof(str)) {
- length = sizeof(str);
- }
-
- return drv_sci_send_try_append((uint32_t) length, (uint8_t*) str);
-
- #else
- return -1;
- #endif
- }
- return length;
+ char str[MAX_BUFFER_LEN];
+ int length = -1;
+ va_list argList;
+
+ va_start(argList, format);
+
+ length = vsnprintf(str, sizeof(str), format, argList);
+
+ va_end(argList);
+
+ if (length > 0) {
+ // According to the C stdlib about vsnprintf:
+ // If the resulting string would be longer than n-1 characters, the
+ // remaining characters are discarded and not stored, but counted
+ // for the value returned by the function.
+ // In consequence we need to trim the value if larger than buffer.
+ if (length > sizeof(str))
+ length = sizeof(str);
+
+ return drv_sci_send_try_append((uint32_t)length, (uint8_t *)str);
+
+ }
+ return length;
}
-int32_t rpp_sci_printf(const char* format, ...)
+int32_t rpp_sci_printf(const char *format, ...)
{
int length = -1;
- va_list argList;
- va_start(argList, format);
- length = rpp_sci_vprintf(format, argList);
- va_end(argList);
+ va_list argList;
- return length;
+ va_start(argList, format);
+ length = rpp_sci_vprintf(format, argList);
+ va_end(argList);
+
+ return length;
}
-int32_t rpp_sci_vprintf(const char* format, va_list argList)
+int32_t rpp_sci_vprintf(const char *format, va_list argList)
{
- char str[MAX_BUFFER_LEN];
- int length = -1;
-
- length = vsnprintf(str, sizeof(str), format, argList);
-
- if(length > 0) {
- #if rppCONFIG_DRV == 1
- // According to the C stdlib about vsnprintf:
- // If the resulting string would be longer than n-1 characters, the
- // remaining characters are discarded and not stored, but counted
- // for the value returned by the function.
- // In consequence we need to trim the value if larger than buffer.
- if(length > sizeof(str)) {
- length = sizeof(str);
- }
- if(drv_sci_send(
- (uint32_t)length,
- (uint8_t*)str,
- portMAX_DELAY) != SUCCESS) {
- return -1;
- }
- #endif
- }
-
- return length;
+ char str[MAX_BUFFER_LEN];
+ int length = -1;
+
+ length = vsnprintf(str, sizeof(str), format, argList);
+
+ if (length > 0) {
+ // According to the C stdlib about vsnprintf:
+ // If the resulting string would be longer than n-1 characters, the
+ // remaining characters are discarded and not stored, but counted
+ // for the value returned by the function.
+ // In consequence we need to trim the value if larger than buffer.
+ if (length > sizeof(str))
+ length = sizeof(str);
+ if (drv_sci_send(
+ (uint32_t)length,
+ (uint8_t *)str,
+ portMAX_DELAY) != SUCCESS)
+ return -1;
+ }
+
+ return length;
}
int8_t rpp_sci_putc(uint8_t byte)
{
- #if rppCONFIG_DRV == 1
- drv_sci_send(1, &byte, portMAX_DELAY);
- #endif
+ drv_sci_send(1, &byte, portMAX_DELAY);
- return SUCCESS;
+ return SUCCESS;
}
int16_t rpp_sci_getc()
{
- uint8_t result = 0;
+ uint8_t result = 0;
- #if rppCONFIG_DRV == 1
- if(drv_sci_receive(1, &result, portMAX_DELAY) == FAILURE) {
- return FAILURE;
- }
- #endif
+ if (drv_sci_receive(1, &result, portMAX_DELAY) == FAILURE)
+ return FAILURE;
- return (int16_t)result;
+ return (int16_t)result;
}
#endif /* !FREERTOS_POSIX */