This commit is contained in:
Matthew Kennedy 2024-09-04 23:13:14 -07:00 committed by GitHub
commit 8c18a4cecc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 106 additions and 3 deletions

View File

@ -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 \

View File

@ -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;
}

View File

@ -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