2 * Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
3 * Copyright (c) 2014 Intel Corporation.
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 #include <sys/ioctl.h>
28 #include <linux/spi/spidev.h>
35 #define SPI_MAX_LENGTH 4096
38 * A strucutre representing the SPI device
42 int devfd; /**< File descriptor to SPI Device */
49 double bus = maa_check_spi();
51 fprintf(stderr, "Failed. SPI platform Error\n");
54 maa_spi_context dev = (maa_spi_context) malloc(sizeof(struct _spi));
55 memset(dev, 0, sizeof(struct _spi));
58 sprintf(path, "/dev/spidev%.1f", bus);
60 dev->devfd = open(path, O_RDWR);
62 fprintf(stderr, "Failed opening SPI Device. bus:%s\n", path);
71 maa_spi_mode(maa_spi_context dev, unsigned short mode)
73 return MAA_ERROR_FEATURE_NOT_IMPLEMENTED;
77 maa_spi_frequency(maa_spi_context dev, int hz)
79 return MAA_ERROR_FEATURE_NOT_IMPLEMENTED;
83 maa_spi_write(maa_spi_context dev, uint8_t data)
85 struct spi_ioc_transfer msg;
86 memset(&msg, 0, sizeof(msg));
91 msg.tx_buf = (unsigned long) &data;
92 msg.rx_buf = (unsigned long) &recv;
93 msg.speed_hz = 100000;
94 msg.bits_per_word = 8;
97 if (ioctl(dev->devfd, SPI_IOC_MESSAGE(1), &msg) < 0) {
98 fprintf(stderr, "Failed to perform dev transfer\n");
105 maa_spi_write_buf(maa_spi_context dev, uint8_t* data, int length)
107 struct spi_ioc_transfer msg;
108 memset(&msg, 0, sizeof(msg));
110 uint8_t* recv = malloc(sizeof(uint8_t) * length);
112 msg.tx_buf = (unsigned long) data;
113 msg.rx_buf = (unsigned long) recv;
114 msg.speed_hz = 100000;
115 msg.bits_per_word = 8;
118 if (ioctl(dev->devfd, SPI_IOC_MESSAGE(1), &msg) < 0) {
119 fprintf(stderr, "Failed to perform dev transfer\n");
126 maa_spi_stop(maa_spi_context dev)
128 return MAA_ERROR_FEATURE_NOT_IMPLEMENTED;