Exploring BeagleBone  V1.0
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Types | Public Member Functions
exploringBB::SPIDevice Class Reference

Generic SPI Device class that can be used to connect to any type of SPI device and read or write to its registers. More...

#include <SPIDevice.h>

Inheritance diagram for exploringBB::SPIDevice:
Inheritance graph
[legend]
Collaboration diagram for exploringBB::SPIDevice:
Collaboration graph
[legend]

Public Types

enum  SPIMODE { MODE0 = 0, MODE1 = 1, MODE2 = 2, MODE3 = 3 }
 The SPI Mode. More...
 

Public Member Functions

 SPIDevice (unsigned int bus, unsigned int device)
 
virtual int open ()
 
virtual unsigned char readRegister (unsigned int registerAddress)
 
virtual unsigned char * readRegisters (unsigned int number, unsigned int fromAddress=0)
 
virtual int writeRegister (unsigned int registerAddress, unsigned char value)
 
virtual void debugDumpRegisters (unsigned int number=0xff)
 
virtual int write (unsigned char value)
 
virtual int write (unsigned char value[], int length)
 
virtual int setSpeed (uint32_t speed)
 
virtual int setMode (SPIDevice::SPIMODE mode)
 
virtual int setBitsPerWord (uint8_t bits)
 
virtual void close ()
 
virtual ~SPIDevice ()
 
virtual int transfer (unsigned char read[], unsigned char write[], int length)
 
- Public Member Functions inherited from exploringBB::BusDevice
 BusDevice (unsigned int bus, unsigned int device)
 
virtual ~BusDevice ()
 

Additional Inherited Members

- Protected Attributes inherited from exploringBB::BusDevice
unsigned int bus
 
unsigned int device
 
int file
 

Detailed Description

Generic SPI Device class that can be used to connect to any type of SPI device and read or write to its registers.

Member Enumeration Documentation

The SPI Mode.

Enumerator
MODE0 

Low at idle, capture on rising clock edge.

MODE1 

Low at idle, capture on falling clock edge.

MODE2 

High at idle, capture on falling clock edge.

MODE3 

High at idle, capture on rising clock edge.

42  {
43  MODE0 = 0,
44  MODE1 = 1,
45  MODE2 = 2,
46  MODE3 = 3
47  };
Low at idle, capture on rising clock edge.
Definition: SPIDevice.h:43
High at idle, capture on falling clock edge.
Definition: SPIDevice.h:45
Low at idle, capture on falling clock edge.
Definition: SPIDevice.h:44
High at idle, capture on rising clock edge.
Definition: SPIDevice.h:46

Constructor & Destructor Documentation

exploringBB::SPIDevice::SPIDevice ( unsigned int  bus,
unsigned int  device 
)

The constructor for the SPIDevice that sets up and opens the SPI connection. The destructor will close the SPI file connection.

Parameters
busThe SPI bus number X (first digit after spidevX.Y)
deviceThe device on the bus Y (second digit after spidevX.Y)
51  :
53  stringstream s;
54  s << SPI_PATH << bus << "." << device;
55  this->filename = string(s.str());
56  this->mode = SPIDevice::MODE3;
57  this->bits = 8;
58  this->speed = 500000;
59  this->delay = 0;
60  this->open();
61 }
BusDevice(unsigned int bus, unsigned int device)
Definition: BusDevice.cpp:34
unsigned int bus
Definition: BusDevice.h:39
#define SPI_PATH
Definition: SPIDevice.h:31
unsigned int device
Definition: BusDevice.h:40
virtual int open()
Definition: SPIDevice.cpp:67
High at idle, capture on rising clock edge.
Definition: SPIDevice.h:46
exploringBB::SPIDevice::~SPIDevice ( )
virtual

The destructor closes the SPI bus device

253  {
254  this->close();
255 }
virtual void close()
Definition: SPIDevice.cpp:245

Member Function Documentation

void exploringBB::SPIDevice::close ( )
virtual

Close the SPI device

Implements exploringBB::BusDevice.

245  {
246  ::close(this->file);
247  this->file = -1;
248 }
int file
Definition: BusDevice.h:41
virtual void close()
Definition: SPIDevice.cpp:245
void exploringBB::SPIDevice::debugDumpRegisters ( unsigned int  number = 0xff)
virtual

