/* * Author: Manivannan Sadhasivam * Copyright (c) 2018, Linaro 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. * * Example usage: Prints "Hello Mraa!" recursively. Press Ctrl+C to exit * */ /* standard headers */ #include #include #include #include /* mraa header */ #include "mraa/uart.h" #ifndef FALSE #define FALSE 0 #define TRUE (!FALSE) #endif /* UART port name */ const char* dev_path = "/dev/ttyS0"; volatile sig_atomic_t flag = 1; void sig_handler(int signum) { if (signum == SIGINT) { fprintf(stdout, "Exiting...\n"); flag = 0; } } int main(int argc, char** argv) { mraa_result_t status = MRAA_SUCCESS; mraa_uart_context uart; char buffer[] = "Hello Mraa!"; int baudrate = 9600, stopbits = 1, databits = 8; mraa_uart_parity_t parity = MRAA_UART_PARITY_NONE; unsigned int ctsrts = FALSE, xonxoff = FALSE; const char* name = NULL; /* install signal handler */ signal(SIGINT, sig_handler); /* initialize mraa for the platform (not needed most of the time) */ mraa_init(); //! [Interesting] /* initialize uart */ uart = mraa_uart_init_raw(dev_path); if (uart == NULL) { fprintf(stderr, "Failed to initialize UART\n"); return EXIT_FAILURE; } /* set serial port parameters */ status = mraa_uart_settings(-1, &dev_path, &name, &baudrate, &databits, &stopbits, &parity, &ctsrts, &xonxoff); if (status != MRAA_SUCCESS) { goto err_exit; } while (flag) { /* send data through uart */ mraa_uart_write(uart, buffer, sizeof(buffer)); sleep(1); } /* stop uart */ mraa_uart_stop(uart); //! [Interesting] /* deinitialize mraa for the platform (not needed most of the time) */ mraa_deinit(); return EXIT_SUCCESS; err_exit: mraa_result_print(status); /* stop uart */ mraa_uart_stop(uart); /* deinitialize mraa for the platform (not needed most of the times) */ mraa_deinit(); return EXIT_FAILURE; }