mirror of
https://github.com/meshtastic/firmware.git
synced 2025-08-25 14:30:28 +00:00
Rework of CH341 HAL
This commit is contained in:
parent
d5cd6f87a0
commit
cc4bdc26c9
@ -194,38 +194,27 @@ void portduinoSetup()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// if we're using a usermode driver, we need to initialize it here, to get a serial number back for mac address
|
// if we're using a usermode driver, we need to initialize it here, to get a serial number back for mac address
|
||||||
uint8_t dmac[6] = {0};
|
|
||||||
if (settingsStrings[spidev] == "ch341") {
|
if (settingsStrings[spidev] == "ch341") {
|
||||||
ch341Hal = new Ch341Hal(0);
|
try {
|
||||||
if (settingsStrings[lora_usb_serial_num] != "") {
|
ch341Hal = new Ch341Hal(0, settingsStrings[lora_usb_serial_num], settingsMap[lora_usb_vid], settingsMap[lora_usb_pid]);
|
||||||
ch341Hal->serial = settingsStrings[lora_usb_serial_num];
|
} catch (std::exception &e) {
|
||||||
}
|
std::cerr << e.what() << std::endl;
|
||||||
ch341Hal->vid = settingsMap[lora_usb_vid];
|
std::cerr << "Could not initialize CH341 device!" << std::endl;
|
||||||
ch341Hal->pid = settingsMap[lora_usb_pid];
|
|
||||||
ch341Hal->init();
|
|
||||||
if (!ch341Hal->isInit()) {
|
|
||||||
std::cout << "Could not initialize CH341 device!" << std::endl;
|
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
char serial[9] = {0};
|
char serial[9] = {0};
|
||||||
ch341Hal->getSerialString(serial, 8);
|
ch341Hal->getSerialString(serial, 8);
|
||||||
std::cout << "Serial " << serial << std::endl;
|
std::cout << "Serial " << serial << std::endl;
|
||||||
if (strlen(serial) == 8 && settingsStrings[mac_address].length() < 12) {
|
if (strlen(serial) == 8 && settingsStrings[mac_address].length() < 12) {
|
||||||
uint8_t hash[32] = {0};
|
uint8_t *hash = (uint8_t*)serial;
|
||||||
memcpy(hash, serial, 8);
|
|
||||||
crypto->hash(hash, 8);
|
crypto->hash(hash, 8);
|
||||||
dmac[0] = (hash[0] << 4) | 2;
|
|
||||||
dmac[1] = hash[1];
|
|
||||||
dmac[2] = hash[2];
|
|
||||||
dmac[3] = hash[3];
|
|
||||||
dmac[4] = hash[4];
|
|
||||||
dmac[5] = hash[5];
|
|
||||||
char macBuf[13] = {0};
|
char macBuf[13] = {0};
|
||||||
sprintf(macBuf, "%02X%02X%02X%02X%02X%02X", dmac[0], dmac[1], dmac[2], dmac[3], dmac[4], dmac[5]);
|
sprintf(macBuf, "%02X%02X%02X%02X%02X%02X", ((hash[0] << 4) | 2), hash[1], hash[2], hash[3], hash[4], hash[5]);
|
||||||
settingsStrings[mac_address] = macBuf;
|
settingsStrings[mac_address] = macBuf;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint8_t dmac[6] = {0};
|
||||||
getMacAddr(dmac);
|
getMacAddr(dmac);
|
||||||
if (dmac[0] == 0 && dmac[1] == 0 && dmac[2] == 0 && dmac[3] == 0 && dmac[4] == 0 && dmac[5] == 0) {
|
if (dmac[0] == 0 && dmac[1] == 0 && dmac[2] == 0 && dmac[3] == 0 && dmac[4] == 0 && dmac[5] == 0) {
|
||||||
std::cout << "*** Blank MAC Address not allowed!" << std::endl;
|
std::cout << "*** Blank MAC Address not allowed!" << std::endl;
|
||||||
@ -568,4 +557,4 @@ bool MAC_from_string(std::string mac_str, uint8_t *dmac)
|
|||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,9 +4,10 @@
|
|||||||
// include RadioLib
|
// include RadioLib
|
||||||
#include "platform/portduino/PortduinoGlue.h"
|
#include "platform/portduino/PortduinoGlue.h"
|
||||||
#include <RadioLib.h>
|
#include <RadioLib.h>
|
||||||
#include <csignal>
|
|
||||||
#include <libpinedio-usb.h>
|
#include <libpinedio-usb.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <csignal>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
// include the library for Raspberry GPIO pins
|
// include the library for Raspberry GPIO pins
|
||||||
|
|
||||||
@ -26,30 +27,44 @@ class Ch341Hal : public RadioLibHal
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
// default constructor - initializes the base HAL and any needed private members
|
// default constructor - initializes the base HAL and any needed private members
|
||||||
explicit Ch341Hal(uint8_t spiChannel, uint32_t spiSpeed = 2000000, uint8_t spiDevice = 0, uint8_t gpioDevice = 0)
|
explicit Ch341Hal(uint8_t spiChannel, std::string serial = "", uint32_t vid = 0x1A86, uint32_t pid = 0x5512, uint32_t spiSpeed = 2000000, uint8_t spiDevice = 0, uint8_t gpioDevice = 0)
|
||||||
: RadioLibHal(PI_INPUT, PI_OUTPUT, PI_LOW, PI_HIGH, PI_RISING, PI_FALLING)
|
: RadioLibHal(PI_INPUT, PI_OUTPUT, PI_LOW, PI_HIGH, PI_RISING, PI_FALLING)
|
||||||
{
|
{
|
||||||
|
if (serial != "") {
|
||||||
|
strncpy(pinedio.serial_number, serial.c_str(), 8);
|
||||||
|
pinedio_set_option(&pinedio, PINEDIO_OPTION_SEARCH_SERIAL, 1);
|
||||||
|
}
|
||||||
|
// LOG_INFO("USB Serial: %s", pinedio.serial_number);
|
||||||
|
|
||||||
|
// There is no vendor with 0x0 -> so check
|
||||||
|
if (vid != 0x0) {
|
||||||
|
pinedio_set_option(&pinedio, PINEDIO_OPTION_VID, vid);
|
||||||
|
pinedio_set_option(&pinedio, PINEDIO_OPTION_PID, pid);
|
||||||
|
}
|
||||||
|
int32_t ret = pinedio_init(&pinedio, NULL);
|
||||||
|
if (ret != 0) {
|
||||||
|
std::string s = "Could not open SPI: ";
|
||||||
|
throw (s + std::to_string(ret));
|
||||||
|
}
|
||||||
|
|
||||||
|
pinedio_set_option(&pinedio, PINEDIO_OPTION_AUTO_CS, 0);
|
||||||
|
pinedio_set_pin_mode(&pinedio, 3, true);
|
||||||
|
pinedio_set_pin_mode(&pinedio, 5, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
~Ch341Hal()
|
||||||
|
{
|
||||||
|
pinedio_deinit(&pinedio);
|
||||||
}
|
}
|
||||||
|
|
||||||
void getSerialString(char *_serial, size_t len)
|
void getSerialString(char *_serial, size_t len)
|
||||||
{
|
{
|
||||||
if (!pinedio_is_init) {
|
len = len > 8 ? 8 : len;
|
||||||
return;
|
|
||||||
}
|
|
||||||
strncpy(_serial, pinedio.serial_number, len);
|
strncpy(_serial, pinedio.serial_number, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
void init() override
|
void init() override {}
|
||||||
{
|
void term() override {}
|
||||||
// now the SPI
|
|
||||||
spiBegin();
|
|
||||||
}
|
|
||||||
|
|
||||||
void term() override
|
|
||||||
{
|
|
||||||
// stop the SPI
|
|
||||||
spiEnd();
|
|
||||||
}
|
|
||||||
|
|
||||||
// GPIO-related methods (pinMode, digitalWrite etc.) should check
|
// GPIO-related methods (pinMode, digitalWrite etc.) should check
|
||||||
// RADIOLIB_NC as an alias for non-connected pins
|
// RADIOLIB_NC as an alias for non-connected pins
|
||||||
@ -79,7 +94,7 @@ class Ch341Hal : public RadioLibHal
|
|||||||
|
|
||||||
void attachInterrupt(uint32_t interruptNum, void (*interruptCb)(void), uint32_t mode) override
|
void attachInterrupt(uint32_t interruptNum, void (*interruptCb)(void), uint32_t mode) override
|
||||||
{
|
{
|
||||||
if ((interruptNum == RADIOLIB_NC)) {
|
if (interruptNum == RADIOLIB_NC) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// LOG_DEBUG("Attach interrupt to pin %d", interruptNum);
|
// LOG_DEBUG("Attach interrupt to pin %d", interruptNum);
|
||||||
@ -88,22 +103,16 @@ class Ch341Hal : public RadioLibHal
|
|||||||
|
|
||||||
void detachInterrupt(uint32_t interruptNum) override
|
void detachInterrupt(uint32_t interruptNum) override
|
||||||
{
|
{
|
||||||
if ((interruptNum == RADIOLIB_NC)) {
|
if (interruptNum == RADIOLIB_NC) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// LOG_DEBUG("Detach interrupt from pin %d", interruptNum);
|
// LOG_DEBUG("Detach interrupt from pin %d", interruptNum);
|
||||||
|
|
||||||
pinedio_deattach_interrupt(&this->pinedio, (pinedio_int_pin)interruptNum);
|
pinedio_deattach_interrupt(&this->pinedio, (pinedio_int_pin)interruptNum);
|
||||||
}
|
}
|
||||||
|
|
||||||
void delay(unsigned long ms) override
|
void delay(unsigned long ms) override
|
||||||
{
|
{
|
||||||
if (ms == 0) {
|
delayMicroseconds(ms * 1000);
|
||||||
sched_yield();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
usleep(ms * 1000);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void delayMicroseconds(unsigned long us) override
|
void delayMicroseconds(unsigned long us) override
|
||||||
@ -133,62 +142,26 @@ class Ch341Hal : public RadioLibHal
|
|||||||
|
|
||||||
long pulseIn(uint32_t pin, uint32_t state, unsigned long timeout) override
|
long pulseIn(uint32_t pin, uint32_t state, unsigned long timeout) override
|
||||||
{
|
{
|
||||||
fprintf(stderr, "pulseIn for pin %u is not supported!\n", pin);
|
std::cerr << "pulseIn for pin " << pin << "is not supported!" << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void spiBegin()
|
void spiBegin() {}
|
||||||
{
|
|
||||||
if (!pinedio_is_init) {
|
|
||||||
if (serial != "") {
|
|
||||||
strncpy(pinedio.serial_number, serial.c_str(), 8);
|
|
||||||
pinedio_set_option(&pinedio, PINEDIO_OPTION_SEARCH_SERIAL, 1);
|
|
||||||
}
|
|
||||||
pinedio_set_option(&pinedio, PINEDIO_OPTION_PID, pid);
|
|
||||||
pinedio_set_option(&pinedio, PINEDIO_OPTION_VID, vid);
|
|
||||||
int32_t ret = pinedio_init(&pinedio, NULL);
|
|
||||||
if (ret != 0) {
|
|
||||||
fprintf(stderr, "Could not open SPI: %d\n", ret);
|
|
||||||
} else {
|
|
||||||
pinedio_is_init = true;
|
|
||||||
// LOG_INFO("USB Serial: %s", pinedio.serial_number);
|
|
||||||
pinedio_set_option(&pinedio, PINEDIO_OPTION_AUTO_CS, 0);
|
|
||||||
pinedio_set_pin_mode(&pinedio, 3, true);
|
|
||||||
pinedio_set_pin_mode(&pinedio, 5, true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void spiBeginTransaction() {}
|
void spiBeginTransaction() {}
|
||||||
|
|
||||||
void spiTransfer(uint8_t *out, size_t len, uint8_t *in)
|
void spiTransfer(uint8_t *out, size_t len, uint8_t *in)
|
||||||
{
|
{
|
||||||
int32_t result = pinedio_transceive(&this->pinedio, out, in, len);
|
int32_t ret = pinedio_transceive(&this->pinedio, out, in, len);
|
||||||
if (result < 0) {
|
if (ret < 0) {
|
||||||
fprintf(stderr, "Could not perform SPI transfer: %d\n", result);
|
std::cerr << "Could not perform SPI transfer: " << ret << std::endl;;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void spiEndTransaction() {}
|
void spiEndTransaction() {}
|
||||||
|
void spiEnd() {}
|
||||||
void spiEnd()
|
|
||||||
{
|
|
||||||
if (pinedio_is_init) {
|
|
||||||
pinedio_deinit(&pinedio);
|
|
||||||
pinedio_is_init = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool isInit() { return pinedio_is_init; }
|
|
||||||
|
|
||||||
std::string serial = "";
|
|
||||||
uint32_t pid = 0x5512;
|
|
||||||
uint32_t vid = 0x1A86;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// the HAL can contain any additional private members
|
|
||||||
pinedio_inst pinedio = {0};
|
pinedio_inst pinedio = {0};
|
||||||
bool pinedio_is_init = false;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user