Private
Public Access
2
0

tools: add new tools directory

This is the start of the series of commits focussed on
cleaning up the exmaples/ directory.

This commit moves mraa-gpio, mraa-i2c, mraa-uart files
out of examples directory into new tools directory. Moved
files are the good cadidates to be treated as tools
instead of examples.

Signed-off-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
Manivannan Sadhasivam
2017-10-20 13:56:58 +05:30
committed by Brendan Le Foll
parent c24a8f8b20
commit ec26f92fe6
6 changed files with 26 additions and 18 deletions

View File

@@ -9,17 +9,12 @@ add_executable (spi_mcp4261 spi_mcp4261.c)
add_executable (mmap-io2 mmap-io2.c)
add_executable (blink_onboard blink_onboard.c)
add_executable (uart uart.c)
add_executable (mraa-gpio mraa-gpio.c)
add_executable (mraa-i2c mraa-i2c.c)
add_executable (mraa-uart mraa-uart.c)
add_executable (spi_max7219 spi_max7219.c)
if (NOT ANDROID_TOOLCHAIN)
add_executable (iio_driver iio_driver.c)
endif ()
include_directories(${PROJECT_SOURCE_DIR}/api)
# FIXME Hack to access mraa internal types used by mraa-i2c
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${PROJECT_SOURCE_DIR}/api/mraa)
target_link_libraries (hellomraa mraa)
@@ -33,9 +28,6 @@ target_link_libraries (spi_mcp4261 mraa)
target_link_libraries (mmap-io2 mraa)
target_link_libraries (blink_onboard mraa)
target_link_libraries (uart mraa)
target_link_libraries (mraa-gpio mraa)
target_link_libraries (mraa-i2c mraa)
target_link_libraries (mraa-uart mraa)
target_link_libraries (spi_max7219 mraa)
if (NOT ANDROID_TOOLCHAIN)
target_link_libraries (iio_driver mraa)
@@ -57,13 +49,3 @@ if (NOT ANDROID_TOOLCHAIN)
endif ()
install (DIRECTORY ${PROJECT_SOURCE_DIR}/examples/ DESTINATION ${CMAKE_INSTALL_DATADIR}/mraa/examples)
if (INSTALLGPIOTOOL AND NOT INSTALLTOOLS)
install (TARGETS mraa-gpio DESTINATION bin)
endif()
if (INSTALLTOOLS)
install (TARGETS mraa-gpio DESTINATION bin)
install (TARGETS mraa-i2c DESTINATION bin)
install (TARGETS mraa-uart DESTINATION bin)
endif()

View File

