diff --git a/firmware/bootloader/openblt_chibios/openblt_can.cpp b/firmware/bootloader/openblt_chibios/openblt_can.cpp new file mode 100644 index 0000000000..b4d0d57e33 --- /dev/null +++ b/firmware/bootloader/openblt_chibios/openblt_can.cpp @@ -0,0 +1,96 @@ +#include "pch.h" + +#include "hal.h" + +extern "C" { + #include "boot.h" +} + +/************************************************************************************//** +** \brief Initializes the CAN controller and synchronizes it to the CAN bus. +** \return none. +** +****************************************************************************************/ +extern "C" void CanInit(void) +{ + // TODO +} + + +/************************************************************************************//** +** \brief Transmits a packet formatted for the communication interface. +** \param data Pointer to byte array with data that it to be transmitted. +** \param len Number of bytes that are to be transmitted. +** \return none. +** +****************************************************************************************/ +extern "C" void CanTransmitPacket(blt_int8u *data, blt_int8u len) +{ + blt_int32u txMsgId = BOOT_COM_CAN_TX_MSG_ID; + CANTxFrame frame; + + if ((txMsgId & 0x80000000) == 0) + { + /* set the 11-bit CAN identifier. */ + frame.SID = txMsgId; + frame.IDE = false; + } + else + { + txMsgId &= ~0x80000000; + /* set the 29-bit CAN identifier. */ + frame.EID = txMsgId & ~0x80000000; // negate the ID-type bit + frame.IDE = true; + } + + // Copy data/DLC + frame.DLC = len; + memcpy(frame.data8, data, len); + + canTransmitTimeout(&CAND1, CAN_ANY_MAILBOX, &frame, TIME_MS2I(100)); +} + +/************************************************************************************//** +** \brief Receives a communication interface packet if one is present. +** \param data Pointer to byte array where the data is to be stored. +** \param len Pointer where the length of the packet is to be stored. +** \return BLT_TRUE is a packet was received, BLT_FALSE otherwise. +** +****************************************************************************************/ +extern "C" blt_bool CanReceivePacket(blt_int8u *data, blt_int8u *len) +{ + constexpr blt_int32u rxMsgId = BOOT_COM_CAN_RX_MSG_ID; + blt_bool result = BLT_FALSE; + CANRxFrame frame; + + if (MSG_OK != canReceiveTimeout(&CAND1, CAN_ANY_MAILBOX, &frame, TIME_IMMEDIATE)) { + // no message was waiting + return BLT_FALSE; + } + + // Check that the ID type matches this frame (std vs ext) + constexpr bool configuredAsExt = (rxMsgId & 0x80000000) == 0; + if (configuredAsExt != frame.IDE) { + // Wrong frame type + return BLT_FALSE; + } + + // Check that the frame's ID matches + if (frame.IDE) { + if (frame.EID != (rxMsgId & ~0x80000000)) { + // Wrong ID + return BLT_FALSE; + } + } else { + if (frame.SID != rxMsgId) { + // Wrong ID + return BLT_FALSE; + } + } + + // Copy data and length out + *len = frame.DLC; + memcpy(data, frame.data8, frame.DLC); + + return BLT_TRUE; +} diff --git a/firmware/bootloader/src/Makefile b/firmware/bootloader/src/Makefile index 0bebf98d08..37832a4d58 100644 --- a/firmware/bootloader/src/Makefile +++ b/firmware/bootloader/src/Makefile @@ -22,8 +22,6 @@ DDEFS += -DHAL_USE_FLASH=FALSE DDEFS += -DEFI_USE_UART_DMA=FALSE DDEFS += -DEFI_USB_SERIAL=TRUE -DHAL_USE_USB_MSD=FALSE -# disable CAN -DDEFS += -DEFI_CAN_SUPPORT=FALSE DDEFS += -DEFI_UNIT_TEST=0 -DEFI_PROD_CODE=1 -DEFI_SIMULATOR=0 @@ -187,6 +185,7 @@ CPPSRC = $(ALLCPPSRC) \ $(PROJECT_DIR)/util/efilib.cpp \ $(PROJECT_DIR)/hw_layer/pin_repository.cpp \ $(RUSEFI_LIB_CPP) \ + $(PROJECT_DIR)/bootloader/openblt_chibios/openblt_can.cpp \ $(PROJECT_DIR)/bootloader/openblt_chibios/openblt_chibios.cpp \ $(PROJECT_DIR)/bootloader/openblt_chibios/openblt_flash.cpp \ $(PROJECT_DIR)/bootloader/openblt_chibios/openblt_usb.cpp \