A simple method to dump the registers to the standard output – useful for debugging

Parameters
numberthe number of registers to dump

Implements exploringBB::BusDevice.

178  {
179  cout << "SPI Mode: " << this->mode << endl;
180  cout << "Bits per word: " << (int)this->bits << endl;
181  cout << "Max speed: " << this->speed << endl;
182  cout << "Dumping Registers for Debug Purposes:" << endl;
183  unsigned char *registers = this->readRegisters(number);
184  for(int i=0; i<(int)number; i++){
185  cout << HEX(*(registers+i)) << " ";
186  if (i%16==15) cout << endl;
187  }
188  cout << dec;
189 }
#define HEX(x)
Macro for filling in leading 0 on HEX outputs.
Definition: SPIDevice.cpp:41
virtual unsigned char * readRegisters(unsigned int number, unsigned int fromAddress=0)
Definition: SPIDevice.cpp:124
int exploringBB::SPIDevice::open ( )
virtual

This method opens the file connection to the SPI device.

Returns
0 on a successful open of the file

Implements exploringBB::BusDevice.

67  {
68  //cout << "Opening the file: " << filename.c_str() << endl;
69  if ((this->file = ::open(filename.c_str(), O_RDWR))<0){
70  perror("SPI: Can't open device.");
71  return -1;
72  }
73  if (this->setMode(this->mode)==-1) return -1;
74  if (this->setSpeed(this->speed)==-1) return -1;
75  if (this->setBitsPerWord(this->bits)==-1) return -1;
76  return 0;
77 }
int file
Definition: BusDevice.h:41
virtual int setMode(SPIDevice::SPIMODE mode)
Definition: SPIDevice.cpp:212
virtual int setBitsPerWord(uint8_t bits)
Definition: SPIDevice.cpp:229
virtual int setSpeed(uint32_t speed)
Definition: SPIDevice.cpp:195
virtual int open()
Definition: SPIDevice.cpp:67
unsigned char exploringBB::SPIDevice::readRegister ( unsigned int  registerAddress)
virtual

A method to read a single register at the SPI address

Parameters
registerAddressthe address of the register from the device datasheet
Returns
the character that is returned from the address

Implements exploringBB::BusDevice.

108  {
109  unsigned char send[2], receive[2];
110  memset(send, 0, sizeof send);
111  memset(receive, 0, sizeof receive);
112  send[0] = (unsigned char) (0x80 | registerAddress);
113  this->transfer(send, receive, 2);
114  //cout << "The value that was received is: " << (int) receive[1] << endl;
115  return receive[1];
116 }
virtual int transfer(unsigned char read[], unsigned char write[], int length)
Definition: SPIDevice.cpp:87
unsigned char * exploringBB::SPIDevice::readRegisters ( unsigned int  number,
unsigned int  fromAddress = 0 
)
virtual

A method to read a number of registers as a data array

Parameters
numberthe number of registers to read
fromAddressthe starting address of the block of data
Returns
the data array that is returned (memory allocated by the method)

Implements exploringBB::BusDevice.

124  {
125  unsigned char* data = new unsigned char[number];
126  unsigned char send[number+1], receive[number+1];
127  memset(send, 0, sizeof send);
128  send[0] = (unsigned char) (0x80 | 0x40 | fromAddress); //set read bit and MB bit
129  this->transfer(send, receive, number+1);
130  memcpy(data, receive+1, number); //ignore the first (address) byte in the array returned
131  return data;
132 }
virtual int transfer(unsigned char read[], unsigned char write[], int length)
Definition: SPIDevice.cpp:87
int exploringBB::SPIDevice::setBitsPerWord ( uint8_t  bits)
virtual

Set the number of bits per word of the SPI bus

Parameters
bitsthe number of bits per word
229  {
230  this->bits = bits;
231  if (ioctl(this->file, SPI_IOC_WR_BITS_PER_WORD, &this->bits)==-1){
232  perror("SPI: Can't set bits per word.");
233  return -1;
234  }
235  if (ioctl(this->file, SPI_IOC_RD_BITS_PER_WORD, &this->bits)==-1){
236  perror("SPI: Can't get bits per word.");
237  return -1;
238  }
239  return 0;
240 }
int file
Definition: BusDevice.h:41
int exploringBB::SPIDevice::setMode ( SPIDevice::SPIMODE  mode)
virtual

