spi: fix use of memset sizeof param
[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
32 #include "spi.h"
33
34 #define MAX_SIZE 64
35 #define SPI_MAX_LENGTH 4096
36
37 /**
38  * A strucutre representing the SPI device
39  */
40 struct _spi {
41     /*@{*/
42     int devfd; /**< File descriptor to SPI Device */
43     /*@}*/
44 };
45
46 maa_spi_context
47 maa_spi_init()
48 {
49     double bus = maa_check_spi();
50     if(bus < 0) {
51         fprintf(stderr, "Failed. SPI platform Error\n");
52         return NULL;
53     }
54     maa_spi_context dev = (maa_spi_context) malloc(sizeof(struct _spi));
55     memset(dev, 0, sizeof(struct _spi));
56
57     char path[MAX_SIZE];
58     sprintf(path, "/dev/spidev%.1f", bus);
59
60     dev->devfd = open(path, O_RDWR);
61     if (dev->devfd < 0) {
62         fprintf(stderr, "Failed opening SPI Device. bus:%s\n", path);
63         free(dev);
64         return NULL;
65     }
66
67     return dev;
68 }
69
70 maa_result_t
71 maa_spi_mode(maa_spi_context dev, unsigned short mode)
72 {
73     return MAA_ERROR_FEATURE_NOT_IMPLEMENTED;
74 }
75
76 maa_result_t
77 maa_spi_frequency(maa_spi_context dev, int hz)
78 {
79     return MAA_ERROR_FEATURE_NOT_IMPLEMENTED;
80 }
81
82 uint8_t
83 maa_spi_write(maa_spi_context dev, uint8_t data)
84 {
85     struct spi_ioc_transfer msg;
86     memset(&msg, 0, sizeof(msg));
87
88     uint16_t length = 1;
89
90     uint8_t recv = 0;
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;
95     msg.delay_usecs = 0;
96     msg.len = length;
97     if (ioctl(dev->devfd, SPI_IOC_MESSAGE(1), &msg) < 0) {
98         fprintf(stderr, "Failed to perform dev transfer\n");
99         return -1;
100     }
101     return recv;
102 }
103
104 uint8_t*
105 maa_spi_write_buf(maa_spi_context dev, uint8_t* data, int length)
106 {
107     struct spi_ioc_transfer msg;
108     memset(&msg, 0, sizeof(msg));
109
110     uint8_t* recv = malloc(sizeof(uint8_t) * length);
111
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;
116     msg.delay_usecs = 0;
117     msg.len = length;
118     if (ioctl(dev->devfd, SPI_IOC_MESSAGE(1), &msg) < 0) {
119         fprintf(stderr, "Failed to perform dev transfer\n");
120         return NULL;
121     }
122     return recv;
123 }
124
125 maa_result_t
126 maa_spi_stop(maa_spi_context dev)
127 {
128     return MAA_ERROR_FEATURE_NOT_IMPLEMENTED;
129 }