Statistics
| Branch: | Revision:

ddr4s / fw / wiringPi / wiringPi / wiringPiSPI.c @ 32:cadb9025f1e0

History | View | Annotate | Download (4.03 KB)

1
/*
2
 * wiringPiSPI.c:
3
 *        Simplified SPI access routines
4
 *        Copyright (c) 2012-2015 Gordon Henderson
5
 ***********************************************************************
6
 * This file is part of wiringPi:
7
 *        https://projects.drogon.net/raspberry-pi/wiringpi/
8
 *
9
 *    wiringPi is free software: you can redistribute it and/or modify
10
 *    it under the terms of the GNU Lesser General Public License as
11
 *    published by the Free Software Foundation, either version 3 of the
12
 *    License, or (at your option) any later version.
13
 *
14
 *    wiringPi is distributed in the hope that it will be useful,
15
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *    GNU Lesser General Public License for more details.
18
 *
19
 *    You should have received a copy of the GNU Lesser General Public
20
 *    License along with wiringPi.
21
 *    If not, see <http://www.gnu.org/licenses/>.
22
 ***********************************************************************
23
 */
24

    
25

    
26
#include <stdio.h>
27
#include <stdint.h>
28
#include <stdlib.h>
29
#include <fcntl.h>
30
#include <errno.h>
31
#include <string.h>
32
#include <sys/ioctl.h>
33
#include <asm/ioctl.h>
34
#include <linux/spi/spidev.h>
35

    
36
#include "wiringPi.h"
37

    
38
#include "wiringPiSPI.h"
39

    
40

    
41
// The SPI bus parameters
42
//        Variables as they need to be passed as pointers later on
43

    
44
//static const char       *spiDev0  = "/dev/spidev0.0" ;
45
//static const char       *spiDev1  = "/dev/spidev0.1" ;
46
static const uint8_t     spiBPW   = 8 ;
47
static const uint16_t    spiDelay = 0 ;
48

    
49
static uint32_t    spiSpeeds [2] ;
50
static int         spiFds [2] ;
51

    
52

    
53
/*
54
 * wiringPiSPIGetFd:
55
 *        Return the file-descriptor for the given channel
56
 *********************************************************************************
57
 */
58

    
59
int wiringPiSPIGetFd (int channel)
60
{
61
  return spiFds [channel & 1] ;
62
}
63

    
64

    
65
/*
66
 * wiringPiSPIDataRW:
67
 *        Write and Read a block of data over the SPI bus.
68
 *        Note the data ia being read into the transmit buffer, so will
69
 *        overwrite it!
70
 *        This is also a full-duplex operation.
71
 *********************************************************************************
72
 */
73

    
74
int wiringPiSPIDataRW (int channel, unsigned char *data, int len)
75
{
76
  struct spi_ioc_transfer spi ;
77

    
78
  channel &= 1 ;
79

    
80
// Mentioned in spidev.h but not used in the original kernel documentation
81
//        test program )-:
82

    
83
  memset (&spi, 0, sizeof (spi)) ;
84

    
85
  spi.tx_buf        = (unsigned long)data ;
86
  spi.rx_buf        = (unsigned long)data ;
87
  spi.len           = len ;
88
  spi.delay_usecs   = spiDelay ;
89
  spi.speed_hz      = spiSpeeds [channel] ;
90
  spi.bits_per_word = spiBPW ;
91

    
92
  return ioctl (spiFds [channel], SPI_IOC_MESSAGE(1), &spi) ;
93
}
94

    
95

    
96
/*
97
 * wiringPiSPISetupMode:
98
 *        Open the SPI device, and set it up, with the mode, etc.
99
 *********************************************************************************
100
 */
101

    
102
int wiringPiSPISetupMode (int channel, int speed, int mode)
103
{
104
  int fd ;
105
  char spiDev [32] ;
106

    
107
  mode    &= 3 ;        // Mode is 0, 1, 2 or 3
108

    
109
// Channel can be anything - lets hope for the best
110
//  channel &= 1 ;        // Channel is 0 or 1
111

    
112
  snprintf (spiDev, 31, "/dev/spidev0.%d", channel) ;
113

    
114
  if ((fd = open (spiDev, O_RDWR)) < 0)
115
    return wiringPiFailure (WPI_ALMOST, "Unable to open SPI device: %s\n", strerror (errno)) ;
116

    
117
  spiSpeeds [channel] = speed ;
118
  spiFds    [channel] = fd ;
119

    
120
// Set SPI parameters.
121

    
122
  if (ioctl (fd, SPI_IOC_WR_MODE, &mode)            < 0)
123
    return wiringPiFailure (WPI_ALMOST, "SPI Mode Change failure: %s\n", strerror (errno)) ;
124
  
125
  if (ioctl (fd, SPI_IOC_WR_BITS_PER_WORD, &spiBPW) < 0)
126
    return wiringPiFailure (WPI_ALMOST, "SPI BPW Change failure: %s\n", strerror (errno)) ;
127

    
128
  if (ioctl (fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed)   < 0)
129
    return wiringPiFailure (WPI_ALMOST, "SPI Speed Change failure: %s\n", strerror (errno)) ;
130

    
131
  return fd ;
132
}
133

    
134

    
135
/*
136
 * wiringPiSPISetup:
137
 *        Open the SPI device, and set it up, etc. in the default MODE 0
138
 *********************************************************************************
139
 */
140

    
141
int wiringPiSPISetup (int channel, int speed)
142
{
143
  return wiringPiSPISetupMode (channel, speed, 0) ;
144
}