Set the mode of the SPI bus

Parameters
modethe enumerated SPI mode
212  {
213  this->mode = mode;
214  if (ioctl(this->file, SPI_IOC_WR_MODE, &this->mode)==-1){
215  perror("SPI: Can't set SPI mode.");
216  return -1;
217  }
218  if (ioctl(this->file, SPI_IOC_RD_MODE, &this->mode)==-1){
219  perror("SPI: Can't get SPI mode.");
220  return -1;
221  }
222  return 0;
223 }
int file
Definition: BusDevice.h:41
int exploringBB::SPIDevice::setSpeed ( uint32_t  speed)
virtual

Set the speed of the SPI bus

Parameters
speedthe speed in Hz
195  {
196  this->speed = speed;
197  if (ioctl(this->file, SPI_IOC_WR_MAX_SPEED_HZ, &this->speed)==-1){
198  perror("SPI: Can't set max speed HZ");
199  return -1;
200  }
201  if (ioctl(this->file, SPI_IOC_RD_MAX_SPEED_HZ, &this->speed)==-1){
202  perror("SPI: Can't get max speed HZ.");
203  return -1;
204  }
205  return 0;
206 }
int file
Definition: BusDevice.h:41
int exploringBB::SPIDevice::transfer ( unsigned char  send[],
unsigned char  receive[],
int  length 
)
virtual

Generic method to transfer data to and from the SPI device. It is used by the following methods to read and write registers.

Parameters
sendThe array of data to send to the SPI device
receiveThe array of data to receive from the SPI device
lengthThe length of the array to send
Returns
-1 on failure
87  {
88  struct spi_ioc_transfer transfer;
89  transfer.tx_buf = (unsigned long) send;
90  transfer.rx_buf = (unsigned long) receive;
91  transfer.len = length;
92  transfer.speed_hz = this->speed;
93  transfer.bits_per_word = this->bits;
94  transfer.delay_usecs = this->delay;
95  int status = ioctl(this->file, SPI_IOC_MESSAGE(1), &transfer);
96  if (status < 0) {
97  perror("SPI: SPI_IOC_MESSAGE Failed");
98  return -1;
99  }
100  return status;
101 }
int file
Definition: BusDevice.h:41
virtual int transfer(unsigned char read[], unsigned char write[], int length)
Definition: SPIDevice.cpp:87
int exploringBB::SPIDevice::write ( unsigned char  value)
virtual

A write method that writes a single character to the SPI bus

Parameters
valuethe value to write to the bus
Returns
returns 0 if successful

Implements exploringBB::BusDevice.

139  {
140  unsigned char null_return = 0x00;
141  //printf("[%02x]", value);
142  this->transfer(&value, &null_return, 1);
143  return 0;
144 }
virtual int transfer(unsigned char read[], unsigned char write[], int length)
Definition: SPIDevice.cpp:87
int exploringBB::SPIDevice::write ( unsigned char  value[],
int  length 
)
virtual

A write method that writes a block of data of the length to the bus.

Parameters
valuethe array of data to write to the device
lengththe length of the data array
Returns
returns 0 if successful
152  {
153  unsigned char null_return = 0x00;
154  this->transfer(value, &null_return, length);
155  return 0;
156 }
virtual int transfer(unsigned char read[], unsigned char write[], int length)
Definition: SPIDevice.cpp:87
int exploringBB::SPIDevice::writeRegister ( unsigned int  registerAddress,
unsigned char  value 
)
virtual

Writes a value to a defined register address (check the datasheet for the device)

Parameters
registerAddressthe address of the register to write to
valuethe value to write to the register
Returns
returns 0 if successful

Implements exploringBB::BusDevice.

164  {
165  unsigned char send[2], receive[2];
166  memset(receive, 0, sizeof receive);
167  send[0] = (unsigned char) registerAddress;
168  send[1] = value;
169  //cout << "The value that was written is: " << (int) send[1] << endl;
170  this->transfer(send, receive, 2);
171  return 0;
172 }
virtual int transfer(unsigned char read[], unsigned char write[], int length)
Definition: SPIDevice.cpp:87

The documentation for this class was generated from the following files: