mraa: rename from maa to mraa
[contrib/mraa.git] / src / spi / spi.c
1 /*
2  * Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
3  * Copyright (c) 2014 Intel Corporation.
4  *
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:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
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.
23  */
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/ioctl.h>
28 #include <linux/spi/spidev.h>
29 #include <stdio.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32
33 #include "spi.h"
34 #include "mraa_internal.h"
35
36 #define MAX_SIZE 64
37 #define SPI_MAX_LENGTH 4096
38
39 /**
40  * A structure representing the SPI device
41  */
42 struct _spi {
43     /*@{*/
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 */
49     /*@}*/
50 };
51
52 mraa_spi_context
53 mraa_spi_init(int bus)
54 {
55     mraa_spi_bus_t *spi = mraa_setup_spi(bus);
56     if(bus < 0) {
57         fprintf(stderr, "Failed. SPI platform Error\n");
58         return NULL;
59     }
60     mraa_spi_context dev = (mraa_spi_context) malloc(sizeof(struct _spi));
61     memset(dev, 0, sizeof(struct _spi));
62
63     char path[MAX_SIZE];
64     sprintf(path, "/dev/spidev%u.%u", spi->bus_id, spi->slave_s);
65
66     dev->devfd = open(path, O_RDWR);
67     if (dev->devfd < 0) {
68         fprintf(stderr, "Failed opening SPI Device. bus:%s\n", path);
69         free(dev);
70         return NULL;
71     }
72     dev->bpw = 8;
73     dev->clock = 4000000;
74     dev->lsb = 0;
75     dev->mode = 0;
76
77     return dev;
78 }
79
80 mraa_result_t
81 mraa_spi_mode(mraa_spi_context dev, unsigned short mode)
82 {
83     dev->mode = mode;
84     return MRAA_SUCCESS;
85 }
86
87 mraa_result_t
88 mraa_spi_frequency(mraa_spi_context dev, int hz)
89 {
90     dev->clock = hz;
91     return MRAA_SUCCESS;
92 }
93
94 mraa_result_t
95 mraa_spi_lsbmode(mraa_spi_context dev, mraa_boolean_t lsb)
96 {
97     uint8_t lsb_mode = 0;
98     if (lsb == 1) {
99         lsb_mode = 1;
100     }
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;
104     }
105     dev->lsb = lsb;
106     return MRAA_SUCCESS;
107 }
108
109 mraa_result_t
110 mraa_spi_bit_per_word(mraa_spi_context dev, unsigned int bits)
111 {
112     dev->bpw = bits;
113     return MRAA_SUCCESS;
114 }
115
116 uint8_t
117 mraa_spi_write(mraa_spi_context dev, uint8_t data)
118 {
119     struct spi_ioc_transfer msg;
120     memset(&msg, 0, sizeof(msg));
121
122     uint16_t length = 1;
123
124     uint8_t recv = 0;
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;
129     msg.delay_usecs = 0;
130     msg.len = length;
131     if (ioctl(dev->devfd, SPI_IOC_MESSAGE(1), &msg) < 0) {
132         fprintf(stderr, "Failed to perform dev transfer\n");
133         return -1;
134     }
135     return recv;
136 }
137
138 uint8_t*
139 mraa_spi_write_buf(mraa_spi_context dev, uint8_t* data, int length)
140 {
141     struct spi_ioc_transfer msg;
142     memset(&msg, 0, sizeof(msg));
143
144     uint8_t* recv = malloc(sizeof(uint8_t) * length);
145
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;
150     msg.delay_usecs = 0;
151     msg.len = length;
152     if (ioctl(dev->devfd, SPI_IOC_MESSAGE(1), &msg) < 0) {
153         fprintf(stderr, "Failed to perform dev transfer\n");
154         return NULL;
155     }
156     return recv;
157 }
158
159 mraa_result_t
160 mraa_spi_stop(mraa_spi_context dev)
161 {
162     close(dev->devfd);
163     return MRAA_SUCCESS;
164 }