Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
SPI.hxx
Go to the documentation of this file.
1
34#ifndef _FREERTOS_DRIVERS_COMMON_SPI_HXX_
35#define _FREERTOS_DRIVERS_COMMON_SPI_HXX_
36
37#include <unistd.h>
38#include <compiler.h>
39
40#include "BlockOrWakeUp.hxx"
41#include "SimpleLog.hxx"
42#include "Devtab.hxx"
43#include "nmranet_config.h"
44#include "os/OS.hxx"
45#include "spi/spidev.h"
46
52class SPI : public Node
53{
54public:
56 typedef void (*ChipSelectMethod)();
57
63 __attribute__((optimize("-O3")))
64 int transfer_with_cs_assert(struct spi_ioc_transfer *msg)
65 {
66 csAssert();
67 int result = transfer(msg);
68 csDeassert();
69 return result;
70 }
71
79 __attribute__((optimize("-O3")))
80 int transfer_with_cs_assert_polled(struct spi_ioc_transfer *msgs,
81 int num = 1)
82 {
83 int count = 0;
84 int result;
85
86 csAssert();
87 for (int i = 0; i < num; ++i, ++msgs)
88 {
89 result = transfer_polled(msgs);
90 if (UNLIKELY(result < 0))
91 {
92 // something bad happened, reset the bus and bail
93 csDeassert();
94 return result;
95 }
96 count += msgs->len;
97 }
98 csDeassert();
99 return count;
100 }
101
107 __attribute__((optimize("-O3")))
108 int transfer_messages(struct spi_ioc_transfer *msgs, int num);
109
110protected:
120 SPI(const char *name, ChipSelectMethod cs_assert,
121 ChipSelectMethod cs_deassert, OSMutex *bus_lock = nullptr)
122 : Node(name)
123 , csAssert(cs_assert)
124 , csDeassert(cs_deassert)
125 , speedHz(1000000)
126 , bitsPerWord(8)
127 , mode(SPI_MODE_0)
128 , lsbFirst(false)
130 {
131 }
132
136 {
137 HASSERT(0);
138 }
139
141 void bus_lock()
142 {
143 if (busLock)
144 {
145 busLock->lock();
146 }
147 }
148
151 {
152 if (busLock)
153 {
154 busLock->unlock();
155 }
156 }
157
162 virtual int transfer(struct spi_ioc_transfer *msg) = 0;
163
168 virtual int transfer_polled(struct spi_ioc_transfer *msg) = 0;
169
173 virtual int update_configuration() = 0;
174
181 int ioctl(File *file, unsigned long int key, unsigned long data) override;
182
185
188
190 uint32_t speedHz;
191
193 uint8_t bitsPerWord;
194
196 uint8_t mode;
197
200
201private:
208 ssize_t read(File *file, void *buf, size_t count) override;
209
216 ssize_t write(File *file, const void *buf, size_t count) override;
217
219 void flush_buffers() override {}
220
223
227
229};
230
231#endif /* _FREERTOS_DRIVERS_COMMON_SPI_HXX_ */
const char * name
device name
Definition Devtab.hxx:266
Node information.
Definition Devtab.hxx:549
This class provides a mutex API.
Definition OS.hxx:427
void lock()
Lock a mutex.
Definition OS.hxx:446
void unlock()
Unlock a mutex.
Definition OS.hxx:453
Private data for an SPI device.
Definition SPI.hxx:53
ssize_t read(File *file, void *buf, size_t count) override
Read from a file or device.
Definition SPI.cxx:93
uint8_t mode
one of four SPI modes
Definition SPI.hxx:196
ssize_t write(File *file, const void *buf, size_t count) override
Write to a file or device.
Definition SPI.cxx:129
SPI()
Default constructor.
virtual int transfer(struct spi_ioc_transfer *msg)=0
Method to transmit/receive the data.
ChipSelectMethod csDeassert
function pointer to a method that deasserts chip select.
Definition SPI.hxx:187
int transfer_messages(struct spi_ioc_transfer *msgs, int num)
Conduct multiple message transfers with one stop at the end.
Definition SPI.cxx:49
uint32_t speedHz
Max default speed in Hz.
Definition SPI.hxx:190
ChipSelectMethod csAssert
function pointer to a method that asserts chip select.
Definition SPI.hxx:184
virtual int update_configuration()=0
Update the configuration of the bus.
~SPI()
Destructor.
Definition SPI.hxx:135
void flush_buffers() override
Discards all pending buffers.
Definition SPI.hxx:219
virtual int transfer_polled(struct spi_ioc_transfer *msg)=0
Method to transmit/receive the data, but always in polled mode.
int transfer_with_cs_assert(struct spi_ioc_transfer *msg)
Method to transmit/receive the data.
Definition SPI.hxx:64
void bus_unlock()
Unlock the bus shared by many chip selects.
Definition SPI.hxx:150
void bus_lock()
Lock the bus shared by many chip selects.
Definition SPI.hxx:141
bool lsbFirst
transmit LSB first if true
Definition SPI.hxx:199
uint8_t bitsPerWord
number of bits per word transaction
Definition SPI.hxx:193
OSMutex * busLock
Mutual exclusion for the bus among many chip selects.
Definition SPI.hxx:222
int ioctl(File *file, unsigned long int key, unsigned long data) override
Request an ioctl transaction.
Definition SPI.cxx:165
void(* ChipSelectMethod)()
Function point for the chip select assert and deassert methods.
Definition SPI.hxx:56
int transfer_with_cs_assert_polled(struct spi_ioc_transfer *msgs, int num=1)
Method to transmit/receive the data.
Definition SPI.hxx:80
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
Definition macros.h:138
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Removes default copy-constructor and assignment added by C++.
Definition macros.h:171
File information.
Definition Devtab.hxx:52