A class to interface with the CH341 USB to SPI adapter.
More...
#include <CH341SPI.hpp>
|
static const uint8_t | PIN_D0 = 0x01 |
|
static const uint8_t | PIN_D1 = 0x02 |
|
static const uint8_t | PIN_D2 = 0x04 |
|
static const uint8_t | PIN_D3 = 0x08 |
|
static const uint8_t | PIN_D4 = 0x10 |
|
static const uint8_t | PIN_D5 = 0x20 |
|
static constexpr uint8_t | INPUT = 0 |
|
static constexpr uint8_t | OUTPUT = 1 |
|
static constexpr uint8_t | INPUT_PULLUP = 2 |
|
A class to interface with the CH341 USB to SPI adapter.
◆ CH341SPI()
CH341SPI::CH341SPI |
( |
int |
device_index = 0 , |
|
|
bool |
lsb_first = false |
|
) |
| |
Constructor for CH341SPI.
- Parameters
-
device_index | Index of the CH341 device to use (default is 0). |
lsb_first | Set to true if least significant bit should be sent first (default is false). |
◆ ~CH341SPI()
◆ close()
◆ configStream()
bool CH341SPI::configStream |
( |
| ) |
|
|
private |
Configures the SPI stream.
- Returns
- True if the configuration was successful, false otherwise.
◆ configureInterrupt()
bool CH341SPI::configureInterrupt |
( |
uint8_t |
pin, |
|
|
bool |
enable |
|
) |
| |
|
inlineoverridevirtual |
Configure interrupt settings for a pin.
- Parameters
-
pin | The pin number |
enable | True to enable the interrupt, false to disable it |
- Returns
- True if the operation was successful, false otherwise
Implements SPIInterface.
◆ digitalRead()
bool CH341SPI::digitalRead |
( |
uint8_t |
pin | ) |
|
|
virtual |
Reads a digital value from a specified pin.
- Parameters
-
- Returns
- The value read from the pin (true for high, false for low).
Implements SPIInterface.
◆ digitalWrite()
bool CH341SPI::digitalWrite |
( |
uint8_t |
pin, |
|
|
bool |
value |
|
) |
| |
|
virtual |
Writes a digital value to a specified pin.
- Parameters
-
pin | The pin number. |
value | The value to write (true for high, false for low). |
- Returns
- True if the operation was successful, false otherwise.
Implements SPIInterface.
◆ enableInterrupt()
bool CH341SPI::enableInterrupt |
( |
bool |
enable | ) |
|
|
overridevirtual |
Enable or disable interrupts.
- Parameters
-
enable | True to enable interrupts, false to disable them |
- Returns
- True if successful, false otherwise
Implements SPIInterface.
◆ enablePins()
bool CH341SPI::enablePins |
( |
bool |
enable | ) |
|
|
private |
Enables or disables the pins.
- Parameters
-
enable | Set to true to enable pins, false to disable. |
- Returns
- True if the operation was successful, false otherwise.
◆ interruptMonitoringThread()
void CH341SPI::interruptMonitoringThread |
( |
| ) |
|
|
private |
Thread function for monitoring interrupts.
◆ isActive()
bool CH341SPI::isActive |
( |
| ) |
const |
|
inlineoverridevirtual |
Check if the device is active/connected.
- Returns
- True if the device is active, false otherwise
Implements SPIInterface.
◆ open()
Opens the SPI connection.
- Returns
- True if the connection was successfully opened, false otherwise.
Implements SPIInterface.
◆ pinMode()
bool CH341SPI::pinMode |
( |
uint8_t |
pin, |
|
|
uint8_t |
mode |
|
) |
| |
|
virtual |
Sets the mode of a specified pin.
- Parameters
-
pin | The pin number. |
mode | The mode to set (e.g., input, output). |
- Returns
- True if the operation was successful, false otherwise.
Implements SPIInterface.
◆ setInterruptCallback()
Set interrupt callback.
- Parameters
-
callback | Function to call when interrupt occurs |
- Returns
- True if successful, false otherwise
Implements SPIInterface.
◆ swapBits()
uint8_t CH341SPI::swapBits |
( |
uint8_t |
byte | ) |
|
|
private |
Swaps the bits of a byte.
- Parameters
-
byte | The byte whose bits are to be swapped. |
- Returns
- The byte with swapped bits.
◆ transfer()
std::vector< uint8_t > CH341SPI::transfer |
( |
const std::vector< uint8_t > & |
write_data, |
|
|
size_t |
read_length = 0 |
|
) |
| |
|
virtual |
Transfers data over SPI.
- Parameters
-
write_data | Data to be written to the SPI bus. |
read_length | Number of bytes to read from the SPI bus (default is 0). |
- Returns
- A vector containing the data read from the SPI bus.
Implements SPIInterface.
◆ _gpio_direction
uint8_t CH341SPI::_gpio_direction |
|
private |
◆ _gpio_output
uint8_t CH341SPI::_gpio_output |
|
private |
Output state of GPIO pins.
◆ context
libusb_context* CH341SPI::context |
|
private |
◆ device
libusb_device_handle* CH341SPI::device |
|
private |
Handle to the libusb device.
◆ device_index
int CH341SPI::device_index |
|
private |
Index of the CH341 device.
◆ interruptCallback
Callback function for interrupts.
◆ interruptEnabled
bool CH341SPI::interruptEnabled |
|
private |
Flag to indicate if interrupts are enabled.
◆ interruptThread
std::thread CH341SPI::interruptThread |
|
private |
Thread for monitoring interrupts.
◆ is_open
Flag to indicate whether the device is open and active.
◆ lsb_first
Flag to indicate if LSB should be sent first.
◆ PIN_D0
const uint8_t CH341SPI::PIN_D0 = 0x01 |
|
static |
◆ PIN_D1
const uint8_t CH341SPI::PIN_D1 = 0x02 |
|
static |
◆ PIN_D2
const uint8_t CH341SPI::PIN_D2 = 0x04 |
|
static |
◆ PIN_D3
const uint8_t CH341SPI::PIN_D3 = 0x08 |
|
static |
◆ PIN_D4
const uint8_t CH341SPI::PIN_D4 = 0x10 |
|
static |
◆ PIN_D5
const uint8_t CH341SPI::PIN_D5 = 0x20 |
|
static |
◆ threadRunning
bool CH341SPI::threadRunning |
|
private |
Flag to indicate if the interrupt monitoring thread is running.
The documentation for this class was generated from the following files: