mirror of
https://github.com/betaflight/betaflight.git
synced 2025-07-19 14:25:20 +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/rcc.h"
|
||||||
#include "drivers/system.h"
|
#include "drivers/system.h"
|
||||||
|
|
||||||
|
#include "pg/rx_spi.h"
|
||||||
|
|
||||||
#include "rx_spi.h"
|
#include "rx_spi.h"
|
||||||
|
|
||||||
#define DISABLE_RX() {IOHi(DEFIO_IO(RX_NSS_PIN));}
|
static busDevice_t rxSpiDevice;
|
||||||
#define ENABLE_RX() {IOLo(DEFIO_IO(RX_NSS_PIN));}
|
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 (!rxSpiConfig->spibus) {
|
||||||
|
return false;
|
||||||
if (hardwareInitialised) {
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const SPIDevice rxSPIDevice = spiDeviceByInstance(RX_SPI_INSTANCE);
|
spiBusSetInstance(busdev, spiInstanceByDevice(SPI_CFG_TO_DEV(rxSpiConfig->spibus)));
|
||||||
const IO_t rxCsPin = DEFIO_IO(RX_NSS_PIN);
|
|
||||||
IOInit(rxCsPin, OWNER_RX_SPI_CS, rxSPIDevice + 1);
|
const IO_t rxCsPin = IOGetByTag(rxSpiConfig->csnTag);
|
||||||
|
IOInit(rxCsPin, OWNER_RX_SPI_CS, 0);
|
||||||
IOConfigGPIO(rxCsPin, SPI_IO_CS_CFG);
|
IOConfigGPIO(rxCsPin, SPI_IO_CS_CFG);
|
||||||
|
busdev->busdev_u.spi.csnPin = rxCsPin;
|
||||||
|
|
||||||
DISABLE_RX();
|
DISABLE_RX();
|
||||||
|
|
||||||
#ifdef RX_SPI_INSTANCE
|
spiSetDivisor(busdev->busdev_u.spi.instance, SPI_CLOCK_STANDARD);
|
||||||
spiSetDivisor(RX_SPI_INSTANCE, SPI_CLOCK_STANDARD);
|
|
||||||
#endif
|
return true;
|
||||||
hardwareInitialised = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t rxSpiTransferByte(uint8_t data)
|
uint8_t rxSpiTransferByte(uint8_t data)
|
||||||
{
|
{
|
||||||
#ifdef RX_SPI_INSTANCE
|
return spiTransferByte(busdev->busdev_u.spi.instance, data);
|
||||||
return spiTransferByte(RX_SPI_INSTANCE, data);
|
|
||||||
#else
|
|
||||||
return 0;
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t rxSpiWriteByte(uint8_t data)
|
uint8_t rxSpiWriteByte(uint8_t data)
|
||||||
|
|
|
@ -24,7 +24,9 @@
|
||||||
|
|
||||||
#define RX_SPI_MAX_PAYLOAD_SIZE 32
|
#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 rxSpiTransferByte(uint8_t data);
|
||||||
uint8_t rxSpiWriteByte(uint8_t data);
|
uint8_t rxSpiWriteByte(uint8_t data);
|
||||||
uint8_t rxSpiWriteCommand(uint8_t command, 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);
|
adcConfigMutable()->current.enabled = (batteryConfig()->currentMeterSource == CURRENT_METER_ADC);
|
||||||
|
|
||||||
// The FrSky D SPI RX sends RSSI_ADC_PIN (if configured) as A2
|
// 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());
|
adcInit(adcConfig());
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -2399,10 +2399,10 @@ static void cliBeeper(char *cmdline)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef USE_RX_FRSKY_SPI
|
||||||
void cliFrSkyBind(char *cmdline){
|
void cliFrSkyBind(char *cmdline){
|
||||||
UNUSED(cmdline);
|
UNUSED(cmdline);
|
||||||
switch (rxSpiConfig()->rx_spi_protocol) {
|
switch (rxSpiConfig()->rx_spi_protocol) {
|
||||||
#ifdef USE_RX_FRSKY_SPI
|
|
||||||
case RX_SPI_FRSKY_D:
|
case RX_SPI_FRSKY_D:
|
||||||
case RX_SPI_FRSKY_X:
|
case RX_SPI_FRSKY_X:
|
||||||
frSkySpiBind();
|
frSkySpiBind();
|
||||||
|
@ -2410,13 +2410,13 @@ void cliFrSkyBind(char *cmdline){
|
||||||
cliPrint("Binding...");
|
cliPrint("Binding...");
|
||||||
|
|
||||||
break;
|
break;
|
||||||
#endif
|
|
||||||
default:
|
default:
|
||||||
cliPrint("Not supported.");
|
cliPrint("Not supported.");
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
static void printMap(uint8_t dumpMask, const rxConfig_t *rxConfig, const rxConfig_t *defaultRxConfig)
|
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_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 ),
|
DEFA( OWNER_SPI_PREINIT_OPU, PG_SPI_PREINIT_OPU_CONFIG, spiCs_t, csnTag, SPI_PREINIT_OPU_COUNT ),
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef USE_RX_SPI
|
||||||
|
DEFS( OWNER_RX_SPI_CS, PG_RX_SPI_CONFIG, rxSpiConfig_t, csnTag ),
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
#undef DEFS
|
#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) },
|
{ "serialrx_halfduplex", VAR_UINT8 | MASTER_VALUE | MODE_LOOKUP, .config.lookup = { TABLE_OFF_ON }, PG_RX_CONFIG, offsetof(rxConfig_t, halfDuplex) },
|
||||||
#ifdef USE_RX_SPI
|
#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_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
|
#endif
|
||||||
|
|
||||||
// PG_ADC_CONFIG
|
// PG_ADC_CONFIG
|
||||||
|
|
|
@ -22,15 +22,23 @@
|
||||||
|
|
||||||
#ifdef USE_RX_SPI
|
#ifdef USE_RX_SPI
|
||||||
|
|
||||||
|
#include "drivers/io.h"
|
||||||
|
#include "drivers/bus_spi.h"
|
||||||
|
|
||||||
#include "pg/pg.h"
|
#include "pg/pg.h"
|
||||||
#include "pg/pg_ids.h"
|
#include "pg/pg_ids.h"
|
||||||
#include "pg/rx_spi.h"
|
#include "pg/rx_spi.h"
|
||||||
|
|
||||||
#include "rx/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,
|
void pgResetFn_rxSpiConfig(rxSpiConfig_t *rxSpiConfig)
|
||||||
.rx_spi_protocol = RX_SPI_DEFAULT_PROTOCOL,
|
{
|
||||||
);
|
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
|
#endif
|
||||||
|
|
|
@ -25,10 +25,16 @@
|
||||||
#include "pg/pg.h"
|
#include "pg/pg.h"
|
||||||
|
|
||||||
typedef struct rxSpiConfig_s {
|
typedef struct rxSpiConfig_s {
|
||||||
|
// RX protocol
|
||||||
uint8_t rx_spi_protocol; // type of SPI RX protocol
|
uint8_t rx_spi_protocol; // type of SPI RX protocol
|
||||||
// nrf24: 0 = v202 250kbps. (Must be enabled by FEATURE_RX_NRF24 first.)
|
// nrf24: 0 = v202 250kbps. (Must be enabled by FEATURE_RX_NRF24 first.)
|
||||||
uint32_t rx_spi_id;
|
uint32_t rx_spi_id;
|
||||||
uint8_t rx_spi_rf_channel_count;
|
uint8_t rx_spi_rf_channel_count;
|
||||||
|
|
||||||
|
// SPI Bus
|
||||||
|
ioTag_t csnTag;
|
||||||
|
uint8_t spibus;
|
||||||
|
|
||||||
} rxSpiConfig_t;
|
} rxSpiConfig_t;
|
||||||
|
|
||||||
PG_DECLARE(rxSpiConfig_t, rxSpiConfig);
|
PG_DECLARE(rxSpiConfig_t, rxSpiConfig);
|
||||||
|
|
|
@ -38,7 +38,7 @@
|
||||||
|
|
||||||
#include "pg/pg.h"
|
#include "pg/pg.h"
|
||||||
#include "pg/pg_ids.h"
|
#include "pg/pg_ids.h"
|
||||||
#include "pg/rx.h"
|
#include "pg/rx_spi.h"
|
||||||
|
|
||||||
#include "rx/flysky_defs.h"
|
#include "rx/flysky_defs.h"
|
||||||
#include "rx/rx.h"
|
#include "rx/rx.h"
|
||||||
|
@ -356,9 +356,9 @@ static rx_spi_received_e flySkyReadAndProcess (uint8_t *payload, const uint32_t
|
||||||
return result;
|
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) {
|
if (protocol != flySkyConfig()->protocol) {
|
||||||
PG_RESET(flySkyConfig);
|
PG_RESET(flySkyConfig);
|
||||||
|
|
|
@ -31,8 +31,8 @@ typedef struct flySkyConfig_s {
|
||||||
|
|
||||||
PG_DECLARE(flySkyConfig_t, flySkyConfig);
|
PG_DECLARE(flySkyConfig_t, flySkyConfig);
|
||||||
|
|
||||||
struct rxConfig_s;
|
struct rxSpiConfig_s;
|
||||||
struct rxRuntimeConfig_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);
|
void flySkySetRcDataFromPayload(uint16_t *rcData, const uint8_t *payload);
|
||||||
rx_spi_received_e flySkyDataReceived(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;
|
bool ret = false;
|
||||||
|
|
||||||
rxSpiDeviceInit();
|
if (!rxSpiDeviceInit(rxSpiConfig)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (rxSpiSetProtocol(rxSpiConfig->rx_spi_protocol)) {
|
if (rxSpiSetProtocol(rxSpiConfig->rx_spi_protocol)) {
|
||||||
ret = protocolInit(rxSpiConfig, rxRuntimeConfig);
|
ret = protocolInit(rxSpiConfig, rxRuntimeConfig);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue