Skip to content

Commit

Permalink
#234 Added support for sharing the deck SPI bus.
Browse files Browse the repository at this point in the history
Added spiBeginTransaction and spiEndTransaction to lock the bus so it
can be shared. The deck drivers uSD, Loco and Flow were all updated and
can now be used simultainiously.
  • Loading branch information
tobbeanton committed Jun 28, 2017
1 parent dc08cfa commit ab6d531
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 46 deletions.
2 changes: 1 addition & 1 deletion src/config/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
#define EXTRX_TASK_STACKSIZE configMINIMAL_STACK_SIZE
#define UART_RX_TASK_STACKSIZE configMINIMAL_STACK_SIZE
#define VL53_TASK_STACKSIZE (2 * configMINIMAL_STACK_SIZE)
#define USDLOG_TASK_STACKSIZE configMINIMAL_STACK_SIZE
#define USDLOG_TASK_STACKSIZE (2 * configMINIMAL_STACK_SIZE)
#define USDWRITE_TASK_STACKSIZE (2 * configMINIMAL_STACK_SIZE)

//The radio channel. From 0 to 125
Expand Down
41 changes: 18 additions & 23 deletions src/deck/api/deck_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ static bool isInit = false;

static SemaphoreHandle_t txComplete;
static SemaphoreHandle_t rxComplete;
static SemaphoreHandle_t spiMutex;

static void spiDMAInit();
static void spiConfigureWithSpeed(uint16_t baudRatePrescaler);

void spiBegin(void)
{
Expand All @@ -95,6 +97,7 @@ void spiBegin(void)
// such that the the semaphore must first be 'given' before it can be 'taken'
txComplete = xSemaphoreCreateBinary();
rxComplete = xSemaphoreCreateBinary();
spiMutex = xSemaphoreCreateMutex();

/*!< Enable the SPI clock */
SPI_CLK_INIT(SPI_CLK, ENABLE);
Expand Down Expand Up @@ -134,12 +137,12 @@ void spiBegin(void)
spiDMAInit();

/*!< SPI configuration */
spiConfigureSlow();
spiConfigureWithSpeed(SPI_BAUDRATE_2MHZ);

isInit = true;
}

void spiDMAInit()
static void spiDMAInit()
{
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
Expand Down Expand Up @@ -183,7 +186,7 @@ void spiDMAInit()
NVIC_Init(&NVIC_InitStructure);
}

void spiConfigureSlow()
static void spiConfigureWithSpeed(uint16_t baudRatePrescaler)
{
SPI_InitTypeDef SPI_InitStructure;

Expand All @@ -198,26 +201,7 @@ void spiConfigureSlow()
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 0; // Not used

SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; //~1.35 MHz
SPI_Init(SPI, &SPI_InitStructure);
}

void spiConfigureFast()
{
SPI_InitTypeDef SPI_InitStructure;

SPI_I2S_DeInit(SPI);

SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 0; // Not used

SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; //~21 MHz
SPI_InitStructure.SPI_BaudRatePrescaler = baudRatePrescaler;
SPI_Init(SPI, &SPI_InitStructure);
}

Expand Down Expand Up @@ -263,6 +247,17 @@ bool spiExchange(size_t length, const uint8_t * data_tx, uint8_t * data_rx)
return result;
}

void spiBeginTransaction(uint16_t baudRatePrescaler)
{
xSemaphoreTake(spiMutex, portMAX_DELAY);
spiConfigureWithSpeed(baudRatePrescaler);
}

void spiEndTransaction()
{
xSemaphoreGive(spiMutex);
}

void __attribute__((used)) SPI_TX_DMA_IRQHandler(void)
{
portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
Expand Down
9 changes: 7 additions & 2 deletions src/deck/drivers/src/flowdeck.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ static void registerWrite(uint8_t reg, uint8_t value)
// Set MSB to 1 for write
reg |= 0x80u;

spiBeginTransaction(SPI_BAUDRATE_2MHZ);
digitalWrite(NCS_PIN, LOW);

sleepus(50);
Expand All @@ -116,6 +117,7 @@ static void registerWrite(uint8_t reg, uint8_t value)
sleepus(50);

digitalWrite(NCS_PIN, HIGH);
spiEndTransaction();
sleepus(200);
}

