Skip to content

Commit

Permalink
updated dib spi comm
Browse files Browse the repository at this point in the history
  • Loading branch information
mvladic committed Jun 18, 2020
1 parent caf4c4b commit 1ee70cd
Show file tree
Hide file tree
Showing 22 changed files with 232 additions and 128 deletions.
8 changes: 6 additions & 2 deletions src/eez/index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

namespace eez {

ModuleInfo::ModuleInfo(uint16_t moduleType_, uint16_t moduleCategory_, const char *moduleName_, const char *moduleBrand_, uint16_t latestModuleRevision_, FlashMethod flashMethod_, uint32_t flashDuration_, uint32_t spiBaudRatePrescaler_)
ModuleInfo::ModuleInfo(uint16_t moduleType_, uint16_t moduleCategory_, const char *moduleName_, const char *moduleBrand_, uint16_t latestModuleRevision_, FlashMethod flashMethod_, uint32_t flashDuration_, uint32_t spiBaudRatePrescaler_, bool spiCrcCalculationEnable_)
: moduleType(moduleType_)
, moduleCategory(moduleCategory_)
, moduleName(moduleName_)
Expand All @@ -43,6 +43,7 @@ ModuleInfo::ModuleInfo(uint16_t moduleType_, uint16_t moduleCategory_, const cha
, flashMethod(flashMethod_)
, flashDuration(flashDuration_)
, spiBaudRatePrescaler(spiBaudRatePrescaler_)
, spiCrcCalculationEnable(spiCrcCalculationEnable_)
{
}

Expand Down Expand Up @@ -112,12 +113,15 @@ void Module::tick() {
void Module::onPowerDown() {
}

void Module::onSpiIrq() {
}

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

struct NoneModuleInfo : public ModuleInfo {
public:
NoneModuleInfo()
: ModuleInfo(MODULE_TYPE_NONE, MODULE_CATEGORY_NONE, "None", "None", 0, FLASH_METHOD_NONE, 0, 0)
: ModuleInfo(MODULE_TYPE_NONE, MODULE_CATEGORY_NONE, "None", "None", 0, FLASH_METHOD_NONE, 0, 0, false)
{
}

Expand Down
4 changes: 3 additions & 1 deletion src/eez/index.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ struct ModuleInfo {
FlashMethod flashMethod;
uint32_t flashDuration;
uint32_t spiBaudRatePrescaler;
bool spiCrcCalculationEnable;

ModuleInfo(uint16_t moduleType, uint16_t moduleCategory, const char *moduleName, const char *moduleBrand, uint16_t latestModuleRevision, FlashMethod flashMethod, uint32_t flashDuration_, uint32_t spiBaudRatePrescaler_);
ModuleInfo(uint16_t moduleType, uint16_t moduleCategory, const char *moduleName, const char *moduleBrand, uint16_t latestModuleRevision, FlashMethod flashMethod, uint32_t flashDuration_, uint32_t spiBaudRatePrescaler_, bool spiCrcCalculationEnable_);

virtual Module *createModule(uint8_t slotIndex, uint16_t moduleRevision) = 0;
virtual int getSlotView(SlotViewType slotViewType, int slotIndex, int cursor);
Expand All @@ -87,6 +88,7 @@ struct Module {
virtual void initChannels();
virtual void tick();
virtual void onPowerDown();
virtual void onSpiIrq();
};

static const int NUM_SLOTS = 3;
Expand Down
17 changes: 5 additions & 12 deletions src/eez/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,25 +187,18 @@ extern "C" void vApplicationStackOverflowHook(TaskHandle_t xTask, signed char *p
}

extern "C" void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
int slotIndex = -1;

if (GPIO_Pin == SPI2_IRQ_Pin) {
slotIndex = 0;
using namespace eez;
if (GPIO_Pin == SPI2_IRQ_Pin) {
sendMessageToPsu(PSU_MESSAGE_SPI_IRQ, 0, 0);
} else if (GPIO_Pin == SPI4_IRQ_Pin) {
slotIndex = 1;
sendMessageToPsu(PSU_MESSAGE_SPI_IRQ, 1, 0);
} else if (GPIO_Pin == SPI5_IRQ_Pin) {
slotIndex = 2;
sendMessageToPsu(PSU_MESSAGE_SPI_IRQ, 2, 0);
} else if (GPIO_Pin == SD_DETECT_Pin) {
eez::psu::sd_card::onSdDetectInterrupt();
return;
} else if (GPIO_Pin == ENC_A_Pin || GPIO_Pin == ENC_B_Pin) {
eez::mcu::encoder::onPinInterrupt();
}

if (slotIndex != -1) {
using namespace eez;
sendMessageToPsu(PSU_MESSAGE_SPI_IRQ, slotIndex, 0);
}
}
#endif

Expand Down
31 changes: 20 additions & 11 deletions src/eez/modules/bp3c/comm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@
#define CONF_MASTER_SYNC_TIMEOUT_MS 500
#define CONF_MASTER_SYNC_IRQ_TIMEOUT_MS 50

#if defined(EEZ_PLATFORM_STM32)
static GPIO_TypeDef *SPI_IRQ_GPIO_Port[] = { SPI2_IRQ_GPIO_Port, SPI4_IRQ_GPIO_Port, SPI5_IRQ_GPIO_Port };
static const uint16_t SPI_IRQ_Pin[] = { SPI2_IRQ_Pin, SPI4_IRQ_Pin, SPI5_IRQ_Pin };
#endif

namespace eez {
namespace bp3c {
namespace comm {
Expand All @@ -64,7 +59,7 @@ bool masterSynchro(int slotIndex) {
if (rxBuffer[0] == SPI_SLAVE_SYNBYTE) {
uint32_t startIrq = millis();
while (true) {
if (HAL_GPIO_ReadPin(SPI_IRQ_GPIO_Port[slotIndex], SPI_IRQ_Pin[slotIndex]) == GPIO_PIN_SET) {
if (HAL_GPIO_ReadPin(spi::IRQ_GPIO_Port[slotIndex], spi::IRQ_Pin[slotIndex]) == GPIO_PIN_SET) {
slot.firmwareMajorVersion = rxBuffer[1];
slot.firmwareMinorVersion = rxBuffer[2];
slot.idw0 = (rxBuffer[3] << 24) | (rxBuffer[4] << 16) | (rxBuffer[5] << 8) | rxBuffer[6];
Expand Down Expand Up @@ -104,18 +99,32 @@ bool masterSynchro(int slotIndex) {
#endif
}

bool transfer(int slotIndex, uint8_t *output, uint8_t *input, uint32_t bufferSize) {
TransferResult transfer(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);
spi::transfer(slotIndex, output, input, bufferSize);
auto result = spi::transfer(slotIndex, output, input, bufferSize);
spi::deselect(slotIndex);

uint32_t crc = HAL_CRC_Calculate(&hcrc, (uint32_t *)input, bufferSize - 4);
return crc == *((uint32_t *)(input + bufferSize - 4));
if (g_slots[slotIndex]->moduleInfo->spiCrcCalculationEnable) {
if (spi::handle[slotIndex]->ErrorCode == HAL_SPI_ERROR_CRC) {
return TRANSFER_STATUS_CRC_ERROR;
} else {
return (TransferResult)result;
}
} else {
if (result == HAL_OK) {
uint32_t crc = HAL_CRC_Calculate(&hcrc, (uint32_t *)input, bufferSize - 4);
return crc == *((uint32_t *)(input + bufferSize - 4)) ? TRANSFER_STATUS_OK : TRANSFER_STATUS_CRC_ERROR;
} else {
return (TransferResult)result;
}
}
#endif

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

Expand Down
10 changes: 9 additions & 1 deletion src/eez/modules/bp3c/comm.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ namespace comm {

bool masterSynchro(int slotIndex);

bool transfer(int slotIndex, uint8_t *output, uint8_t *input, uint32_t bufferSize);
enum TransferResult {
TRANSFER_STATUS_OK,
TRANSFER_STATUS_ERROR,
TRANSFER_STATUS_BUSY,
TRANSFER_STATUS_TIMEOUT,
TRANSFER_STATUS_CRC_ERROR
};

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

} // namespace comm
} // namespace bp3c
Expand Down
21 changes: 11 additions & 10 deletions src/eez/modules/bp3c/flash_slave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,19 @@ bool waitForAck(int slotIndex) {
uint8_t txData = 0;
uint8_t rxData;

spi::select(slotIndex, spi::CHIP_SLAVE_MCU);
spi::select(slotIndex, spi::CHIP_SLAVE_MCU_BOOTLOADER);
spi::transmit(slotIndex, &txData, 1);
spi::deselect(slotIndex);

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

if (rxData == ACK) {
// received ACK
txData = ACK;

spi::select(slotIndex, spi::CHIP_SLAVE_MCU);
spi::select(slotIndex, spi::CHIP_SLAVE_MCU_BOOTLOADER);
spi::transmit(slotIndex, &txData, 1);
spi::deselect(slotIndex);

Expand All @@ -153,7 +153,7 @@ bool syncWithSlave(int slotIndex) {

txData = BL_SPI_SOF;

spi::select(slotIndex, spi::CHIP_SLAVE_MCU);
spi::select(slotIndex, spi::CHIP_SLAVE_MCU_BOOTLOADER);
spi::transmit(slotIndex, &txData, 1);
spi::deselect(slotIndex);

Expand Down Expand Up @@ -190,15 +190,15 @@ bool eraseAll(int slotIndex) {
txData[1] = CMD_EXTENDED_ERASE;
txData[2] = CRC_MASK ^ CMD_EXTENDED_ERASE;

spi::select(slotIndex, spi::CHIP_SLAVE_MCU);
spi::select(slotIndex, spi::CHIP_SLAVE_MCU_BOOTLOADER);
spi::transmit(slotIndex, txData, 3);
spi::deselect(slotIndex);

if (!waitForAck(slotIndex)) {
return false;
}

spi::select(slotIndex, spi::CHIP_SLAVE_MCU);
spi::select(slotIndex, spi::CHIP_SLAVE_MCU_BOOTLOADER);
spi::transmit(slotIndex, buffer, 3);
spi::deselect(slotIndex);

Expand Down Expand Up @@ -259,23 +259,23 @@ bool writeMemory(int slotIndex, uint32_t address, const uint8_t *buffer, uint32_
txData[1] = CMD_WRITE_MEMORY;
txData[2] = CRC_MASK ^ CMD_WRITE_MEMORY;

spi::select(slotIndex, spi::CHIP_SLAVE_MCU);
spi::select(slotIndex, spi::CHIP_SLAVE_MCU_BOOTLOADER);
spi::transmit(slotIndex, txData, 3);
spi::deselect(slotIndex);

if (!waitForAck(slotIndex)) {
return false;
}

spi::select(slotIndex, spi::CHIP_SLAVE_MCU);
spi::select(slotIndex, spi::CHIP_SLAVE_MCU_BOOTLOADER);
spi::transmit(slotIndex, addressAndCrc, 5);
spi::deselect(slotIndex);

if (!waitForAck(slotIndex)) {
return false;
}

spi::select(slotIndex, spi::CHIP_SLAVE_MCU);
spi::select(slotIndex, spi::CHIP_SLAVE_MCU_BOOTLOADER);
spi::transmit(slotIndex, &numBytes, 1);
spi::transmit(slotIndex, (uint8_t *)buffer, bufferSize);
spi::transmit(slotIndex, &crc, 1);
Expand Down Expand Up @@ -374,6 +374,8 @@ void enterBootloaderMode(int slotIndex) {
}

void leaveBootloaderMode() {
g_bootloaderMode = false;

#if defined(EEZ_PLATFORM_STM32)
// disable BOOT0 flag
io_exp::writeToOutputPort(0b10000000);
Expand All @@ -384,7 +386,6 @@ void leaveBootloaderMode() {
psu::testChannels();
#endif

g_bootloaderMode = false;
psu::profile::recallFromLocation(10);

#if defined(EEZ_PLATFORM_STM32)
Expand Down
25 changes: 16 additions & 9 deletions src/eez/modules/dib-dcm220/channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,13 @@ struct DcmModuleInfo : public PsuModuleInfo {
DcmModuleInfo(uint16_t moduleType, const char *moduleName, uint16_t latestModuleRevision)
: PsuModuleInfo(moduleType, moduleName, "Envox", latestModuleRevision, FLASH_METHOD_STM32_BOOTLOADER_UART, 0,
#if defined(EEZ_PLATFORM_STM32)
SPI_BAUDRATEPRESCALER_16
SPI_BAUDRATEPRESCALER_16,
false,
#else
0
0,
false,
#endif
, 2)
2)
{
}

Expand Down Expand Up @@ -413,15 +415,20 @@ struct DcmModule : public PsuModule {

#if defined(EEZ_PLATFORM_STM32)
void transfer() {
if (bp3c::comm::transfer(slotIndex, output, input, BUFFER_SIZE)) {
auto status = bp3c::comm::transfer(slotIndex, output, input, BUFFER_SIZE);
if (status == bp3c::comm::TRANSFER_STATUS_OK) {
numCrcErrors = 0;
} else {
if (++numCrcErrors >= 4) {
event_queue::pushEvent(event_queue::EVENT_ERROR_SLOT1_CRC_CHECK_ERROR + slotIndex);
synchronized = false;
testResult = TEST_FAILED;
if (status == bp3c::comm::TRANSFER_STATUS_CRC_ERROR) {
if (++numCrcErrors >= 4) {
event_queue::pushEvent(event_queue::EVENT_ERROR_SLOT1_CRC_CHECK_ERROR + slotIndex);
synchronized = false;
testResult = TEST_FAILED;
} else {
DebugTrace("Slot %d CRC %d\n", slotIndex + 1, numCrcErrors);
}
} else {
DebugTrace("Slot %d CRC %d\n", slotIndex + 1, numCrcErrors);
DebugTrace("Slot %d SPI transfer error %d\n", slotIndex + 1, status);
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/eez/modules/dib-dcp405/channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ struct DcpChannel : public Channel {
struct DcpModuleInfo : public PsuModuleInfo {
public:
DcpModuleInfo()
: PsuModuleInfo(MODULE_TYPE_DCP405, "DCP405", "Envox", MODULE_REVISION_DCP405_R2B11, FLASH_METHOD_NONE, 0, 0, 1)
: PsuModuleInfo(MODULE_TYPE_DCP405, "DCP405", "Envox", MODULE_REVISION_DCP405_R2B11, FLASH_METHOD_NONE, 0, 0, false, 1)
{
}

Expand All @@ -821,6 +821,13 @@ struct DcpModule : public PsuModule {
: PsuModule(slotIndex, moduleInfo, moduleRevision)
{
}

#if defined(EEZ_PLATFORM_STM32)
void onSpiIrq() {
auto dcpChannel = (DcpChannel *)Channel::getBySlotIndex(slotIndex);
dcpChannel->onSpiIrq();
}
#endif
};

Module *DcpModuleInfo::createModule(uint8_t slotIndex, uint16_t moduleRevision) {
Expand Down
Loading

0 comments on commit 1ee70cd

Please sign in to comment.