diff --git a/firmware/bootloader/Makefile b/firmware/bootloader/Makefile index 02f2363b4b..bd16cb18f3 100644 --- a/firmware/bootloader/Makefile +++ b/firmware/bootloader/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 # Cache is disabled on F7, H7 DDEFS += -DSTM32_SRAM2_NOCACHE=FALSE -DSTM32_NOCACHE_SRAM1_SRAM2=FALSE -DSTM32_NOCACHE_SRAM3=FALSE @@ -189,8 +187,10 @@ CPPSRC = $(ALLCPPSRC) \ $(HW_LAYER_EMS_CPP) \ $(BOARDCPPSRC) \ $(PROJECT_DIR)/util/efilib.cpp \ + $(PROJECT_DIR)/hw_layer/drivers/can/can_config.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 \ diff --git a/firmware/bootloader/openblt_chibios/openblt_can.cpp b/firmware/bootloader/openblt_chibios/openblt_can.cpp new file mode 100644 index 0000000000..0c59382e27 --- /dev/null +++ b/firmware/bootloader/openblt_chibios/openblt_can.cpp @@ -0,0 +1,101 @@ +#include "pch.h" + +#include "hal.h" + +#include "can_hw.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: init pins? + + auto cfg = findCanConfig(B500KBPS); + canStart(&CAND1, cfg); +} + + +/************************************************************************************//** +** \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/hw_layer/openblt/blt_conf.h b/firmware/hw_layer/openblt/blt_conf.h index d868067897..72d161aa41 100644 --- a/firmware/hw_layer/openblt/blt_conf.h +++ b/firmware/hw_layer/openblt/blt_conf.h @@ -71,7 +71,7 @@ * */ /** \brief Enable/disable CAN transport layer. */ -#define BOOT_COM_CAN_ENABLE (0) +#define BOOT_COM_CAN_ENABLE (1) /** \brief Configure the desired CAN baudrate. */ #define BOOT_COM_CAN_BAUDRATE (500000) /** \brief Configure CAN message ID target->host. */ @@ -83,6 +83,8 @@ /** \brief Configure number of bytes in the host->target CAN message. */ #define BOOT_COM_CAN_RX_MAX_DATA (8) +#define BOOT_COM_CAN_CHANNEL_INDEX (1) + /* The RS232 communication interface is selected by setting the BOOT_COM_RS232_ENABLE * configurable to 1. Configurable BOOT_COM_RS232_BAUDRATE selects the communication speed * in bits/second. The maximum amount of data bytes in a message for data transmission