diff --git a/src/bittybuzz/buzz_scripts/main.bzz b/src/bittybuzz/buzz_scripts/main.bzz index 31cfb1f9e..f99174043 100644 --- a/src/bittybuzz/buzz_scripts/main.bzz +++ b/src/bittybuzz/buzz_scripts/main.bzz @@ -1,7 +1,7 @@ -include "behaviors/follow_leader.bzz" +include "behaviors/blinky.bzz" function init() { - exec = create_exec(100); + exec = create_exec(2000); } function step() { diff --git a/src/bsp/src/stm32/hal/include/hal/esp_spi.h b/src/bsp/src/stm32/hal/include/hal/esp_spi.h index eda79a9f6..c4b0e2348 100644 --- a/src/bsp/src/stm32/hal/include/hal/esp_spi.h +++ b/src/bsp/src/stm32/hal/include/hal/esp_spi.h @@ -73,6 +73,12 @@ void EspSpi_WriteCS(bool state); */ bool EspSpi_ReadCS(); +/** + * Read the USER0 pin + * @return Status of the pin + */ +bool EspSpi_ReadUser0(); + /** * @brief Callback used when reception has finished. Calls the user callback provided in * espSpiTransmitDma() diff --git a/src/bsp/src/stm32/hal/src/esp_spi.c b/src/bsp/src/stm32/hal/src/esp_spi.c index c46717d97..1581f3f2b 100644 --- a/src/bsp/src/stm32/hal/src/esp_spi.c +++ b/src/bsp/src/stm32/hal/src/esp_spi.c @@ -71,6 +71,8 @@ void EspSpi_WriteCS(bool state) { bool EspSpi_ReadCS() { return HAL_GPIO_ReadPin(ESP_CS_GPIO_Port, ESP_CS_Pin) == GPIO_PIN_SET; } +bool EspSpi_ReadUser0() { return HAL_GPIO_ReadPin(ESP_USER0_Port, ESP_USER0_Pin) == GPIO_PIN_SET; } + void EspSpi_RxCallback() { if (rxCpltCallbackFct != NULL && rxCallbackContext != NULL) { rxCpltCallbackFct(rxCallbackContext); diff --git a/src/bsp/src/stm32/include/SpiEsp.h b/src/bsp/src/stm32/include/SpiEsp.h index 3d0f43eea..68e15da9c 100644 --- a/src/bsp/src/stm32/include/SpiEsp.h +++ b/src/bsp/src/stm32/include/SpiEsp.h @@ -33,14 +33,13 @@ class SpiEsp : public ICommInterface { ICRC& m_crc; ILogger& m_logger; - enum class transmitState { IDLE, SENDING_HEADER, SENDING_PAYLOAD, ERROR } m_txState; + enum class transmitState { IDLE, SENDING_HEADER, SENDING_PAYLOAD } m_txState; enum class receiveState { IDLE, RECEIVING_HEADER, PARSING_HEADER, RECEIVING_PAYLOAD, - VALIDATE_CRC, - ERROR + VALIDATE_CRC } m_rxState; struct message { @@ -51,7 +50,7 @@ class SpiEsp : public ICommInterface { std::array m_data; CircularBuff m_circularBuf; - TaskHandle_t m_receivingTaskHandle, m_sendingTaskHandle = nullptr; + TaskHandle_t m_receivingTaskHandle, m_sendingTaskHandle, m_driverTaskHandle = nullptr; EspHeader::Header m_outboundHeader; EspHeader::Header* m_inboundHeader; diff --git a/src/bsp/src/stm32/src/SpiEsp.cpp b/src/bsp/src/stm32/src/SpiEsp.cpp index abec4e95b..8e7b7fad1 100644 --- a/src/bsp/src/stm32/src/SpiEsp.cpp +++ b/src/bsp/src/stm32/src/SpiEsp.cpp @@ -5,7 +5,8 @@ #include void task(void* context) { - constexpr uint16_t loopRate = 20; + // This is to give time for ESP to be ready for next transaction + constexpr uint16_t loopRate = 3; while (true) { static_cast(context)->execute(); Task::delay(loopRate); @@ -54,14 +55,13 @@ bool SpiEsp::send(const uint8_t* buffer, uint16_t length) { m_txState = transmitState::SENDING_HEADER; // Wait for transmission to be over. Will be notified when ACK received or upon error m_sendingTaskHandle = xTaskGetCurrentTaskHandle(); - ulTaskNotifyTake(pdTRUE, portMAX_DELAY); - if (m_txState == transmitState::ERROR) { - m_logger.log(LogLevel::Error, "Error occurred..."); - return false; + m_hasSentPayload = false; + while (!m_hasSentPayload) { + ulTaskNotifyTake(pdTRUE, 20); } m_logger.log(LogLevel::Debug, "Payload sent!"); m_sendingTaskHandle = nullptr; - return m_crcOK; + return true; } bool SpiEsp::receive(uint8_t* buffer, uint16_t length) { if (buffer == nullptr || length > ESP_SPI_MAX_MESSAGE_LENGTH) { @@ -82,21 +82,20 @@ bool SpiEsp::isConnected() const { return m_isConnected; } void SpiEsp::execute() { uint32_t txLengthBytes = 0; - uint32_t rxLengthBytes = 0; + uint32_t rxLengthBytes = EspHeader::sizeBytes; auto* txBuffer = (uint8_t*)&m_outboundHeader; // Send header by default; + if (m_driverTaskHandle == nullptr) { + m_driverTaskHandle = xTaskGetCurrentTaskHandle(); + } switch (m_rxState) { case receiveState::IDLE: - if (!m_inboundRequest) { - m_inboundRequest = EspSpi_ReadCS(); - } + m_inboundRequest = EspSpi_ReadUser0(); if (m_inboundRequest || m_txState != transmitState::IDLE) { - rxLengthBytes = EspHeader::sizeBytes; m_rxState = receiveState::RECEIVING_HEADER; } break; case receiveState::RECEIVING_HEADER: - rxLengthBytes = EspHeader::sizeBytes; break; case receiveState::PARSING_HEADER: m_inboundHeader = (EspHeader::Header*)m_inboundMessage.m_data.data(); @@ -106,10 +105,8 @@ void SpiEsp::execute() { m_logger.log(LogLevel::Debug, "Bytes were: | %d | %d | %d | %d |", m_inboundMessage.m_data[0], m_inboundMessage.m_data[1], m_inboundMessage.m_data[2], m_inboundMessage.m_data[3]); - m_rxState = receiveState::ERROR; - if (m_hasSentPayload) { - m_txState = transmitState::ERROR; - } + m_inboundMessage.m_sizeBytes = 0; + m_rxState = receiveState::RECEIVING_HEADER; m_isConnected = false; break; } @@ -135,17 +132,8 @@ void SpiEsp::execute() { m_outboundHeader.systemState.stmSystemState.failedCrc = 0; m_rxState = receiveState::RECEIVING_PAYLOAD; } else { - rxLengthBytes = EspHeader::sizeBytes; m_rxState = receiveState::RECEIVING_HEADER; } - // Payload has been sent. Check crc and notify sending task - if (m_hasSentPayload) { - m_hasSentPayload = false; - m_crcOK = !m_inboundHeader->systemState.stmSystemState.failedCrc; - if (m_sendingTaskHandle != nullptr) { - xTaskNotifyGive(m_sendingTaskHandle); - } - } break; case receiveState::RECEIVING_PAYLOAD: rxLengthBytes = m_inboundHeader->txSizeBytes; @@ -172,17 +160,6 @@ void SpiEsp::execute() { m_inboundMessage.m_payloadSize = 0; m_inboundMessage.m_sizeBytes = 0; m_rxState = receiveState::RECEIVING_HEADER; - rxLengthBytes = EspHeader::sizeBytes; - break; - case receiveState::ERROR: - rxLengthBytes = EspHeader::sizeBytes; - m_inboundMessage.m_sizeBytes = 0; - m_rxState = receiveState::RECEIVING_HEADER; - CircularBuff_clear(&m_circularBuf); - if (m_receivingTaskHandle != nullptr) { - xTaskNotifyGive(m_receivingTaskHandle); - } - m_logger.log(LogLevel::Debug, "Error within Spi driver ESP - RX"); break; } if (m_inboundRequest && m_txState == transmitState::IDLE) { @@ -201,28 +178,19 @@ void SpiEsp::execute() { case transmitState::SENDING_PAYLOAD: txLengthBytes = m_outboundMessage.m_sizeBytes; txBuffer = m_outboundMessage.m_data.data(); - m_hasSentPayload = false; - m_crcOK = false; - break; - case transmitState::ERROR: m_crcOK = false; - EspSpi_WriteCS(true); - if (m_sendingTaskHandle != nullptr) { - xTaskNotifyGive(m_sendingTaskHandle); - } - m_txState = transmitState::SENDING_HEADER; - m_logger.log(LogLevel::Debug, "Error within Spi driver ESP - TX"); break; } - if ((m_inboundRequest || m_outboundMessage.m_sizeBytes != 0 || - m_inboundMessage.m_sizeBytes != 0) && - m_txState != transmitState::ERROR && m_rxState != receiveState::ERROR) { + if (m_inboundRequest || m_outboundMessage.m_sizeBytes != 0 || + m_inboundMessage.m_sizeBytes != 0) { + EspSpi_WriteCS(false); uint32_t finalSize = std::max(txLengthBytes, rxLengthBytes); m_inboundMessage.m_data.fill(0); EspSpi_TransmitReceiveDma(txBuffer, m_inboundMessage.m_data.data(), finalSize, SpiEsp::espTxRxCallback, this); + ulTaskNotifyTake(pdTRUE, 20); } } @@ -237,7 +205,7 @@ void SpiEsp::updateOutboundHeader() { void SpiEsp::espInterruptCallback(void* context) { auto* instance = static_cast(context); // Interrupt is on both falling edge and rising edge. - instance->m_inboundRequest = EspSpi_ReadCS(); + instance->m_inboundRequest = EspSpi_ReadUser0(); } void SpiEsp::espTxRxCallback(void* context) { @@ -255,16 +223,18 @@ void SpiEsp::espTxRxCallback(void* context) { // This should never be called. The state machine should never be in any other state during // the ISR. instance->m_logger.log(LogLevel::Error, "Interrupted called on invalid state"); - instance->m_txState = transmitState::ERROR; break; } - + BaseType_t yield; if (instance->m_txState == transmitState::SENDING_PAYLOAD) { instance->m_txState = transmitState::IDLE; instance->m_outboundMessage.m_sizeBytes = 0; instance->m_outboundMessage.m_payloadSize = 0; instance->m_hasSentPayload = true; + vTaskNotifyGiveFromISR(instance->m_sendingTaskHandle, &yield); } + vTaskNotifyGiveFromISR(instance->m_driverTaskHandle, &yield); + portYIELD_FROM_ISR(yield); } ConnectionType SpiEsp::getType() const { return ConnectionType::SPI; } diff --git a/src/message-handler/include/message-handler/MessageDispatcher.h b/src/message-handler/include/message-handler/MessageDispatcher.h index 7f3945af5..fcce1e9dd 100644 --- a/src/message-handler/include/message-handler/MessageDispatcher.h +++ b/src/message-handler/include/message-handler/MessageDispatcher.h @@ -11,7 +11,7 @@ #include #include -class MessageDispatcher : IMessageDispatcher { +class MessageDispatcher : public IMessageDispatcher { public: MessageDispatcher(ICircularQueue& buzzOutputQ, ICircularQueue& hostOutputQ, diff --git a/src/message-handler/src/HiveMindHostApiRequestHandler.cpp b/src/message-handler/src/HiveMindHostApiRequestHandler.cpp index fb42ca0c5..67150bf4b 100644 --- a/src/message-handler/src/HiveMindHostApiRequestHandler.cpp +++ b/src/message-handler/src/HiveMindHostApiRequestHandler.cpp @@ -30,6 +30,9 @@ bool HiveMindHostApiRequestHandler::handleHiveMindHostApiRequest( uint16_t requestId, const MessageDTO& message, const HiveMindHostApiRequestDTO& request) { if (std::holds_alternative(request.getRequest())) { + if (m_bsp.getUUId() != message.getSourceId()) { + return m_remoteQueue.push(message); + } return m_hostQueue.push(message); } @@ -48,6 +51,9 @@ bool HiveMindHostApiRequestHandler::handleHiveMindHostApiRequest( HiveMindHostApiResponseDTO hmResp(neighborResp); ResponseDTO resp(requestId, hmResp); MessageDTO msg(m_bsp.getUUId(), message.getSourceId(), resp); + if (m_bsp.getUUId() != message.getSourceId()) { + return m_remoteQueue.push(msg); + } return m_hostQueue.push(msg); } @@ -65,6 +71,9 @@ bool HiveMindHostApiRequestHandler::handleHiveMindHostApiRequest( HiveMindHostApiResponseDTO hmResp(neighborResp); ResponseDTO resp(requestId, hmResp); MessageDTO msg(m_bsp.getUUId(), message.getSourceId(), resp); + if (m_bsp.getUUId() != message.getSourceId()) { + return m_remoteQueue.push(msg); + } return m_hostQueue.push(msg); } diff --git a/src/os/freertos/include/freertos/FreeRTOSConfig.h b/src/os/freertos/include/freertos/FreeRTOSConfig.h index ccffed50f..f676ec908 100644 --- a/src/os/freertos/include/freertos/FreeRTOSConfig.h +++ b/src/os/freertos/include/freertos/FreeRTOSConfig.h @@ -64,6 +64,7 @@ #define configSUPPORT_DYNAMIC_ALLOCATION 1 #define configSUPPORT_STATIC_ALLOCATION 1 #define configUSE_PORT_OPTIMISED_TASK_SELECTION 0 +#define configRECORD_STACK_HIGH_ADDRESS 1 /* Co-routine definitions. */ #define configUSE_CO_ROUTINES 0