Bela
Real-time, ultra-low-latency audio and sensor processing system for BeagleBone Black
Loading...
Searching...
No Matches
Spi.h
1/*
2MIT License
3
4Copyright (c) 2020 Jeremiah Rose
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22SOFTWARE.
23*/
24
25#pragma once
26
27#include <stddef.h>
28#include <linux/spi/spidev.h>
29
30class Spi {
31public:
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();
93private:
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};
void cleanup()
Definition Spi.cpp:141
int setNumBits(unsigned char numBits)
Definition Spi.cpp:103
int transfer(unsigned char *send, unsigned char *receive, size_t numBytes)
Definition Spi.cpp:123
int setup(const Settings &settings)
Definition Spi.cpp:38
int setSpeed(unsigned long speed)
Definition Spi.cpp:113
int setMode(unsigned char mode)
Definition Spi.cpp:94
Definition Spi.h:39
unsigned char numBits
numBits No. of bits per transaction. 8, 16, 24 or 32
Definition Spi.h:43
const char * device
device path to the device file (e.g.: /dev/spidev2.1)
Definition Spi.h:40
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
unsigned short delay
delay after the last bit transfer before deselecting the device
Definition Spi.h:42
unsigned long speed
requested clock rate in Hz
Definition Spi.h:41