-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspidev_lib.c
More file actions
122 lines (98 loc) · 3.3 KB
/
spidev_lib.c
File metadata and controls
122 lines (98 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
*
* This file is based on of pyA20.
* spi_lib.c python SPI extension.
*
* Copyright (c) 2014 Stefan Mavrodiev @ OLIMEX LTD, <support@olimex.com>
*
* pyA20 is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
* Adpated by Philippe Van Hecke <lemouchon@gmail.com>
*/
#include <stdio.h>
#include <stdint.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/ioctl.h>
//#include <libexplain/ioctl.h>
#include <string.h>
#include <linux/spi/spidev.h>
#include "spidev_lib.h"
int spi_open(char *device, spi_config_t config) {
int fd;
/* Open block device */
fd = open(device, O_RDWR);
if (fd < 0) {
return fd;
}
/* Set SPI_POL and SPI_PHA */
if (ioctl(fd, SPI_IOC_WR_MODE, &config.mode) < 0) {
return -1;
}
if (ioctl(fd, SPI_IOC_RD_MODE, &config.mode) < 0) {
return -1;
}
/* Set bits per word*/
if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &config.bits_per_word) < 0) {
return -1;
}
if (ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &config.bits_per_word) < 0) {
return -1;
}
/* Set SPI speed*/
if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &config.speed) < 0) {
return -1;
}
if (ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &config.speed) < 0) {
return -1;
}
/* Return file descriptor */
return fd;
}
int spi_close(int fd) {
return close(fd);
}
int spi_xfer(int fd, uint8_t *tx_buffer, uint8_t tx_len, uint8_t *rx_buffer, uint8_t rx_len)
{
int l_nIntResult;
struct spi_ioc_transfer spi_message[1];
memset(spi_message, 0, sizeof(spi_message));
spi_message[0].rx_buf = (unsigned long)rx_buffer;
spi_message[0].tx_buf = (unsigned long)tx_buffer;
spi_message[0].len = tx_len;
l_nIntResult = ioctl(fd, SPI_IOC_MESSAGE(1), spi_message);
if ( l_nIntResult < 0 )
{
//printf("%s: ioctl err: %d [ %s ]", __FUNCTION__, l_nIntResult, explain_ioctl(fd, SPI_IOC_MESSAGE(1), spi_message));
printf("%s: ioctl err: %d [ %s ]\r\n", __FUNCTION__, l_nIntResult, strerror(errno));
}
return l_nIntResult;
}
int spi_read(int fd, uint8_t *rx_buffer, uint8_t rx_len){
struct spi_ioc_transfer spi_message[1];
memset(spi_message, 0, sizeof(spi_message));
spi_message[0].rx_buf = (unsigned long)rx_buffer;
spi_message[0].len = rx_len;
return ioctl(fd, SPI_IOC_MESSAGE(1), spi_message);
}
int spi_write(int fd, uint8_t *tx_buffer, uint8_t tx_len){
struct spi_ioc_transfer spi_message[1];
memset(spi_message, 0, sizeof(spi_message));
spi_message[0].tx_buf = (unsigned long)tx_buffer;
spi_message[0].len = tx_len;
return ioctl(fd, SPI_IOC_MESSAGE(1), spi_message);
}