Bela
Real-time, ultra-low-latency audio and sensor processing system for BeagleBone Black
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
Spi.h
1 /*
2 MIT License
3 
4 Copyright (c) 2020 Jeremiah Rose
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24 
25 #pragma once
26 
27 #include <stddef.h>
28 #include <linux/spi/spidev.h>
29 
30 class Spi {
31 public:
32  /* Enum SPI Modes*/
33  typedef enum {
34  MODE0 = SPI_MODE_0,
35  MODE1 = SPI_MODE_1,
36  MODE2 = SPI_MODE_2,
37  MODE3 = SPI_MODE_3
38  } Mode;
39  struct Settings {
40  const char* device;
41  unsigned long speed;
42  unsigned short delay;
43  unsigned char numBits;
44  unsigned int mode;
45  };
46 
47  Spi();
48  ~Spi();
54  int setup(const Settings& settings);
65  int transfer(unsigned char *send, unsigned char *receive,
66  size_t numBytes);
73  int setMode(unsigned char mode);
80  int setNumBits(unsigned char numBits);
88  int setSpeed(unsigned long speed);
92  void cleanup();
93 private:
94  const char* device;
95  unsigned long speed;
96  unsigned short delay;
97  unsigned char numBits;
98  unsigned char mode;
99  int fd = -1;
100  struct spi_ioc_transfer transaction;
101  int openDevice(const char* device);
102 };
const char * device
device path to the device file (e.g.: /dev/spidev2.1)
Definition: Spi.h:40
Definition: Spi.h:30
int transfer(unsigned char *send, unsigned char *receive, size_t numBytes)
Definition: Spi.cpp:123
int setNumBits(unsigned char numBits)
Definition: Spi.cpp:103
Definition: Spi.h:39
int setSpeed(unsigned long speed)
Definition: Spi.cpp:113
unsigned int mode
SPI mode (e.g.: Spi::MODE3). This is not a Mode because the user can specify a custom mode by OR'ing ...
Definition: Spi.h:44
void cleanup()
Definition: Spi.cpp:141
unsigned short delay
delay after the last bit transfer before deselecting the device
Definition: Spi.h:42
int setMode(unsigned char mode)
Definition: Spi.cpp:94
unsigned char numBits
numBits No. of bits per transaction. 8, 16, 24 or 32
Definition: Spi.h:43
int setup(const Settings &settings)
Definition: Spi.cpp:38
unsigned long speed
requested clock rate in Hz
Definition: Spi.h:41