@@ -1,240 +0,0 @@
/*
* Author: Brendan Le Foll <brendan.le.foll@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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "mraa/gpio.h"
struct gpio_source {
int pin;
mraa_gpio_context context;
};
void
print_version()
{
fprintf(stdout, "Version %s on %s\n", mraa_get_version(), mraa_get_platform_name());
}
void
print_help()
{
fprintf(stdout, "list List pins\n");
fprintf(stdout, "set pin level Set pin to level (0/1)\n");
fprintf(stdout, "setraw pin level Set pin to level (0/1) via mmap (if available)\n");
fprintf(stdout, "get pin Get pin level\n");
fprintf(stdout, "getraw pin Get pin level via mmap (if available)\n");
fprintf(stdout, "monitor pin Monitor pin level changes\n");
fprintf(stdout, "version Get mraa version and board name\n");
}
void
print_command_error()
{
fprintf(stdout, "Invalid command, options are:\n");
print_help();
}
int
list_platform_pins(uint8_t platform_offset)
{
int pin_count = mraa_get_platform_pin_count(platform_offset);
int i;
for (i = 0; i < pin_count; ++i) {
int pin_id = platform_offset > 0 ? mraa_get_sub_platform_id(i) : i;
char* pin_name = mraa_get_pin_name(pin_id);
if (strcmp(pin_name, "INVALID") != 0 && mraa_pin_mode_test(pin_id, MRAA_PIN_VALID)) {
fprintf(stdout, "%02d ", pin_id);
fprintf(stdout, "%*s: ", (MRAA_PIN_NAME_SIZE - 1), pin_name);
if (mraa_pin_mode_test(pin_id, MRAA_PIN_GPIO))
fprintf(stdout, "GPIO ");
if (mraa_pin_mode_test(pin_id, MRAA_PIN_I2C))
fprintf(stdout, "I2C ");
if (mraa_pin_mode_test(pin_id, MRAA_PIN_SPI))
fprintf(stdout, "SPI ");
if (mraa_pin_mode_test(pin_id, MRAA_PIN_PWM))
fprintf(stdout, "PWM ");
if (mraa_pin_mode_test(pin_id, MRAA_PIN_AIO))
fprintf(stdout, "AIO ");
if (mraa_pin_mode_test(pin_id, MRAA_PIN_UART))
fprintf(stdout, "UART ");
fprintf(stdout, "\n");
}
}
return pin_count;
}
int
list_pins()
{
int pin_count = 0;
pin_count += list_platform_pins(MRAA_MAIN_PLATFORM_OFFSET);
pin_count += list_platform_pins(MRAA_SUB_PLATFORM_OFFSET);
if (pin_count == 0) {
fprintf(stdout, "No Pins\n");
}
return pin_count;
}
mraa_result_t
gpio_set(int pin, int level, mraa_boolean_t raw)
{
mraa_gpio_context gpio;
if (raw) {
gpio = mraa_gpio_init_raw(pin);
}
else {
gpio = mraa_gpio_init(pin);
}
if (gpio != NULL) {
mraa_gpio_dir(gpio, MRAA_GPIO_OUT);
if (raw != 0) {
if (mraa_gpio_use_mmaped(gpio, 1) != MRAA_SUCCESS) {
fprintf(stdout,
"mmapped access to gpio %d not supported, falling back to normal mode\n", pin);
}
}
mraa_gpio_write(gpio, level);
return MRAA_SUCCESS;
}
return MRAA_ERROR_INVALID_RESOURCE;
}
mraa_result_t
gpio_get(int pin, int* level, mraa_boolean_t raw)
{
mraa_gpio_context gpio = mraa_gpio_init(pin);
if (gpio != NULL) {
mraa_gpio_dir(gpio, MRAA_GPIO_IN);
if (raw != 0) {
if (mraa_gpio_use_mmaped(gpio, 1) != MRAA_SUCCESS) {
fprintf(stdout,
"mmapped access to gpio %d not supported, falling back to normal mode\n", pin);
}
}
*level = mraa_gpio_read(gpio);
return MRAA_SUCCESS;
}
return MRAA_ERROR_INVALID_RESOURCE;
}
void
gpio_isr_handler(void* args)
{
struct gpio_source* gpio_info = (struct gpio_source*) args;
int level = mraa_gpio_read(gpio_info->context);
fprintf(stdout, "Pin %d = %d\n", gpio_info->pin, level);
}
mraa_result_t
gpio_isr_start(struct gpio_source* gpio_info)
{
gpio_info->context = mraa_gpio_init(gpio_info->pin);
if (gpio_info->context != NULL) {
mraa_result_t status = mraa_gpio_dir(gpio_info->context, MRAA_GPIO_IN);
if (status == MRAA_SUCCESS) {
status = mraa_gpio_isr(gpio_info->context, MRAA_GPIO_EDGE_BOTH, &gpio_isr_handler, gpio_info);
}
return status;
} else {
return MRAA_ERROR_INVALID_RESOURCE;
}
}
mraa_result_t
gpio_isr_stop(struct gpio_source* gpio_info)
{
mraa_gpio_isr_exit(gpio_info->context);
mraa_gpio_close(gpio_info->context);
return MRAA_SUCCESS;
}
int
main(int argc, char** argv)
{
if (argc == 1) {
print_command_error();
}
if (argc > 1) {
if (strcmp(argv[1], "list") == 0) {
list_pins();
} else if (strcmp(argv[1], "help") == 0) {
print_help();
} else if (strcmp(argv[1], "version") == 0) {
print_version();
} else if ((strcmp(argv[1], "set") == 0) || (strcmp(argv[1], "setraw") == 0)) {
if (argc == 4) {
int pin = atoi(argv[2]);
mraa_boolean_t rawmode = strcmp(argv[1], "setraw") == 0;
if (gpio_set(pin, atoi(argv[3]), rawmode) != MRAA_SUCCESS)
fprintf(stdout, "Could not initialize gpio %d\n", pin);
} else {
print_command_error();
}
} else if ((strcmp(argv[1], "get") == 0) || (strcmp(argv[1], "getraw") == 0)) {
if (argc == 3) {
int pin = atoi(argv[2]);
int level;
mraa_boolean_t rawmode = strcmp(argv[1], "getraw") == 0;
if (gpio_get(pin, &level, rawmode) == MRAA_SUCCESS) {
fprintf(stdout, "Pin %d = %d\n", pin, level);
} else {
fprintf(stdout, "Could not initialize gpio %d\n", pin);
}
} else {
print_command_error();
}
} else if (strcmp(argv[1], "monitor") == 0) {
if (argc == 3) {
int pin = atoi(argv[2]);
struct gpio_source gpio_info;
gpio_info.pin = pin;
if (gpio_isr_start(&gpio_info) == MRAA_SUCCESS) {
fprintf(stdout, "Monitoring level changes to pin %d. Press RETURN to exit.\n", pin);
gpio_isr_handler(&gpio_info);
char aux = 0;
do {
fflush(stdin);
fscanf(stdin, "%c", &aux);
} while (aux != '\n');
gpio_isr_stop(&gpio_info);
} else {
fprintf(stdout, "Failed to register ISR for pin %d\n", pin);
}
} else {
print_command_error();
}
} else {
print_command_error();
}
}
return 0;
}

View File

@@ -1,275 +0,0 @@
/*
* Author: Henry Bruce <henry.bruce@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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "mraa/i2c.h"
#include "mraa_internal_types.h"
extern mraa_board_t* plat;
void
print_version()
{
fprintf(stdout, "Version %s on %s", mraa_get_version(), mraa_get_platform_name());
if (plat != NULL && plat->sub_platform != NULL)
fprintf(stdout, " with %s", plat->sub_platform->platform_name);
fprintf(stdout, "\n");
}
void
print_help()
{
fprintf(stdout, "version Get mraa version and board name\n");
fprintf(stdout, "list List available busses\n");
fprintf(stdout, "detect bus List detected devices on specified bus\n");
fprintf(stdout, "get bus device reg Get value from specified device register\n");
fprintf(stdout, "set bus device reg value Set specified device register to value\n");
}
void
print_command_error()
{
fprintf(stdout, "Invalid command, options are:\n");
print_help();
}
void
print_bus(mraa_board_t* board)
{
int i, bus;
for (i = 0; i < board->i2c_bus_count; ++i) {
char* busType;
switch (board->platform_type) {
case MRAA_FTDI_FT4222:
busType = "ft4222";
bus = mraa_get_sub_platform_id(i);
break;
default:
busType = "linux";
bus = i;
break;
}
int id = board->i2c_bus[i].bus_id;
fprintf(stdout, "Bus %3d: id=%02d type=%s ", bus, id, busType);
if (i == board->def_i2c_bus)
fprintf(stdout, " default");
if (id == -1)
fprintf(stdout, " disabled");
fprintf(stdout, "\n");
}
}
void
print_busses()
{
print_bus(plat);
if (mraa_has_sub_platform())
print_bus(plat->sub_platform);
}
mraa_result_t
i2c_get(int bus, uint8_t device_address, uint8_t register_address, uint8_t* data)
{
mraa_result_t status = MRAA_SUCCESS;
mraa_i2c_context i2c = mraa_i2c_init(bus);
if (i2c == NULL) {
return MRAA_ERROR_NO_RESOURCES;
}
status = mraa_i2c_address(i2c, device_address);
if (status != MRAA_SUCCESS) {
goto i2c_get_exit;
}
status = mraa_i2c_write_byte(i2c, register_address);
if (status != MRAA_SUCCESS) {
goto i2c_get_exit;
}
status = mraa_i2c_read(i2c, data, 1) == 1 ? MRAA_SUCCESS : MRAA_ERROR_UNSPECIFIED;
if (status != MRAA_SUCCESS) {
goto i2c_get_exit;
}
i2c_get_exit:
mraa_i2c_stop(i2c);
return status;
}
mraa_result_t
i2c_set(int bus, uint8_t device_address, uint8_t register_address, uint8_t data)
{
mraa_result_t status = MRAA_SUCCESS;
mraa_i2c_context i2c = mraa_i2c_init(bus);
if (i2c == NULL) {
return MRAA_ERROR_NO_RESOURCES;
}
status = mraa_i2c_address(i2c, device_address);
if (status != MRAA_SUCCESS) {
fprintf(stderr, "Could not set i2c device address\n");
goto i2c_set_exit;
}
status = mraa_i2c_write_byte_data(i2c, data, register_address);
if (status != MRAA_SUCCESS) {
fprintf(stderr, "Could not write to i2c register. Status = %d\n", status);
goto i2c_set_exit;
}
i2c_set_exit:
mraa_i2c_stop(i2c);
return status;
}
void
i2c_detect_devices(int bus)
{
mraa_i2c_context i2c = mraa_i2c_init(bus);
if (i2c == NULL) {
return;
}
int addr;
for (addr = 0x0; addr < 0x80; ++addr) {
uint8_t value;
if ((addr) % 16 == 0)
printf("%02x: ", addr);
if (i2c_get(bus, addr, 0, &value) == MRAA_SUCCESS)
printf("%02x ", addr);
else
printf("-- ");
if ((addr + 1) % 16 == 0)
printf("\n");
}
}
int
process_command(int argc, char** argv)
{
int status = 0;
if (strncmp(argv[1], "help", strlen("help") + 1) == 0) {
print_help();
return 0;
} else if (strncmp(argv[1], "version", strlen("version") + 1) == 0) {
print_version();
return 0;
} else if (strncmp(argv[1], "list", strlen("list") + 1) == 0) {
print_busses();
return 0;
} else if (strncmp(argv[1], "detect", strlen("detect") + 1) == 0) {
if (argc == 3) {
int bus = strtol(argv[2], NULL, 0);
i2c_detect_devices(bus);
return 0;
} else {
print_command_error();
return 1;
}
} else if ((strncmp(argv[1], "get", strlen("get") + 1) == 0) ||
(strncmp(argv[1], "getrpt", strlen("getrpt") + 1) == 0)) {
if (argc == 5) {
int interation = 0;
mraa_boolean_t should_repeat = strncmp(argv[1], "getrpt", strlen("getrpt") + 1) == 0;
int bus = strtol(argv[2], NULL, 0);
uint8_t device_address = strtol(argv[3], NULL, 0);
uint8_t register_address = strtol(argv[4], NULL, 0);
// fprintf(stdout, "Device %02X, Register = %02X\n", device_address, register_address);
uint8_t data;
do {
if (i2c_get(bus, device_address, register_address, &data) == MRAA_SUCCESS) {
if (should_repeat)
fprintf(stdout, "%4d: ", interation);
fprintf(stdout, "Register %#02X = %#02X\n", register_address, data);
status = 0;
} else {
fprintf(stdout, "i2c get failed\n");
status = 1;
}
interation++;
usleep(10000);
} while (should_repeat && status == 0);
} else {
print_command_error();
status = 1;
}
return status;
} else if ((strncmp(argv[1], "set", strlen("set") + 1) == 0)) {
if (argc == 6) {
int bus = strtol(argv[2], NULL, 0);
uint8_t device_address = strtol(argv[3], NULL, 0);
uint8_t register_address = strtol(argv[4], NULL, 0);
uint8_t value = strtol(argv[5], NULL, 0);
fprintf(stdout, "Device %02X, Register = %02X, Value = %02X\n", device_address,
register_address, value);
if (i2c_set(bus, device_address, register_address, value) != MRAA_SUCCESS) {
fprintf(stdout, "i2c set failed\n");
return 0;
}
return 1;
} else {
print_command_error();
return 1;
}
} else {
print_command_error();
return 1;
}
}
void
run_interactive_mode()
{
char command[80];
while (1) {
int i, argc = 1;
char* argv[32];
char* arg;
argv[0] = "mraa-i2c";
fprintf(stdout, "Command: ");
fgets(command, 80, stdin);
command[strlen(command) - 1] = 0;
if (strncmp(command, "q", strlen("q") + 1) == 0)
return;
char* str = strtok(command, " ");
int len = 0;
while (str != NULL) {
len = strlen(str) + 1;
arg = malloc(len);
argv[argc++] = strncpy(arg, str, len);
str = strtok(NULL, " ");
}
process_command(argc, argv);
for (i = 1; i < argc; ++i)
free(argv[i]);
}
}
int
main(int argc, char** argv)
{
mraa_set_log_level(7);
if (argc == 1) {
run_interactive_mode();
return 0;
} else
return process_command(argc, argv);
}

View File

@@ -1,301 +0,0 @@
/*
* Author: Tapani Utriainen <tapani@technexion.com>
* Copyright (c) 2017 TechNexion Ltd.
*
* 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 <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include "mraa.h"
#ifndef FALSE
#define FALSE 0
#define TRUE (!FALSE)
#endif
static const char *undefined = "<undefined>";
static const char paritymode_table[] = {
[MRAA_UART_PARITY_NONE] = 'N',
[MRAA_UART_PARITY_EVEN] = 'E',
[MRAA_UART_PARITY_ODD] = 'O',
[MRAA_UART_PARITY_MARK] = 'M',
[MRAA_UART_PARITY_SPACE] = 'S'
};
/* ------------------------------------------------------------------------ */
mraa_uart_context
uarttool_find(const char *str) {
/* Try to find the uart by name, then by index, by devpath and as last resort create a raw uart */
int uart_index = mraa_uart_lookup(str);
if (uart_index < 0) {
/* name lookup failed, try index if the first character is a digit */
if (isdigit(*str)) {
uart_index = atoi(str);
} else {
const char *devpath;
int number_of_uarts = mraa_get_uart_count();
for (uart_index = 0; uart_index < number_of_uarts; uart_index++) {
mraa_result_t res = mraa_uart_settings(uart_index, NULL, &devpath, NULL, NULL, NULL, NULL, NULL, NULL);
if (res == MRAA_SUCCESS && devpath != NULL && !strcmp(devpath, str)) break;
}
if (uart_index >= number_of_uarts) {
return mraa_uart_init_raw(str);
}
}
}
return mraa_uart_init(uart_index);
}
/* ------------------------------------------------------------------------ */
double
now(void) {
struct timeval tv;
gettimeofday(&tv, NULL);
return (double)tv.tv_usec * 1e-6 + (double)tv.tv_sec;
}
/* ------------------------------------------------------------------------ */
void
uarttool_read_and_print(mraa_uart_context uart, double receive_timeout) {
char buf;
do {
double start = now();
if (mraa_uart_data_available(uart, 1.0 + receive_timeout * 1000.0)) {
mraa_uart_read(uart, &buf, 1);
putchar(buf);
fflush(stdout);
}
receive_timeout -= now() - start;
} while (receive_timeout > 0.0);
}
/* ------------------------------------------------------------------------ */
void
uarttool_usage(const char *name) {
printf("Usage: %s { list | dev device } [ baud bps ] [ databits d ] [ parity p ] [ stopbits s ] [ ctsrts mode ] [ send string ] [ recv timeout ] [ show ]\n\n", name);
printf("Simple tool to test UART functionality. Needs either list or dev arguments, the others are optional\n");
printf(" list : lists uarts on the system (non intrusive)\n");
printf(" dev : select uart device, can be by name, by device name or by index (as listed in list)\n");
printf(" baud : set the baudrate to given parameter, in bits per second\n");
printf(" parity : set parity mode to given parameter - can be E, O, or N\n");
printf(" stopbits : set the number of stopbits - can be 1 or 2\n");
printf(" ctsrts : set CTS/RTS flow control to either on or off\n");
printf(" send : transmits a string\n");
printf(" recv : reads data on uart for timeout seconds, and displays the result on stdout\n");
printf(" show : show settings of selected uart\n");
}
/* ------------------------------------------------------------------------ */
int
main(int argc, const char** argv) {
mraa_uart_context uart = NULL;
int uart_index, i;
int baudrate = 115200, stopbits = 1, databits = 8;
mraa_uart_parity_t parity = MRAA_UART_PARITY_NONE;
unsigned int ctsrts = FALSE, xonxoff = FALSE;
const char *name = NULL, *dev = NULL;
double recieve_timeout = 0.0;
int recieve = FALSE; /* whether we are requested to receive */
int send = FALSE; /* whether we are requested to send or not */
const char *to_send; /* data to send, assigned during parsing of command line */
int show = FALSE; /* whether to show uart settings after everything else*/
/* Initialize MRAA. Init is done automatically if libmraa is compiled
with a compiler that supports __attribute__((constructor)), like
GCC and CLANG
*/
mraa_init();
if (argc <= 1) {
uarttool_usage(argv[0]);
} else
if (!strcmp(argv[1], "list")) {
int number_of_uarts = mraa_get_uart_count();
for (uart_index = 0; uart_index < number_of_uarts; uart_index++) {
mraa_result_t res = mraa_uart_settings(uart_index, &name, &dev, &baudrate, &databits, &stopbits, &parity, &ctsrts, &xonxoff);
if (res == MRAA_SUCCESS) {
if (name == NULL) name = undefined;
if (dev == NULL) dev = undefined;
printf("%i. %-12s %-16s %7i %i%c%i %s %s\n", uart_index, name, dev, baudrate, databits, paritymode_table[parity], stopbits, ctsrts?"CTSRTS":"(none)", xonxoff?"XONXOFF":"(none) ");
} else {
printf("%i. ---- error reading uart information ----\n", uart_index);
}
}
} else {
for (i=1; i<argc; i++) {
if (!strcmp(argv[i], "dev")) {
uart = uarttool_find(argv[i+1]);
if (uart == NULL) {
fprintf(stderr, "%s : cannot find uart %s\n", argv[0], argv[i+1]);
break;
}
dev = mraa_uart_get_dev_path(uart);
mraa_result_t res = mraa_uart_settings(-1, &dev, &name, &baudrate, &databits, &stopbits, &parity, &ctsrts, &xonxoff);
if (res != MRAA_SUCCESS) {
fprintf(stderr, "warning: problems accessing uart settings, attempting to continue\n");
}
} else
/* Baudrate setting */
if (!strcmp(argv[i], "baud") || !strcmp(argv[i], "baudrate")) {
if (i+1 >= argc || !isdigit(argv[i+1][0])) {
fprintf(stderr, "%s : %s needs number as argument\n", argv[0], argv[i]);
break;
}
baudrate = atoi(argv[i+1]);
if (uart != NULL) {
mraa_uart_set_baudrate(uart, baudrate);
}
i++;
} else
/* Accept a few variants of the flow control setting name */
if (!strcmp(argv[i], "ctsrts") || !strcmp(argv[i], "rtscts") || !strcmp(argv[i], "crtscts")) {
if (i+1 >= argc || argv[i+1][0] != 'o') {
fprintf(stderr, "%s : ctsrts needs either on or off as argument\n", argv[0]);
break;
}
ctsrts = !strcmp(argv[i+1], "on") ? 1u : 0u;
xonxoff = 0u;
if (uart != NULL) {
mraa_uart_set_flowcontrol(uart, xonxoff, ctsrts);
}
i++;
} else
/* Number of stopbits */
if (!strcmp(argv[i], "stopbits")) {
if (i+1 >= argc || !isdigit(argv[i+1][0])) {
fprintf(stderr, "%s : %s needs number as argument\n", argv[0], argv[i]);
break;
}
stopbits = atoi(argv[i+1]);
if (stopbits < 1 || stopbits > 2) {
stopbits = 1;
fprintf(stderr, "%s : invalid number of stopbits, bust be 1 or 2\n", argv[0]);
}
if (uart != NULL) {
mraa_uart_set_mode(uart, databits, parity, stopbits);
}
i++;
} else
/* Databits */
if (!strcmp(argv[i], "databits") || !strcmp(argv[i], "bytesize")) {
if (i+1 >= argc || !isdigit(argv[i+1][0])) {
fprintf(stderr, "%s : %s needs number as argument\n", argv[0], argv[i]);
break;
}
databits = atoi(argv[i+1]);
if (uart != NULL) {
mraa_uart_set_mode(uart, databits, parity, stopbits);
}
i++;
} else
/* Parity mode, uses paritmode array */
if (!strcmp(argv[i], "paritybits") || !strcmp(argv[i], "parity")) {
int j;
if (i+1 >= argc) {
fprintf(stderr, "%s : %s needs 'O','E' or 'N' as argument\n", argv[0], argv[i]);
break;
}
/* Even if there were gaps between declared array elements, they would be initialized to 0 compile time */
for (j=0; j<sizeof(paritymode_table); j++) {
if (paritymode_table[j] == toupper(argv[i+1][0])) {
parity = j;
break;
}
}
if (j >= sizeof(paritymode_table)) {
fprintf(stderr, "warning: unrecognized parity mode %s (ignoring)\n", argv[i+1]);
}
if (uart != NULL) {
mraa_uart_set_mode(uart, databits, parity, stopbits);
}
i++;
} else
/* Show settings */
if (!strcmp(argv[i], "show")) {
show = TRUE;
} else
/* Send data */
if (!strcmp(argv[i], "send")) {
if (i+1 >= argc) {
fprintf(stderr, "%s : %s needs string as argument\n", argv[0], argv[i]);
break;
}
send = TRUE;
to_send = argv[i+1];
i++;
} else
if (!strcmp(argv[i], "receive") || !strcmp(argv[i], "recieve") || !strcmp(argv[i], "recv")) {
if (i+1 >= argc) {
fprintf(stderr, "%s : %s needs a timeout (in seconds, fractional ok) as argument\n", argv[0], argv[i]);
break;
}
recieve = TRUE;
recieve_timeout = atof(argv[i+1]);
i++;
}
}
if (i == argc && uart != NULL) {
if (send) {
mraa_uart_write(uart, to_send, strlen(to_send)+1);
}
if (recieve) {
uarttool_read_and_print(uart, recieve_timeout);
}
if (show) {
if (recieve) putchar('\n');
dev = mraa_uart_get_dev_path(uart);
mraa_uart_settings(-1, &dev, &name, &baudrate, &databits, &stopbits, &parity, &ctsrts, &xonxoff);
printf("%-12s %-16s %7i %i%c%i %s %s\n", name!=NULL?name:undefined, dev, baudrate, databits, paritymode_table[parity], stopbits, ctsrts?"CTS/RTS":"(no hw)", xonxoff?"XONXOFF":"(no sw)");
}
} else {
uarttool_usage(argv[0]);
}
}
mraa_deinit();
return EXIT_SUCCESS;
}