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>
34 #include "mraa_internal.h"
37 #define SPI_MAX_LENGTH 4096
40 * A structure representing the SPI device
44 int devfd; /**< File descriptor to SPI Device */
45 int mode; /**< Spi mode see spidev.h */
46 int clock; /**< clock to run transactions at */
47 mraa_boolean_t lsb; /**< least significant bit mode */
48 unsigned int bpw; /**< Bits per word */
53 mraa_spi_init(int bus)
55 mraa_spi_bus_t *spi = mraa_setup_spi(bus);
57 fprintf(stderr, "Failed. SPI platform Error\n");
60 mraa_spi_context dev = (mraa_spi_context) malloc(sizeof(struct _spi));
61 memset(dev, 0, sizeof(struct _spi));
64 sprintf(path, "/dev/spidev%u.%u", spi->bus_id, spi->slave_s);
66 dev->devfd = open(path, O_RDWR);
68 fprintf(stderr, "Failed opening SPI Device. bus:%s\n", path);
81 mraa_spi_mode(mraa_spi_context dev, unsigned short mode)
88 mraa_spi_frequency(mraa_spi_context dev, int hz)
95 mraa_spi_lsbmode(mraa_spi_context dev, mraa_boolean_t lsb)
101 if (ioctl (dev->devfd, SPI_IOC_WR_LSB_FIRST, &lsb_mode) < 0) {
102 fprintf(stderr, "Failed to set bit order\n");
103 return MRAA_ERROR_INVALID_RESOURCE;
110 mraa_spi_bit_per_word(mraa_spi_context dev, unsigned int bits)
117 mraa_spi_write(mraa_spi_context dev, uint8_t data)
119 struct spi_ioc_transfer msg;
120 memset(&msg, 0, sizeof(msg));
125 msg.tx_buf = (unsigned long) &data;
126 msg.rx_buf = (unsigned long) &recv;
127 msg.speed_hz = dev->clock;
128 msg.bits_per_word = dev->bpw;
131 if (ioctl(dev->devfd, SPI_IOC_MESSAGE(1), &msg) < 0) {
132 fprintf(stderr, "Failed to perform dev transfer\n");
139 mraa_spi_write_buf(mraa_spi_context dev, uint8_t* data, int length)
141 struct spi_ioc_transfer msg;
142 memset(&msg, 0, sizeof(msg));
144 uint8_t* recv = malloc(sizeof(uint8_t) * length);
146 msg.tx_buf = (unsigned long) data;
147 msg.rx_buf = (unsigned long) recv;
148 msg.speed_hz = dev->clock;
149 msg.bits_per_word = dev->bpw;
152 if (ioctl(dev->devfd, SPI_IOC_MESSAGE(1), &msg) < 0) {
153 fprintf(stderr, "Failed to perform dev transfer\n");
160 mraa_spi_stop(mraa_spi_context dev)