examples: cleanup examples directory
Following changes are done as a part of cleanup: 1. Moved the platform specific `C` examples from top level examples/ directory to platform/ subdirectory and renamed helloedison.c to gpio_edison.c 2. C specific examples are moved to a new c/ subdirectory. As a part of this process, examples are modified to follow same standards and few new examples are also added. 3. Include the newly added C examples to relevant API documentation 4. Ran clang-format for all source files in c/, c++/, platform/ subdirectories 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:
committed by
Brendan Le Foll
parent
3fb65de4b8
commit
e562c774cf
36
examples/c/CMakeLists.txt
Normal file
36
examples/c/CMakeLists.txt
Normal file
@@ -0,0 +1,36 @@
|
||||
add_executable(aio aio.c)
|
||||
add_executable(gpio gpio.c)
|
||||
add_executable(gpio_advanced gpio_advanced.c)
|
||||
add_executable(hellomraa hellomraa.c)
|
||||
add_executable(i2c_hmc5883l i2c_hmc5883l.c)
|
||||
add_executable(i2c_mpu6050 i2c_mpu6050.c)
|
||||
add_executable(led led.c)
|
||||
add_executable(pwm pwm.c)
|
||||
add_executable(spi spi.c)
|
||||
add_executable(uart uart.c)
|
||||
add_executable(uart_advanced uart_advanced.c)
|
||||
if (NOT ANDROID_TOOLCHAIN)
|
||||
add_executable(iio iio.c)
|
||||
endif()
|
||||
|
||||
include_directories(${PROJECT_SOURCE_DIR}/api)
|
||||
include_directories(${PROJECT_SOURCE_DIR}/api/mraa)
|
||||
|
||||
target_link_libraries(aio mraa)
|
||||
target_link_libraries(gpio mraa)
|
||||
target_link_libraries(gpio_advanced mraa)
|
||||
target_link_libraries(hellomraa mraa)
|
||||
target_link_libraries(i2c_hmc5883l mraa m)
|
||||
target_link_libraries(i2c_mpu6050 mraa)
|
||||
target_link_libraries(led mraa)
|
||||
target_link_libraries(pwm mraa)
|
||||
target_link_libraries(spi mraa)
|
||||
target_link_libraries(uart mraa)
|
||||
target_link_libraries(uart_advanced mraa)
|
||||
if (NOT ANDROID_TOOLCHAIN)
|
||||
target_link_libraries(iio mraa)
|
||||
endif()
|
||||
if (ONEWIRE)
|
||||
add_executable (uart_ow uart_ow.c)
|
||||
target_link_libraries (uart_ow mraa)
|
||||
endif ()
|
||||
99
examples/c/aio.c
Normal file
99
examples/c/aio.c
Normal file
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* Author: Nandkishor Sonar
|
||||
* Contributors: Alex Tereschenko <alext.mkrs@gmail.com>
|
||||
* Contributors: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
|
||||
* 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.
|
||||
*
|
||||
* Example usage: Reads ADC A0 value continuously. Press Ctrl+C to exit.
|
||||
*/
|
||||
|
||||
/* standard headers */
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/* mraa header */
|
||||
#include "mraa/aio.h"
|
||||
|
||||
/* AIO port */
|
||||
#define AIO_PORT 0
|
||||
|
||||
volatile sig_atomic_t flag = 1;
|
||||
|
||||
void
|
||||
sig_handler(int signum)
|
||||
{
|
||||
if (signum == SIGINT) {
|
||||
fprintf(stdout, "Exiting...\n");
|
||||
flag = 0;
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
mraa_result_t status = MRAA_SUCCESS;
|
||||
mraa_aio_context aio;
|
||||
uint16_t value = 0;
|
||||
float float_value = 0.0;
|
||||
|
||||
signal(SIGINT, sig_handler);
|
||||
|
||||
/* initialize mraa for the platform (not needed most of the times) */
|
||||
mraa_init();
|
||||
|
||||
//! [Interesting]
|
||||
/* initialize AIO */
|
||||
aio = mraa_aio_init(AIO_PORT);
|
||||
if (aio == NULL) {
|
||||
fprintf(stderr, "Failed to initialize AIO\n");
|
||||
mraa_deinit();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
while (flag) {
|
||||
value = mraa_aio_read(aio);
|
||||
float_value = mraa_aio_read_float(aio);
|
||||
fprintf(stdout, "ADC A0 read %X - %d\n", value, value);
|
||||
fprintf(stdout, "ADC A0 read float - %.5f\n", float_value);
|
||||
}
|
||||
|
||||
/* close AIO */
|
||||
status = mraa_aio_close(aio);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
//! [Interesting]
|
||||
/* deinitialize mraa for the platform (not needed most of the times) */
|
||||
mraa_deinit();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
|
||||
err_exit:
|
||||
mraa_result_print(status);
|
||||
|
||||
/* deinitialize mraa for the platform (not needed most of the times) */
|
||||
mraa_deinit();
|
||||
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
147
examples/c/gpio.c
Normal file
147
examples/c/gpio.c
Normal file
@@ -0,0 +1,147 @@
|
||||
/*
|
||||
* Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
|
||||
* Contributors: Alex Tereschenko <alext.mkrs@gmail.com>
|
||||
* Contributors: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
|
||||
* 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.
|
||||
*
|
||||
* Example usage: Toggles GPIO's 23 and 24 recursively. Press Ctrl+C to exit
|
||||
*
|
||||
*/
|
||||
|
||||
/* standard headers */
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/* mraa header */
|
||||
#include "mraa/gpio.h"
|
||||
|
||||
/* gpio declaration */
|
||||
#define GPIO_PIN_1 23
|
||||
#define GPIO_PIN_2 24
|
||||
|
||||
volatile sig_atomic_t flag = 1;
|
||||
|
||||
void
|
||||
sig_handler(int signum)
|
||||
{
|
||||
if (signum == SIGINT) {
|
||||
fprintf(stdout, "Exiting...\n");
|
||||
flag = 0;
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
mraa_result_t status = MRAA_SUCCESS;
|
||||
mraa_gpio_context gpio_1, gpio_2;
|
||||
|
||||
/* install signal handler */
|
||||
signal(SIGINT, sig_handler);
|
||||
|
||||
/* initialize mraa for the platform (not needed most of the times) */
|
||||
mraa_init();
|
||||
|
||||
//! [Interesting]
|
||||
/* initialize GPIO pin */
|
||||
gpio_1 = mraa_gpio_init(GPIO_PIN_1);
|
||||
if (gpio_1 == NULL) {
|
||||
fprintf(stderr, "Failed to initialize GPIO %d\n", GPIO_PIN_1);
|
||||
mraa_deinit();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
/* initialize GPIO pin */
|
||||
gpio_2 = mraa_gpio_init(GPIO_PIN_2);
|
||||
if (gpio_2 == NULL) {
|
||||
fprintf(stderr, "Failed to initialize GPIO %d\n", GPIO_PIN_2);
|
||||
mraa_deinit();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
/* set GPIO to output */
|
||||
status = mraa_gpio_dir(gpio_1, MRAA_GPIO_OUT);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
/* set GPIO to output */
|
||||
status = mraa_gpio_dir(gpio_2, MRAA_GPIO_OUT);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
/* toggle both GPIO's */
|
||||
while (flag) {
|
||||
status = mraa_gpio_write(gpio_1, 1);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
status = mraa_gpio_write(gpio_2, 0);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
sleep(1);
|
||||
|
||||
status = mraa_gpio_write(gpio_1, 0);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
status = mraa_gpio_write(gpio_2, 1);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
/* release gpio's */
|
||||
status = mraa_gpio_close(gpio_1);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
/* close GPIO */
|
||||
status = mraa_gpio_close(gpio_2);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
//! [Interesting]
|
||||
/* deinitialize mraa for the platform (not needed most of the times) */
|
||||
mraa_deinit();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
|
||||
err_exit:
|
||||
mraa_result_print(status);
|
||||
|
||||
/* deinitialize mraa for the platform (not needed most of the times) */
|
||||
mraa_deinit();
|
||||
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
94
examples/c/gpio_advanced.c
Normal file
94
examples/c/gpio_advanced.c
Normal file
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* Author: Brendan Le Foll
|
||||
* Contributors: Alex Tereschenko <alext.mkrs@gmail.com>
|
||||
* Contributors: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
|
||||
* 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.
|
||||
*
|
||||
* Example usage: Configures GPIO pin for interrupt and waits 30 seconds for the isr to trigger
|
||||
*
|
||||
*/
|
||||
|
||||
/* standard headers */
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/* mraa header */
|
||||
#include "mraa/gpio.h"
|
||||
|
||||
#define GPIO_PIN 6
|
||||
|
||||
void
|
||||
int_handler(void* args)
|
||||
{
|
||||
fprintf(stdout, "ISR triggered\n");
|
||||
}
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
mraa_result_t status = MRAA_SUCCESS;
|
||||
mraa_gpio_context gpio;
|
||||
|
||||
/* initialize mraa for the platform (not needed most of the times) */
|
||||
mraa_init();
|
||||
|
||||
//! [Interesting]
|
||||
/* initialize GPIO pin */
|
||||
gpio = mraa_gpio_init(GPIO_PIN);
|
||||
if (gpio == NULL) {
|
||||
fprintf(stderr, "Failed to initialize GPIO %d\n", GPIO_PIN);
|
||||
mraa_deinit();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
/* set GPIO to input */
|
||||
status = mraa_gpio_dir(gpio, MRAA_GPIO_IN);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
/* configure ISR for GPIO */
|
||||
status = mraa_gpio_isr(gpio, MRAA_GPIO_EDGE_BOTH, &int_handler, NULL);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
/* wait 30 seconds isr trigger */
|
||||
sleep(30);
|
||||
|
||||
/* close GPIO */
|
||||
mraa_gpio_close(gpio);
|
||||
|
||||
//! [Interesting]
|
||||
/* deinitialize mraa for the platform (not needed most of the times) */
|
||||
mraa_deinit();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
|
||||
err_exit:
|
||||
mraa_result_print(status);
|
||||
|
||||
/* deinitialize mraa for the platform (not needed most of the times) */
|
||||
mraa_deinit();
|
||||
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
41
examples/c/hellomraa.c
Normal file
41
examples/c/hellomraa.c
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* 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 <string.h>
|
||||
#include <syslog.h>
|
||||
//! [Interesting]
|
||||
#include "mraa.h"
|
||||
|
||||
int
|
||||
main(int argc, char** argv)
|
||||
{
|
||||
const char* board_name = mraa_get_platform_name();
|
||||
fprintf(stdout, "hello mraa\n Version: %s\n Running on %s\n", mraa_get_version(), board_name);
|
||||
|
||||
mraa_deinit();
|
||||
|
||||
return MRAA_SUCCESS;
|
||||
}
|
||||
//! [Interesting]
|
||||
224
examples/c/i2c_hmc5883l.c
Normal file
224
examples/c/i2c_hmc5883l.c
Normal file
@@ -0,0 +1,224 @@
|
||||
/*
|
||||
* 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.
|
||||
*
|
||||
* Example usage: Outputs X,Y,Z co-ordinates and direction recursively using
|
||||
* HMC5883L. Press Ctrl+C to exit.
|
||||
*
|
||||
*/
|
||||
|
||||
/* standard headers */
|
||||
#include <math.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/* mraa header */
|
||||
#include "mraa/i2c.h"
|
||||
|
||||
#define MAX_BUFFER_LENGTH 6
|
||||
#define HMC5883L_I2C_ADDR 0x1E
|
||||
|
||||
/* configuration registers */
|
||||
#define HMC5883L_CONF_REG_A 0x00
|
||||
#define HMC5883L_CONF_REG_B 0x01
|
||||
|
||||
/* mode register */
|
||||
#define HMC5883L_MODE_REG 0x02
|
||||
|
||||
/* data register */
|
||||
#define HMC5883L_X_MSB_REG 0
|
||||
#define HMC5883L_X_LSB_REG 1
|
||||
#define HMC5883L_Z_MSB_REG 2
|
||||
#define HMC5883L_Z_LSB_REG 3
|
||||
#define HMC5883L_Y_MSB_REG 4
|
||||
#define HMC5883L_Y_LSB_REG 5
|
||||
#define DATA_REG_SIZE 6
|
||||
|
||||
/* status register */
|
||||
#define HMC5883L_STATUS_REG 0x09
|
||||
|
||||
/* ID registers */
|
||||
#define HMC5883L_ID_A_REG 0x0A
|
||||
#define HMC5883L_ID_B_REG 0x0B
|
||||
#define HMC5883L_ID_C_REG 0x0C
|
||||
|
||||
#define HMC5883L_CONT_MODE 0x00
|
||||
#define HMC5883L_DATA_REG 0x03
|
||||
|
||||
/* scales */
|
||||
#define GA_0_88_REG 0x00 << 5
|
||||
#define GA_1_3_REG 0x01 << 5
|
||||
#define GA_1_9_REG 0x02 << 5
|
||||
#define GA_2_5_REG 0x03 << 5
|
||||
#define GA_4_0_REG 0x04 << 5
|
||||
#define GA_4_7_REG 0x05 << 5
|
||||
#define GA_5_6_REG 0x06 << 5
|
||||
#define GA_8_1_REG 0x07 << 5
|
||||
|
||||
/* digital resolutions */
|
||||
#define SCALE_0_73_MG 0.73
|
||||
#define SCALE_0_92_MG 0.92
|
||||
#define SCALE_1_22_MG 1.22
|
||||
#define SCALE_1_52_MG 1.52
|
||||
#define SCALE_2_27_MG 2.27
|
||||
#define SCALE_2_56_MG 2.56
|
||||
#define SCALE_3_03_MG 3.03
|
||||
#define SCALE_4_35_MG 4.35
|
||||
|
||||
#define I2C_BUS 0
|
||||
|
||||
volatile sig_atomic_t flag = 1;
|
||||
|
||||
void
|
||||
sig_handler(int signum)
|
||||
{
|
||||
if (signum == SIGINT) {
|
||||
fprintf(stdout, "Exiting...\n");
|
||||
flag = 0;
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
mraa_result_t status = MRAA_SUCCESS;
|
||||
mraa_i2c_context i2c;
|
||||
float direction = 0;
|
||||
int16_t x = 0, y = 0, z = 0;
|
||||
uint8_t rx_tx_buf[MAX_BUFFER_LENGTH];
|
||||
|
||||
/* install signal handler */
|
||||
signal(SIGINT, sig_handler);
|
||||
|
||||
/* initialize mraa for the platform (not needed most of the times) */
|
||||
mraa_init();
|
||||
|
||||
//! [Interesting]
|
||||
i2c = mraa_i2c_init(I2C_BUS);
|
||||
if (i2c == NULL) {
|
||||
fprintf(stderr, "Failed to initialize I2C\n");
|
||||
mraa_deinit();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
/* set slave address */
|
||||
mraa_i2c_address(i2c, HMC5883L_I2C_ADDR);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
/* set device gain */
|
||||
rx_tx_buf[0] = HMC5883L_CONF_REG_B;
|
||||
rx_tx_buf[1] = GA_1_3_REG;
|
||||
|
||||
status = mraa_i2c_write(i2c, rx_tx_buf, 2);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
status = mraa_i2c_address(i2c, HMC5883L_I2C_ADDR);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
/* set continuous mode */
|
||||
rx_tx_buf[0] = HMC5883L_MODE_REG;
|
||||
rx_tx_buf[1] = HMC5883L_CONT_MODE;
|
||||
|
||||
status = mraa_i2c_write(i2c, rx_tx_buf, 2);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
while (flag) {
|
||||
#if 0
|
||||
int i = 0;
|
||||
/*
|
||||
* alternative, equivalent method which helps to understand exactly what
|
||||
* the below does
|
||||
*/
|
||||
mraa_i2c_address(i2c, HMC5883L_I2C_ADDR);
|
||||
for (i = 0; i < DATA_REG_SIZE; i++) {
|
||||
mraa_i2c_read_byte_data(i2c, HMC5883L_DATA_REG+i);
|
||||
}
|
||||
#endif
|
||||
/* select data register to read */
|
||||
status = mraa_i2c_address(i2c, HMC5883L_I2C_ADDR);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
status = mraa_i2c_write_byte(i2c, HMC5883L_DATA_REG);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
/* do a incremental read from the chosen address */
|
||||
status = mraa_i2c_address(i2c, HMC5883L_I2C_ADDR);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
/* this call behaves very similarly to the Wire receive() call */
|
||||
mraa_i2c_read(i2c, rx_tx_buf, DATA_REG_SIZE);
|
||||
|
||||
x = (rx_tx_buf[HMC5883L_X_MSB_REG] << 8) | rx_tx_buf[HMC5883L_X_LSB_REG];
|
||||
z = (rx_tx_buf[HMC5883L_Z_MSB_REG] << 8) | rx_tx_buf[HMC5883L_Z_LSB_REG];
|
||||
y = (rx_tx_buf[HMC5883L_Y_MSB_REG] << 8) | rx_tx_buf[HMC5883L_Y_LSB_REG];
|
||||
|
||||
/* scale and calculate direction */
|
||||
direction = atan2(y * SCALE_0_92_MG, x * SCALE_0_92_MG);
|
||||
|
||||
/* check if the signs are reversed */
|
||||
if (direction < 0) {
|
||||
direction += 2 * M_PI;
|
||||
}
|
||||
|
||||
fprintf(stdout, "Compass scaled data x : %f, y : %f, z : %f\n", x * SCALE_0_92_MG,
|
||||
y * SCALE_0_92_MG, z * SCALE_0_92_MG);
|
||||
fprintf(stdout, "Heading : %f\n", direction * 180 / M_PI);
|
||||
|
||||
sleep(2);
|
||||
}
|
||||
|
||||
/* stop i2c */
|
||||
mraa_i2c_stop(i2c);
|
||||
|
||||
//! [Interesting]
|
||||
/* deinitialize mraa for the platform (not needed most of the times) */
|
||||
mraa_deinit();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
|
||||
err_exit:
|
||||
mraa_result_print(status);
|
||||
|
||||
/* stop i2c */
|
||||
mraa_i2c_stop(i2c);
|
||||
|
||||
/* deinitialize mraa for the platform (not needed most of the times) */
|
||||
mraa_deinit();
|
||||
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
170
examples/c/i2c_mpu6050.c
Normal file
170
examples/c/i2c_mpu6050.c
Normal file
@@ -0,0 +1,170 @@
|
||||
/*
|
||||
* Author: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
|
||||
* 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: Outputs Accelerometer and Gyroscope value from MPU6050 recursively.
|
||||
* Press Ctrl+C to exit.
|
||||
*
|
||||
*/
|
||||
|
||||
/* standard headers */
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/* mraa header */
|
||||
#include "mraa/i2c.h"
|
||||
|
||||
#define I2C_BUS 0
|
||||
|
||||
/* register definitions */
|
||||
#define MPU6050_ADDR 0x68
|
||||
#define MPU6050_REG_PWR_MGMT_1 0x6b
|
||||
#define MPU6050_REG_RAW_ACCEL_X 0x3b
|
||||
#define MPU6050_REG_RAW_ACCEL_Y 0x3d
|
||||
#define MPU6050_REG_RAW_ACCEL_Z 0x3f
|
||||
#define MPU6050_REG_RAW_GYRO_X 0x43
|
||||
#define MPU6050_REG_RAW_GYRO_Y 0x45
|
||||
#define MPU6050_REG_RAW_GYRO_Z 0x47
|
||||
|
||||
/* bit definitions */
|
||||
#define MPU6050_RESET 0x80
|
||||
#define MPU6050_SLEEP (1 << 6)
|
||||
#define MPU6050_PLL_GYRO_X (1 << 1)
|
||||
|
||||
/* accelerometer scale factor for (+/-)2g */
|
||||
#define MPU6050_ACCEL_SCALE 16384.0
|
||||
|
||||
/* gyroscope scale factor for (+/-)250/s */
|
||||
#define MPU6050_GYRO_SCALE 131.0
|
||||
|
||||
volatile sig_atomic_t flag = 1;
|
||||
|
||||
void
|
||||
sig_handler(int signum)
|
||||
{
|
||||
if (signum == SIGINT) {
|
||||
fprintf(stdout, "Exiting...\n");
|
||||
flag = 0;
|
||||
}
|
||||
}
|
||||
|
||||
int16_t
|
||||
i2c_read_word(mraa_i2c_context dev, uint8_t command)
|
||||
{
|
||||
return be16toh(mraa_i2c_read_word_data(dev, command));
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
mraa_result_t status = MRAA_SUCCESS;
|
||||
mraa_i2c_context i2c;
|
||||
uint8_t data;
|
||||
int16_t accel_data[3];
|
||||
int16_t gyro_data[3];
|
||||
int ret;
|
||||
|
||||
/* install signal handler */
|
||||
signal(SIGINT, sig_handler);
|
||||
|
||||
/* initialize mraa for the platform (not needed most of the times) */
|
||||
mraa_init();
|
||||
|
||||
//! [Interesting]
|
||||
/* initialize I2C bus */
|
||||
i2c = mraa_i2c_init(I2C_BUS);
|
||||
if (i2c == NULL) {
|
||||
fprintf(stderr, "Failed to initialize I2C\n");
|
||||
mraa_deinit();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
/* set slave address */
|
||||
status = mraa_i2c_address(i2c, MPU6050_ADDR);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
/* reset the sensor */
|
||||
status = mraa_i2c_write_byte_data(i2c, MPU6050_RESET, MPU6050_REG_PWR_MGMT_1);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
sleep(5);
|
||||
|
||||
/* configure power management register */
|
||||
ret = mraa_i2c_read_byte_data(i2c, MPU6050_REG_PWR_MGMT_1);
|
||||
if (ret == -1) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
data = ret;
|
||||
data |= MPU6050_PLL_GYRO_X;
|
||||
data &= ~(MPU6050_SLEEP);
|
||||
|
||||
status = mraa_i2c_write_byte_data(i2c, data, MPU6050_REG_PWR_MGMT_1);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
sleep(5);
|
||||
|
||||
while (flag) {
|
||||
/* read raw accel data */
|
||||
accel_data[0] = i2c_read_word(i2c, MPU6050_REG_RAW_ACCEL_X) / MPU6050_ACCEL_SCALE;
|
||||
accel_data[1] = i2c_read_word(i2c, MPU6050_REG_RAW_ACCEL_Y) / MPU6050_ACCEL_SCALE;
|
||||
accel_data[2] = i2c_read_word(i2c, MPU6050_REG_RAW_ACCEL_Z) / MPU6050_ACCEL_SCALE;
|
||||
|
||||
/* read raw gyro data */
|
||||
gyro_data[0] = i2c_read_word(i2c, MPU6050_REG_RAW_GYRO_X) / MPU6050_GYRO_SCALE;
|
||||
gyro_data[1] = i2c_read_word(i2c, MPU6050_REG_RAW_GYRO_Y) / MPU6050_GYRO_SCALE;
|
||||
gyro_data[2] = i2c_read_word(i2c, MPU6050_REG_RAW_GYRO_Z) / MPU6050_GYRO_SCALE;
|
||||
|
||||
fprintf(stdout, "accel: x:%d y:%d z:%d\n", accel_data[0], accel_data[1], accel_data[2]);
|
||||
fprintf(stdout, "gyro: x:%d y:%d z:%d\n\n", gyro_data[0], gyro_data[1], gyro_data[2]);
|
||||
|
||||
sleep(2);
|
||||
}
|
||||
|
||||
/* stop i2c */
|
||||
mraa_i2c_stop(i2c);
|
||||
|
||||
//! [Interesting]
|
||||
/* deinitialize mraa for the platform (not needed most of the times) */
|
||||
mraa_deinit();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
|
||||
err_exit:
|
||||
mraa_result_print(status);
|
||||
|
||||
/* stop i2c */
|
||||
mraa_i2c_stop(i2c);
|
||||
|
||||
/* deinitialize mraa for the platform (not needed most of the times) */
|
||||
mraa_deinit();
|
||||
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
172
examples/c/iio.c
Normal file
172
examples/c/iio.c
Normal file
@@ -0,0 +1,172 @@
|
||||
/*
|
||||
* Author: Brendan Le Foll
|
||||
* Copyright (c) 2015 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.
|
||||
*
|
||||
* Example usage: Sets accelerometer scale, enables the x axis measurement and sets the trigger.
|
||||
* Waits 100 seconds for the buffer trigger to happen.
|
||||
*/
|
||||
|
||||
/* standard headers */
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/* mraa header */
|
||||
#include "mraa/iio.h"
|
||||
|
||||
/* IIO device */
|
||||
#define IIO_DEV 0
|
||||
|
||||
static void
|
||||
printword(uint16_t input, mraa_iio_channel* chan)
|
||||
{
|
||||
int16_t res;
|
||||
|
||||
if (!chan->lendian) {
|
||||
input = be16toh(input);
|
||||
} else {
|
||||
input = le16toh(input);
|
||||
}
|
||||
|
||||
input >>= chan->shift;
|
||||
input &= chan->mask;
|
||||
if (chan->signedd) {
|
||||
res = (int16_t)(input << (16 - chan->bits_used)) >> (16 - chan->bits_used);
|
||||
} else {
|
||||
res = input;
|
||||
}
|
||||
fprintf(stdout, " value = %05f\n", (float) res);
|
||||
}
|
||||
|
||||
void
|
||||
interrupt(char* data, void* args)
|
||||
{
|
||||
mraa_iio_context thisdevice = (mraa_iio_context) args;
|
||||
mraa_iio_channel* channels = mraa_iio_get_channels(thisdevice);
|
||||
int i = 0;
|
||||
|
||||
for (; i < mraa_iio_get_channel_count(thisdevice); i++) {
|
||||
if (channels[i].enabled) {
|
||||
fprintf(stdout, "channel %d - bytes %d\n", channels[i].index, channels[i].bytes);
|
||||
switch (channels[i].bytes) {
|
||||
case 2:
|
||||
printword(*(uint16_t*) (data + channels[i].location), &channels[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
event_interrupt(struct iio_event_data* data)
|
||||
{
|
||||
int chan_type;
|
||||
int modifier;
|
||||
int type;
|
||||
int direction;
|
||||
int channel;
|
||||
int channel2;
|
||||
int different;
|
||||
mraa_iio_event_extract_event(data, &chan_type, &modifier, &type, &direction, &channel, &channel2, &different);
|
||||
|
||||
fprintf(stdout,
|
||||
"event time %lld id %lld extracted chan_type %d modifier %d type %d direction %d "
|
||||
"channel %d channel2 %d different %d\n",
|
||||
data->timestamp, data->id, chan_type, modifier, type, direction, channel, channel2, different);
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
mraa_result_t status = MRAA_SUCCESS;
|
||||
mraa_iio_context iio;
|
||||
float iio_float;
|
||||
int iio_int;
|
||||
|
||||
/* initialize mraa for the platform (not needed most of the times) */
|
||||
mraa_init();
|
||||
|
||||
//! [Interesting]
|
||||
/* initialize iio */
|
||||
iio = mraa_iio_init(IIO_DEV);
|
||||
if (iio == NULL) {
|
||||
fprintf(stderr, "Failed to initialize IIO device\n");
|
||||
mraa_deinit();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
status = mraa_iio_write_float(iio, "in_accel_scale", 0.019163);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
fprintf(stderr, "Failed to write scale to IIO device\n");
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
status = mraa_iio_read_float(iio, "in_accel_scale", &iio_float);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
fprintf(stderr, "Failed to read scale from IIO device\n");
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
fprintf(stdout, "IIO scale: %f\n", iio_float);
|
||||
|
||||
status = mraa_iio_write_int(iio, "scan_elements/in_accel_x_en", 1);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
fprintf(stderr, "Failed to enable axis of the IIO device\n");
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
status = mraa_iio_read_int(iio, "scan_elements/in_accel_x_en", &iio_int);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
fprintf(stderr, "Failed to read enable status from IIO device\n");
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
fprintf(stdout, "IIO enable status: %d\n", iio_int);
|
||||
|
||||
status = mraa_iio_trigger_buffer(iio, interrupt, (void*) iio);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
fprintf(stderr, "Failed to set IIO buffer trigger\n");
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
/* wait for the trigger */
|
||||
fprintf(stdout, "Waiting for the trigger...\n");
|
||||
sleep(100);
|
||||
|
||||
/* close IIO device */
|
||||
mraa_iio_close(iio);
|
||||
|
||||
//! [Interesting]
|
||||
/* deinitialize mraa for the platform (not needed most of the times) */
|
||||
mraa_deinit();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
|
||||
err_exit:
|
||||
mraa_result_print(status);
|
||||
|
||||
/* close IIO device */
|
||||
mraa_iio_close(iio);
|
||||
|
||||
/* deinitialize mraa for the platform (not needed most of the times) */
|
||||
mraa_deinit();
|
||||
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
108
examples/c/led.c
Normal file
108
examples/c/led.c
Normal file
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* Author: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
|
||||
* 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: Reads maximum brightness value for user led and turns it
|
||||
* on/off depending on current state. Then sets led trigger
|
||||
* to heartbeat.
|
||||
*
|
||||
*/
|
||||
|
||||
/* standard headers */
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/* mraa header */
|
||||
#include "mraa/led.h"
|
||||
|
||||
/* LED name */
|
||||
#define USER_LED "user1"
|
||||
|
||||
/* trigger type */
|
||||
#define LED_TRIGGER "heartbeat"
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
mraa_result_t status = MRAA_SUCCESS;
|
||||
mraa_led_context led;
|
||||
int val;
|
||||
|
||||
/* initialize mraa for the platform (not needed most of the time) */
|
||||
mraa_init();
|
||||
|
||||
//! [Interesting]
|
||||
/* initialize LED */
|
||||
led = mraa_led_init(USER_LED);
|
||||
if (led == NULL) {
|
||||
fprintf(stderr, "Failed to initialize LED\n");
|
||||
mraa_deinit();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
/* read maximum brightness */
|
||||
val = mraa_led_read_max_brightness(led);
|
||||
fprintf(stdout, "maximum brightness value for LED is: %d\n", val);
|
||||
if (val >= 1) {
|
||||
val = 0;
|
||||
} else {
|
||||
/* never reached mostly */
|
||||
val = 1;
|
||||
}
|
||||
|
||||
/* turn LED on/off depending on max_brightness value */
|
||||
status = mraa_led_set_brightness(led, val);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
fprintf(stderr, "unable to set LED brightness\n");
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
/* sleep for 5 seconds */
|
||||
sleep(5);
|
||||
|
||||
/* set LED trigger to heartbeat */
|
||||
status = mraa_led_set_trigger(led, LED_TRIGGER);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
fprintf(stderr, "unable to set LED trigger to: heartbeat\n");
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
fprintf(stdout, "LED trigger set to: heartbeat\n");
|
||||
|
||||
/* close LED */
|
||||
mraa_led_close(led);
|
||||
|
||||
//! [Interesting]
|
||||
/* deinitialize mraa for the platform (not needed most of the times) */
|
||||
mraa_deinit();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
|
||||
err_exit:
|
||||
mraa_result_print(status);
|
||||
|
||||
/* deinitialize mraa for the platform (not needed most of the times) */
|
||||
mraa_deinit();
|
||||
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
124
examples/c/pwm.c
Normal file
124
examples/c/pwm.c
Normal file
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
* Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
|
||||
* Contributors: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
|
||||
* 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.
|
||||
*
|
||||
* Example usage: Generates PWM signal of period 200us with variable dutycyle
|
||||
* repeately. Press Ctrl+C to exit
|
||||
*/
|
||||
|
||||
/* standard headers */
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/* mraa header */
|
||||
#include "mraa/pwm.h"
|
||||
|
||||
/* PWM declaration */
|
||||
#define PWM 3
|
||||
|
||||
/* PWM period in us */
|
||||
#define PWM_FREQ 200
|
||||
|
||||
volatile sig_atomic_t flag = 1;
|
||||
|
||||
void
|
||||
sig_handler(int signum)
|
||||
{
|
||||
if (signum == SIGINT) {
|
||||
fprintf(stdout, "Exiting...\n");
|
||||
flag = 0;
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
mraa_result_t status = MRAA_SUCCESS;
|
||||
mraa_pwm_context pwm;
|
||||
float value = 0.0f;
|
||||
float output;
|
||||
|
||||
/* initialize mraa for the platform (not needed most of the times) */
|
||||
mraa_init();
|
||||
|
||||
//! [Interesting]
|
||||
pwm = mraa_pwm_init(PWM);
|
||||
if (pwm == NULL) {
|
||||
fprintf(stderr, "Failed to initialize PWM\n");
|
||||
mraa_deinit();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
/* set PWM period */
|
||||
status = mraa_pwm_period_us(pwm, PWM_FREQ);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
/* enable PWM */
|
||||
status = mraa_pwm_enable(pwm, 1);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
while (flag) {
|
||||
value = value + 0.01f;
|
||||
|
||||
/* write PWM duty cyle */
|
||||
status = mraa_pwm_write(pwm, value);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
usleep(50000);
|
||||
|
||||
if (value >= 1.0f) {
|
||||
value = 0.0f;
|
||||
}
|
||||
|
||||
/* read PWM duty cyle */
|
||||
output = mraa_pwm_read(pwm);
|
||||
fprintf(stdout, "PWM value is %f\n", output);
|
||||
}
|
||||
|
||||
/* close PWM */
|
||||
mraa_pwm_close(pwm);
|
||||
|
||||
//! [Interesting]
|
||||
/* deinitialize mraa for the platform (not needed most of the times) */
|
||||
mraa_deinit();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
|
||||
err_exit:
|
||||
mraa_result_print(status);
|
||||
|
||||
/* close PWM */
|
||||
mraa_pwm_close(pwm);
|
||||
|
||||
/* deinitialize mraa for the platform (not needed most of the times) */
|
||||
mraa_deinit();
|
||||
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
154
examples/c/spi.c
Normal file
154
examples/c/spi.c
Normal file
@@ -0,0 +1,154 @@
|
||||
/*
|
||||
* Author: Michael Ring <mail@michael-ring.org>
|
||||
* Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
|
||||
* Contributors: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
|
||||
* 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.
|
||||
*
|
||||
* Example usage: Display set of patterns on MAX7219 repeately.
|
||||
* Press Ctrl+C to exit
|
||||
*/
|
||||
|
||||
/* standard headers */
|
||||
#include <signal.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/* mraa header */
|
||||
#include "mraa/spi.h"
|
||||
|
||||
/* SPI declaration */
|
||||
#define SPI_BUS 0
|
||||
|
||||
/* SPI frequency in Hz */
|
||||
#define SPI_FREQ 400000
|
||||
|
||||
uint16_t pat[] = { 0x01aa, 0x0255, 0x03aa, 0x0455, 0x05aa, 0x0655, 0x07aa, 0x0855 };
|
||||
uint16_t pat_inv[] = { 0x0155, 0x02aa, 0x0355, 0x04aa, 0x0555, 0x06aa, 0x0755, 0x08aa };
|
||||
uint16_t pat_clear[] = { 0x0100, 0x0200, 0x0300, 0x0400, 0x0500, 0x0600, 0x0700, 0x0800 };
|
||||
|
||||
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_spi_context spi;
|
||||
int i, j;
|
||||
|
||||
/* initialize mraa for the platform (not needed most of the times) */
|
||||
mraa_init();
|
||||
|
||||
//! [Interesting]
|
||||
/* initialize SPI bus */
|
||||
spi = mraa_spi_init(SPI_BUS);
|
||||
if (spi == NULL) {
|
||||
fprintf(stderr, "Failed to initialize SPI\n");
|
||||
mraa_deinit();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
/* set SPI frequency */
|
||||
status = mraa_spi_frequency(spi, SPI_FREQ);
|
||||
if (status != MRAA_SUCCESS)
|
||||
goto err_exit;
|
||||
|
||||
/* set big endian mode */
|
||||
status = mraa_spi_lsbmode(spi, 0);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
/* MAX7219/21 chip needs the data in word size */
|
||||
status = mraa_spi_bit_per_word(spi, 16);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
fprintf(stdout, "Failed to set SPI Device to 16Bit mode\n");
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
/* do not decode bits */
|
||||
mraa_spi_write_word(spi, 0x0900);
|
||||
|
||||
/* brightness of LEDs */
|
||||
mraa_spi_write_word(spi, 0x0a05);
|
||||
|
||||
/* show all scan lines */
|
||||
mraa_spi_write_word(spi, 0x0b07);
|
||||
|
||||
/* set display on */
|
||||
mraa_spi_write_word(spi, 0x0c01);
|
||||
|
||||
/* testmode off */
|
||||
mraa_spi_write_word(spi, 0x0f00);
|
||||
|
||||
while (flag) {
|
||||
/* set display pattern */
|
||||
mraa_spi_write_buf_word(spi, pat, 16);
|
||||
|
||||
sleep(2);
|
||||
|
||||
/* set inverted display pattern */
|
||||
mraa_spi_write_buf_word(spi, pat_inv, 16);
|
||||
|
||||
sleep(2);
|
||||
|
||||
/* clear the LED's */
|
||||
mraa_spi_write_buf_word(spi, pat_clear, 16);
|
||||
|
||||
/* cycle through all LED's */
|
||||
for (i = 1; i <= 8; i++) {
|
||||
for (j = 0; j < 8; j++) {
|
||||
mraa_spi_write_word(spi, (i << 8) + (1 << j));
|
||||
sleep(1);
|
||||
}
|
||||
mraa_spi_write_word(spi, i << 8);
|
||||
}
|
||||
}
|
||||
|
||||
/* stop spi */
|
||||
mraa_spi_stop(spi);
|
||||
|
||||
//! [Interesting]
|
||||
/* deinitialize mraa for the platform (not needed most of the times) */
|
||||
mraa_deinit();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
|
||||
err_exit:
|
||||
mraa_result_print(status);
|
||||
|
||||
/* stop spi */
|
||||
mraa_spi_stop(spi);
|
||||
|
||||
/* deinitialize mraa for the platform (not needed most of the times) */
|
||||
mraa_deinit();
|
||||
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
93
examples/c/uart.c
Normal file
93
examples/c/uart.c
Normal file
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
|
||||
* Author: Brendan Le Foll <brendan.le.foll@intel.com>
|
||||
* Contributors: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
|
||||
* Copyright (c) 2014, 2015 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.
|
||||
*
|
||||
* Example usage: Prints "Hello Mraa!" recursively. Press Ctrl+C to exit
|
||||
*
|
||||
*/
|
||||
|
||||
/* standard headers */
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/* mraa header */
|
||||
#include "mraa/uart.h"
|
||||
|
||||
#define UART 0
|
||||
|
||||
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_uart_context uart;
|
||||
char buffer[] = "Hello Mraa!";
|
||||
|
||||
/* install signal handler */
|
||||
signal(SIGINT, sig_handler);
|
||||
|
||||
/* initialize mraa for the platform (not needed most of the times) */
|
||||
mraa_init();
|
||||
|
||||
//! [Interesting]
|
||||
/* initialize UART */
|
||||
uart = mraa_uart_init(UART);
|
||||
if (uart == NULL) {
|
||||
fprintf(stderr, "Failed to initialize UART\n");
|
||||
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 times) */
|
||||
mraa_deinit();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
|
||||
err_exit:
|
||||
/* deinitialize mraa for the platform (not needed most of the times) */
|
||||
mraa_deinit();
|
||||
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
115
examples/c/uart_advanced.c
Normal file
115
examples/c/uart_advanced.c
Normal file
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
* Author: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
|
||||
* 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 <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/* 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;
|
||||
}
|
||||
105
examples/c/uart_ow.c
Normal file
105
examples/c/uart_ow.c
Normal file
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
* Author: Jon Trulson <jtrulson@ics.com>
|
||||
* Copyright (c) 2016 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.
|
||||
*
|
||||
* Example usage: Search and print the found OW device ID recursively.
|
||||
*
|
||||
*/
|
||||
|
||||
/* standard headers */
|
||||
#include "stdio.h"
|
||||
|
||||
/* mraa header */
|
||||
#include "mraa.h"
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
mraa_uart_ow_context uart_ow;
|
||||
mraa_result_t status;
|
||||
uint8_t id[MRAA_UART_OW_ROMCODE_SIZE];
|
||||
uint8_t count = 0;
|
||||
|
||||
//! [Interesting]
|
||||
uart_ow = mraa_uart_ow_init(0);
|
||||
if (uart_ow == NULL) {
|
||||
fprintf(stderr, "Failed to initialize UART OW\n");
|
||||
mraa_deinit();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
// Reset the ow bus and see if anything is present
|
||||
status = mraa_uart_ow_reset(uart_ow);
|
||||
if (status == MRAA_SUCCESS) {
|
||||
fprintf(stdout, "Reset succeeded, device(s) detected!\n");
|
||||
} else {
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
fprintf(stdout, "Looking for devices...\n");
|
||||
|
||||
/*
|
||||
* we are essentially doing a binary tree search through the 64
|
||||
* bit address space. id is modified during this search, and will
|
||||
* be set to the valid rom code for each device found.
|
||||
*
|
||||
* start the search from scratch
|
||||
*/
|
||||
status = mraa_uart_ow_rom_search(uart_ow, 1, id);
|
||||
if (status != MRAA_SUCCESS) {
|
||||
goto err_exit;
|
||||
}
|
||||
|
||||
while (status == MRAA_SUCCESS) {
|
||||
/* The first byte (id[0]]) is the device type (family) code.
|
||||
* The last byte (id[7]) is the rom code CRC value. The
|
||||
* intervening bytes (id[1]-id[6]) are the unique 48 bit
|
||||
* device ID.
|
||||
*/
|
||||
fprintf(stdout, "Device %02d Type 0x%02x ID %02x%02x%02x%02x%02x%02x CRC 0x%02x\n", count,
|
||||
id[0], id[6], id[5], id[4], id[3], id[2], id[1], id[7]);
|
||||
count++;
|
||||
|
||||
// continue the search with start argument set to 0
|
||||
status = mraa_uart_ow_rom_search(uart_ow, 0, id);
|
||||
}
|
||||
|
||||
/* stop uart_ow */
|
||||
mraa_uart_ow_stop(uart_ow);
|
||||
|
||||
//! [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_ow */
|
||||
mraa_uart_ow_stop(uart_ow);
|
||||
|
||||
/* deinitialize mraa for the platform (not needed most of the times) */
|
||||
mraa_deinit();
|
||||
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
Reference in New Issue
Block a user