mirror of
https://github.com/iNavFlight/inav.git
synced 2025-07-26 09:45:33 +03:00
SDCard & I2C bugfixes (#513)
* Fix usage of uninitialised I2C bus * Fix SDCard not working issue * Fix DMA compilation issues on F4
This commit is contained in:
parent
de24cd0842
commit
b91d37e9c8
6 changed files with 78 additions and 9 deletions
|
@ -680,6 +680,8 @@ static void writeIntraframe(void)
|
||||||
blackboxHistory[2] = blackboxHistory[0];
|
blackboxHistory[2] = blackboxHistory[0];
|
||||||
//And advance the current state over to a blank space ready to be filled
|
//And advance the current state over to a blank space ready to be filled
|
||||||
blackboxHistory[0] = ((blackboxHistory[0] - blackboxHistoryRing + 1) % 3) + blackboxHistoryRing;
|
blackboxHistory[0] = ((blackboxHistory[0] - blackboxHistoryRing + 1) % 3) + blackboxHistoryRing;
|
||||||
|
|
||||||
|
blackboxLoggedAnyFrames = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void blackboxWriteMainStateArrayUsingAveragePredictor(int arrOffsetInHistory, int count)
|
static void blackboxWriteMainStateArrayUsingAveragePredictor(int arrOffsetInHistory, int count)
|
||||||
|
@ -831,6 +833,8 @@ static void writeInterframe(void)
|
||||||
blackboxHistory[2] = blackboxHistory[1];
|
blackboxHistory[2] = blackboxHistory[1];
|
||||||
blackboxHistory[1] = blackboxHistory[0];
|
blackboxHistory[1] = blackboxHistory[0];
|
||||||
blackboxHistory[0] = ((blackboxHistory[0] - blackboxHistoryRing + 1) % 3) + blackboxHistoryRing;
|
blackboxHistory[0] = ((blackboxHistory[0] - blackboxHistoryRing + 1) % 3) + blackboxHistoryRing;
|
||||||
|
|
||||||
|
blackboxLoggedAnyFrames = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Write the contents of the global "slowHistory" to the log as an "S" frame. Because this data is logged so
|
/* Write the contents of the global "slowHistory" to the log as an "S" frame. Because this data is logged so
|
||||||
|
|
|
@ -65,6 +65,11 @@ static i2cDevice_t i2cHardwareMap[] = {
|
||||||
{ .dev = I2C2, .scl = IO_TAG(I2C2_SCL), .sda = IO_TAG(I2C2_SDA), .rcc = RCC_APB1(I2C2), .overClock = I2C2_OVERCLOCK }
|
{ .dev = I2C2, .scl = IO_TAG(I2C2_SCL), .sda = IO_TAG(I2C2_SDA), .rcc = RCC_APB1(I2C2), .overClock = I2C2_OVERCLOCK }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static bool deviceInitialised[] = {
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
};
|
||||||
|
|
||||||
static bool i2cOverClock;
|
static bool i2cOverClock;
|
||||||
|
|
||||||
void i2cSetOverclock(uint8_t overClock)
|
void i2cSetOverclock(uint8_t overClock)
|
||||||
|
@ -90,7 +95,7 @@ void i2cInit(I2CDevice device)
|
||||||
|
|
||||||
I2C_TypeDef *I2Cx;
|
I2C_TypeDef *I2Cx;
|
||||||
I2Cx = i2c->dev;
|
I2Cx = i2c->dev;
|
||||||
|
|
||||||
IO_t scl = IOGetByTag(i2c->scl);
|
IO_t scl = IOGetByTag(i2c->scl);
|
||||||
IO_t sda = IOGetByTag(i2c->sda);
|
IO_t sda = IOGetByTag(i2c->sda);
|
||||||
|
|
||||||
|
@ -116,8 +121,10 @@ void i2cInit(I2CDevice device)
|
||||||
I2C_Init(I2Cx, &i2cInit);
|
I2C_Init(I2Cx, &i2cInit);
|
||||||
|
|
||||||
I2C_StretchClockCmd(I2Cx, ENABLE);
|
I2C_StretchClockCmd(I2Cx, ENABLE);
|
||||||
|
|
||||||
I2C_Cmd(I2Cx, ENABLE);
|
I2C_Cmd(I2Cx, ENABLE);
|
||||||
|
|
||||||
|
deviceInitialised[device] = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t i2cGetErrorCounter(void)
|
uint16_t i2cGetErrorCounter(void)
|
||||||
|
@ -127,6 +134,9 @@ uint16_t i2cGetErrorCounter(void)
|
||||||
|
|
||||||
bool i2cWrite(I2CDevice device, uint8_t addr_, uint8_t reg, uint8_t data)
|
bool i2cWrite(I2CDevice device, uint8_t addr_, uint8_t reg, uint8_t data)
|
||||||
{
|
{
|
||||||
|
if (!deviceInitialised[device])
|
||||||
|
return false;
|
||||||
|
|
||||||
addr_ <<= 1;
|
addr_ <<= 1;
|
||||||
|
|
||||||
I2C_TypeDef *I2Cx;
|
I2C_TypeDef *I2Cx;
|
||||||
|
@ -193,6 +203,9 @@ bool i2cWrite(I2CDevice device, uint8_t addr_, uint8_t reg, uint8_t data)
|
||||||
|
|
||||||
bool i2cRead(I2CDevice device, uint8_t addr_, uint8_t reg, uint8_t len, uint8_t* buf)
|
bool i2cRead(I2CDevice device, uint8_t addr_, uint8_t reg, uint8_t len, uint8_t* buf)
|
||||||
{
|
{
|
||||||
|
if (!deviceInitialised[device])
|
||||||
|
return false;
|
||||||
|
|
||||||
addr_ <<= 1;
|
addr_ <<= 1;
|
||||||
|
|
||||||
I2C_TypeDef *I2Cx;
|
I2C_TypeDef *I2Cx;
|
||||||
|
|
|
@ -123,9 +123,16 @@ void spiInitDevice(SPIDevice device)
|
||||||
IOInit(IOGetByTag(spi->mosi), OWNER_SPI, RESOURCE_SPI_MOSI, device + 1);
|
IOInit(IOGetByTag(spi->mosi), OWNER_SPI, RESOURCE_SPI_MOSI, device + 1);
|
||||||
|
|
||||||
#if defined(STM32F3) || defined(STM32F4)
|
#if defined(STM32F3) || defined(STM32F4)
|
||||||
IOConfigGPIOAF(IOGetByTag(spi->sck), SPI_IO_AF_CFG, spi->af);
|
if (spi->sdcard || spi->nrf24l01) {
|
||||||
IOConfigGPIOAF(IOGetByTag(spi->miso), SPI_IO_AF_CFG, spi->af);
|
IOConfigGPIOAF(IOGetByTag(spi->sck), SPI_IO_AF_SCK_CFG, spi->af);
|
||||||
IOConfigGPIOAF(IOGetByTag(spi->mosi), SPI_IO_AF_CFG, spi->af);
|
IOConfigGPIOAF(IOGetByTag(spi->miso), SPI_IO_AF_MISO_CFG, spi->af);
|
||||||
|
IOConfigGPIOAF(IOGetByTag(spi->mosi), SPI_IO_AF_CFG, spi->af);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
IOConfigGPIOAF(IOGetByTag(spi->sck), SPI_IO_AF_CFG, spi->af);
|
||||||
|
IOConfigGPIOAF(IOGetByTag(spi->miso), SPI_IO_AF_CFG, spi->af);
|
||||||
|
IOConfigGPIOAF(IOGetByTag(spi->mosi), SPI_IO_AF_CFG, spi->af);
|
||||||
|
}
|
||||||
|
|
||||||
if (spi->nss) {
|
if (spi->nss) {
|
||||||
IOConfigGPIOAF(IOGetByTag(spi->nss), SPI_IO_CS_CFG, spi->af);
|
IOConfigGPIOAF(IOGetByTag(spi->nss), SPI_IO_CS_CFG, spi->af);
|
||||||
|
|
|
@ -69,11 +69,47 @@ void dmaInit(void)
|
||||||
// TODO: Do we need this?
|
// TODO: Do we need this?
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef STM32F4
|
||||||
|
dmaHandlerIdentifier_e dmaFindHandlerIdentifier(DMA_Stream_TypeDef* stream)
|
||||||
|
{
|
||||||
|
dmaHandlerIdentifier_e i;
|
||||||
|
|
||||||
|
for (i = 0; i < (sizeof(dmaDescriptors) / sizeof(dmaDescriptors[0])); i++) {
|
||||||
|
if (stream == dmaDescriptors[i].stream) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Shouldn't get here
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
dmaHandlerIdentifier_e dmaFindHandlerIdentifier(DMA_Channel_TypeDef* channel)
|
||||||
|
{
|
||||||
|
dmaHandlerIdentifier_e i;
|
||||||
|
|
||||||
|
for (i = 0; i < (sizeof(dmaDescriptors) / sizeof(dmaDescriptors[0])); i++) {
|
||||||
|
if (channel == dmaDescriptors[i].channel) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Shouldn't get here
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void dmaEnableClock(dmaHandlerIdentifier_e identifier)
|
||||||
|
{
|
||||||
|
RCC_AHBPeriphClockCmd(dmaDescriptors[identifier].rcc, ENABLE);
|
||||||
|
}
|
||||||
|
|
||||||
void dmaSetHandler(dmaHandlerIdentifier_e identifier, dmaCallbackHandlerFuncPtr callback, uint32_t priority, uint32_t userParam)
|
void dmaSetHandler(dmaHandlerIdentifier_e identifier, dmaCallbackHandlerFuncPtr callback, uint32_t priority, uint32_t userParam)
|
||||||
{
|
{
|
||||||
NVIC_InitTypeDef NVIC_InitStructure;
|
NVIC_InitTypeDef NVIC_InitStructure;
|
||||||
|
|
||||||
RCC_AHBPeriphClockCmd(dmaDescriptors[identifier].rcc, ENABLE);
|
dmaEnableClock(identifier);
|
||||||
|
|
||||||
dmaDescriptors[identifier].irqHandlerCallback = callback;
|
dmaDescriptors[identifier].irqHandlerCallback = callback;
|
||||||
dmaDescriptors[identifier].userParam = userParam;
|
dmaDescriptors[identifier].userParam = userParam;
|
||||||
|
|
||||||
|
|
|
@ -108,6 +108,13 @@ typedef struct dmaChannelDescriptor_s {
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef STM32F4
|
||||||
|
dmaHandlerIdentifier_e dmaFindHandlerIdentifier(DMA_Stream_TypeDef* stream);
|
||||||
|
#else
|
||||||
|
dmaHandlerIdentifier_e dmaFindHandlerIdentifier(DMA_Channel_TypeDef* channel);
|
||||||
|
#endif
|
||||||
|
|
||||||
void dmaInit(void);
|
void dmaInit(void);
|
||||||
|
void dmaEnableClock(dmaHandlerIdentifier_e identifier);
|
||||||
void dmaSetHandler(dmaHandlerIdentifier_e identifier, dmaCallbackHandlerFuncPtr callback, uint32_t priority, uint32_t userParam);
|
void dmaSetHandler(dmaHandlerIdentifier_e identifier, dmaCallbackHandlerFuncPtr callback, uint32_t priority, uint32_t userParam);
|
||||||
|
|
||||||
|
|
|
@ -24,6 +24,7 @@
|
||||||
|
|
||||||
#include "nvic.h"
|
#include "nvic.h"
|
||||||
#include "io.h"
|
#include "io.h"
|
||||||
|
#include "dma.h"
|
||||||
|
|
||||||
#include "bus_spi.h"
|
#include "bus_spi.h"
|
||||||
#include "system.h"
|
#include "system.h"
|
||||||
|
@ -31,6 +32,8 @@
|
||||||
#include "sdcard.h"
|
#include "sdcard.h"
|
||||||
#include "sdcard_standard.h"
|
#include "sdcard_standard.h"
|
||||||
|
|
||||||
|
#include "build/debug.h"
|
||||||
|
|
||||||
#ifdef AFATFS_USE_INTROSPECTIVE_LOGGING
|
#ifdef AFATFS_USE_INTROSPECTIVE_LOGGING
|
||||||
#define SDCARD_PROFILING
|
#define SDCARD_PROFILING
|
||||||
#endif
|
#endif
|
||||||
|
@ -414,9 +417,6 @@ static void sdcard_sendDataBlockBegin(uint8_t *buffer, bool multiBlockWrite)
|
||||||
if (useDMAForTx) {
|
if (useDMAForTx) {
|
||||||
#ifdef SDCARD_DMA_CHANNEL_TX
|
#ifdef SDCARD_DMA_CHANNEL_TX
|
||||||
// Queue the transmission of the sector payload
|
// Queue the transmission of the sector payload
|
||||||
#ifdef SDCARD_DMA_CLK
|
|
||||||
RCC_AHB1PeriphClockCmd(SDCARD_DMA_CLK, ENABLE);
|
|
||||||
#endif
|
|
||||||
DMA_InitTypeDef DMA_InitStructure;
|
DMA_InitTypeDef DMA_InitStructure;
|
||||||
|
|
||||||
DMA_StructInit(&DMA_InitStructure);
|
DMA_StructInit(&DMA_InitStructure);
|
||||||
|
@ -544,6 +544,8 @@ void sdcard_init(bool useDMA)
|
||||||
{
|
{
|
||||||
#ifdef SDCARD_DMA_CHANNEL_TX
|
#ifdef SDCARD_DMA_CHANNEL_TX
|
||||||
useDMAForTx = useDMA;
|
useDMAForTx = useDMA;
|
||||||
|
dmaEnableClock(dmaFindHandlerIdentifier(SDCARD_DMA_CHANNEL_TX));
|
||||||
|
DMA_Cmd(SDCARD_DMA_CHANNEL_TX, DISABLE);
|
||||||
#else
|
#else
|
||||||
// DMA is not available
|
// DMA is not available
|
||||||
(void) useDMA;
|
(void) useDMA;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue