Skip to content

Commit

Permalink
DMA transfer with MIO168
Browse files Browse the repository at this point in the history
  • Loading branch information
mvladic committed Jun 20, 2020
1 parent 1ee70cd commit ce419db
Show file tree
Hide file tree
Showing 16 changed files with 911 additions and 503 deletions.
5 changes: 5 additions & 0 deletions src/eez/index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ void Module::onPowerDown() {
void Module::onSpiIrq() {
}

void Module::onSpiDmaTransferCompleted(int status) {

}


////////////////////////////////////////////////////////////////////////////////

struct NoneModuleInfo : public ModuleInfo {
Expand Down
1 change: 1 addition & 0 deletions src/eez/index.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ struct Module {
virtual void tick();
virtual void onPowerDown();
virtual void onSpiIrq();
virtual void onSpiDmaTransferCompleted(int status);
};

static const int NUM_SLOTS = 3;
Expand Down
3 changes: 3 additions & 0 deletions src/eez/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ int main(int argc, char **argv) {
//MX_TIM8_Init();
MX_TIM12_Init();

MX_TIM7_Init();
HAL_TIM_Base_Start_IT(&htim7);

/* Enable I-Cache */
SCB_EnableICache();

Expand Down
18 changes: 18 additions & 0 deletions src/eez/modules/bp3c/comm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ bool masterSynchro(int slotIndex) {
if (diff > CONF_MASTER_SYNC_IRQ_TIMEOUT_MS) {
break;
}

osDelay(1);
}
} else {
// DebugTrace("Slot %d: %02X\n", slotIndex + 1, rxBuffer[0]);
Expand All @@ -86,6 +88,8 @@ bool masterSynchro(int slotIndex) {
slot.idw2 = 0;
return false;
}

osDelay(1);
}
#endif

Expand Down Expand Up @@ -128,6 +132,20 @@ TransferResult transfer(int slotIndex, uint8_t *output, uint8_t *input, uint32_t
#endif
}

TransferResult transferDMA(int slotIndex, uint8_t *output, uint8_t *input, uint32_t bufferSize) {
#if defined(EEZ_PLATFORM_STM32)
spi::handle[slotIndex]->ErrorCode = 0;

spi::select(slotIndex, spi::CHIP_SLAVE_MCU, false);
auto result = spi::transferDMA(slotIndex, output, input, bufferSize);
return (TransferResult)result;
#endif

#if defined(EEZ_PLATFORM_SIMULATOR)
return TRANSFER_STATUS_OK;
#endif
}

} // namespace comm
} // namespace bp3c
} // namespace eez
1 change: 1 addition & 0 deletions src/eez/modules/bp3c/comm.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ enum TransferResult {
};

TransferResult transfer(int slotIndex, uint8_t *output, uint8_t *input, uint32_t bufferSize);
TransferResult transferDMA(int slotIndex, uint8_t *output, uint8_t *input, uint32_t bufferSize);

} // namespace comm
} // namespace bp3c
Expand Down
44 changes: 30 additions & 14 deletions src/eez/modules/dib-mio168/dib-mio168.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
#include <eez/platform/stm32/spi.h>
#endif

#include "eez/debug.h"
#include "eez/firmware.h"
#include "eez/index.h"
#include "eez/hmi.h"
#include "eez/gui/gui.h"
#include "eez/modules/psu/event_queue.h"
#include "eez/modules/bp3c/comm.h"
#include <eez/debug.h>
#include <eez/firmware.h>
#include <eez/index.h>
#include <eez/hmi.h>
#include <eez/gui/gui.h>
#include <eez/modules/psu/event_queue.h>
#include <eez/modules/bp3c/comm.h>
#include <eez/modules/bp3c/flash_slave.h>

#include "./dib-mio168.h"
Expand Down Expand Up @@ -70,7 +70,7 @@ struct Mio168ModuleInfo : public ModuleInfo {
}
};

#define BUFFER_SIZE 16
#define BUFFER_SIZE 1024

