| 1 | /* |
| 2 | * |
| 3 | */ |
| 4 | |
| 5 | #ifndef RF24_UTILITY_PIGPIO_SPI_H_ |
| 6 | #define RF24_UTILITY_PIGPIO_SPI_H_ |
| 7 | |
| 8 | #include <cstdint> |
| 9 | #include <stdexcept> |
| 10 | |
| 11 | /** Specific exception for SPI errors */ |
| 12 | class SPIException : public std::runtime_error |
| 13 | { |
| 14 | public: |
| 15 | explicit SPIException(const std::string& msg) |
| 16 | : std::runtime_error(msg) |
| 17 | { |
| 18 | } |
| 19 | }; |
| 20 | |
| 21 | class SPI |
| 22 | { |
| 23 | |
| 24 | public: |
| 25 | /** |
| 26 | * SPI constructor |
| 27 | */ |
| 28 | SPI(); |
| 29 | |
| 30 | /** |
| 31 | * Start SPI |
| 32 | */ |
| 33 | void begin(int busNo, uint32_t spi_speed); |
| 34 | |
| 35 | /** |
| 36 | * Transfer a single byte |
| 37 | * @param tx Byte to send |
| 38 | * @return Data returned via spi |
| 39 | */ |
| 40 | uint8_t transfer(char tx); |
| 41 | |
| 42 | /** |
| 43 | * Transfer a buffer of data |
| 44 | * @param txBuf Transmit buffer |
| 45 | * @param rxBuf Receive buffer |
| 46 | * @param len Length of the data |
| 47 | */ |
| 48 | void transfernb(char* txBuf, char* rxBuf, uint32_t len); |
| 49 | |
| 50 | /** |
| 51 | * Transfer a buffer of data without an rx buffer |
| 52 | * @param buf Pointer to a buffer of data |
| 53 | * @param len Length of the data |
| 54 | */ |
| 55 | void transfern(char* buf, uint32_t len) |
| 56 | { |
| 57 | transfernb(buf, buf, len); |
| 58 | } |
| 59 | |
| 60 | ~SPI(); |
| 61 | |
| 62 | private: |
| 63 | unsigned spiHandle; |
| 64 | bool spiIsInitialized = false; |
| 65 | void init(uint32_t spi_speed); |
| 66 | }; |
| 67 | |
| 68 | #endif /* RF24_UTILITY_PIGPIO_SPI_H_ */ |