Added new SPI driver helpers.

Changed the gertboard code to use it
and ran more tests on he Gertboard code.
This commit is contained in:
Gordon Henderson
2012-08-27 20:56:14 +01:00
parent 4666573910
commit 99095e3fa0
7 changed files with 331 additions and 100 deletions

View File

@@ -38,12 +38,12 @@ LIBS =
SRC = wiringPi.c wiringPiFace.c wiringSerial.c wiringShift.c \
gertboard.c \
piNes.c \
lcd.c piHiPri.c piThread.c softPwm.c
lcd.c piHiPri.c piThread.c softPwm.c wiringPiSPI.c
OBJ = wiringPi.o wiringPiFace.o wiringSerial.o wiringShift.o \
gertboard.o \
piNes.o \
lcd.o piHiPri.o piThread.o softPwm.o
lcd.o piHiPri.o piThread.o softPwm.o wiringPiSPI.o
all: $(TARGET)
@@ -78,6 +78,7 @@ install: $(TARGET)
install -m 0644 piNes.h /usr/local/include
install -m 0644 softPwm.h /usr/local/include
install -m 0644 lcd.h /usr/local/include
install -m 0644 wiringPiSPI.h /usr/local/include
install -m 0644 libwiringPi.a /usr/local/lib
uninstall:

View File