Expand All @@ -127,6 +129,7 @@ static uint8_t registerRead(uint8_t reg)
// Set MSB to 0 for read
reg &= ~0x80u;

spiBeginTransaction(SPI_BAUDRATE_2MHZ);
digitalWrite(NCS_PIN, LOW);

sleepus(50);
Expand All @@ -138,6 +141,7 @@ static uint8_t registerRead(uint8_t reg)
sleepus(50);

digitalWrite(NCS_PIN, HIGH);
spiEndTransaction();
sleepus(200);

return data;
Expand All @@ -146,13 +150,16 @@ static uint8_t registerRead(uint8_t reg)
static void readMotion(motionBurst_t * motion)
{
uint8_t address = 0x16;

spiBeginTransaction(SPI_BAUDRATE_2MHZ);
digitalWrite(NCS_PIN,LOW);
sleepus(50);
spiExchange(1, &address, &address);
sleepus(50);
spiExchange(sizeof(motionBurst_t), (uint8_t*)motion, (uint8_t*)motion);
sleepus(50);
digitalWrite(NCS_PIN, HIGH);
spiEndTransaction();
sleepus(50);

uint16_t realShutter = (motion->shutter >> 8) & 0x0FF;
Expand Down Expand Up @@ -312,8 +319,6 @@ static void pamotionInit()
digitalWrite(NCS_PIN, HIGH);

spiBegin();
spiConfigureSlow();

vTaskDelay(M2T(40));

digitalWrite(NCS_PIN, HIGH);
Expand Down
21 changes: 7 additions & 14 deletions src/deck/drivers/src/locodeck.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ static uwbAlgorithm_t *algorithm = &uwbTwrTagAlgorithm;
#endif

static bool isInit = false;
static xSemaphoreHandle spiSemaphore;
static SemaphoreHandle_t irqSemaphore;
static dwDevice_t dwm_device;
static dwDevice_t *dwm = &dwm_device;
Expand Down Expand Up @@ -191,35 +190,32 @@ bool lpsGetLppShort(lpsLppShortPacket_t* shortPacket)

static uint8_t spiTxBuffer[196];
static uint8_t spiRxBuffer[196];
static uint16_t spiSpeed = SPI_BAUDRATE_2MHZ;

/************ Low level ops for libdw **********/
static void spiWrite(dwDevice_t* dev, const void *header, size_t headerLength,
const void* data, size_t dataLength)
{
xSemaphoreTake(spiSemaphore, portMAX_DELAY);

spiBeginTransaction(spiSpeed);
digitalWrite(CS_PIN, LOW);
memcpy(spiTxBuffer, header, headerLength);
memcpy(spiTxBuffer+headerLength, data, dataLength);
spiExchange(headerLength+dataLength, spiTxBuffer, spiRxBuffer);
digitalWrite(CS_PIN, HIGH);

xSemaphoreGive(spiSemaphore);
spiEndTransaction();
}

static void spiRead(dwDevice_t* dev, const void *header, size_t headerLength,
void* data, size_t dataLength)
{
xSemaphoreTake(spiSemaphore, portMAX_DELAY);

spiBeginTransaction(spiSpeed);
digitalWrite(CS_PIN, LOW);
memcpy(spiTxBuffer, header, headerLength);
memset(spiTxBuffer+headerLength, 0, dataLength);
spiExchange(headerLength+dataLength, spiTxBuffer, spiRxBuffer);
memcpy(data, spiRxBuffer+headerLength, dataLength);
digitalWrite(CS_PIN, HIGH);

xSemaphoreGive(spiSemaphore);
spiEndTransaction();
}

void __attribute__((used)) EXTI11_Callback(void)
Expand All @@ -240,11 +236,11 @@ static void spiSetSpeed(dwDevice_t* dev, dwSpiSpeed_t speed)
{
if (speed == dwSpiSpeedLow)
{
spiConfigureSlow();
spiSpeed = SPI_BAUDRATE_2MHZ;
}
else if (speed == dwSpiSpeedHigh)
{
spiConfigureFast();
spiSpeed = SPI_BAUDRATE_21MHZ;
}
}

Expand Down Expand Up @@ -301,9 +297,6 @@ static void dwm1000Init(DeckInfo *info)
GPIO_WriteBit(GPIOC, GPIO_Pin_10, 1);
vTaskDelay(M2T(10));

// Semaphore that protect the SPI communication
spiSemaphore = xSemaphoreCreateMutex();

// Initialize the driver
dwInit(dwm, &dwOps); // Init libdw

Expand Down
17 changes: 14 additions & 3 deletions src/deck/drivers/src/usddeck.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ static crc crcTable[256];
static usdLogConfig_t usdLogConfig;

static BYTE exchangeBuff[512];
static uint16_t spiSpeed;

#ifdef USD_RUN_DISKIO_FUNCTION_TESTS
DWORD workBuff[512]; /* 2048 byte working buffer */
#endif
Expand Down Expand Up @@ -125,20 +127,22 @@ static DISKIO_LowLevelDriver_t fatDrv =
static void initSpi(void)
{
spiBegin(); /* Enable SPI function */
spiSpeed = SPI_BAUDRATE_2MHZ;

pinMode(USD_CS_PIN, OUTPUT);
csHigh();
digitalWrite(USD_CS_PIN, 1);

// FIXME: DELAY of 10ms?
}

static void setSlowSpiMode(void)
{
spiConfigureSlow();
spiSpeed = SPI_BAUDRATE_2MHZ;
}

static void setFastSpiMode(void)
{
spiConfigureFast();
spiSpeed = SPI_BAUDRATE_21MHZ;
}

/* Exchange a byte */
Expand Down Expand Up @@ -166,10 +170,17 @@ static void xmitSpiMulti(const BYTE *buff, UINT btx)
static void csHigh(void)
{
digitalWrite(USD_CS_PIN, 1);

// Dummy clock (force DO hi-z for multiple slave SPI)
// Moved here from fatfs_sd.c to handle bus release
xchgSpi(0xFF);

spiEndTransaction();
}

static void csLow(void)
{
spiBeginTransaction(spiSpeed);
digitalWrite(USD_CS_PIN, 0);
}

Expand Down
11 changes: 9 additions & 2 deletions src/deck/interface/deck_spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,19 @@
#include <stdbool.h>
#include <string.h>

// Based on 84MHz peripheral clock
#define SPI_BAUDRATE_21MHZ SPI_BaudRatePrescaler_4 // 21MHz
#define SPI_BAUDRATE_12MHZ SPI_BaudRatePrescaler_8 // 11.5MHz
#define SPI_BAUDRATE_6MHZ SPI_BaudRatePrescaler_16 // 5.25MHz
#define SPI_BAUDRATE_3MHZ SPI_BaudRatePrescaler_32 // 2.625MHz
#define SPI_BAUDRATE_2MHZ SPI_BaudRatePrescaler_64 // 1.3125MHz

/**
* Initialize the SPI.
*/
void spiBegin(void);
void spiConfigureSlow(void);
void spiConfigureFast(void);
void spiBeginTransaction(uint16_t baudRatePrescaler);
void spiEndTransaction();

/* Send the data_tx buffer and receive into the data_rx buffer */
bool spiExchange(size_t length, const uint8_t *data_tx, uint8_t *data_rx);
Expand Down
3 changes: 2 additions & 1 deletion src/drivers/src/fatfs_sd.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ static void deselect(sdSpiContext_t *context) {
context->csHigh();

// Dummy clock (force DO hi-z for multiple slave SPI)
context->xchgSpi(0xFF);
// which has been moved to higher layer.
//context->xchgSpi(0xFF);
}


Expand Down

0 comments on commit ab6d531

Please sign in to comment.