mirror of
https://github.com/betaflight/betaflight.git
synced 2025-07-16 04:45:24 +03:00
Add SPI bus configurability
This commit is contained in:
parent
1a035aa0f3
commit
560af0b6ff
10 changed files with 59 additions and 33 deletions
|
@ -37,39 +37,39 @@
|
|||
#include "drivers/rcc.h"
|
||||
#include "drivers/system.h"
|
||||
|
||||
#include "pg/rx_spi.h"
|
||||
|
||||
#include "rx_spi.h"
|
||||
|
||||
#define DISABLE_RX() {IOHi(DEFIO_IO(RX_NSS_PIN));}
|
||||
#define ENABLE_RX() {IOLo(DEFIO_IO(RX_NSS_PIN));}
|
||||
static busDevice_t rxSpiDevice;
|
||||
static busDevice_t *busdev = &rxSpiDevice;
|
||||
|
||||
void rxSpiDeviceInit(void)
|
||||
#define DISABLE_RX() {IOHi(busdev->busdev_u.spi.csnPin);}
|
||||
#define ENABLE_RX() {IOLo(busdev->busdev_u.spi.csnPin);}
|
||||
|
||||
bool rxSpiDeviceInit(const rxSpiConfig_t *rxSpiConfig)
|
||||
{
|
||||
static bool hardwareInitialised = false;
|
||||
|
||||
if (hardwareInitialised) {
|
||||
return;
|
||||
if (!rxSpiConfig->spibus) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const SPIDevice rxSPIDevice = spiDeviceByInstance(RX_SPI_INSTANCE);
|
||||
const IO_t rxCsPin = DEFIO_IO(RX_NSS_PIN);
|
||||
IOInit(rxCsPin, OWNER_RX_SPI_CS, rxSPIDevice + 1);
|
||||
spiBusSetInstance(busdev, spiInstanceByDevice(SPI_CFG_TO_DEV(rxSpiConfig->spibus)));
|
||||
|
||||
const IO_t rxCsPin = IOGetByTag(rxSpiConfig->csnTag);
|
||||
IOInit(rxCsPin, OWNER_RX_SPI_CS, 0);
|
||||
IOConfigGPIO(rxCsPin, SPI_IO_CS_CFG);
|
||||
busdev->busdev_u.spi.csnPin = rxCsPin;
|
||||
|
||||
DISABLE_RX();
|
||||
|
||||
#ifdef RX_SPI_INSTANCE
|
||||
spiSetDivisor(RX_SPI_INSTANCE, SPI_CLOCK_STANDARD);
|
||||
#endif
|
||||
hardwareInitialised = true;
|
||||
spiSetDivisor(busdev->busdev_u.spi.instance, SPI_CLOCK_STANDARD);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t rxSpiTransferByte(uint8_t data)
|
||||
{
|
||||
#ifdef RX_SPI_INSTANCE
|
||||
return spiTransferByte(RX_SPI_INSTANCE, data);
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
return spiTransferByte(busdev->busdev_u.spi.instance, data);
|
||||
}
|
||||
|
||||
uint8_t rxSpiWriteByte(uint8_t data)
|
||||
|
|
|
@ -24,7 +24,9 @@
|
|||
|
||||
#define RX_SPI_MAX_PAYLOAD_SIZE 32
|
||||
|
||||
void rxSpiDeviceInit(void);
|
||||
struct rxSpiConfig_s;
|
||||
|
||||
bool rxSpiDeviceInit(const struct rxSpiConfig_s *rxSpiConfig);
|
||||
uint8_t rxSpiTransferByte(uint8_t data);
|
||||
uint8_t rxSpiWriteByte(uint8_t data);
|
||||
uint8_t rxSpiWriteCommand(uint8_t command, uint8_t data);
|
||||
|
|
|
@ -466,7 +466,10 @@ void init(void)
|
|||
adcConfigMutable()->current.enabled = (batteryConfig()->currentMeterSource == CURRENT_METER_ADC);
|
||||
|
||||
// The FrSky D SPI RX sends RSSI_ADC_PIN (if configured) as A2
|
||||
adcConfigMutable()->rssi.enabled = feature(FEATURE_RSSI_ADC) || (feature(FEATURE_RX_SPI) && rxSpiConfig()->rx_spi_protocol == RX_SPI_FRSKY_D);
|
||||
adcConfigMutable()->rssi.enabled = feature(FEATURE_RSSI_ADC);
|
||||
#ifdef USE_RX_SPI
|
||||
adcConfigMutable()->rssi.enabled |= (feature(FEATURE_RX_SPI) && rxSpiConfig()->rx_spi_protocol == RX_SPI_FRSKY_D);
|
||||
#endif
|
||||
adcInit(adcConfig());
|
||||
#endif
|
||||
|
||||
|
|
|
@ -2399,10 +2399,10 @@ static void cliBeeper(char *cmdline)
|
|||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_RX_FRSKY_SPI
|
||||
void cliFrSkyBind(char *cmdline){
|
||||
UNUSED(cmdline);
|
||||
switch (rxSpiConfig()->rx_spi_protocol) {
|
||||
#ifdef USE_RX_FRSKY_SPI
|
||||
case RX_SPI_FRSKY_D:
|
||||
case RX_SPI_FRSKY_X:
|
||||
frSkySpiBind();
|
||||
|
@ -2410,13 +2410,13 @@ void cliFrSkyBind(char *cmdline){
|
|||
cliPrint("Binding...");
|
||||
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
cliPrint("Not supported.");
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static void printMap(uint8_t dumpMask, const rxConfig_t *rxConfig, const rxConfig_t *defaultRxConfig)
|
||||
{
|
||||
|
@ -3586,6 +3586,9 @@ const cliResourceValue_t resourceTable[] = {
|
|||
DEFA( OWNER_SPI_PREINIT_IPU, PG_SPI_PREINIT_IPU_CONFIG, spiCs_t, csnTag, SPI_PREINIT_IPU_COUNT ),
|
||||
DEFA( OWNER_SPI_PREINIT_OPU, PG_SPI_PREINIT_OPU_CONFIG, spiCs_t, csnTag, SPI_PREINIT_OPU_COUNT ),
|
||||
#endif
|
||||
#ifdef USE_RX_SPI
|
||||
DEFS( OWNER_RX_SPI_CS, PG_RX_SPI_CONFIG, rxSpiConfig_t, csnTag ),
|
||||
#endif
|
||||
};
|
||||
|
||||
#undef DEFS
|
||||
|
|
|
@ -533,6 +533,7 @@ const clivalue_t valueTable[] = {
|
|||
{ "serialrx_halfduplex", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, .config.lookup = { TABLE_OFF_ON }, PG_RX_CONFIG, offsetof(rxConfig_t, halfDuplex) },
|
||||
#ifdef USE_RX_SPI
|
||||
{ "rx_spi_protocol", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, .config.lookup = { TABLE_RX_SPI }, PG_RX_SPI_CONFIG, offsetof(rxSpiConfig_t, rx_spi_protocol) },
|
||||
{ "rx_spi_bus", VAR_UINT8 | MASTER_VALUE, .config.minmax = { 0, SPIDEV_COUNT }, PG_RX_SPI_CONFIG, offsetof(rxSpiConfig_t, spibus) },
|
||||
#endif
|
||||
|
||||
// PG_ADC_CONFIG
|
||||
|
|
|
@ -22,15 +22,23 @@
|
|||
|
||||
#ifdef USE_RX_SPI
|
||||
|
||||
#include "drivers/io.h"
|
||||
#include "drivers/bus_spi.h"
|
||||
|
||||
#include "pg/pg.h"
|
||||
#include "pg/pg_ids.h"
|
||||
#include "pg/rx_spi.h"
|
||||
|
||||
#include "rx/rx_spi.h"
|
||||
|
||||
PG_REGISTER_WITH_RESET_TEMPLATE(rxSpiConfig_t, rxSpiConfig, PG_RX_SPI_CONFIG, 0);
|
||||
PG_REGISTER_WITH_RESET_FN(rxSpiConfig_t, rxSpiConfig, PG_RX_SPI_CONFIG, 0);
|
||||
|
||||
PG_RESET_TEMPLATE(rxSpiConfig_t, rxSpiConfig,
|
||||
.rx_spi_protocol = RX_SPI_DEFAULT_PROTOCOL,
|
||||
);
|
||||
void pgResetFn_rxSpiConfig(rxSpiConfig_t *rxSpiConfig)
|
||||
{
|
||||
rxSpiConfig->rx_spi_protocol = RX_SPI_DEFAULT_PROTOCOL;
|
||||
|
||||
// Basic SPI
|
||||
rxSpiConfig->csnTag = IO_TAG(RX_NSS_PIN);
|
||||
rxSpiConfig->spibus = SPI_DEV_TO_CFG(spiDeviceByInstance(RX_SPI_INSTANCE));
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -25,10 +25,16 @@
|
|||
#include "pg/pg.h"
|
||||
|
||||
typedef struct rxSpiConfig_s {
|
||||
// RX protocol
|
||||
uint8_t rx_spi_protocol; // type of SPI RX protocol
|
||||
// nrf24: 0 = v202 250kbps. (Must be enabled by FEATURE_RX_NRF24 first.)
|
||||
uint32_t rx_spi_id;
|
||||
uint8_t rx_spi_rf_channel_count;
|
||||
|
||||
// SPI Bus
|
||||
ioTag_t csnTag;
|
||||
uint8_t spibus;
|
||||
|
||||
} rxSpiConfig_t;
|
||||
|
||||
PG_DECLARE(rxSpiConfig_t, rxSpiConfig);
|
||||
|
|
|
@ -38,7 +38,7 @@
|
|||
|
||||
#include "pg/pg.h"
|
||||
#include "pg/pg_ids.h"
|
||||
#include "pg/rx.h"
|
||||
#include "pg/rx_spi.h"
|
||||
|
||||
#include "rx/flysky_defs.h"
|
||||
#include "rx/rx.h"
|
||||
|
@ -356,9 +356,9 @@ static rx_spi_received_e flySkyReadAndProcess (uint8_t *payload, const uint32_t
|
|||
return result;
|
||||
}
|
||||
|
||||
bool flySkyInit (const struct rxConfig_s *rxConfig, struct rxRuntimeConfig_s *rxRuntimeConfig)
|
||||
bool flySkyInit (const rxSpiConfig_t *rxSpiConfig, struct rxRuntimeConfig_s *rxRuntimeConfig)
|
||||
{
|
||||
protocol = rxConfig->rx_spi_protocol;
|
||||
protocol = rxSpiConfig->rx_spi_protocol;
|
||||
|
||||
if (protocol != flySkyConfig()->protocol) {
|
||||
PG_RESET(flySkyConfig);
|
||||
|
|
|
@ -31,8 +31,8 @@ typedef struct flySkyConfig_s {
|
|||
|
||||
PG_DECLARE(flySkyConfig_t, flySkyConfig);
|
||||
|
||||
struct rxConfig_s;
|
||||
struct rxSpiConfig_s;
|
||||
struct rxRuntimeConfig_s;
|
||||
bool flySkyInit(const struct rxConfig_s *rxConfig, struct rxRuntimeConfig_s *rxRuntimeConfig);
|
||||
bool flySkyInit(const struct rxSpiConfig_s *rxConfig, struct rxRuntimeConfig_s *rxRuntimeConfig);
|
||||
void flySkySetRcDataFromPayload(uint16_t *rcData, const uint8_t *payload);
|
||||
rx_spi_received_e flySkyDataReceived(uint8_t *payload);
|
||||
|
|
|
@ -172,7 +172,10 @@ bool rxSpiInit(const rxSpiConfig_t *rxSpiConfig, rxRuntimeConfig_t *rxRuntimeCon
|
|||
{
|
||||
bool ret = false;
|
||||
|
||||
rxSpiDeviceInit();
|
||||
if (!rxSpiDeviceInit(rxSpiConfig)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (rxSpiSetProtocol(rxSpiConfig->rx_spi_protocol)) {
|
||||
ret = protocolInit(rxSpiConfig, rxRuntimeConfig);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue