2014-07-10 00:55:50 +01:00
|
|
|
/*
|
|
|
|
|
* Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
|
|
|
|
|
* Copyright (c) 2014 Intel Corporation.
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
|
* the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#include "uart.h"
|
|
|
|
|
#include "mraa_internal.h"
|
|
|
|
|
|
|
|
|
|
mraa_uart_context
|
|
|
|
|
mraa_uart_init(int index)
|
|
|
|
|
{
|
2015-01-20 14:44:28 +00:00
|
|
|
if (plat == NULL) {
|
|
|
|
|
syslog(LOG_ERR, "uart: platform not initialised");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-20 18:43:58 +01:00
|
|
|
if (advance_func->uart_init_pre != NULL) {
|
2015-01-20 14:44:28 +00:00
|
|
|
if (advance_func->uart_init_pre(index) != MRAA_SUCCESS) {
|
|
|
|
|
syslog(LOG_ERR, "uart: failure in pre-init platform hook");
|
2014-10-20 18:43:58 +01:00
|
|
|
return NULL;
|
2015-01-20 14:44:28 +00:00
|
|
|
}
|
2014-10-20 18:43:58 +01:00
|
|
|
}
|
|
|
|
|
|
2015-01-20 14:44:28 +00:00
|
|
|
if (plat->uart_dev_count == 0) {
|
|
|
|
|
syslog(LOG_ERR, "uart: platform has no UARTs defined");
|
2014-07-10 00:55:50 +01:00
|
|
|
return NULL;
|
2015-01-20 14:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (plat->uart_dev_count <= index) {
|
|
|
|
|
syslog(LOG_ERR, "uart: platform has only %i", plat->uart_dev_count);
|
|
|
|
|
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 (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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-10 00:55:50 +01:00
|
|
|
|
|
|
|
|
mraa_uart_context dev = (mraa_uart_context) malloc(sizeof(struct _uart));
|
2015-01-20 14:44:28 +00:00
|
|
|
if (dev == NULL) {
|
|
|
|
|
syslog(LOG_CRIT, "uart: Failed to allocate memory for context");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2014-07-10 00:55:50 +01:00
|
|
|
memset(dev, 0, sizeof(struct _uart));
|
|
|
|
|
|
|
|
|
|
dev->index = index;
|
2015-04-29 15:51:07 -06:00
|
|
|
dev->fd = -1;
|
2015-01-20 16:22:04 +00:00
|
|
|
dev->path = (char*) plat->uart_dev[index].device_path;
|
2014-10-19 16:09:19 +01:00
|
|
|
if (advance_func->uart_init_post != NULL) {
|
|
|
|
|
mraa_result_t ret = advance_func->uart_init_post(dev);
|
|
|
|
|
if (ret != MRAA_SUCCESS) {
|
|
|
|
|
free(dev);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-10 00:55:50 +01:00
|
|
|
|
|
|
|
|
return dev;
|
|
|
|
|
}
|
2015-01-20 15:10:47 +00:00
|
|
|
|
|
|
|
|
char*
|
|
|
|
|
mraa_uart_get_dev_path(mraa_uart_context dev)
|
|
|
|
|
{
|
|
|
|
|
if (dev == NULL) {
|
|
|
|
|
syslog(LOG_ERR, "uart: get_device_path failed, context is NULL");
|
2015-02-18 12:38:44 -07:00
|
|
|
return NULL;
|
2015-01-20 15:10:47 +00:00
|
|
|
}
|
|
|
|
|
if (dev->path == NULL) {
|
|
|
|
|
syslog(LOG_ERR, "uart: device path undefined");
|
2015-02-18 12:38:44 -07:00
|
|
|
return NULL;
|
2015-01-20 15:10:47 +00:00
|
|
|
}
|
|
|
|
|
return dev->path;
|
|
|
|
|
}
|