Skip to content

Commit

Permalink
#37 SPI for DCP ADC, DAC and IOEXP
Browse files Browse the repository at this point in the history
  • Loading branch information
mvladic committed Jul 1, 2020
1 parent 614785d commit 8da786f
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/eez/drivers/tc77.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ float readTemperature(uint8_t slotIndex) {
uint8_t result[2];

spi::select(slotIndex, spi::CHIP_TEMP_SENSOR);
spi::transfer(slotIndex, output, result, 2);
spi::transfer2(slotIndex, output, result);
spi::deselect(slotIndex);

uint16_t adc = (((uint16_t)result[0]) << 8) | result[1];
Expand Down
2 changes: 1 addition & 1 deletion src/eez/modules/bp3c/comm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ TransferResult transferDMA(int slotIndex, uint8_t *output, uint8_t *input, uint3
#if defined(EEZ_PLATFORM_STM32)
spi::handle[slotIndex]->ErrorCode = 0;

spi::select(slotIndex, spi::CHIP_SLAVE_MCU, false);
spi::select(slotIndex, spi::CHIP_SLAVE_MCU);
auto result = spi::transferDMA(slotIndex, output, input, bufferSize);
return (TransferResult)result;
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/eez/modules/bp3c/flash_slave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ bool waitForAck(int slotIndex) {
spi::deselect(slotIndex);

spi::select(slotIndex, spi::CHIP_SLAVE_MCU_BOOTLOADER);
spi::transfer(slotIndex, &txData, &rxData, 1);
spi::transfer1(slotIndex, &txData, &rxData);
spi::deselect(slotIndex);

if (rxData == ACK) {
Expand Down
12 changes: 6 additions & 6 deletions src/eez/modules/dib-dcp405/adc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,15 +213,15 @@ void AnalogDigitalConverter::init() {

// Send RESET command
data[0] = ADC_RESET;
spi::transfer(slotIndex, data, result, 1);
spi::transfer1(slotIndex, data, result);

delay(1);

data[0] = ADC_WR3S1;
data[1] = getReg1Val();
data[2] = ADC_REG2_VAL;
data[3] = ADC_REG3_VAL;
spi::transfer(slotIndex, data, result, 4);
spi::transfer4(slotIndex, data, result);

spi::deselect(slotIndex);
#endif
Expand All @@ -242,7 +242,7 @@ bool AnalogDigitalConverter::test() {
data[3] = 0;

spi::select(slotIndex, spi::CHIP_ADC);
spi::transfer(slotIndex, data, result, 4);
spi::transfer4(slotIndex, data, result);
spi::deselect(slotIndex);

uint8_t reg1 = result[1];
Expand Down Expand Up @@ -293,7 +293,7 @@ void AnalogDigitalConverter::start(AdcDataType adcDataType_) {
data[2] = ADC_START;

spi::select(slotIndex, spi::CHIP_ADC);
spi::transfer(slotIndex, data, result, 3);
spi::transfer3(slotIndex, data, result);
spi::deselect(slotIndex);
#endif
}
Expand All @@ -308,7 +308,7 @@ float AnalogDigitalConverter::read() {
data[2] = 0;

spi::select(slotIndex, spi::CHIP_ADC);
spi::transfer(slotIndex, data, result, 3);
spi::transfer3(slotIndex, data, result);
spi::deselect(slotIndex);

uint16_t dmsb = result[1];
Expand Down Expand Up @@ -378,7 +378,7 @@ void AnalogDigitalConverter::readAllRegisters(uint8_t registers[]) {
data[4] = 0;

spi::select(slotIndex, spi::CHIP_ADC);
spi::transfer(slotIndex, data, result, 5);
spi::transfer5(slotIndex, data, result);
spi::deselect(slotIndex);

registers[0] = result[1];
Expand Down
2 changes: 1 addition & 1 deletion src/eez/modules/dib-dcp405/dac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ void DigitalAnalogConverter::set(uint8_t buffer, uint16_t value, RampOption ramp
data[2] = value & 0xFF;

spi::select(slotIndex, spi::CHIP_DAC);
spi::transfer(slotIndex, data, result, 3);
spi::transfer3(slotIndex, data, result);
spi::deselect(slotIndex);
}

Expand Down
4 changes: 2 additions & 2 deletions src/eez/modules/dib-dcp405/ioexp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ uint8_t IOExpander::read(uint8_t reg) {
uint8_t result[3];

spi::select(slotIndex, spi::CHIP_IOEXP);
spi::transfer(slotIndex, data, result, 3);
spi::transfer3(slotIndex, data, result);
spi::deselect(slotIndex);

return result[2];
Expand All @@ -400,7 +400,7 @@ void IOExpander::write(uint8_t reg, uint8_t val) {

spi::select(slotIndex, spi::CHIP_IOEXP);

spi::transfer(slotIndex, data, result, 3);
spi::transfer3(slotIndex, data, result);

if (reg == REG_GPIOA) {
gpioWritten = (gpioWritten & 0xFF00) | val;
Expand Down
186 changes: 158 additions & 28 deletions src/eez/platform/stm32/spi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ static int g_chip[] = { -1, -1, -1 };
GPIO_TypeDef *IRQ_GPIO_Port[] = { SPI2_IRQ_GPIO_Port, SPI4_IRQ_GPIO_Port, SPI5_IRQ_GPIO_Port };
const uint16_t IRQ_Pin[] = { SPI2_IRQ_Pin, SPI4_IRQ_Pin, SPI5_IRQ_Pin };

void select(uint8_t slotIndex, int chip, bool critical) {
if (critical) {
taskENTER_CRITICAL();
}

void select(uint8_t slotIndex, int chip) {
auto &slot = *g_slots[slotIndex];

if (g_chip[slotIndex] != chip) {
Expand Down Expand Up @@ -75,55 +71,189 @@ void select(uint8_t slotIndex, int chip, bool critical) {
if (slot.moduleInfo->moduleType == MODULE_TYPE_DCP405) {
if (chip == CHIP_DAC) {
// 00
HAL_GPIO_WritePin(SPI_CSA_GPIO_Port[slotIndex], SPI_CSA_Pin[slotIndex], GPIO_PIN_RESET);
HAL_GPIO_WritePin(SPI_CSB_GPIO_Port[slotIndex], SPI_CSB_Pin[slotIndex], GPIO_PIN_RESET);
SPI_CSA_GPIO_Port[slotIndex]->BSRR = (uint32_t)SPI_CSA_Pin[slotIndex] << 16U; // RESET CSB
SPI_CSB_GPIO_Port[slotIndex]->BSRR = (uint32_t)SPI_CSB_Pin[slotIndex] << 16U; // RESET CSB
} else if (chip == CHIP_ADC) {
// 01
HAL_GPIO_WritePin(SPI_CSA_GPIO_Port[slotIndex], SPI_CSA_Pin[slotIndex], GPIO_PIN_SET);
HAL_GPIO_WritePin(SPI_CSB_GPIO_Port[slotIndex], SPI_CSB_Pin[slotIndex], GPIO_PIN_RESET);
SPI_CSA_GPIO_Port[slotIndex]->BSRR = SPI_CSA_Pin[slotIndex]; // SET CSA
SPI_CSB_GPIO_Port[slotIndex]->BSRR = (uint32_t)SPI_CSB_Pin[slotIndex] << 16U; // RESET CSB
} else if (chip == CHIP_IOEXP) {
// 10
HAL_GPIO_WritePin(SPI_CSA_GPIO_Port[slotIndex], SPI_CSA_Pin[slotIndex], GPIO_PIN_RESET);
HAL_GPIO_WritePin(SPI_CSB_GPIO_Port[slotIndex], SPI_CSB_Pin[slotIndex], GPIO_PIN_SET);
SPI_CSA_GPIO_Port[slotIndex]->BSRR = (uint32_t)SPI_CSA_Pin[slotIndex] << 16U; // RESET CSB
SPI_CSB_GPIO_Port[slotIndex]->BSRR = SPI_CSB_Pin[slotIndex]; // SET CSA
} else {
// TEMP_SENSOR
// 11
HAL_GPIO_WritePin(SPI_CSA_GPIO_Port[slotIndex], SPI_CSA_Pin[slotIndex], GPIO_PIN_SET);
HAL_GPIO_WritePin(SPI_CSB_GPIO_Port[slotIndex], SPI_CSB_Pin[slotIndex], GPIO_PIN_SET);
SPI_CSA_GPIO_Port[slotIndex]->BSRR = SPI_CSA_Pin[slotIndex]; // SET CSA
SPI_CSB_GPIO_Port[slotIndex]->BSRR = SPI_CSB_Pin[slotIndex]; // SET CSA
}
} else {
HAL_GPIO_WritePin(SPI_CSA_GPIO_Port[slotIndex], SPI_CSA_Pin[slotIndex], GPIO_PIN_RESET);
SPI_CSA_GPIO_Port[slotIndex]->BSRR = (uint32_t)SPI_CSA_Pin[slotIndex] << 16U; // RESET CSB
}
}

void deselect(uint8_t slotIndex, bool critical) {
void deselect(uint8_t slotIndex) {
auto &slot = *g_slots[slotIndex];

if (slot.moduleInfo->moduleType == MODULE_TYPE_DCP405) {
// 01 ADC
HAL_GPIO_WritePin(SPI_CSA_GPIO_Port[slotIndex], SPI_CSA_Pin[slotIndex], GPIO_PIN_SET);
HAL_GPIO_WritePin(SPI_CSB_GPIO_Port[slotIndex], SPI_CSB_Pin[slotIndex], GPIO_PIN_RESET);
SPI_CSA_GPIO_Port[slotIndex]->BSRR = SPI_CSA_Pin[slotIndex]; // SET CSA
SPI_CSB_GPIO_Port[slotIndex]->BSRR = (uint32_t)SPI_CSB_Pin[slotIndex] << 16U; // RESET CSB
} else {
HAL_GPIO_WritePin(SPI_CSA_GPIO_Port[slotIndex], SPI_CSA_Pin[slotIndex], GPIO_PIN_SET);
SPI_CSA_GPIO_Port[slotIndex]->BSRR = SPI_CSA_Pin[slotIndex]; // SET CSA
}
}

if (critical) {
taskEXIT_CRITICAL();
}
HAL_StatusTypeDef transfer1(uint8_t slotIndex, uint8_t *input, uint8_t *output) {
auto hspi = handle[slotIndex];
auto spi = hspi->Instance;

/* Check if the SPI is already enabled */
if ((spi->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE) {
/* Enable SPI peripheral */
__HAL_SPI_ENABLE(hspi);
}

SET_BIT(spi->CR2, SPI_RXFIFO_THRESHOLD);

__IO uint8_t *pDR = (__IO uint8_t *)&spi->DR;

while (!(spi->SR & SPI_SR_TXE));
*pDR = *input;
while (!(spi->SR & SPI_SR_RXNE));
*output = *pDR;

return HAL_OK;
}

HAL_StatusTypeDef transfer2(uint8_t slotIndex, uint8_t *input, uint8_t *output) {
auto hspi = handle[slotIndex];
auto spi = hspi->Instance;

/* Check if the SPI is already enabled */
if ((spi->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE) {
/* Enable SPI peripheral */
__HAL_SPI_ENABLE(hspi);
}

CLEAR_BIT(spi->CR2, SPI_RXFIFO_THRESHOLD);

__IO uint16_t *pDR = (__IO uint16_t *)&spi->DR;

while (!(spi->SR & SPI_SR_TXE));
*pDR = *(uint16_t *)input;

while (!(spi->SR & SPI_SR_RXNE));
*(uint16_t *)output = *pDR;

return HAL_OK;
}

HAL_StatusTypeDef transfer3(uint8_t slotIndex, uint8_t *input, uint8_t *output) {
auto hspi = handle[slotIndex];
auto spi = hspi->Instance;

/* Check if the SPI is already enabled */
if ((spi->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE) {
/* Enable SPI peripheral */
__HAL_SPI_ENABLE(hspi);
}

CLEAR_BIT(spi->CR2, SPI_RXFIFO_THRESHOLD);

__IO uint16_t *pDR = (__IO uint16_t *)&spi->DR;

while (!(spi->SR & SPI_SR_TXE));
*pDR = *(uint16_t *)input;

while (!(spi->SR & SPI_SR_RXNE));
*(uint16_t *)output = *pDR;

SET_BIT(spi->CR2, SPI_RXFIFO_THRESHOLD);

while (!(spi->SR & SPI_SR_TXE));
*(__IO uint8_t *)pDR = input[2];

while (!(spi->SR & SPI_SR_RXNE));
output[2] = *(__IO uint8_t *)pDR;

return HAL_OK;
}

HAL_StatusTypeDef transfer4(uint8_t slotIndex, uint8_t *input, uint8_t *output) {
auto hspi = handle[slotIndex];
auto spi = hspi->Instance;

/* Check if the SPI is already enabled */
if ((spi->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE) {
/* Enable SPI peripheral */
__HAL_SPI_ENABLE(hspi);
}

CLEAR_BIT(spi->CR2, SPI_RXFIFO_THRESHOLD);

__IO uint16_t *pDR = (__IO uint16_t *)&spi->DR;

while (!(spi->SR & SPI_SR_TXE));
*pDR = ((uint16_t *)input)[0];

while (!(spi->SR & SPI_SR_RXNE));
((uint16_t *)output)[0] = *pDR;

while (!(spi->SR & SPI_SR_TXE));
*pDR = ((uint16_t *)input)[1];

while (!(spi->SR & SPI_SR_RXNE));
((uint16_t *)output)[1] = *pDR;

return HAL_OK;
}

HAL_StatusTypeDef transfer5(uint8_t slotIndex, uint8_t *input, uint8_t *output) {
auto hspi = handle[slotIndex];
auto spi = hspi->Instance;

/* Check if the SPI is already enabled */
if ((spi->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE) {
/* Enable SPI peripheral */
__HAL_SPI_ENABLE(hspi);
}

CLEAR_BIT(spi->CR2, SPI_RXFIFO_THRESHOLD);

__IO uint16_t *pDR = (__IO uint16_t *)&spi->DR;

while (!(spi->SR & SPI_SR_TXE));
*pDR = ((uint16_t *)input)[0];

while (!(spi->SR & SPI_SR_RXNE));
((uint16_t *)output)[0] = *pDR;

while (!(spi->SR & SPI_SR_TXE));
*pDR = ((uint16_t *)input)[1];

while (!(spi->SR & SPI_SR_RXNE));
((uint16_t *)output)[1] = *pDR;

SET_BIT(spi->CR2, SPI_RXFIFO_THRESHOLD);

while (!(spi->SR & SPI_SR_TXE));
*(__IO uint8_t *)pDR = input[4];

while (!(spi->SR & SPI_SR_RXNE));
output[4] = *(__IO uint8_t *)pDR;

return HAL_OK;
}

HAL_StatusTypeDef transfer(uint8_t slotIndex, uint8_t *input, uint8_t *output, uint16_t size) {
return HAL_SPI_TransmitReceive(handle[slotIndex], input, output, size, 100);
return HAL_SPI_TransmitReceive(handle[slotIndex], input, output, size, 100);
}

HAL_StatusTypeDef transmit(uint8_t slotIndex, uint8_t *input, uint16_t size) {
return HAL_SPI_Transmit(handle[slotIndex], input, size, 100);
}

HAL_StatusTypeDef receive(uint8_t slotIndex, uint8_t *output, uint16_t size) {
return HAL_SPI_Receive(handle[slotIndex], output, size, 100);
}

HAL_StatusTypeDef transferDMA(uint8_t slotIndex, uint8_t *input, uint8_t *output, uint16_t size) {
return HAL_SPI_TransmitReceive_DMA(handle[slotIndex], input, output, size);
}
Expand All @@ -145,7 +275,7 @@ void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi) {
slotIndex = 2;
}

deselect(slotIndex, false);
deselect(slotIndex);

auto &slot = *g_slots[slotIndex];

Expand All @@ -166,7 +296,7 @@ void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi) {
slotIndex = 2;
}

deselect(slotIndex, false);
deselect(slotIndex);

auto &slot = *g_slots[slotIndex];

Expand Down
12 changes: 9 additions & 3 deletions src/eez/platform/stm32/spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,18 @@ static const int CHIP_SLAVE_MCU_BOOTLOADER = 5;

void init(uint8_t slotIndex, int chip);

void select(uint8_t slotIndex, int chip, bool critical = true);
void deselect(uint8_t slotIndex, bool critical = true);
void select(uint8_t slotIndex, int chip);
void deselect(uint8_t slotIndex);

HAL_StatusTypeDef transfer1(uint8_t slotIndex, uint8_t *input, uint8_t *output);
HAL_StatusTypeDef transfer2(uint8_t slotIndex, uint8_t *input, uint8_t *output);
HAL_StatusTypeDef transfer3(uint8_t slotIndex, uint8_t *input, uint8_t *output);
HAL_StatusTypeDef transfer4(uint8_t slotIndex, uint8_t *input, uint8_t *output);
HAL_StatusTypeDef transfer5(uint8_t slotIndex, uint8_t *input, uint8_t *output);

HAL_StatusTypeDef transfer(uint8_t slotIndex, uint8_t *input, uint8_t *output, uint16_t size);

HAL_StatusTypeDef transmit(uint8_t slotIndex, uint8_t *input, uint16_t size);
HAL_StatusTypeDef receive(uint8_t slotIndex, uint8_t *output, uint16_t size);

HAL_StatusTypeDef transferDMA(uint8_t slotIndex, uint8_t *input, uint8_t *output, uint16_t size);

Expand Down

0 comments on commit 8da786f

Please sign in to comment.