2014-05-22 14:17:33 +01:00
|
|
|
/*
|
|
|
|
|
* Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
|
2018-01-24 15:09:29 +05:30
|
|
|
* Contributors: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
|
2014-05-22 14:17:33 +01:00
|
|
|
* Copyright (c) 2014 Intel Corporation.
|
|
|
|
|
*
|
2019-05-09 09:47:11 -07:00
|
|
|
* SPDX-License-Identifier: MIT
|
2018-01-24 15:09:29 +05:30
|
|
|
*
|
|
|
|
|
* Example usage: Sends data continuously to a Spi device. Press Ctrl+C to exit
|
|
|
|
|
*
|
2014-05-22 14:17:33 +01:00
|
|
|
*/
|
|
|
|
|
|
2018-01-24 15:09:29 +05:30
|
|
|
/* standard headers */
|
|
|
|
|
#include <iostream>
|
2014-05-22 14:17:33 +01:00
|
|
|
#include <signal.h>
|
2018-01-24 15:09:29 +05:30
|
|
|
#include <stdlib.h>
|
2017-11-17 17:22:34 +05:30
|
|
|
#include <unistd.h>
|
2014-05-22 14:17:33 +01:00
|
|
|
|
2018-01-24 15:09:29 +05:30
|
|
|
/* mraa headers */
|
|
|
|
|
#include "mraa/common.hpp"
|
|
|
|
|
#include "mraa/spi.hpp"
|
|
|
|
|
|
|
|
|
|
#define SPI_PORT 0
|
2014-05-22 14:17:33 +01:00
|
|
|
|
2018-01-24 15:09:29 +05:30
|
|
|
volatile sig_atomic_t flag = 1;
|
2014-05-22 14:17:33 +01:00
|
|
|
|
|
|
|
|
void
|
2018-01-24 15:09:29 +05:30
|
|
|
sig_handler(int signum)
|
2014-05-22 14:17:33 +01:00
|
|
|
{
|
2018-01-24 15:09:29 +05:30
|
|
|
if (signum == SIGINT) {
|
|
|
|
|
std::cout << "Exiting..." << std::endl;
|
|
|
|
|
flag = 0;
|
2014-05-22 14:17:33 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-23 14:39:12 +00:00
|
|
|
int
|
2018-01-24 15:09:29 +05:30
|
|
|
main(void)
|
2014-05-22 14:17:33 +01:00
|
|
|
{
|
2018-01-24 15:09:29 +05:30
|
|
|
uint8_t data[] = { 0x00, 100 };
|
|
|
|
|
uint8_t rxBuf[2];
|
|
|
|
|
uint8_t* recv;
|
|
|
|
|
int i;
|
|
|
|
|
|
2014-05-30 17:10:52 +01:00
|
|
|
signal(SIGINT, sig_handler);
|
|
|
|
|
|
2015-03-23 14:39:12 +00:00
|
|
|
//! [Interesting]
|
2018-01-24 15:09:29 +05:30
|
|
|
mraa::Spi spi(SPI_PORT);
|
2014-05-22 14:17:33 +01:00
|
|
|
|
2018-01-24 15:09:29 +05:30
|
|
|
while (flag) {
|
2014-05-22 14:17:33 +01:00
|
|
|
for (i = 90; i < 130; i++) {
|
|
|
|
|
data[1] = i;
|
2017-08-08 13:55:23 -07:00
|
|
|
recv = spi.write(data, 2);
|
2018-01-24 15:09:29 +05:30
|
|
|
std::cout << "Writing -%i" << i << std::endl;
|
2014-10-18 09:25:19 -07:00
|
|
|
if (recv) {
|
2018-01-24 15:09:29 +05:30
|
|
|
std::cout << "RECIVED-%i-%i" << recv[0] << recv[1] << std::endl;
|
2014-10-18 09:25:19 -07:00
|
|
|
free(recv);
|
|
|
|
|
}
|
2014-05-22 14:17:33 +01:00
|
|
|
usleep(100000);
|
|
|
|
|
}
|
2018-01-24 15:09:29 +05:30
|
|
|
|
2014-05-22 14:17:33 +01:00
|
|
|
for (i = 130; i > 90; i--) {
|
|
|
|
|
data[1] = i;
|
2017-08-08 13:55:23 -07:00
|
|
|
if (spi.transfer(data, rxBuf, 2) == mraa::SUCCESS) {
|
2018-01-24 15:09:29 +05:30
|
|
|
std::cout << "Writing -%i" << i << std::endl;
|
|
|
|
|
std::cout << "RECIVED-%i-%i" << rxBuf[0] << rxBuf[1] << std::endl;
|
2014-10-18 09:25:19 -07:00
|
|
|
}
|
2014-05-22 14:17:33 +01:00
|
|
|
usleep(100000);
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-03-23 14:39:12 +00:00
|
|
|
//! [Interesting]
|
2014-05-22 14:17:33 +01:00
|
|
|
|
2018-01-24 15:09:29 +05:30
|
|
|
return EXIT_SUCCESS;
|
2014-05-22 14:17:33 +01:00
|
|
|
}
|