mirror of
https://github.com/Genaker/LoraSA.git
synced 2026-08-07 09:22:46 +02:00
Factor out bus configuration
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
#include "bus.h"
|
||||
#include <Wire.h>
|
||||
|
||||
bool initUARTs(Config &config)
|
||||
{
|
||||
if (config.uart0.enabled)
|
||||
{
|
||||
Uart0.end();
|
||||
Uart0.begin(config.uart0.clock_freq, SERIAL_8N1, config.uart0.rx,
|
||||
config.uart0.tx);
|
||||
}
|
||||
|
||||
if (config.uart1.enabled)
|
||||
{
|
||||
Uart1.end();
|
||||
Uart1.begin(config.uart1.clock_freq, SERIAL_8N1, config.uart1.rx,
|
||||
config.uart1.tx);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
SPIClass &hspi = *(new SPIClass(HSPI)); // not usable until initSPIs
|
||||
|
||||
bool initSPIs(Config &config)
|
||||
{
|
||||
if (config.spi1.enabled)
|
||||
{
|
||||
delete (&hspi);
|
||||
hspi = *(new SPIClass(config.spi1.bus_num));
|
||||
if (config.spi1.clock_freq > 0)
|
||||
{
|
||||
hspi.setFrequency(config.spi1.clock_freq);
|
||||
}
|
||||
|
||||
// if all the pins are -1, then will use the default for SPI bus_num
|
||||
hspi.begin(config.spi1.clk, config.spi1.miso, config.spi1.mosi);
|
||||
Serial.printf("Initialized SPI%d: SC:%d MISO:%d MOSI:%d clock:%d\n",
|
||||
(int)config.spi1.bus_num, (int)config.spi1.clk,
|
||||
(int)config.spi1.miso, (int)config.spi1.mosi,
|
||||
(int)config.spi1.clock_freq);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t _scanSupportedDevicesOnWire(TwoWire &w, int bus_num);
|
||||
|
||||
uint8_t wireDevices;
|
||||
uint8_t wire1Devices;
|
||||
|
||||
bool initWires(Config &config)
|
||||
{
|
||||
wireDevices = _scanSupportedDevicesOnWire(Wire, 0);
|
||||
|
||||
if (config.wire1.enabled)
|
||||
{
|
||||
#if SOC_I2C_NUM > 1
|
||||
// if you want to use default pins, configure -1
|
||||
// if you want to use default clock speed, configure 0
|
||||
if (!Wire1.begin(config.wire1.sda, config.wire1.scl, config.wire1.clock_freq))
|
||||
{
|
||||
Serial.println("Failed to initialize Wire1");
|
||||
return false;
|
||||
}
|
||||
|
||||
wire1Devices = _scanSupportedDevicesOnWire(Wire1, 1);
|
||||
#endif
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t _scanSupportedDevicesOnWire(TwoWire &w, int bus_num)
|
||||
{
|
||||
uint8_t res = 0;
|
||||
|
||||
for (int i = 0; known_i2c_devices[i].address > 0; i++)
|
||||
{
|
||||
w.beginTransmission(known_i2c_devices[i].address);
|
||||
delay(2);
|
||||
if (w.endTransmission() == 0)
|
||||
{
|
||||
Serial.printf("Found supported device on Wire%d: %s(%X)\n", bus_num,
|
||||
known_i2c_devices[i].name.c_str(),
|
||||
(int)known_i2c_devices[i].address);
|
||||
res |= 1 << i;
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
#include <config.h>
|
||||
|
||||
struct
|
||||
{
|
||||
String name;
|
||||
uint8_t address;
|
||||
} known_i2c_devices[] = {{"HMC5883L", 0x1e}, {"QMC5883L", 0x0d}, {" last record ", 0}};
|
||||
|
||||
enum I2CDevices
|
||||
{
|
||||
// powers of 2
|
||||
HMC5883L = 1,
|
||||
QMC5883L = 2
|
||||
};
|
||||
|
||||
extern uint8_t wireDevices;
|
||||
extern uint8_t wire1Devices;
|
||||
|
||||
extern SPIClass &hspi;
|
||||
|
||||
// abstract away a reference to Serial vs Serial0 vs Serial1, so it compiles
|
||||
#ifndef ARDUINO_USB_CDC_ON_BOOT
|
||||
#define Uart0 Serial
|
||||
#else
|
||||
#define Uart0 Serial0
|
||||
#endif
|
||||
|
||||
#if SOC_UART_NUM > 1
|
||||
#define Uart1 Serial1
|
||||
#else
|
||||
#define Uart1 Uart0
|
||||
#endif
|
||||
|
||||
bool initSPIs(Config &config);
|
||||
|
||||
bool initUARTs(Config &config);
|
||||
|
||||
bool initWires(Config &config);
|
||||
+4
-6
@@ -86,9 +86,8 @@ bool Comms::initComms(Config &c)
|
||||
if (c.listen_on_serial0.equalsIgnoreCase("readline"))
|
||||
{
|
||||
// comms using readline plaintext protocol
|
||||
Comms0 = new ReadlineComms("UART0", SERIAL0);
|
||||
SERIAL0.onReceive(_onReceive0, false);
|
||||
SERIAL0.begin(115200);
|
||||
Comms0 = new ReadlineComms("UART0", Uart0);
|
||||
Uart0.onReceive(_onReceive0, false);
|
||||
|
||||
Serial.println("Initialized communications on Serial0 using readline protocol");
|
||||
}
|
||||
@@ -102,9 +101,8 @@ bool Comms::initComms(Config &c)
|
||||
if (c.listen_on_serial1.equalsIgnoreCase("readline"))
|
||||
{
|
||||
// comms using readline plaintext protocol
|
||||
Comms1 = new ReadlineComms("UART1", Serial1);
|
||||
Serial1.onReceive(_onReceive1, false);
|
||||
Serial1.begin(115200);
|
||||
Comms1 = new ReadlineComms("UART1", Uart1);
|
||||
Uart1.onReceive(_onReceive1, false);
|
||||
|
||||
Serial.println("Initialized communications on Serial1 using readline protocol");
|
||||
}
|
||||
|
||||
+2
-7
@@ -5,14 +5,9 @@
|
||||
#include <LoRaBoards.h>
|
||||
|
||||
#include <LiLyGo.h>
|
||||
#include <bus.h>
|
||||
#include <config.h>
|
||||
|
||||
#ifndef ARDUINO_USB_CDC_ON_BOOT
|
||||
#define SERIAL0 Serial
|
||||
#else
|
||||
#define SERIAL0 Serial0
|
||||
#endif
|
||||
|
||||
#ifndef SCAN_MAX_RESULT_KHZ_SCALE
|
||||
// kHz scale: round frequency, so it fits into 2 bytes
|
||||
// 2500000 / 40 = 62500, scale 40 fits 2.5GHz into two bytes
|
||||
@@ -135,7 +130,7 @@ struct Comms
|
||||
|
||||
struct NoopComms : Comms
|
||||
{
|
||||
NoopComms() : Comms("no-op", SERIAL0) {};
|
||||
NoopComms() : Comms("no-op", Uart0) {};
|
||||
|
||||
virtual bool send(Message &) { return true; };
|
||||
virtual void _onReceive() {};
|
||||
|
||||
Reference in New Issue
Block a user