2022-11-05 07:36:27 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <cstdint>
|
2023-05-06 02:28:46 +08:00
|
|
|
#include <mutex>
|
|
|
|
#include <string>
|
2022-11-05 07:36:27 +08:00
|
|
|
#include <vector>
|
|
|
|
|
2023-01-30 05:55:17 +08:00
|
|
|
#ifndef __APPLE__
|
2022-11-11 10:37:10 +08:00
|
|
|
#include <linux/spi/spidev.h>
|
2023-01-30 05:55:17 +08:00
|
|
|
#endif
|
2022-11-11 10:37:10 +08:00
|
|
|
|
2022-11-05 07:36:27 +08:00
|
|
|
#include <libusb-1.0/libusb.h>
|
|
|
|
|
2022-11-11 10:37:10 +08:00
|
|
|
|
2022-11-05 07:36:27 +08:00
|
|
|
#define TIMEOUT 0
|
2022-11-09 05:21:07 +08:00
|
|
|
#define SPI_BUF_SIZE 1024
|
2022-11-05 07:36:27 +08:00
|
|
|
|
|
|
|
|
|
|
|
// comms base class
|
|
|
|
class PandaCommsHandle {
|
|
|
|
public:
|
|
|
|
PandaCommsHandle(std::string serial) {};
|
|
|
|
virtual ~PandaCommsHandle() {};
|
|
|
|
virtual void cleanup() = 0;
|
|
|
|
|
2023-01-22 11:42:10 +08:00
|
|
|
std::string hw_serial;
|
2022-11-05 07:36:27 +08:00
|
|
|
std::atomic<bool> connected = true;
|
|
|
|
std::atomic<bool> comms_healthy = true;
|
|
|
|
static std::vector<std::string> list();
|
|
|
|
|
|
|
|
// HW communication
|
|
|
|
virtual int control_write(uint8_t request, uint16_t param1, uint16_t param2, unsigned int timeout=TIMEOUT) = 0;
|
|
|
|
virtual int control_read(uint8_t request, uint16_t param1, uint16_t param2, unsigned char *data, uint16_t length, unsigned int timeout=TIMEOUT) = 0;
|
|
|
|
virtual int bulk_write(unsigned char endpoint, unsigned char* data, int length, unsigned int timeout=TIMEOUT) = 0;
|
|
|
|
virtual int bulk_read(unsigned char endpoint, unsigned char* data, int length, unsigned int timeout=TIMEOUT) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class PandaUsbHandle : public PandaCommsHandle {
|
|
|
|
public:
|
|
|
|
PandaUsbHandle(std::string serial);
|
|
|
|
~PandaUsbHandle();
|
|
|
|
int control_write(uint8_t request, uint16_t param1, uint16_t param2, unsigned int timeout=TIMEOUT);
|
|
|
|
int control_read(uint8_t request, uint16_t param1, uint16_t param2, unsigned char *data, uint16_t length, unsigned int timeout=TIMEOUT);
|
|
|
|
int bulk_write(unsigned char endpoint, unsigned char* data, int length, unsigned int timeout=TIMEOUT);
|
|
|
|
int bulk_read(unsigned char endpoint, unsigned char* data, int length, unsigned int timeout=TIMEOUT);
|
|
|
|
void cleanup();
|
|
|
|
|
|
|
|
static std::vector<std::string> list();
|
|
|
|
|
|
|
|
private:
|
|
|
|
libusb_context *ctx = NULL;
|
|
|
|
libusb_device_handle *dev_handle = NULL;
|
2023-01-21 05:03:02 +08:00
|
|
|
std::recursive_mutex hw_lock;
|
2022-11-05 07:36:27 +08:00
|
|
|
void handle_usb_issue(int err, const char func[]);
|
|
|
|
};
|
2022-11-09 05:21:07 +08:00
|
|
|
|
2023-01-30 05:55:17 +08:00
|
|
|
#ifndef __APPLE__
|
2022-11-09 05:21:07 +08:00
|
|
|
class PandaSpiHandle : public PandaCommsHandle {
|
|
|
|
public:
|
|
|
|
PandaSpiHandle(std::string serial);
|
|
|
|
~PandaSpiHandle();
|
|
|
|
int control_write(uint8_t request, uint16_t param1, uint16_t param2, unsigned int timeout=TIMEOUT);
|
|
|
|
int control_read(uint8_t request, uint16_t param1, uint16_t param2, unsigned char *data, uint16_t length, unsigned int timeout=TIMEOUT);
|
|
|
|
int bulk_write(unsigned char endpoint, unsigned char* data, int length, unsigned int timeout=TIMEOUT);
|
|
|
|
int bulk_read(unsigned char endpoint, unsigned char* data, int length, unsigned int timeout=TIMEOUT);
|
|
|
|
void cleanup();
|
|
|
|
|
|
|
|
static std::vector<std::string> list();
|
|
|
|
|
|
|
|
private:
|
|
|
|
int spi_fd = -1;
|
|
|
|
uint8_t tx_buf[SPI_BUF_SIZE];
|
|
|
|
uint8_t rx_buf[SPI_BUF_SIZE];
|
2023-01-21 05:03:02 +08:00
|
|
|
inline static std::recursive_mutex hw_lock;
|
2022-11-11 10:37:10 +08:00
|
|
|
|
|
|
|
int wait_for_ack(spi_ioc_transfer &transfer, uint8_t ack);
|
2023-05-20 14:24:52 +08:00
|
|
|
int bulk_transfer(uint8_t endpoint, uint8_t *tx_data, uint16_t tx_len, uint8_t *rx_data, uint16_t rx_len, unsigned int timeout);
|
2022-11-11 10:37:10 +08:00
|
|
|
int spi_transfer(uint8_t endpoint, uint8_t *tx_data, uint16_t tx_len, uint8_t *rx_data, uint16_t max_rx_len);
|
2023-05-20 14:24:52 +08:00
|
|
|
int spi_transfer_retry(uint8_t endpoint, uint8_t *tx_data, uint16_t tx_len, uint8_t *rx_data, uint16_t max_rx_len, unsigned int timeout);
|
2022-11-09 05:21:07 +08:00
|
|
|
};
|
2023-01-30 05:55:17 +08:00
|
|
|
#endif
|