}
}
+static mraa_uart_context
+mraa_uart_init_internal(mraa_adv_func_t* func_table)
+{
+ mraa_uart_context dev = (mraa_uart_context) calloc(1, sizeof(struct _uart));
+ if (dev == NULL) {
+ syslog(LOG_CRIT, "uart: Failed to allocate memory for context");
+ return NULL;
+ }
+ dev->index = -1;
+ dev->fd = -1;
+ dev->advance_func = func_table;
+
+ return dev;
+}
+
mraa_uart_context
mraa_uart_init(int index)
{
return NULL;
}
- if (advance_func->uart_init_pre != NULL) {
- if (advance_func->uart_init_pre(index) != MRAA_SUCCESS) {
+ if (mraa_is_sub_platform_id(index)) {
+ syslog(LOG_NOTICE, "uart: Using sub platform is not supported");
+ return NULL;
+ }
+
+ if (plat->adv_func->uart_init_pre != NULL) {
+ if (plat->adv_func->uart_init_pre(index) != MRAA_SUCCESS) {
syslog(LOG_ERR, "uart: failure in pre-init platform hook");
return NULL;
}
return NULL;
}
- int pos = plat->uart_dev[index].rx;
- if (pos >= 0) {
- if (plat->pins[pos].uart.mux_total > 0) {
- if (mraa_setup_mux_mapped(plat->pins[pos].uart) != MRAA_SUCCESS) {
- syslog(LOG_ERR, "uart: failed to setup muxes for RX pin");
- return NULL;
+ if (!plat->no_bus_mux) {
+ int pos = plat->uart_dev[index].rx;
+ if (pos >= 0) {
+ if (plat->pins[pos].uart.mux_total > 0) {
+ if (mraa_setup_mux_mapped(plat->pins[pos].uart) != MRAA_SUCCESS) {
+ syslog(LOG_ERR, "uart: failed to setup muxes for RX pin");
+ return NULL;
+ }
}
}
- }
- if (pos >= 0) {
pos = plat->uart_dev[index].tx;
- if (plat->pins[pos].uart.mux_total > 0) {
- if (mraa_setup_mux_mapped(plat->pins[pos].uart) != MRAA_SUCCESS) {
- syslog(LOG_ERR, "uart: failed to setup muxes for TX pin");
- return NULL;
+ if (pos >= 0) {
+ if (plat->pins[pos].uart.mux_total > 0) {
+ if (mraa_setup_mux_mapped(plat->pins[pos].uart) != MRAA_SUCCESS) {
+ syslog(LOG_ERR, "uart: failed to setup muxes for TX pin");
+ return NULL;
+ }
}
}
}
}
dev->index = index; //Set the board Index.
- if (advance_func->uart_init_post != NULL) {
- mraa_result_t ret = advance_func->uart_init_post(dev);
+ if (IS_FUNC_DEFINED(dev, uart_init_post)) {
+ mraa_result_t ret = dev->advance_func->uart_init_post(dev);
if (ret != MRAA_SUCCESS) {
free(dev);
return NULL;
mraa_uart_context
mraa_uart_init_raw(const char* path)
{
- mraa_uart_context dev = (mraa_uart_context) malloc(sizeof(struct _uart));
+ mraa_uart_context dev = mraa_uart_init_internal(plat == NULL ? NULL : plat->adv_func);
if (dev == NULL) {
- syslog(LOG_CRIT, "uart: Failed to allocate memory for context");
+ syslog(LOG_ERR, "uart: Failed to allocate memory for context");
return NULL;
}
- memset(dev, 0, sizeof(struct _uart));
-
- dev->index = -1;
- dev->fd = -1;
dev->path = path;
if (!dev->path) {
// handling, such as flow control or line editing semantics.
// cfmakeraw is not POSIX!
cfmakeraw(&termio);
+ if (tcsetattr(dev->fd, TCSAFLUSH, &termio) < 0) {
+ syslog(LOG_ERR, "uart: tcsetattr() failed after cfmakeraw()");
+ close(dev->fd);
+ free(dev);
+ return NULL;
+ }
if (mraa_uart_set_baudrate(dev, 9600) != MRAA_SUCCESS) {
close(dev->fd);
return MRAA_ERROR_INVALID_HANDLE;
}
- if (!tcdrain(dev->fd)) {
+ if (tcdrain(dev->fd) == -1) {
return MRAA_ERROR_FEATURE_NOT_SUPPORTED;
}
termio.c_cflag &= ~(PARENB | PARODD);
break;
case MRAA_UART_PARITY_EVEN:
+ termio.c_cflag |= PARENB;
termio.c_cflag &= ~PARODD;
- termio.c_cflag |= PARODD;
break;
case MRAA_UART_PARITY_ODD:
termio.c_cflag |= PARENB | PARODD;