@@ -38,23 +38,16 @@
#include <sys/ioctl.h>
#include <linux/spi/spidev.h>
#include "wiringPiSPI.h"
#include "gertboard.h"
// The A-D convertor won't run at more than 1MHz @ 3.3v
// The SPI bus parameters
// Variables as they need to be passed as pointers later on
static char *spiA2D = "/dev/spidev0.0" ;
static char *spiD2A = "/dev/spidev0.1" ;
static uint8_t spiMode = 0 ;
static uint8_t spiBPW = 8 ;
static uint32_t spiSpeed = 100000 ; // 1MHz
static uint16_t spiDelay = 0;
// Locals here to keep track of everything
static int spiFdA2D ;
static int spiFdD2A ;
#define SPI_ADC_SPEED 1000000
#define SPI_DAC_SPEED 1000000
#define SPI_A2D 0
#define SPI_D2A 1
/*
@@ -66,10 +59,7 @@ static int spiFdD2A ;
void gertboardAnalogWrite (int chan, int value)
{
uint8_t spiBufTx [2] ;
uint8_t spiBufRx [2] ;
struct spi_ioc_transfer spi ;
uint8_t spiData [2] ;
uint8_t chanBits, dataBits ;
if (chan == 0)
@@ -80,17 +70,10 @@ void gertboardAnalogWrite (int chan, int value)
chanBits |= ((value >> 4) & 0x0F) ;
dataBits = ((value << 4) & 0xF0) ;
spiBufTx [0] = chanBits ;
spiBufTx [1] = dataBits ;
spiData [0] = chanBits ;
spiData [1] = dataBits ;
spi.tx_buf = (unsigned long)spiBufTx ;
spi.rx_buf = (unsigned long)spiBufRx ;
spi.len = 2 ;
spi.delay_usecs = spiDelay ;
spi.speed_hz = spiSpeed ;
spi.bits_per_word = spiBPW ;
ioctl (spiFdD2A, SPI_IOC_MESSAGE(1), &spi) ;
wiringPiSPIDataRW (SPI_D2A, spiData, 2) ;
}
@@ -103,60 +86,21 @@ void gertboardAnalogWrite (int chan, int value)
int gertboardAnalogRead (int chan)
{
uint8_t spiBufTx [4] ;
uint8_t spiBufRx [4] ;
struct spi_ioc_transfer spi ;
uint8_t spiData [2] ;
uint8_t chanBits ;
if (chan == 0)
chanBits = 0b0110100 ;
chanBits = 0b11010000 ;
else
chanBits = 0b0111100 ;
chanBits = 0b11110000 ;
spiBufTx [0] = chanBits ;
spiBufTx [1] = 0 ;
spiData [0] = chanBits ;
spiData [1] = 0 ;
spi.tx_buf = (unsigned long)spiBufTx ;
spi.rx_buf = (unsigned long)spiBufRx ;
spi.len = 4 ;
spi.delay_usecs = spiDelay ;
spi.speed_hz = spiSpeed ;
spi.bits_per_word = spiBPW ;
wiringPiSPIDataRW (SPI_A2D, spiData, 2) ;
ioctl (spiFdA2D, SPI_IOC_MESSAGE(1), &spi) ;
return spiBufRx [0] << 8 | spiBufRx [1] ;
}
/*
* setParams:
* Output the SPI bus parameters to the given device
*********************************************************************************
*/
static int setParams (int fd)
{
if (ioctl (fd, SPI_IOC_WR_MODE, &spiMode) < 0)
return -1 ;
if (ioctl (fd, SPI_IOC_RD_MODE, &spiMode) < 0)
return -1 ;
if (ioctl (fd, SPI_IOC_WR_BITS_PER_WORD, &spiBPW) < 0)
return -1 ;
if (ioctl (fd, SPI_IOC_RD_BITS_PER_WORD, &spiBPW) < 0)
return -1 ;
if (ioctl (fd, SPI_IOC_WR_MAX_SPEED_HZ, &spiSpeed) < 0)
return -1 ;
if (ioctl (fd, SPI_IOC_RD_MAX_SPEED_HZ, &spiSpeed) < 0)
return -1 ;
return 0 ;
return ((spiData [0] << 7) | (spiData [1] >> 1)) & 0x3FF ;
}
@@ -168,16 +112,10 @@ static int setParams (int fd)
int gertboardSPISetup (void)
{
if ((spiFdA2D = open (spiA2D, O_RDWR)) < 0)
if (wiringPiSPISetup (SPI_A2D, SPI_ADC_SPEED) < 0)
return -1 ;
if (setParams (spiFdA2D) != 0)
return -1 ;
if ((spiFdD2A = open (spiD2A, O_RDWR)) < 0)
return -1 ;
if (setParams (spiFdD2A) != 0)
if (wiringPiSPISetup (SPI_D2A, SPI_DAC_SPEED) < 0)
return -1 ;
return 0 ;

117
wiringPi/wiringPiSPI.c Normal file
View File

@@ -0,0 +1,117 @@
/*
* wiringPiSPI.c:
* Simplified SPI access routines
* Copyright (c) 2012 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* wiringPi 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with wiringPi.
* If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <stdint.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/spi/spidev.h>
#include "wiringPiSPI.h"
// The SPI bus parameters
// Variables as they need to be passed as pointers later on
static char *spiDev0 = "/dev/spidev0.0" ;
static char *spiDev1 = "/dev/spidev0.1" ;
static uint8_t spiMode = 0 ;
static uint8_t spiBPW = 8 ;
static uint16_t spiDelay = 0;
static uint32_t spiSpeeds [2] ;
static int spiFds [2] ;
/*
* wiringPiSPIGetFd:
* Return the file-descriptor for the given channel
*********************************************************************************
*/
int wiringPiSPIGetFd (int channel)
{
return spiFds [channel &1] ;
}
/*
* wiringPiSPIDataRW:
* Write and Read a block of data over the SPI bus.
* Note the data ia being read into the transmit buffer, so will
* overwrite it!
* This is also a full-duplex operation.
*********************************************************************************
*/
int wiringPiSPIDataRW (int channel, unsigned char *data, int len)
{
struct spi_ioc_transfer spi ;
channel &= 1 ;
spi.tx_buf = (unsigned long)data ;
spi.rx_buf = (unsigned long)data ;
spi.len = len ;
spi.delay_usecs = spiDelay ;
spi.speed_hz = spiSpeeds [channel] ;
spi.bits_per_word = spiBPW ;
return ioctl (spiFds [channel], SPI_IOC_MESSAGE(1), &spi) ;
}
/*
* wiringPiSPISetup:
* Open the SPI device, and set it up, etc.
*********************************************************************************
*/
int wiringPiSPISetup (int channel, int speed)
{
int fd ;
channel &= 1 ;
if ((fd = open (channel == 0 ? spiDev0 : spiDev1, O_RDWR)) < 0)
return -1 ;
spiSpeeds [channel] = speed ;
spiFds [channel] = fd ;
// Set SPI parameters.
// Why are we reading it afterwriting it? I've no idea, but for now I'm blindly
// copying example code I've seen online...
if (ioctl (fd, SPI_IOC_WR_MODE, &spiMode) < 0) return -1 ;
if (ioctl (fd, SPI_IOC_RD_MODE, &spiMode) < 0) return -1 ;
if (ioctl (fd, SPI_IOC_WR_BITS_PER_WORD, &spiBPW) < 0) return -1 ;
if (ioctl (fd, SPI_IOC_RD_BITS_PER_WORD, &spiBPW) < 0) return -1 ;
if (ioctl (fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0) return -1 ;
if (ioctl (fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) < 0) return -1 ;
return fd ;
}

35
wiringPi/wiringPiSPI.h Normal file
View File

@@ -0,0 +1,35 @@
/*
* wiringPiSPI.h:
* Simplified SPI access routines
* Copyright (c) 2012 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* wiringPi 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with wiringPi.
* If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#ifdef __cplusplus
extern "C" {
#endif
int wiringPiSPIGetFd (int channel) ;
int wiringPiSPIDataRW (int channel, unsigned char *data, int len) ;
int wiringPiSPISetup (int channel, int speed) ;
#ifdef __cplusplus
}
#endif