diff --git a/firmware/bootloader/openblt_chibios/openblt_chibios.cpp b/firmware/bootloader/openblt_chibios/openblt_chibios.cpp index 87eb238515..3488655616 100644 --- a/firmware/bootloader/openblt_chibios/openblt_chibios.cpp +++ b/firmware/bootloader/openblt_chibios/openblt_chibios.cpp @@ -14,14 +14,36 @@ void TimerReset() { } void CopService() { } void TimerUpdate() { } -void FlashInit() { } -blt_bool FlashVerifyChecksum() { - return BLT_TRUE; +blt_int32u TimerGet() { + return 0; } -blt_addr FlashGetUserProgBaseAddress() { - return 0x08008000; -} +/************************************************************************************//** +** \brief Copies data from the source to the destination address. +** \param dest Destination address for the data. +** \param src Source address of the data. +** \param len length of the data in bytes. +** \return none. +** +****************************************************************************************/ +void CpuMemCopy(blt_addr dest, blt_addr src, blt_int16u len) +{ + blt_int8u *from, *to; + + /* set casted pointers */ + from = (blt_int8u *)src; + to = (blt_int8u *)dest; + + /* copy all bytes from source address to destination address */ + while (len-- > 0) + { + /* store byte value from source to destination */ + *to++ = *from++; + /* keep the watchdog happy */ + CopService(); + } +} /*** end of CpuMemCopy ***/ + /** \brief Pointer to the user program's reset vector. */ #define CPU_USER_PROGRAM_STARTADDR_PTR ((blt_addr)(NvmGetUserProgBaseAddress() + 0x00000004)) diff --git a/firmware/bootloader/openblt_chibios/openblt_flash.cpp b/firmware/bootloader/openblt_chibios/openblt_flash.cpp new file mode 100644 index 0000000000..eeff044db6 --- /dev/null +++ b/firmware/bootloader/openblt_chibios/openblt_flash.cpp @@ -0,0 +1,30 @@ +extern "C" { + #include "boot.h" + #include "flash.h" +} + +void FlashInit() { } + +blt_bool FlashVerifyChecksum() { + return BLT_TRUE; +} + +blt_addr FlashGetUserProgBaseAddress() { + return 0x08008000; +} + +blt_bool FlashWrite(blt_addr addr, blt_int32u len, blt_int8u *data) { + return BLT_TRUE; +} + +blt_bool FlashErase(blt_addr addr, blt_int32u len) { + return BLT_TRUE; +} + +blt_bool FlashDone(void) { + return BLT_TRUE; +} + +blt_bool FlashWriteChecksum() { + return BLT_TRUE; +} diff --git a/firmware/bootloader/openblt_chibios/openblt_usb.cpp b/firmware/bootloader/openblt_chibios/openblt_usb.cpp new file mode 100644 index 0000000000..469f74d41b --- /dev/null +++ b/firmware/bootloader/openblt_chibios/openblt_usb.cpp @@ -0,0 +1,130 @@ +#include "pch.h" +#include "usbcfg.h" +#include "usbconsole.h" + +extern "C" { + #include "boot.h" + #include "rs232.h" +} + +void Rs232Init() { + // Set up USB serial + usb_serial_start(); +} + +#define RS232_CTO_RX_PACKET_TIMEOUT_MS (100u) + +static blt_bool Rs232ReceiveByte(blt_int8u *data); +static void Rs232TransmitByte(blt_int8u data); + +/************************************************************************************//** +** \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. +** +****************************************************************************************/ +void Rs232TransmitPacket(blt_int8u *data, blt_int8u len) +{ + blt_int16u data_index; + + /* verify validity of the len-paramenter */ + // ASSERT_RT(len <= BOOT_COM_RS232_TX_MAX_DATA); + + /* first transmit the length of the packet */ + Rs232TransmitByte(len); + + /* transmit all the packet bytes one-by-one */ + for (data_index = 0; data_index < len; data_index++) + { + /* keep the watchdog happy */ + CopService(); + /* write byte */ + Rs232TransmitByte(data[data_index]); + } +} /*** end of Rs232TransmitPacket ***/ + +/************************************************************************************//** +** \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 if a packet was received, BLT_FALSE otherwise. +** +****************************************************************************************/ +blt_bool Rs232ReceivePacket(blt_int8u *data, blt_int8u *len) +{ + static blt_int8u xcpCtoReqPacket[BOOT_COM_RS232_RX_MAX_DATA+1]; /* one extra for length */ + static blt_int8u xcpCtoRxLength; + static blt_bool xcpCtoRxInProgress = BLT_FALSE; + static blt_int32u xcpCtoRxStartTime = 0; + + /* start of cto packet received? */ + if (xcpCtoRxInProgress == BLT_FALSE) + { + /* store the message length when received */ + if (Rs232ReceiveByte(&xcpCtoReqPacket[0]) == BLT_TRUE) + { + if ( (xcpCtoReqPacket[0] > 0) && + (xcpCtoReqPacket[0] <= BOOT_COM_RS232_RX_MAX_DATA) ) + { + /* store the start time */ + xcpCtoRxStartTime = TimerGet(); + /* reset packet data count */ + xcpCtoRxLength = 0; + /* indicate that a cto packet is being received */ + xcpCtoRxInProgress = BLT_TRUE; + } + } + } + else + { + /* store the next packet byte */ + if (Rs232ReceiveByte(&xcpCtoReqPacket[xcpCtoRxLength+1]) == BLT_TRUE) + { + /* increment the packet data count */ + xcpCtoRxLength++; + + /* check to see if the entire packet was received */ + if (xcpCtoRxLength == xcpCtoReqPacket[0]) + { + /* copy the packet data */ + CpuMemCopy((blt_int32u)data, (blt_int32u)&xcpCtoReqPacket[1], xcpCtoRxLength); + /* done with cto packet reception */ + xcpCtoRxInProgress = BLT_FALSE; + /* set the packet length */ + *len = xcpCtoRxLength; + /* packet reception complete */ + return BLT_TRUE; + } + } + else + { + /* check packet reception timeout */ + if (TimerGet() > (xcpCtoRxStartTime + RS232_CTO_RX_PACKET_TIMEOUT_MS)) + { + /* cancel cto packet reception due to timeout. note that that automaticaly + * discards the already received packet bytes, allowing the host to retry. + */ + xcpCtoRxInProgress = BLT_FALSE; + } + } + } + /* packet reception not yet complete */ + return BLT_FALSE; +} /*** end of Rs232ReceivePacket ***/ + +static blt_bool Rs232ReceiveByte(blt_int8u *data) +{ + if (!is_usb_serial_ready()) { + return BLT_FALSE; + } + + auto bytesRead = chnReadTimeout(&SDU1, data, 1, TIME_IMMEDIATE); + + return bytesRead == 0 ? BLT_FALSE : BLT_TRUE; +} + +static void Rs232TransmitByte(blt_int8u data) +{ + chnWriteTimeout(&SDU1, &data, 1, TIME_INFINITE); +} diff --git a/firmware/bootloader/src/Makefile b/firmware/bootloader/src/Makefile index d59c86c04a..a4453b8b4a 100644 --- a/firmware/bootloader/src/Makefile +++ b/firmware/bootloader/src/Makefile @@ -181,8 +181,10 @@ CSRC = $(ALLCSRC) \ $(HW_LAYER_DRIVERS_CORE) \ $(PROJECT_DIR)/hw_layer/openblt/shared_params.c \ $(PROJECT_DIR)/hw_layer/main_hardfault.c \ - $(PROJECT_DIR)/ext/openblt/Target/Source/boot.c \ $(PROJECT_DIR)/ext/openblt/Target/Source/backdoor.c \ + $(PROJECT_DIR)/ext/openblt/Target/Source/boot.c \ + $(PROJECT_DIR)/ext/openblt/Target/Source/com.c \ + $(PROJECT_DIR)/ext/openblt/Target/Source/xcp.c \ $(PROJECT_DIR)/bootloader/openblt_chibios/nvm.c \ $(PROJECT_DIR)/hw_layer/openblt/hooks.c \ @@ -196,6 +198,8 @@ CPPSRC = $(ALLCPPSRC) \ $(PROJECT_DIR)/hw_layer/pin_repository.cpp \ $(RUSEFI_LIB_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 \ src/rusefi_stubs.cpp \ src/main.cpp diff --git a/firmware/bootloader/src/main.cpp b/firmware/bootloader/src/main.cpp index 28a80ad291..ba6f304a3d 100644 --- a/firmware/bootloader/src/main.cpp +++ b/firmware/bootloader/src/main.cpp @@ -14,9 +14,6 @@ int main(void) { baseMCUInit(); - // Set up USB - usb_serial_start(); - // Init openblt shared params SharedParamsInit();