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 if (advance_func->spi_init_pre != NULL) {
56 if (advance_func->spi_init_pre(bus) != MRAA_SUCCESS)
60 mraa_spi_bus_t *spi = mraa_setup_spi(bus);
62 fprintf(stderr, "Failed. SPI platform Error\n");
65 mraa_spi_context dev = (mraa_spi_context) malloc(sizeof(struct _spi));
66 memset(dev, 0, sizeof(struct _spi));
69 sprintf(path, "/dev/spidev%u.%u", spi->bus_id, spi->slave_s);
71 dev->devfd = open(path, O_RDWR);
73 fprintf(stderr, "Failed opening SPI Device. bus:%s\n", path);
82 if (advance_func->spi_init_post != NULL) {
83 mraa_result_t ret = advance_func->spi_init_post(dev);
84 if (ret != MRAA_SUCCESS) {
94 mraa_spi_mode(mraa_spi_context dev, mraa_spi_mode_t mode)
99 spi_mode = SPI_MODE_0;
102 spi_mode = SPI_MODE_1;
105 spi_mode = SPI_MODE_2;
108 spi_mode = SPI_MODE_3;
111 spi_mode = SPI_MODE_0;
115 if (ioctl (dev->devfd, SPI_IOC_WR_MODE, &spi_mode) < 0) {
116 fprintf(stderr, "Failed to set spi mode\n");
117 return MRAA_ERROR_INVALID_RESOURCE;
120 dev->mode = spi_mode;
125 mraa_spi_frequency(mraa_spi_context dev, int hz)
132 mraa_spi_lsbmode(mraa_spi_context dev, mraa_boolean_t lsb)
134 uint8_t lsb_mode = 0;
138 if (ioctl (dev->devfd, SPI_IOC_WR_LSB_FIRST, &lsb_mode) < 0) {
139 fprintf(stderr, "Failed to set bit order\n");
140 return MRAA_ERROR_INVALID_RESOURCE;
147 mraa_spi_bit_per_word(mraa_spi_context dev, unsigned int bits)
154 mraa_spi_write(mraa_spi_context dev, uint8_t data)
156 struct spi_ioc_transfer msg;
157 memset(&msg, 0, sizeof(msg));
162 msg.tx_buf = (unsigned long) &data;
163 msg.rx_buf = (unsigned long) &recv;
164 msg.speed_hz = dev->clock;
165 msg.bits_per_word = dev->bpw;
168 if (ioctl(dev->devfd, SPI_IOC_MESSAGE(1), &msg) < 0) {
169 fprintf(stderr, "Failed to perform dev transfer\n");
176 mraa_spi_write_buf(mraa_spi_context dev, uint8_t* data, int length)
178 struct spi_ioc_transfer msg;
179 memset(&msg, 0, sizeof(msg));
181 uint8_t* recv = malloc(sizeof(uint8_t) * length);
183 msg.tx_buf = (unsigned long) data;
184 msg.rx_buf = (unsigned long) recv;
185 msg.speed_hz = dev->clock;
186 msg.bits_per_word = dev->bpw;
189 if (ioctl(dev->devfd, SPI_IOC_MESSAGE(1), &msg) < 0) {
190 fprintf(stderr, "Failed to perform dev transfer\n");
197 mraa_spi_stop(mraa_spi_context dev)