From cfda9c99d7818521ae1f1056f261f123608b3452 Mon Sep 17 00:00:00 2001 From: Abhishek Malik Date: Wed, 14 Mar 2018 12:27:46 -0700 Subject: [PATCH] UART: Fixing UART issues on UP2 Signed-off-by: Abhishek Malik --- include/mraa_internal.h | 10 ++++++++++ src/mraa.c | 37 +++++++++++++++++++++++++++++++++---- src/x86/up2.c | 6 +++++- 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/include/mraa_internal.h b/include/mraa_internal.h index 6a6bfce..b9cb129 100644 --- a/include/mraa_internal.h +++ b/include/mraa_internal.h @@ -168,6 +168,16 @@ mraa_result_t mraa_atoi(char* intStr, int* value); */ int mraa_find_i2c_bus_pci(const char* pci_device, const char *pci_id, const char* adapter_name); +/** + * helper function to find the uart device based on pci data + * + * @param pci_dev_path points to the location of tty device which corresponds + * to the uart device available on the platform + * @param dev_name as retrieved from pci_dev_path + * @return Result of the operation + */ +mraa_result_t mraa_find_uart_bus_pci(const char* pci_dev_path, char** dev_name); + #if defined(IMRAA) /** * read Imraa subplatform lock file, caller is responsible to free return diff --git a/src/mraa.c b/src/mraa.c index 797ca36..b917dc4 100644 --- a/src/mraa.c +++ b/src/mraa.c @@ -282,21 +282,22 @@ mraa_deinit() } free(sub_plat); } -#if defined(JSONPLAT) if (plat->platform_type == MRAA_JSON_PLATFORM) { // Free the platform name free(plat->platform_name); plat->platform_name = NULL; + } - int i = 0; - // Free the UART device path + int i = 0; + // Free the UART device path + if ((plat->platform_type == MRAA_JSON_PLATFORM) || (plat->platform_type == MRAA_UP2)) { for (i = 0; i < plat->uart_dev_count; i++) { if (plat->uart_dev[i].device_path != NULL) { free(plat->uart_dev[i].device_path); } } } -#endif + free(plat); plat = NULL; @@ -1135,6 +1136,34 @@ mraa_link_targets(const char* filename, const char* targetname) } } +mraa_result_t +mraa_find_uart_bus_pci(const char* pci_dev_path, char** dev_name) +{ + char path[PATH_MAX]; + const int max_allowable_len = 16; + snprintf(path, PATH_MAX - 1, "%s", pci_dev_path); + if (!mraa_file_exist(path)) { + return MRAA_ERROR_INVALID_PARAMETER; + } + + struct dirent** namelist; + int n = scandir(path, &namelist, NULL, alphasort); + if (n <= 0) { + syslog(LOG_ERR, "Failed to find expected UART bus: %s", strerror(errno)); + return MRAA_ERROR_INVALID_RESOURCE; + } + + *dev_name = (char*) malloc(sizeof(char) * max_allowable_len); + + snprintf(*dev_name, max_allowable_len, "/dev/%s", namelist[n - 1]->d_name); + while (n--) { + free(namelist[n]); + } + free(namelist); + syslog(LOG_INFO, "UART device: %s selected for initialization", *dev_name); + return MRAA_SUCCESS; +} + static int mraa_count_i2c_files(const char* path, const struct stat* sb, int flag, struct FTW* ftwb) { diff --git a/src/x86/up2.c b/src/x86/up2.c index 5a2c7e2..87be439 100644 --- a/src/x86/up2.c +++ b/src/x86/up2.c @@ -242,7 +242,11 @@ mraa_up2_board() // Configure UART b->uart_dev_count = 0; b->def_uart_dev = 0; - b->uart_dev[0].device_path = "/dev/ttyS1"; + // setting up a default path + if (mraa_find_uart_bus_pci("/sys/bus/pci/devices/0000:00:18.1/dw-apb-uart.9/tty/", + &(b->uart_dev[0].device_path)) != MRAA_SUCCESS) { + goto error; + } // Configure UART #1 (default) mraa_up2_get_pin_index(b, "UART_RX", &(b->uart_dev[0].rx));