struct Mio168Module : public Module {
public:
Expand Down Expand Up @@ -137,17 +137,33 @@ struct Mio168Module : public Module {
output[0] = inputPinStates;
output[1] = outputPinStates;

auto status = bp3c::comm::transfer(slotIndex, output, input, BUFFER_SIZE);
auto status = bp3c::comm::transferDMA(slotIndex, output, input, BUFFER_SIZE);
if (status != bp3c::comm::TRANSFER_STATUS_OK) {
auto &slot = *g_slots[slotIndex];
slot.onSpiDmaTransferCompleted(status);
}
}

void onSpiDmaTransferCompleted(int status) override {
if (status == bp3c::comm::TRANSFER_STATUS_OK) {
numCrcErrors = 0;

inputPinStates = input[0];

uint16_t *inputU16 = (uint16_t *)(input + 2);
analogInputValues[0] = inputU16[0];
analogInputValues[1] = inputU16[1];
analogInputValues[2] = inputU16[2];
analogInputValues[3] = inputU16[3];
static uint32_t totalSamples = 0;

uint16_t *inputU16 = (uint16_t *)(input + 24);

uint16_t numSamples = inputU16[-1];

analogInputValues[(totalSamples + 0) % 4] = inputU16[0];
analogInputValues[(totalSamples + 1) % 4] = inputU16[1];
analogInputValues[(totalSamples + 2) % 4] = inputU16[2];
analogInputValues[(totalSamples + 3) % 4] = inputU16[3];

analogInputValues[3] = numSamples;

totalSamples += numSamples;
} else {
if (status == bp3c::comm::TRANSFER_STATUS_CRC_ERROR) {
if (++numCrcErrors >= 10) {
Expand Down
4 changes: 0 additions & 4 deletions src/eez/modules/psu/psu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,6 @@ TestResult PsuModule::getTestResult() {
////////////////////////////////////////////////////////////////////////////////

void init() {
#if defined(EEZ_PLATFORM_STM32)
MX_TIM7_Init();
HAL_TIM_Base_Start_IT(&htim7);
#endif
}

void onThreadMessage(uint8_t type, uint32_t param) {
Expand Down
68 changes: 61 additions & 7 deletions src/eez/platform/stm32/spi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
#include <spi.h>
#include <cmsis_os.h>

#include <eez/platform/stm32/spi.h>

#include <eez/index.h>
#include <eez/platform/stm32/spi.h>
#include <eez/modules/bp3c/comm.h>

namespace eez {
namespace spi {
Expand All @@ -41,8 +41,10 @@ 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) {
taskENTER_CRITICAL();
void select(uint8_t slotIndex, int chip, bool critical) {
if (critical) {
taskENTER_CRITICAL();
}

auto &slot = *g_slots[slotIndex];

Expand Down Expand Up @@ -94,7 +96,7 @@ void select(uint8_t slotIndex, int chip) {
}
}

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

if (slot.moduleInfo->moduleType == MODULE_TYPE_DCP405) {
Expand All @@ -105,7 +107,9 @@ void deselect(uint8_t slotIndex) {
HAL_GPIO_WritePin(SPI_CSA_GPIO_Port[slotIndex], SPI_CSA_Pin[slotIndex], GPIO_PIN_SET);
}

taskEXIT_CRITICAL();
if (critical) {
taskEXIT_CRITICAL();
}
}

HAL_StatusTypeDef transfer(uint8_t slotIndex, uint8_t *input, uint8_t *output, uint16_t size) {
Expand All @@ -120,7 +124,57 @@ 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);
}

} // namespace spi
} // namespace eez

#endif // EEZ_PLATFORM_STM32
void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi) {
using namespace eez;
using namespace eez::spi;
using namespace eez::bp3c::comm;

uint8_t slotIndex;
if (hspi == handle[0]) {
slotIndex = 0;
} else if (hspi == handle[1]) {
slotIndex = 1;
} else {
slotIndex = 2;
}

deselect(slotIndex, false);

auto &slot = *g_slots[slotIndex];

slot.onSpiDmaTransferCompleted(TRANSFER_STATUS_OK);
}

void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi) {
using namespace eez;
using namespace eez::spi;
using namespace eez::bp3c::comm;

uint8_t slotIndex;
if (hspi == handle[0]) {
slotIndex = 0;
} else if (hspi == handle[1]) {
slotIndex = 1;
} else {
slotIndex = 2;
}

deselect(slotIndex, false);

auto &slot = *g_slots[slotIndex];

if (spi::handle[slotIndex]->ErrorCode == HAL_SPI_ERROR_CRC) {
slot.onSpiDmaTransferCompleted(TRANSFER_STATUS_CRC_ERROR);
} else {
slot.onSpiDmaTransferCompleted(TRANSFER_STATUS_ERROR);
}
}

#endif // EEZ_PLATFORM_STM32
6 changes: 4 additions & 2 deletions src/eez/platform/stm32/spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ static const int CHIP_SLAVE_MCU_BOOTLOADER = 5;

void init(uint8_t slotIndex, int chip);

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

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);

} // namespace spi
} // namespace eez
Loading

0 comments on commit ce419db

Please sign in to comment.