From 8b9785c295283b0b3b5b9bb05fae88b3b6023bcc Mon Sep 17 00:00:00 2001 From: Cantonplas Date: Mon, 7 Jul 2025 12:42:21 +0200 Subject: [PATCH 01/11] Temporary work, it might work? --- Core/Inc/BLCU/BLCU.hpp | 2 +- .../BLCU_StateMachine/BLCU_StateMachine.hpp | 1 + Core/Inc/Code_generation/JSON_ADE | 2 +- Core/Src/Runes/generated_metadata.cpp | 6 ++--- Core/Src/main.cpp | 22 +++++++++---------- deps/ST-LIB | 2 +- 6 files changed, 18 insertions(+), 17 deletions(-) diff --git a/Core/Inc/BLCU/BLCU.hpp b/Core/Inc/BLCU/BLCU.hpp index 3e5f2df..9459618 100644 --- a/Core/Inc/BLCU/BLCU.hpp +++ b/Core/Inc/BLCU/BLCU.hpp @@ -27,7 +27,7 @@ namespace BLCU{ } void start(){ - STLIB::start("00:80:e1:00:00:00",ip, mask, gateway, UART::uart2); + STLIB::start("00:80:e1:06:07:10",ip, mask, gateway, UART::uart2); BLCU::__tcp_start(); BTFTP::start(); diff --git a/Core/Inc/BLCU/BLCU_StateMachine/BLCU_StateMachine.hpp b/Core/Inc/BLCU/BLCU_StateMachine/BLCU_StateMachine.hpp index a8ccf98..e8ec1ff 100644 --- a/Core/Inc/BLCU/BLCU_StateMachine/BLCU_StateMachine.hpp +++ b/Core/Inc/BLCU/BLCU_StateMachine/BLCU_StateMachine.hpp @@ -82,6 +82,7 @@ namespace BLCU{ //Enter actions BLCU::specific_state_machine.add_enter_action([&](){ + BLCU::boots[BLCU::orders_data.target].turn_off(); BLCU::resets[BLCU::orders_data.target].turn_off(); BLCU::__turn_on_all_boards(); BLCU::orders_data.clean_data(); diff --git a/Core/Inc/Code_generation/JSON_ADE b/Core/Inc/Code_generation/JSON_ADE index 7e5fcd2..73427d4 160000 --- a/Core/Inc/Code_generation/JSON_ADE +++ b/Core/Inc/Code_generation/JSON_ADE @@ -1 +1 @@ -Subproject commit 7e5fcd275db37ebefc7ee9a8c5ee75d329f30998 +Subproject commit 73427d4a6366593b9ffc71e5926782802209a9be diff --git a/Core/Src/Runes/generated_metadata.cpp b/Core/Src/Runes/generated_metadata.cpp index 2bb322e..ff0ba30 100644 --- a/Core/Src/Runes/generated_metadata.cpp +++ b/Core/Src/Runes/generated_metadata.cpp @@ -5,11 +5,11 @@ extern "C"{ const char DESCRIPTION[255] __attribute__((section(".metadata_pool")))= "****************" // placeholder for beggining - "20250624T153513" // DateTime using ISO-8601 format + "20250707T124121" // DateTime using ISO-8601 format " " // alignment - "f8e647d0" // STLIB commit + "f73885a9" // STLIB commit "--------" // ADJ commit - "fa5dc1b1" // Board commit + "e030ecf7" // Board commit // the '=' is used for unparsing ; } \ No newline at end of file diff --git a/Core/Src/main.cpp b/Core/Src/main.cpp index 3e1fa9a..2ff4d60 100644 --- a/Core/Src/main.cpp +++ b/Core/Src/main.cpp @@ -36,17 +36,17 @@ int main(void) { &BLCU::orders_data.target, }; - HeapOrder read_program_order = { - 701, - BLCU::read_program, - &BLCU::orders_data.target, - }; - - HeapOrder erase_program_order = { - 702, - BLCU::erase_program, - &BLCU::orders_data.target, - }; + // HeapOrder read_program_order = { + // 701, + // BLCU::read_program, + // &BLCU::orders_data.target, + // }; + + // HeapOrder erase_program_order = { + // 702, + // BLCU::erase_program, + // &BLCU::orders_data.target, + // }; diff --git a/deps/ST-LIB b/deps/ST-LIB index f8e647d..f73885a 160000 --- a/deps/ST-LIB +++ b/deps/ST-LIB @@ -1 +1 @@ -Subproject commit f8e647d0c4f7d37dde4d6138719530086165ea76 +Subproject commit f73885a99171d3e75c6453efc6d9c9c958849538 From 726aac5a1d661ef045975cc858f88d8299ebf8f9 Mon Sep 17 00:00:00 2001 From: Cantonplas Date: Sat, 12 Jul 2025 22:14:42 +0200 Subject: [PATCH 02/11] Work in progress --- Core/Inc/BLCU/BLCU.hpp | 91 +++++++- .../BLCU/BLCU_Definition/BLCU_Definition.hpp | 212 ++++++++--------- .../BLCU/BLCU_Orders/BLCU_PublicFunctions.hpp | 13 -- Core/Inc/BLCU/BLCU_SetUps/BLCU_SetUps.hpp | 116 ++++----- .../BLCU_StateMachine/BLCU_StateMachine.hpp | 220 +++++++++--------- Core/Inc/BLCU/TCP.hpp | 32 +++ Core/Inc/Code_generation/JSON_ADE | 2 +- Core/Src/BLCU/BLCU.cpp | 166 +++++++++++++ Core/Src/BLCU/TCP.cpp | 22 ++ Core/Src/Runes/generated_metadata.cpp | 4 +- 10 files changed, 579 insertions(+), 299 deletions(-) create mode 100644 Core/Inc/BLCU/TCP.hpp create mode 100644 Core/Src/BLCU/BLCU.cpp create mode 100644 Core/Src/BLCU/TCP.cpp diff --git a/Core/Inc/BLCU/BLCU.hpp b/Core/Inc/BLCU/BLCU.hpp index 9459618..6fb129b 100644 --- a/Core/Inc/BLCU/BLCU.hpp +++ b/Core/Inc/BLCU/BLCU.hpp @@ -7,16 +7,90 @@ #pragma once -#include "BLCU_Orders/BLCU_Orders.hpp" -#include "BLCU_Orders/BLCU_PrivateFunctions.hpp" -#include "BLCU_Orders/BLCU_PublicFunctions.hpp" -#include "BLCU_Protections/BLCU_Protections.hpp" -#include "BLCU_SetUps/BLCU_SetUps.hpp" -#include "BLCU_Starts/BLCU_Starts.hpp" -#include "BLCU_StateMachine/BLCU_StateMachine.hpp" +#include "ST-LIB.hpp" +#include "FDCBootloader/FDCBootloader.hpp" #include "FDCBootloader/BootloaderTFTP.hpp" -namespace BLCU{ +#define BLCU_ID ((uint8_t)1) + + enum Target{ + NOTARGET, + VCU, // 1 + HVSCU, // 2 + BCU, // 3 + BMSL, // 4 + LCU, // 5 + PCU, // 6 + + }; + class BLCU{ + private: + + enum GeneralStates{ + INITIAL, + OPERATIONAL, + FAULT, + }; + + + enum SpecificStates{ + READY, + BOOTING, + }; + + static constexpr uint16_t max_tcp_connection_timeout = 30000; + + StateMachine general_state_machine; + StateMachine specific_state_machine; + + unordered_map resets; + unordered_map boots; + + uint8_t fdcan; + DigitalOutput LED_OPERATIONAL; + DigitalOutput LED_FAULT; + DigitalOutput LED_CAN; + DigitalOutput LED_FLASH; + DigitalOutput LED_SLEEP; + + static constexpr string ip{"192.168.0.27"}; + static constexpr string mask{"255.255.0.0"}; + static constexpr string gateway{"192.168.1.1"}; + static constexpr uint32_t port{50500}; + static ServerSocket* tcp_socket;//Hacer nullptr + bool tcp_timeout = false; + + bool programming_error = false; + + void finish_write_read_order(bool error_ok); + void abort_booting(); + void turn_off_all_boards(); + void turn_on_all_boards(); + void send_to_bootmode(); + void stop_booting(); + + + void setup_state_machine(); + void setup_specific_state_machine(); + + + public: + + BLCU(); + void init(); + void update(); + + //PUBLIC VARIABLES: + Target current_target; + + //ORDER FUNCTIONS: + void read_program(); + void reset_all(); + void get_version(); + void write_program(); + void erase_program(); + + }; void set_up() { @@ -40,4 +114,3 @@ namespace BLCU{ STLIB::update(); } -} diff --git a/Core/Inc/BLCU/BLCU_Definition/BLCU_Definition.hpp b/Core/Inc/BLCU/BLCU_Definition/BLCU_Definition.hpp index 7bd1fdb..0874e99 100644 --- a/Core/Inc/BLCU/BLCU_Definition/BLCU_Definition.hpp +++ b/Core/Inc/BLCU/BLCU_Definition/BLCU_Definition.hpp @@ -1,106 +1,106 @@ -#pragma once - -#include "ST-LIB.hpp" -#include "FDCBootloader/FDCBootloader.hpp" -#include "FDCBootloader/BootloaderTFTP.hpp" -#include "BLCU/Utilities/orders_data.hpp" - -#define BLCU_IP "192.168.0.27" -#define BLCU_MASK "255.255.0.0" -#define BLCU_GATEWAY "192.168.1.1" -#define BLCU_PORT ((uint32_t)50500) -#define BLCU_ID ((uint8_t)1) - -extern HeapOrder ack; -extern HeapOrder nack; -extern HeapOrder get_version_order; - -namespace BLCU { - enum GeneralStates{ - INITIAL, - OPERATIONAL, - FAULT, - }; - - - enum SpecificStates{ - READY, - BOOTING, - }; - - static constexpr uint16_t max_tcp_connection_timeout = 30000; - - StateMachine specific_state_machine; - StateMachine general_state_machine; - - unordered_map resets; - unordered_map boots; - - uint8_t fdcan; - uint8_t uart; - uint8_t initial_led_timer; - DigitalOutput LED_OPERATIONAL; - DigitalOutput LED_FAULT; - DigitalOutput LED_CAN; - DigitalOutput LED_FLASH; - DigitalOutput LED_SLEEP; - - Target current_target; - - string ip, mask, gateway; - uint32_t port; - ServerSocket* tcp_socket; - bool tcp_timeout = false; - - bool programming_error = false; - - orders_data_t orders_data = { - BLCU::Target::NOTARGET, - 0 - }; - - void set_up(); - void start(); - void update(); - - /***********************************************/ - // Orders methods - /***********************************************/ - void reset_all(); - void get_version(); - void read_program(); - void write_program(); - void erase_program(); - - void finish_write_read_order(bool error_ok); - void __end_booting(); - void __abort_booting(); - void __finish_booting(); - void __send_to_bootmode(const BLCU::Target& target); - void __turn_off_all_boards(); - - /***********************************************/ - // Start methods - /***********************************************/ - void __tcp_start(); - void __resets_start(); - void __boots_start(); - void __leds_start(); - - /***********************************************/ - // SetUp methods - /***********************************************/ - - void __set_up_general_state_machine(); - void __set_up_specific_state_machine(); - void __set_up_state_machine(); //TODO: revisar - void __set_up_protections(); - void __set_up_peripherals(); - void __set_up_fdcan(); - void __set_up_uart(); - void __set_up_ethernet(); - void __set_up_resets(); - void __set_up_boots(); - void __set_up_leds(); - -} +// #pragma once + +// #include "ST-LIB.hpp" +// #include "FDCBootloader/FDCBootloader.hpp" +// #include "FDCBootloader/BootloaderTFTP.hpp" +// #include "BLCU/Utilities/orders_data.hpp" + +// #define BLCU_IP "192.168.0.27" +// #define BLCU_MASK "255.255.0.0" +// #define BLCU_GATEWAY "192.168.1.1" +// #define BLCU_PORT ((uint32_t)50500) +// #define BLCU_ID ((uint8_t)1) + +// extern HeapOrder ack; +// extern HeapOrder nack; +// extern HeapOrder get_version_order; + +// namespace BLCU { +// enum GeneralStates{ +// INITIAL, +// OPERATIONAL, +// FAULT, +// }; + + +// enum SpecificStates{ +// READY, +// BOOTING, +// }; + +// static constexpr uint16_t max_tcp_connection_timeout = 30000; + +// StateMachine specific_state_machine; +// StateMachine general_state_machine; + +// unordered_map resets; +// unordered_map boots; + +// uint8_t fdcan; +// uint8_t uart; +// uint8_t initial_led_timer; +// DigitalOutput LED_OPERATIONAL; +// DigitalOutput LED_FAULT; +// DigitalOutput LED_CAN; +// DigitalOutput LED_FLASH; +// DigitalOutput LED_SLEEP; + +// Target current_target; + +// string ip, mask, gateway; +// uint32_t port; +// ServerSocket* tcp_socket; +// bool tcp_timeout = false; + +// bool programming_error = false; + +// orders_data_t orders_data = { +// BLCU::Target::NOTARGET, +// 0 +// }; + +// void set_up(); +// void start(); +// void update(); + +// /***********************************************/ +// // Orders methods +// /***********************************************/ +// void reset_all(); +// void get_version(); +// void read_program(); +// void write_program(); +// void erase_program(); + +// void finish_write_read_order(bool error_ok); +// void __end_booting(); +// void __abort_booting(); +// void __finish_booting(); +// void __send_to_bootmode(const BLCU::Target& target); +// void __turn_off_all_boards(); + +// /***********************************************/ +// // Start methods +// /***********************************************/ +// void __tcp_start(); +// void __resets_start(); +// void __boots_start(); +// void __leds_start(); + +// /***********************************************/ +// // SetUp methods +// /***********************************************/ + +// void __set_up_general_state_machine(); +// void __set_up_specific_state_machine(); +// void __set_up_state_machine(); //TODO: revisar +// void __set_up_protections(); +// void __set_up_peripherals(); +// void __set_up_fdcan(); +// void __set_up_uart(); +// void __set_up_ethernet(); +// void __set_up_resets(); +// void __set_up_boots(); +// void __set_up_leds(); + +// } diff --git a/Core/Inc/BLCU/BLCU_Orders/BLCU_PublicFunctions.hpp b/Core/Inc/BLCU/BLCU_Orders/BLCU_PublicFunctions.hpp index 1deb1ed..9c0fb82 100644 --- a/Core/Inc/BLCU/BLCU_Orders/BLCU_PublicFunctions.hpp +++ b/Core/Inc/BLCU/BLCU_Orders/BLCU_PublicFunctions.hpp @@ -4,7 +4,6 @@ namespace BLCU{ void reset_all(){ - printf("Reset all order received\n"); for (auto& [target, reset]: resets) { reset.turn_off(); @@ -19,13 +18,10 @@ namespace BLCU{ } void get_version(){ - printf("Get version order received\n"); if(BLCU::specific_state_machine.current_state != BLCU::SpecificStates::READY){ BLCU::tcp_socket->send_order(nack); - printf("Not able to execute get version order, since BLCU not ready\n"); return; } - printf("Execute get version order\n"); BLCU::specific_state_machine.force_change_state(BOOTING); uint8_t temporal_value = 0; @@ -42,13 +38,10 @@ namespace BLCU{ } void read_program(){ - printf("Read program order received\n"); if(BLCU::specific_state_machine.current_state != BLCU::SpecificStates::READY){ BLCU::tcp_socket->send_order(nack); - printf("Unable to execute read program order, since BLCU not ready\n"); return; } - printf("Execute read program order\n"); BLCU::specific_state_machine.force_change_state(BOOTING); BTFTP::on(BTFTP::Mode::READ); @@ -57,13 +50,10 @@ namespace BLCU{ } void write_program(){ - printf("Write program order received\n"); if(BLCU::specific_state_machine.current_state != BLCU::SpecificStates::READY){ BLCU::tcp_socket->send_order(nack); - printf("Unable to execute write program order, since BLCU not ready\n"); return; } - printf("Execute write program order\n"); BLCU::specific_state_machine.force_change_state(BOOTING); BTFTP::on(BTFTP::Mode::WRITE); @@ -72,13 +62,10 @@ namespace BLCU{ } void erase_program(){ - printf("Erase program order received\n"); if(BLCU::specific_state_machine.current_state != BLCU::SpecificStates::READY){ BLCU::tcp_socket->send_order(nack); - printf("Unable to execute erase program order, since BLCU not ready\n"); return; } - printf("Execute write program order\n"); BLCU::specific_state_machine.force_change_state(BOOTING); if(not FDCB::erase_memory()){ diff --git a/Core/Inc/BLCU/BLCU_SetUps/BLCU_SetUps.hpp b/Core/Inc/BLCU/BLCU_SetUps/BLCU_SetUps.hpp index 164d96b..10c4be7 100644 --- a/Core/Inc/BLCU/BLCU_SetUps/BLCU_SetUps.hpp +++ b/Core/Inc/BLCU/BLCU_SetUps/BLCU_SetUps.hpp @@ -1,58 +1,58 @@ -#pragma once - -#include "BLCU/BLCU_Definition/BLCU_Definition.hpp" - -namespace BLCU{ - - void __set_up_peripherals(){ - BLCU::__set_up_fdcan(); - BLCU::__set_up_ethernet(); - //BLCU::__set_up_resets(); - //BLCU::__set_up_boots(); - //BLCU::__set_up_leds(); - } - - void __set_up_fdcan(){ - BLCU::fdcan = FDCAN::inscribe< - CANBitRatesSpeed::CAN_1_Mbit, - CANFormat::CAN_FDCAN_FORMAT, - CANIdentifier::CAN_29_BIT_IDENTIFIER, - CANMode::CAN_MODE_NORMAL - >(FDCAN::fdcan1); - } - - - void __set_up_ethernet(){ - BLCU::ip = BLCU_IP; - BLCU::mask = BLCU_MASK; - BLCU::gateway = BLCU_GATEWAY; - BLCU::port = BLCU_PORT; - } - - void __set_up_resets(){ - BLCU::resets[VCU] = DigitalOutput(PA12); - BLCU::resets[HVSCU] = DigitalOutput(PG3); - BLCU::resets[BCU] = DigitalOutput(PD11); - BLCU::resets[BMSL] = DigitalOutput(PD9); - BLCU::resets[LCU] = DigitalOutput(PB12); - BLCU::resets[PCU] = DigitalOutput(PG1); - } - - void __set_up_boots(){ - BLCU::boots[VCU] = DigitalOutput(PA11); - BLCU::boots[HVSCU] = DigitalOutput(PG2); - BLCU::boots[BCU] = DigitalOutput(PD10); - BLCU::boots[BMSL] = DigitalOutput(PD8); - BLCU::boots[LCU] = DigitalOutput(PE15); - BLCU::boots[PCU] = DigitalOutput(PE7); - } - - void __set_up_leds(){ - BLCU::LED_OPERATIONAL = DigitalOutput(PG8); - BLCU::LED_FAULT = DigitalOutput(PG7); - BLCU::LED_CAN = DigitalOutput(PG6); - BLCU::LED_FLASH = DigitalOutput(PG5); - BLCU::LED_SLEEP = DigitalOutput(PG4); - } - -} +// #pragma once + +// #include "BLCU/BLCU_Definition/BLCU_Definition.hpp" + +// namespace BLCU{ + +// void __set_up_peripherals(){ +// BLCU::__set_up_fdcan(); +// BLCU::__set_up_ethernet(); +// //BLCU::__set_up_resets(); +// //BLCU::__set_up_boots(); +// //BLCU::__set_up_leds(); +// } + +// void __set_up_fdcan(){ +// BLCU::fdcan = FDCAN::inscribe< +// CANBitRatesSpeed::CAN_1_Mbit, +// CANFormat::CAN_FDCAN_FORMAT, +// CANIdentifier::CAN_29_BIT_IDENTIFIER, +// CANMode::CAN_MODE_NORMAL +// >(FDCAN::fdcan1); +// } + + +// void __set_up_ethernet(){ +// BLCU::ip = BLCU_IP; +// BLCU::mask = BLCU_MASK; +// BLCU::gateway = BLCU_GATEWAY; +// BLCU::port = BLCU_PORT; +// } + +// void __set_up_resets(){ +// BLCU::resets[VCU] = DigitalOutput(PA12); +// BLCU::resets[HVSCU] = DigitalOutput(PG3); +// BLCU::resets[BCU] = DigitalOutput(PD11); +// BLCU::resets[BMSL] = DigitalOutput(PD9); +// BLCU::resets[LCU] = DigitalOutput(PB12); +// BLCU::resets[PCU] = DigitalOutput(PG1); +// } + +// void __set_up_boots(){ +// BLCU::boots[VCU] = DigitalOutput(PA11); +// BLCU::boots[HVSCU] = DigitalOutput(PG2); +// BLCU::boots[BCU] = DigitalOutput(PD10); +// BLCU::boots[BMSL] = DigitalOutput(PD8); +// BLCU::boots[LCU] = DigitalOutput(PE15); +// BLCU::boots[PCU] = DigitalOutput(PE7); +// } + +// void __set_up_leds(){ +// BLCU::LED_OPERATIONAL = DigitalOutput(PG8); +// BLCU::LED_FAULT = DigitalOutput(PG7); +// BLCU::LED_CAN = DigitalOutput(PG6); +// BLCU::LED_FLASH = DigitalOutput(PG5); +// BLCU::LED_SLEEP = DigitalOutput(PG4); +// } + +// } diff --git a/Core/Inc/BLCU/BLCU_StateMachine/BLCU_StateMachine.hpp b/Core/Inc/BLCU/BLCU_StateMachine/BLCU_StateMachine.hpp index e8ec1ff..d15d377 100644 --- a/Core/Inc/BLCU/BLCU_StateMachine/BLCU_StateMachine.hpp +++ b/Core/Inc/BLCU/BLCU_StateMachine/BLCU_StateMachine.hpp @@ -1,111 +1,111 @@ -#pragma once - -#include "BLCU/BLCU_Definition/BLCU_Definition.hpp" - -namespace BLCU{ - void __set_up_state_machine(){ - __set_up_general_state_machine(); - __set_up_specific_state_machine(); - BLCU::general_state_machine.add_state_machine(specific_state_machine, GeneralStates::OPERATIONAL); - - Time::register_low_precision_alarm(1, [&](){ - BLCU::general_state_machine.check_transitions(); - }); - - } - - void __set_up_general_state_machine(){ - BLCU::general_state_machine = {GeneralStates::INITIAL}; - BLCU::general_state_machine.add_state(GeneralStates::OPERATIONAL); - BLCU::general_state_machine.add_state(GeneralStates::FAULT); - - ProtectionManager::link_state_machine(general_state_machine, GeneralStates::FAULT); - ProtectionManager::set_id(Boards::ID::BLCU_VEHICLE); - - //Enter actions - BLCU::general_state_machine.add_enter_action([&](){ - Time::set_timeout(BLCU::max_tcp_connection_timeout, [&](){ - if(not (BLCU::tcp_socket->state == ServerSocket::ServerState::ACCEPTED )){ - tcp_timeout = true; - } - }); - }, GeneralStates::INITIAL); - - BLCU::general_state_machine.add_enter_action([&](){ - LED_FAULT.turn_on(); - }, GeneralStates::FAULT); - - BLCU::general_state_machine.add_enter_action([&](){ - LED_OPERATIONAL.turn_on(); - }, GeneralStates::OPERATIONAL); - - //Exit actions - BLCU::general_state_machine.add_exit_action([&](){ - LED_OPERATIONAL.turn_off(); - }, GeneralStates::INITIAL); - - BLCU::general_state_machine.add_exit_action([&](){ - LED_FAULT.turn_off(); - }, GeneralStates::FAULT); - - BLCU::general_state_machine.add_exit_action([&](){ - LED_OPERATIONAL.turn_off(); - }, GeneralStates::OPERATIONAL); - - //Transitions - BLCU::general_state_machine.add_transition(GeneralStates::INITIAL, GeneralStates::OPERATIONAL, [&](){ - return BLCU::tcp_socket->state == ServerSocket::ServerState::ACCEPTED; - }); - BLCU::general_state_machine.add_transition(GeneralStates::INITIAL, GeneralStates::FAULT, [&](){ - if(BLCU::tcp_timeout) ErrorHandler("TCP connections could not be established in time and timed out"); - return BLCU::tcp_timeout; - }); - BLCU::general_state_machine.add_transition(GeneralStates::OPERATIONAL, GeneralStates::FAULT, [&](){ - if(BLCU::tcp_socket->state!= ServerSocket::ServerState::ACCEPTED){ - ErrorHandler("TCP connections fell"); - return true; - } - return false; - }); - - //Cyclic actions - BLCU::general_state_machine.add_low_precision_cyclic_action([&](){LED_OPERATIONAL.toggle();}, (chrono::milliseconds)150, INITIAL); - - BLCU::general_state_machine.add_low_precision_cyclic_action(ProtectionManager::check_protections, (chrono::milliseconds)1 , OPERATIONAL); - } - - void __set_up_specific_state_machine(){ - BLCU::specific_state_machine = StateMachine(SpecificStates::READY); - BLCU::specific_state_machine.add_state(SpecificStates::BOOTING); - - - //Enter actions - BLCU::specific_state_machine.add_enter_action([&](){ - BLCU::boots[BLCU::orders_data.target].turn_off(); - BLCU::resets[BLCU::orders_data.target].turn_off(); - BLCU::__turn_on_all_boards(); - BLCU::orders_data.clean_data(); - BLCU::LED_OPERATIONAL.turn_on(); - - }, SpecificStates::READY); - - BLCU::specific_state_machine.add_enter_action([&](){ - BLCU::__turn_off_all_boards(); - BLCU::__send_to_bootmode(BLCU::orders_data.target); - }, SpecificStates::BOOTING); - - //Exit actions - BLCU::specific_state_machine.add_exit_action([&](){ - BLCU::LED_OPERATIONAL.turn_off(); - }, SpecificStates::READY); - - BLCU::specific_state_machine.add_exit_action([&](){ - BLCU::LED_CAN.turn_off(); - }, SpecificStates::BOOTING); - - //Cyclic actions - BLCU::general_state_machine.add_low_precision_cyclic_action([&](){LED_CAN.toggle();}, (chrono::milliseconds)75, SpecificStates::BOOTING); - } - -} +// #pragma once + +// #include "BLCU/BLCU_Definition/BLCU_Definition.hpp" + +// namespace BLCU{ +// void __set_up_state_machine(){ +// __set_up_general_state_machine(); +// __set_up_specific_state_machine(); +// BLCU::general_state_machine.add_state_machine(specific_state_machine, GeneralStates::OPERATIONAL); + +// Time::register_low_precision_alarm(1, [&](){ +// BLCU::general_state_machine.check_transitions(); +// }); + +// } + +// // void __set_up_general_state_machine(){ +// // // BLCU::general_state_machine = {GeneralStates::INITIAL}; +// // // BLCU::general_state_machine.add_state(GeneralStates::OPERATIONAL); +// // // BLCU::general_state_machine.add_state(GeneralStates::FAULT); + +// // // ProtectionManager::link_state_machine(general_state_machine, GeneralStates::FAULT); +// // // ProtectionManager::set_id(Boards::ID::BLCU_VEHICLE); + +// // // //Enter actions +// // // BLCU::general_state_machine.add_enter_action([&](){ +// // // Time::set_timeout(BLCU::max_tcp_connection_timeout, [&](){ +// // // if(not (BLCU::tcp_socket->state == ServerSocket::ServerState::ACCEPTED )){ +// // // tcp_timeout = true; +// // // } +// // // }); +// // // }, GeneralStates::INITIAL); + +// // // BLCU::general_state_machine.add_enter_action([&](){ +// // // LED_FAULT.turn_on(); +// // // }, GeneralStates::FAULT); + +// // // BLCU::general_state_machine.add_enter_action([&](){ +// // // LED_OPERATIONAL.turn_on(); +// // // }, GeneralStates::OPERATIONAL); + +// // // //Exit actions +// // // BLCU::general_state_machine.add_exit_action([&](){ +// // // LED_OPERATIONAL.turn_off(); +// // // }, GeneralStates::INITIAL); + +// // // BLCU::general_state_machine.add_exit_action([&](){ +// // // LED_FAULT.turn_off(); +// // // }, GeneralStates::FAULT); + +// // // BLCU::general_state_machine.add_exit_action([&](){ +// // // LED_OPERATIONAL.turn_off(); +// // // }, GeneralStates::OPERATIONAL); + +// // // //Transitions +// // // BLCU::general_state_machine.add_transition(GeneralStates::INITIAL, GeneralStates::OPERATIONAL, [&](){ +// // // return BLCU::tcp_socket->state == ServerSocket::ServerState::ACCEPTED; +// // // }); +// // // BLCU::general_state_machine.add_transition(GeneralStates::INITIAL, GeneralStates::FAULT, [&](){ +// // // if(BLCU::tcp_timeout) ErrorHandler("TCP connections could not be established in time and timed out"); +// // // return BLCU::tcp_timeout; +// // // }); +// // // BLCU::general_state_machine.add_transition(GeneralStates::OPERATIONAL, GeneralStates::FAULT, [&](){ +// // // if(BLCU::tcp_socket->state!= ServerSocket::ServerState::ACCEPTED){ +// // // ErrorHandler("TCP connections fell"); +// // // return true; +// // // } +// // // return false; +// // // }); + +// // // //Cyclic actions +// // // BLCU::general_state_machine.add_low_precision_cyclic_action([&](){LED_OPERATIONAL.toggle();}, (chrono::milliseconds)150, INITIAL); + +// // // // BLCU::general_state_machine.add_low_precision_cyclic_action(ProtectionManager::check_protections, (chrono::milliseconds)1 , OPERATIONAL); +// // } + +// void __set_up_specific_state_machine(){ +// BLCU::specific_state_machine = StateMachine(SpecificStates::READY); +// BLCU::specific_state_machine.add_state(SpecificStates::BOOTING); + + +// //Enter actions +// BLCU::specific_state_machine.add_enter_action([&](){ +// BLCU::boots[BLCU::orders_data.target].turn_off(); +// BLCU::resets[BLCU::orders_data.target].turn_off(); +// BLCU::__turn_on_all_boards(); +// BLCU::orders_data.clean_data(); +// BLCU::LED_OPERATIONAL.turn_on(); + +// }, SpecificStates::READY); + +// BLCU::specific_state_machine.add_enter_action([&](){ +// BLCU::__turn_off_all_boards(); +// BLCU::__send_to_bootmode(BLCU::orders_data.target); +// }, SpecificStates::BOOTING); + +// //Exit actions +// BLCU::specific_state_machine.add_exit_action([&](){ +// BLCU::LED_OPERATIONAL.turn_off(); +// }, SpecificStates::READY); + +// BLCU::specific_state_machine.add_exit_action([&](){ +// BLCU::LED_CAN.turn_off(); +// }, SpecificStates::BOOTING); + +// //Cyclic actions +// BLCU::general_state_machine.add_low_precision_cyclic_action([&](){LED_CAN.toggle();}, (chrono::milliseconds)75, SpecificStates::BOOTING); +// } + +// } diff --git a/Core/Inc/BLCU/TCP.hpp b/Core/Inc/BLCU/TCP.hpp new file mode 100644 index 0000000..54ade11 --- /dev/null +++ b/Core/Inc/BLCU/TCP.hpp @@ -0,0 +1,32 @@ +#include "BLCU.hpp" +#include "ST-LIB.hpp" + + class TCP{ + private: + + BLCU* board; + static HeapOrder* write_program_order; + static HeapOrder* reset_all_order; + // static HeapOrder* get_version_order; + // static HeapOrder* read_program_order; + // static HeapOrder* erase_program_order; + + public: + TCP(BLCU* board); + + static ServerSocket* tcp_socket; + + static HeapOrder ack = { + 3, + }; + + static HeapOrder nack = { + 4, + }; + + }; + + + + + \ No newline at end of file diff --git a/Core/Inc/Code_generation/JSON_ADE b/Core/Inc/Code_generation/JSON_ADE index 73427d4..7e5fcd2 160000 --- a/Core/Inc/Code_generation/JSON_ADE +++ b/Core/Inc/Code_generation/JSON_ADE @@ -1 +1 @@ -Subproject commit 73427d4a6366593b9ffc71e5926782802209a9be +Subproject commit 7e5fcd275db37ebefc7ee9a8c5ee75d329f30998 diff --git a/Core/Src/BLCU/BLCU.cpp b/Core/Src/BLCU/BLCU.cpp new file mode 100644 index 0000000..5c685fe --- /dev/null +++ b/Core/Src/BLCU/BLCU.cpp @@ -0,0 +1,166 @@ +#include "BLCU/BLCU.hpp" + + +BLCU::BLCU(){ +//-------STATE MACHINE------// + setup_state_machine(); + setup_specific_state_machine(); + general_state_machine.add_state_machine(specific_state_machine, GeneralStates::OPERATIONAL); + Time::register_low_precision_alarm(1, [&](){ + general_state_machine.check_transitions(); + }); + +//-------FDCAN------// + fdcan = FDCAN::inscribe< + CANBitRatesSpeed::CAN_1_Mbit, + CANFormat::CAN_FDCAN_FORMAT, + CANIdentifier::CAN_29_BIT_IDENTIFIER, + CANMode::CAN_MODE_NORMAL + >(FDCAN::fdcan1); + +//-------RESETS------// + resets[VCU] = DigitalOutput(PA12); + resets[HVSCU] = DigitalOutput(PG3); + resets[BCU] = DigitalOutput(PD11); + resets[BMSL] = DigitalOutput(PD9); + resets[LCU] = DigitalOutput(PB12); + resets[PCU] = DigitalOutput(PG1); + +//-------BOOTS------// + boots[VCU] = DigitalOutput(PA11); + boots[HVSCU] = DigitalOutput(PG2); + boots[BCU] = DigitalOutput(PD10); + boots[BMSL] = DigitalOutput(PD8); + boots[LCU] = DigitalOutput(PE15); + boots[PCU] = DigitalOutput(PE7); + + + +//-------LEDS------// + LED_OPERATIONAL = DigitalOutput(PG8); + LED_FAULT = DigitalOutput(PG7); + LED_CAN = DigitalOutput(PG6); + LED_FLASH = DigitalOutput(PG5); + LED_SLEEP = DigitalOutput(PG4); + +} + +void BLCU::init(){ + STLIB::start();//TODO + + for (auto& [target, reset_pin]: BLCU::resets){ + reset_pin.turn_on(); + } + + for (auto& [target, boot_pin]: BLCU::boots){ + boot_pin.turn_off(); + } +} + +void BLCU::setup_state_machine(){ + general_state_machine = {GeneralStates::INITIAL}; + general_state_machine.add_state(GeneralStates::OPERATIONAL); + general_state_machine.add_state(GeneralStates::FAULT); + + ProtectionManager::link_state_machine(general_state_machine, GeneralStates::FAULT); + ProtectionManager::set_id(Boards::ID::BLCU_VEHICLE); + + //Enter actions + general_state_machine.add_enter_action([&](){ + Time::set_timeout(max_tcp_connection_timeout, [&](){ + if(not (tcp_socket->state == ServerSocket::ServerState::ACCEPTED )){ + tcp_timeout = true; + } + }); + }, GeneralStates::INITIAL); + + general_state_machine.add_enter_action([&](){ + LED_FAULT.turn_on(); + }, GeneralStates::FAULT); + + general_state_machine.add_enter_action([&](){ + LED_OPERATIONAL.turn_on(); + }, GeneralStates::OPERATIONAL); + BLCU:: + //Exit actions + general_state_machine.add_exit_action([&](){ + LED_OPERATIONAL.turn_off(); + }, GeneralStates::INITIAL); + + general_state_machine.add_exit_action([&](){ + LED_FAULT.turn_off(); + }, GeneralStates::FAULT); + + general_state_machine.add_exit_action([&](){ + LED_OPERATIONAL.turn_off(); + }, GeneralStates::OPERATIONAL); + + //Transitions + general_state_machine.add_transition(GeneralStates::INITIAL, GeneralStates::OPERATIONAL, [&](){ + return tcp_socket->state == ServerSocket::ServerState::ACCEPTED; + }); + general_state_machine.add_transition(GeneralStates::INITIAL, GeneralStates::FAULT, [&](){ + if(tcp_timeout) ErrorHandler("TCP connections could not be established in time and timed out"); + return tcp_timeout; + }); + general_state_machine.add_transition(GeneralStates::OPERATIONAL, GeneralStates::FAULT, [&](){ + if(tcp_socket->state!= ServerSocket::ServerState::ACCEPTED){ + ErrorHandler("TCP connections fell"); + return true; + } + return false; + }); + + //Cyclic actions + general_state_machine.add_low_precision_cyclic_action([&](){LED_OPERATIONAL.toggle();}, (chrono::milliseconds)150, INITIAL); + + // general_state_machine.add_low_precision_cyclic_action(ProtectionManager::check_protections, (chrono::milliseconds)1 , OPERATIONAL); +} + +void BLCU::setup_specific_state_machine(){ + specific_state_machine = StateMachine(SpecificStates::READY); + specific_state_machine.add_state(SpecificStates::BOOTING); + + + //Enter actions + specific_state_machine.add_enter_action([&](){ + // boots[current_target].turn_off(); + // resets[current_target].turn_off(); + stop_booting(); + turn_on_all_boards(); + LED_OPERATIONAL.turn_on(); + + }, SpecificStates::READY); + + specific_state_machine.add_enter_action([&](){ + turn_off_all_boards(); + send_to_bootmode(); + }, SpecificStates::BOOTING); + + //Exit actions + specific_state_machine.add_exit_action([&](){ + LED_OPERATIONAL.turn_off(); + }, SpecificStates::READY); + + specific_state_machine.add_exit_action([&](){ + LED_CAN.turn_off(); + }, SpecificStates::BOOTING); + + //Cyclic actions + general_state_machine.add_low_precision_cyclic_action([&](){LED_CAN.toggle();}, (chrono::milliseconds)75, SpecificStates::BOOTING); + +} + +void BLCU::finish_write_read_order(bool error_ok){ + BTFTP::off(); + + if(not error_ok){ + BLCU::tcp_socket->send_order(nack); + __abort_booting(); + return; + }else{ + BLCU::tcp_socket->send_order(ack); + } + + BLCU::specific_state_machine.force_change_state(SpecificStates::READY); +} \ No newline at end of file diff --git a/Core/Src/BLCU/TCP.cpp b/Core/Src/BLCU/TCP.cpp new file mode 100644 index 0000000..9b42a7a --- /dev/null +++ b/Core/Src/BLCU/TCP.cpp @@ -0,0 +1,22 @@ +#include "BLCU/TCP.hpp" + +TCP::write_program_order = new HeapOrder{ + 700, + BLCU::write_program, + &BLCU::orders_data.target, +}; + +TCP::reset_all_order = new HeapOrder{ + 704, + BLCU::reset_all, + &BLCU::orders_data.target, +}; + +TCP::board = nullptr; +TCP::tcp_socket = nullptr; + +TCP::TCP(BLCU* board){ + this->board = board; + tcp_socket = new ServerSocket(BLCU->ip, BLCU->port,1000,500,10); + +} \ No newline at end of file diff --git a/Core/Src/Runes/generated_metadata.cpp b/Core/Src/Runes/generated_metadata.cpp index ff0ba30..f2739d7 100644 --- a/Core/Src/Runes/generated_metadata.cpp +++ b/Core/Src/Runes/generated_metadata.cpp @@ -5,11 +5,11 @@ extern "C"{ const char DESCRIPTION[255] __attribute__((section(".metadata_pool")))= "****************" // placeholder for beggining - "20250707T124121" // DateTime using ISO-8601 format + "20250712T144940" // DateTime using ISO-8601 format " " // alignment "f73885a9" // STLIB commit "--------" // ADJ commit - "e030ecf7" // Board commit + "8b9785c2" // Board commit // the '=' is used for unparsing ; } \ No newline at end of file From 860e5b5226d2d2253620350a4f846a7eba287f9d Mon Sep 17 00:00:00 2001 From: Cantonplas Date: Sat, 12 Jul 2025 23:17:54 +0200 Subject: [PATCH 03/11] Work nearly done --- Core/Inc/BLCU/BLCU.hpp | 93 ++++++++------- .../BLCU/BLCU_Definition/BLCU_Definition.hpp | 106 ----------------- Core/Inc/BLCU/BLCU_Orders/BLCU_Orders.hpp | 0 .../BLCU_Orders/BLCU_PrivateFunctions.hpp | 49 -------- .../BLCU/BLCU_Orders/BLCU_PublicFunctions.hpp | 82 ------------- .../BLCU_Protections/BLCU_Protections.hpp | 11 -- Core/Inc/BLCU/BLCU_SetUps/BLCU_SetUps.hpp | 58 --------- Core/Inc/BLCU/BLCU_Starts/BLCU_Starts.hpp | 31 ----- .../BLCU_StateMachine/BLCU_StateMachine.hpp | 111 ------------------ Core/Inc/BLCU/Comms.hpp | 38 ++++++ Core/Inc/BLCU/TCP.hpp | 32 ----- Core/Inc/BLCU/Utilities/Target.hpp | 18 --- Core/Inc/BLCU/Utilities/orders_data.hpp | 17 --- Core/Src/BLCU/BLCU.cpp | 75 ++++++++++-- Core/Src/BLCU/Comms.cpp | 81 +++++++++++++ Core/Src/BLCU/TCP.cpp | 22 ---- Core/Src/Runes/generated_metadata.cpp | 4 +- Core/Src/main.cpp | 66 +---------- 18 files changed, 235 insertions(+), 659 deletions(-) delete mode 100644 Core/Inc/BLCU/BLCU_Definition/BLCU_Definition.hpp delete mode 100644 Core/Inc/BLCU/BLCU_Orders/BLCU_Orders.hpp delete mode 100644 Core/Inc/BLCU/BLCU_Orders/BLCU_PrivateFunctions.hpp delete mode 100644 Core/Inc/BLCU/BLCU_Orders/BLCU_PublicFunctions.hpp delete mode 100644 Core/Inc/BLCU/BLCU_Protections/BLCU_Protections.hpp delete mode 100644 Core/Inc/BLCU/BLCU_SetUps/BLCU_SetUps.hpp delete mode 100644 Core/Inc/BLCU/BLCU_Starts/BLCU_Starts.hpp delete mode 100644 Core/Inc/BLCU/BLCU_StateMachine/BLCU_StateMachine.hpp create mode 100644 Core/Inc/BLCU/Comms.hpp delete mode 100644 Core/Inc/BLCU/TCP.hpp delete mode 100644 Core/Inc/BLCU/Utilities/Target.hpp delete mode 100644 Core/Inc/BLCU/Utilities/orders_data.hpp create mode 100644 Core/Src/BLCU/Comms.cpp delete mode 100644 Core/Src/BLCU/TCP.cpp diff --git a/Core/Inc/BLCU/BLCU.hpp b/Core/Inc/BLCU/BLCU.hpp index 6fb129b..878cf58 100644 --- a/Core/Inc/BLCU/BLCU.hpp +++ b/Core/Inc/BLCU/BLCU.hpp @@ -10,6 +10,7 @@ #include "ST-LIB.hpp" #include "FDCBootloader/FDCBootloader.hpp" #include "FDCBootloader/BootloaderTFTP.hpp" +#include "Comms.hpp" #define BLCU_ID ((uint8_t)1) @@ -25,23 +26,8 @@ }; class BLCU{ private: - - enum GeneralStates{ - INITIAL, - OPERATIONAL, - FAULT, - }; - - - enum SpecificStates{ - READY, - BOOTING, - }; static constexpr uint16_t max_tcp_connection_timeout = 30000; - - StateMachine general_state_machine; - StateMachine specific_state_machine; unordered_map resets; unordered_map boots; @@ -53,17 +39,13 @@ DigitalOutput LED_FLASH; DigitalOutput LED_SLEEP; - static constexpr string ip{"192.168.0.27"}; - static constexpr string mask{"255.255.0.0"}; - static constexpr string gateway{"192.168.1.1"}; - static constexpr uint32_t port{50500}; + static ServerSocket* tcp_socket;//Hacer nullptr bool tcp_timeout = false; bool programming_error = false; void finish_write_read_order(bool error_ok); - void abort_booting(); void turn_off_all_boards(); void turn_on_all_boards(); void send_to_bootmode(); @@ -73,44 +55,61 @@ void setup_state_machine(); void setup_specific_state_machine(); + Comms* tcp =nullptr; public: + enum GeneralStates{ + INITIAL, + OPERATIONAL, + FAULT, + }; + + + enum SpecificStates{ + READY, + BOOTING, + }; + BLCU(); void init(); void update(); + void abort_booting(); + void reset_all(); + //PUBLIC VARIABLES: Target current_target; + + StateMachine general_state_machine; + StateMachine specific_state_machine; - //ORDER FUNCTIONS: - void read_program(); - void reset_all(); - void get_version(); - void write_program(); - void erase_program(); + static constexpr string ip{"192.168.0.27"}; + static constexpr string mask{"255.255.0.0"}; + static constexpr string gateway{"192.168.1.1"}; + static constexpr uint32_t port{50500}; + }; - void set_up() - { - BLCU::__set_up_peripherals(); - BLCU::__set_up_state_machine(); - ProtectionManager::link_state_machine(BLCU::general_state_machine, BLCU_ID); - BLCU::__set_up_protections(); - } - - void start(){ - STLIB::start("00:80:e1:06:07:10",ip, mask, gateway, UART::uart2); - BLCU::__tcp_start(); - BTFTP::start(); - - // BLCU::__resets_start(); - // BLCU::__boots_start(); - // BLCU::__leds_start(); - } - - void update(){ - STLIB::update(); - } + // void set_up() + // { + // BLCU::__set_up_peripherals(); + // BLCU::__set_up_state_machine(); + // ProtectionManager::link_state_machine(BLCU::general_state_machine, BLCU_ID); + // BLCU::__set_up_protections(); + // } + + // void start(){ + // STLIB::start("00:80:e1:06:07:10",ip, mask, gateway, UART::uart2); + // BLCU::__tcp_start(); + + // // BLCU::__resets_start(); + // // BLCU::__boots_start(); + // // BLCU::__leds_start(); + // } + + // void update(){ + // STLIB::update(); + // } diff --git a/Core/Inc/BLCU/BLCU_Definition/BLCU_Definition.hpp b/Core/Inc/BLCU/BLCU_Definition/BLCU_Definition.hpp deleted file mode 100644 index 0874e99..0000000 --- a/Core/Inc/BLCU/BLCU_Definition/BLCU_Definition.hpp +++ /dev/null @@ -1,106 +0,0 @@ -// #pragma once - -// #include "ST-LIB.hpp" -// #include "FDCBootloader/FDCBootloader.hpp" -// #include "FDCBootloader/BootloaderTFTP.hpp" -// #include "BLCU/Utilities/orders_data.hpp" - -// #define BLCU_IP "192.168.0.27" -// #define BLCU_MASK "255.255.0.0" -// #define BLCU_GATEWAY "192.168.1.1" -// #define BLCU_PORT ((uint32_t)50500) -// #define BLCU_ID ((uint8_t)1) - -// extern HeapOrder ack; -// extern HeapOrder nack; -// extern HeapOrder get_version_order; - -// namespace BLCU { -// enum GeneralStates{ -// INITIAL, -// OPERATIONAL, -// FAULT, -// }; - - -// enum SpecificStates{ -// READY, -// BOOTING, -// }; - -// static constexpr uint16_t max_tcp_connection_timeout = 30000; - -// StateMachine specific_state_machine; -// StateMachine general_state_machine; - -// unordered_map resets; -// unordered_map boots; - -// uint8_t fdcan; -// uint8_t uart; -// uint8_t initial_led_timer; -// DigitalOutput LED_OPERATIONAL; -// DigitalOutput LED_FAULT; -// DigitalOutput LED_CAN; -// DigitalOutput LED_FLASH; -// DigitalOutput LED_SLEEP; - -// Target current_target; - -// string ip, mask, gateway; -// uint32_t port; -// ServerSocket* tcp_socket; -// bool tcp_timeout = false; - -// bool programming_error = false; - -// orders_data_t orders_data = { -// BLCU::Target::NOTARGET, -// 0 -// }; - -// void set_up(); -// void start(); -// void update(); - -// /***********************************************/ -// // Orders methods -// /***********************************************/ -// void reset_all(); -// void get_version(); -// void read_program(); -// void write_program(); -// void erase_program(); - -// void finish_write_read_order(bool error_ok); -// void __end_booting(); -// void __abort_booting(); -// void __finish_booting(); -// void __send_to_bootmode(const BLCU::Target& target); -// void __turn_off_all_boards(); - -// /***********************************************/ -// // Start methods -// /***********************************************/ -// void __tcp_start(); -// void __resets_start(); -// void __boots_start(); -// void __leds_start(); - -// /***********************************************/ -// // SetUp methods -// /***********************************************/ - -// void __set_up_general_state_machine(); -// void __set_up_specific_state_machine(); -// void __set_up_state_machine(); //TODO: revisar -// void __set_up_protections(); -// void __set_up_peripherals(); -// void __set_up_fdcan(); -// void __set_up_uart(); -// void __set_up_ethernet(); -// void __set_up_resets(); -// void __set_up_boots(); -// void __set_up_leds(); - -// } diff --git a/Core/Inc/BLCU/BLCU_Orders/BLCU_Orders.hpp b/Core/Inc/BLCU/BLCU_Orders/BLCU_Orders.hpp deleted file mode 100644 index e69de29..0000000 diff --git a/Core/Inc/BLCU/BLCU_Orders/BLCU_PrivateFunctions.hpp b/Core/Inc/BLCU/BLCU_Orders/BLCU_PrivateFunctions.hpp deleted file mode 100644 index ff02f90..0000000 --- a/Core/Inc/BLCU/BLCU_Orders/BLCU_PrivateFunctions.hpp +++ /dev/null @@ -1,49 +0,0 @@ -#pragma once - -#include "BLCU/BLCU_Definition/BLCU_Definition.hpp" - -namespace BLCU{ - void finish_write_read_order(bool error_ok){ - BTFTP::off(); - - if(not error_ok){ - BLCU::tcp_socket->send_order(nack); - __abort_booting(); - return; - }else{ - BLCU::tcp_socket->send_order(ack); - } - - BLCU::specific_state_machine.force_change_state(SpecificStates::READY); - } - - - - void __abort_booting(){ - BLCU::orders_data.clean_data(); - BLCU::programming_error = true; - } - - void __send_to_bootmode(const BLCU::Target& target){ - BLCU::boots[target].turn_on(); - BLCU::resets[target].turn_off(); - - BLCU::resets[target].turn_on(); - BLCU::boots[target].turn_off(); - } - - void __turn_off_all_boards(){ - for (auto& [target, reset_pin]: BLCU::resets) - { - reset_pin.turn_off(); - } - } - - void __turn_on_all_boards(){ - for (auto& [target, reset_pin]: BLCU::resets) - { - reset_pin.turn_on(); - } - } - -} diff --git a/Core/Inc/BLCU/BLCU_Orders/BLCU_PublicFunctions.hpp b/Core/Inc/BLCU/BLCU_Orders/BLCU_PublicFunctions.hpp deleted file mode 100644 index 9c0fb82..0000000 --- a/Core/Inc/BLCU/BLCU_Orders/BLCU_PublicFunctions.hpp +++ /dev/null @@ -1,82 +0,0 @@ -#pragma once - -#include "BLCU/BLCU_Definition/BLCU_Definition.hpp" - -namespace BLCU{ - void reset_all(){ - for (auto& [target, reset]: resets) - { - reset.turn_off(); - } - - for (auto& [target, reset]: resets) - { - reset.turn_on(); - } - - HAL_NVIC_SystemReset();//Good bye :) - } - - void get_version(){ - if(BLCU::specific_state_machine.current_state != BLCU::SpecificStates::READY){ - BLCU::tcp_socket->send_order(nack); - return; - } - BLCU::specific_state_machine.force_change_state(BOOTING); - - uint8_t temporal_value = 0; - if (not FDCB::get_version(temporal_value)){ - BLCU::tcp_socket->send_order(nack); - __abort_booting(); - return; - } - - BLCU::orders_data.version = temporal_value; - BLCU::tcp_socket->send_order(ack); - - BLCU::specific_state_machine.force_change_state(SpecificStates::READY); - } - - void read_program(){ - if(BLCU::specific_state_machine.current_state != BLCU::SpecificStates::READY){ - BLCU::tcp_socket->send_order(nack); - return; - } - BLCU::specific_state_machine.force_change_state(BOOTING); - - BTFTP::on(BTFTP::Mode::READ); - - BLCU::tcp_socket->send_order(ack); - } - - void write_program(){ - if(BLCU::specific_state_machine.current_state != BLCU::SpecificStates::READY){ - BLCU::tcp_socket->send_order(nack); - return; - } - BLCU::specific_state_machine.force_change_state(BOOTING); - - BTFTP::on(BTFTP::Mode::WRITE); - - BLCU::tcp_socket->send_order(ack); - } - - void erase_program(){ - if(BLCU::specific_state_machine.current_state != BLCU::SpecificStates::READY){ - BLCU::tcp_socket->send_order(nack); - return; - } - BLCU::specific_state_machine.force_change_state(BOOTING); - - if(not FDCB::erase_memory()){ - BLCU::tcp_socket->send_order(nack); - __abort_booting(); - return; - }else{ - BLCU::tcp_socket->send_order(ack); - } - - BLCU::specific_state_machine.force_change_state(SpecificStates::READY); - } - -} diff --git a/Core/Inc/BLCU/BLCU_Protections/BLCU_Protections.hpp b/Core/Inc/BLCU/BLCU_Protections/BLCU_Protections.hpp deleted file mode 100644 index b8e5b46..0000000 --- a/Core/Inc/BLCU/BLCU_Protections/BLCU_Protections.hpp +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -#include "BLCU/BLCU_Definition/BLCU_Definition.hpp" - -namespace BLCU{ - void __set_up_protections(){ - //ProtectionManager::_add_protection(&BLCU::programming_error, Boundary(0)); - } - - -} diff --git a/Core/Inc/BLCU/BLCU_SetUps/BLCU_SetUps.hpp b/Core/Inc/BLCU/BLCU_SetUps/BLCU_SetUps.hpp deleted file mode 100644 index 10c4be7..0000000 --- a/Core/Inc/BLCU/BLCU_SetUps/BLCU_SetUps.hpp +++ /dev/null @@ -1,58 +0,0 @@ -// #pragma once - -// #include "BLCU/BLCU_Definition/BLCU_Definition.hpp" - -// namespace BLCU{ - -// void __set_up_peripherals(){ -// BLCU::__set_up_fdcan(); -// BLCU::__set_up_ethernet(); -// //BLCU::__set_up_resets(); -// //BLCU::__set_up_boots(); -// //BLCU::__set_up_leds(); -// } - -// void __set_up_fdcan(){ -// BLCU::fdcan = FDCAN::inscribe< -// CANBitRatesSpeed::CAN_1_Mbit, -// CANFormat::CAN_FDCAN_FORMAT, -// CANIdentifier::CAN_29_BIT_IDENTIFIER, -// CANMode::CAN_MODE_NORMAL -// >(FDCAN::fdcan1); -// } - - -// void __set_up_ethernet(){ -// BLCU::ip = BLCU_IP; -// BLCU::mask = BLCU_MASK; -// BLCU::gateway = BLCU_GATEWAY; -// BLCU::port = BLCU_PORT; -// } - -// void __set_up_resets(){ -// BLCU::resets[VCU] = DigitalOutput(PA12); -// BLCU::resets[HVSCU] = DigitalOutput(PG3); -// BLCU::resets[BCU] = DigitalOutput(PD11); -// BLCU::resets[BMSL] = DigitalOutput(PD9); -// BLCU::resets[LCU] = DigitalOutput(PB12); -// BLCU::resets[PCU] = DigitalOutput(PG1); -// } - -// void __set_up_boots(){ -// BLCU::boots[VCU] = DigitalOutput(PA11); -// BLCU::boots[HVSCU] = DigitalOutput(PG2); -// BLCU::boots[BCU] = DigitalOutput(PD10); -// BLCU::boots[BMSL] = DigitalOutput(PD8); -// BLCU::boots[LCU] = DigitalOutput(PE15); -// BLCU::boots[PCU] = DigitalOutput(PE7); -// } - -// void __set_up_leds(){ -// BLCU::LED_OPERATIONAL = DigitalOutput(PG8); -// BLCU::LED_FAULT = DigitalOutput(PG7); -// BLCU::LED_CAN = DigitalOutput(PG6); -// BLCU::LED_FLASH = DigitalOutput(PG5); -// BLCU::LED_SLEEP = DigitalOutput(PG4); -// } - -// } diff --git a/Core/Inc/BLCU/BLCU_Starts/BLCU_Starts.hpp b/Core/Inc/BLCU/BLCU_Starts/BLCU_Starts.hpp deleted file mode 100644 index 9c73115..0000000 --- a/Core/Inc/BLCU/BLCU_Starts/BLCU_Starts.hpp +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once - -#include "BLCU/BLCU_Definition/BLCU_Definition.hpp" - -namespace BLCU{ - void __tcp_start(){ - BLCU::tcp_socket = new ServerSocket(BLCU::ip, BLCU::port,1000,500,10); - } - - void __resets_start(){ - for (auto& [target, reset_pin]: BLCU::resets) - { - reset_pin.turn_on(); - } - } - - void __boots_start(){ - for (auto& [target, boot_pin]: BLCU::boots) - { - boot_pin.turn_off(); - } - } - - void __leds_start(){ - BLCU::LED_OPERATIONAL.turn_on(); - BLCU::LED_FAULT.turn_off(); - BLCU::LED_CAN.turn_off(); - BLCU::LED_FLASH.turn_off(); - BLCU::LED_SLEEP.turn_off(); - } -} diff --git a/Core/Inc/BLCU/BLCU_StateMachine/BLCU_StateMachine.hpp b/Core/Inc/BLCU/BLCU_StateMachine/BLCU_StateMachine.hpp deleted file mode 100644 index d15d377..0000000 --- a/Core/Inc/BLCU/BLCU_StateMachine/BLCU_StateMachine.hpp +++ /dev/null @@ -1,111 +0,0 @@ - -// #pragma once - -// #include "BLCU/BLCU_Definition/BLCU_Definition.hpp" - -// namespace BLCU{ -// void __set_up_state_machine(){ -// __set_up_general_state_machine(); -// __set_up_specific_state_machine(); -// BLCU::general_state_machine.add_state_machine(specific_state_machine, GeneralStates::OPERATIONAL); - -// Time::register_low_precision_alarm(1, [&](){ -// BLCU::general_state_machine.check_transitions(); -// }); - -// } - -// // void __set_up_general_state_machine(){ -// // // BLCU::general_state_machine = {GeneralStates::INITIAL}; -// // // BLCU::general_state_machine.add_state(GeneralStates::OPERATIONAL); -// // // BLCU::general_state_machine.add_state(GeneralStates::FAULT); - -// // // ProtectionManager::link_state_machine(general_state_machine, GeneralStates::FAULT); -// // // ProtectionManager::set_id(Boards::ID::BLCU_VEHICLE); - -// // // //Enter actions -// // // BLCU::general_state_machine.add_enter_action([&](){ -// // // Time::set_timeout(BLCU::max_tcp_connection_timeout, [&](){ -// // // if(not (BLCU::tcp_socket->state == ServerSocket::ServerState::ACCEPTED )){ -// // // tcp_timeout = true; -// // // } -// // // }); -// // // }, GeneralStates::INITIAL); - -// // // BLCU::general_state_machine.add_enter_action([&](){ -// // // LED_FAULT.turn_on(); -// // // }, GeneralStates::FAULT); - -// // // BLCU::general_state_machine.add_enter_action([&](){ -// // // LED_OPERATIONAL.turn_on(); -// // // }, GeneralStates::OPERATIONAL); - -// // // //Exit actions -// // // BLCU::general_state_machine.add_exit_action([&](){ -// // // LED_OPERATIONAL.turn_off(); -// // // }, GeneralStates::INITIAL); - -// // // BLCU::general_state_machine.add_exit_action([&](){ -// // // LED_FAULT.turn_off(); -// // // }, GeneralStates::FAULT); - -// // // BLCU::general_state_machine.add_exit_action([&](){ -// // // LED_OPERATIONAL.turn_off(); -// // // }, GeneralStates::OPERATIONAL); - -// // // //Transitions -// // // BLCU::general_state_machine.add_transition(GeneralStates::INITIAL, GeneralStates::OPERATIONAL, [&](){ -// // // return BLCU::tcp_socket->state == ServerSocket::ServerState::ACCEPTED; -// // // }); -// // // BLCU::general_state_machine.add_transition(GeneralStates::INITIAL, GeneralStates::FAULT, [&](){ -// // // if(BLCU::tcp_timeout) ErrorHandler("TCP connections could not be established in time and timed out"); -// // // return BLCU::tcp_timeout; -// // // }); -// // // BLCU::general_state_machine.add_transition(GeneralStates::OPERATIONAL, GeneralStates::FAULT, [&](){ -// // // if(BLCU::tcp_socket->state!= ServerSocket::ServerState::ACCEPTED){ -// // // ErrorHandler("TCP connections fell"); -// // // return true; -// // // } -// // // return false; -// // // }); - -// // // //Cyclic actions -// // // BLCU::general_state_machine.add_low_precision_cyclic_action([&](){LED_OPERATIONAL.toggle();}, (chrono::milliseconds)150, INITIAL); - -// // // // BLCU::general_state_machine.add_low_precision_cyclic_action(ProtectionManager::check_protections, (chrono::milliseconds)1 , OPERATIONAL); -// // } - -// void __set_up_specific_state_machine(){ -// BLCU::specific_state_machine = StateMachine(SpecificStates::READY); -// BLCU::specific_state_machine.add_state(SpecificStates::BOOTING); - - -// //Enter actions -// BLCU::specific_state_machine.add_enter_action([&](){ -// BLCU::boots[BLCU::orders_data.target].turn_off(); -// BLCU::resets[BLCU::orders_data.target].turn_off(); -// BLCU::__turn_on_all_boards(); -// BLCU::orders_data.clean_data(); -// BLCU::LED_OPERATIONAL.turn_on(); - -// }, SpecificStates::READY); - -// BLCU::specific_state_machine.add_enter_action([&](){ -// BLCU::__turn_off_all_boards(); -// BLCU::__send_to_bootmode(BLCU::orders_data.target); -// }, SpecificStates::BOOTING); - -// //Exit actions -// BLCU::specific_state_machine.add_exit_action([&](){ -// BLCU::LED_OPERATIONAL.turn_off(); -// }, SpecificStates::READY); - -// BLCU::specific_state_machine.add_exit_action([&](){ -// BLCU::LED_CAN.turn_off(); -// }, SpecificStates::BOOTING); - -// //Cyclic actions -// BLCU::general_state_machine.add_low_precision_cyclic_action([&](){LED_CAN.toggle();}, (chrono::milliseconds)75, SpecificStates::BOOTING); -// } - -// } diff --git a/Core/Inc/BLCU/Comms.hpp b/Core/Inc/BLCU/Comms.hpp new file mode 100644 index 0000000..71a8957 --- /dev/null +++ b/Core/Inc/BLCU/Comms.hpp @@ -0,0 +1,38 @@ +#pragma once +#include "ST-LIB.hpp" + +class BLCU; + +class Comms{ +private: + BLCU* board=nullptr; + HeapOrder* write_program_order=nullptr; + HeapOrder* reset_all_order=nullptr; + // static HeapOrder* get_version_order; + // static HeapOrder* read_program_order; + // static HeapOrder* erase_program_order; + +public: + Comms(BLCU* board); + + ServerSocket* tcp_socket=nullptr; + + void read_program(); + void reset_all(); + void get_version(); + void write_program(); + void erase_program(); + + HeapOrder ack = { + 3, + }; + + HeapOrder nack = { + 4, + }; + +}; + + + + diff --git a/Core/Inc/BLCU/TCP.hpp b/Core/Inc/BLCU/TCP.hpp deleted file mode 100644 index 54ade11..0000000 --- a/Core/Inc/BLCU/TCP.hpp +++ /dev/null @@ -1,32 +0,0 @@ -#include "BLCU.hpp" -#include "ST-LIB.hpp" - - class TCP{ - private: - - BLCU* board; - static HeapOrder* write_program_order; - static HeapOrder* reset_all_order; - // static HeapOrder* get_version_order; - // static HeapOrder* read_program_order; - // static HeapOrder* erase_program_order; - - public: - TCP(BLCU* board); - - static ServerSocket* tcp_socket; - - static HeapOrder ack = { - 3, - }; - - static HeapOrder nack = { - 4, - }; - - }; - - - - - \ No newline at end of file diff --git a/Core/Inc/BLCU/Utilities/Target.hpp b/Core/Inc/BLCU/Utilities/Target.hpp deleted file mode 100644 index a0043f0..0000000 --- a/Core/Inc/BLCU/Utilities/Target.hpp +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once -namespace BLCU{ - -/* enumeration of all possible boot targets - */ - - enum Target{ - NOTARGET, - VCU, // 1 - HVSCU, // 2 - BCU, // 3 - BMSL, // 4 - LCU, // 5 - PCU, // 6 - - }; -} - diff --git a/Core/Inc/BLCU/Utilities/orders_data.hpp b/Core/Inc/BLCU/Utilities/orders_data.hpp deleted file mode 100644 index 0f56d8e..0000000 --- a/Core/Inc/BLCU/Utilities/orders_data.hpp +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once -#include "stdint.h" -#include "Target.hpp" - -namespace BLCU{ - typedef struct { - BLCU::Target target; - uint16_t version; - - void clean_data(){ - target = BLCU::Target::NOTARGET; - version = 0; - } - - }orders_data_t; - -} diff --git a/Core/Src/BLCU/BLCU.cpp b/Core/Src/BLCU/BLCU.cpp index 5c685fe..3d6cd83 100644 --- a/Core/Src/BLCU/BLCU.cpp +++ b/Core/Src/BLCU/BLCU.cpp @@ -6,10 +6,11 @@ BLCU::BLCU(){ setup_state_machine(); setup_specific_state_machine(); general_state_machine.add_state_machine(specific_state_machine, GeneralStates::OPERATIONAL); + ProtectionManager::link_state_machine(BLCU::general_state_machine, BLCU_ID); Time::register_low_precision_alarm(1, [&](){ general_state_machine.check_transitions(); }); - + //-------FDCAN------// fdcan = FDCAN::inscribe< CANBitRatesSpeed::CAN_1_Mbit, @@ -46,7 +47,11 @@ BLCU::BLCU(){ } void BLCU::init(){ - STLIB::start();//TODO + STLIB::start("00:80:e1:06:07:10",ip, mask, gateway, UART::uart2); + + tcp = new Comms(this); + BTFTP::start(); + for (auto& [target, reset_pin]: BLCU::resets){ reset_pin.turn_on(); @@ -57,6 +62,10 @@ void BLCU::init(){ } } +void BLCU::update(){ + STLIB::update(); +} + void BLCU::setup_state_machine(){ general_state_machine = {GeneralStates::INITIAL}; general_state_machine.add_state(GeneralStates::OPERATIONAL); @@ -68,7 +77,7 @@ void BLCU::setup_state_machine(){ //Enter actions general_state_machine.add_enter_action([&](){ Time::set_timeout(max_tcp_connection_timeout, [&](){ - if(not (tcp_socket->state == ServerSocket::ServerState::ACCEPTED )){ + if(not (tcp->tcp_socket->state == ServerSocket::ServerState::ACCEPTED )){ tcp_timeout = true; } }); @@ -97,14 +106,14 @@ void BLCU::setup_state_machine(){ //Transitions general_state_machine.add_transition(GeneralStates::INITIAL, GeneralStates::OPERATIONAL, [&](){ - return tcp_socket->state == ServerSocket::ServerState::ACCEPTED; + return tcp->tcp_socket->state == ServerSocket::ServerState::ACCEPTED; }); general_state_machine.add_transition(GeneralStates::INITIAL, GeneralStates::FAULT, [&](){ if(tcp_timeout) ErrorHandler("TCP connections could not be established in time and timed out"); return tcp_timeout; }); general_state_machine.add_transition(GeneralStates::OPERATIONAL, GeneralStates::FAULT, [&](){ - if(tcp_socket->state!= ServerSocket::ServerState::ACCEPTED){ + if(tcp->tcp_socket->state!= ServerSocket::ServerState::ACCEPTED){ ErrorHandler("TCP connections fell"); return true; } @@ -124,8 +133,6 @@ void BLCU::setup_specific_state_machine(){ //Enter actions specific_state_machine.add_enter_action([&](){ - // boots[current_target].turn_off(); - // resets[current_target].turn_off(); stop_booting(); turn_on_all_boards(); LED_OPERATIONAL.turn_on(); @@ -155,12 +162,58 @@ void BLCU::finish_write_read_order(bool error_ok){ BTFTP::off(); if(not error_ok){ - BLCU::tcp_socket->send_order(nack); - __abort_booting(); + tcp->tcp_socket->send_order(tcp->nack); + abort_booting(); return; }else{ - BLCU::tcp_socket->send_order(ack); + tcp->tcp_socket->send_order(tcp->ack); + } + + specific_state_machine.force_change_state(SpecificStates::READY); +} + +void BLCU::abort_booting(){ + BLCU::programming_error = true; +} + +void BLCU::send_to_bootmode(){ + boots[current_target].turn_on(); + resets[current_target].turn_off(); + //igual hace falta meter un delay(por probar) + resets[current_target].turn_on(); + boots[current_target].turn_off(); +} + +void BLCU::turn_off_all_boards(){ + for (auto& [target, reset_pin]: resets) + { + reset_pin.turn_off(); + } +} + +void BLCU::turn_on_all_boards(){ + for (auto& [target, reset_pin]: resets) + { + reset_pin.turn_on(); + } +} + +void BLCU::stop_booting(){ + boots[current_target].turn_off(); + resets[current_target].turn_off(); + +} + +void BLCU::reset_all(){ + for (auto& [target, reset]: resets) + { + reset.turn_off(); + } + + for (auto& [target, reset]: resets) + { + reset.turn_on(); } - BLCU::specific_state_machine.force_change_state(SpecificStates::READY); + HAL_NVIC_SystemReset();//Good bye :) } \ No newline at end of file diff --git a/Core/Src/BLCU/Comms.cpp b/Core/Src/BLCU/Comms.cpp new file mode 100644 index 0000000..bc1f7c8 --- /dev/null +++ b/Core/Src/BLCU/Comms.cpp @@ -0,0 +1,81 @@ +#include "BLCU/Comms.hpp" +#include "BLCU/BLCU.hpp" + + + +Comms::Comms(BLCU* board): board(board){ + tcp_socket = new ServerSocket(board->ip, board->port,1000,500,10); + write_program_order = new HeapOrder{ + 700, + write_program, + &board->current_target, + }; + + reset_all_order = new HeapOrder{ + 704, + reset_all + }; + +} + +void Comms::read_program(){ + if(board->specific_state_machine.current_state != board->SpecificStates::READY){ + tcp_socket->send_order(nack); + return; + } + board->specific_state_machine.force_change_state(board->SpecificStates::BOOTING); + + BTFTP::on(BTFTP::Mode::READ); + + tcp_socket->send_order(ack); +} + +void Comms::write_program(){ + if(board->specific_state_machine.current_state != board->SpecificStates::READY){ + tcp_socket->send_order(nack); + return; + } + board->specific_state_machine.force_change_state(board->SpecificStates::BOOTING); + + BTFTP::on(BTFTP::Mode::WRITE); + + tcp_socket->send_order(ack); +} + +void Comms::erase_program(){ + if(board->specific_state_machine.current_state != board->SpecificStates::READY){ + tcp_socket->send_order(nack); + return; + } + board->specific_state_machine.force_change_state(board->SpecificStates::BOOTING); + + if(not FDCB::erase_memory()){ + tcp_socket->send_order(nack); + board->abort_booting(); + return; + }else{ + tcp_socket->send_order(ack); + } + + board->specific_state_machine.force_change_state(board->SpecificStates::READY); +} + +void Comms::get_version(){ + if(board->specific_state_machine.current_state != board->SpecificStates::READY){ + tcp_socket->send_order(nack); + return; + } + board->specific_state_machine.force_change_state(board->SpecificStates::BOOTING); + + uint8_t temporal_value = 0; + if (not FDCB::get_version(temporal_value)){ + tcp_socket->send_order(nack); + board->abort_booting(); + return; + } + + tcp_socket->send_order(ack); + + board->specific_state_machine.force_change_state(board->SpecificStates::READY); +} + diff --git a/Core/Src/BLCU/TCP.cpp b/Core/Src/BLCU/TCP.cpp deleted file mode 100644 index 9b42a7a..0000000 --- a/Core/Src/BLCU/TCP.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include "BLCU/TCP.hpp" - -TCP::write_program_order = new HeapOrder{ - 700, - BLCU::write_program, - &BLCU::orders_data.target, -}; - -TCP::reset_all_order = new HeapOrder{ - 704, - BLCU::reset_all, - &BLCU::orders_data.target, -}; - -TCP::board = nullptr; -TCP::tcp_socket = nullptr; - -TCP::TCP(BLCU* board){ - this->board = board; - tcp_socket = new ServerSocket(BLCU->ip, BLCU->port,1000,500,10); - -} \ No newline at end of file diff --git a/Core/Src/Runes/generated_metadata.cpp b/Core/Src/Runes/generated_metadata.cpp index f2739d7..084d4e8 100644 --- a/Core/Src/Runes/generated_metadata.cpp +++ b/Core/Src/Runes/generated_metadata.cpp @@ -5,11 +5,11 @@ extern "C"{ const char DESCRIPTION[255] __attribute__((section(".metadata_pool")))= "****************" // placeholder for beggining - "20250712T144940" // DateTime using ISO-8601 format + "20250712T231721" // DateTime using ISO-8601 format " " // alignment "f73885a9" // STLIB commit "--------" // ADJ commit - "8b9785c2" // Board commit + "726aac5a" // Board commit // the '=' is used for unparsing ; } \ No newline at end of file diff --git a/Core/Src/main.cpp b/Core/Src/main.cpp index 2ff4d60..3541c35 100644 --- a/Core/Src/main.cpp +++ b/Core/Src/main.cpp @@ -8,79 +8,21 @@ #include "BLCU/BLCU.hpp" -HeapOrder ack = { - 3, -}; - - -HeapOrder nack = { - 4, -}; - -HeapOrder get_version_order = { - 703, - BLCU::get_version, - &BLCU::orders_data.target, - &BLCU::orders_data.version, -}; - - int main(void) { #ifdef SIM_ON SharedMemory::start(); #endif - HeapOrder write_program_order = { - 700, - BLCU::write_program, - &BLCU::orders_data.target, - }; - - // HeapOrder read_program_order = { - // 701, - // BLCU::read_program, - // &BLCU::orders_data.target, - // }; - // HeapOrder erase_program_order = { - // 702, - // BLCU::erase_program, - // &BLCU::orders_data.target, - // }; - - - HeapOrder reset_all_order = { - 704, - BLCU::reset_all, - &BLCU::orders_data.target, - }; - DigitalOutput BootPCU(PE7); - DigitalOutput NReset(PG1); - - BLCU::set_up(); - BLCU::start(); - // RCC_ClocksTypeDef RCC_Clocks; - // HAL_RCC_GetClockConfig() + - // BootPCU.turn_off(); - // HAL_Delay(100); - // NReset.turn_on(); - // BLCU::orders_data.target = BLCU::Target::PCU; - // BLCU::write_program(); - - + BLCU board; + board.init(); - // BootPCU.turn_on(); - // NReset.turn_off(); - // HAL_Delay(100); - // NReset.turn_on(); - // HAL_Delay(100); - // BootPCU.turn_off(); - while (1) { - BLCU::update(); + board.update(); } } From 2ed77dfe160fd17294aea75a06c63764721f85ce Mon Sep 17 00:00:00 2001 From: Cantonplas Date: Sat, 12 Jul 2025 23:31:58 +0200 Subject: [PATCH 04/11] It compiles, testing required --- Core/Inc/BLCU/Comms.hpp | 3 +++ Core/Src/BLCU/Comms.cpp | 21 +++++++++++---------- Core/Src/Runes/generated_metadata.cpp | 4 ++-- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/Core/Inc/BLCU/Comms.hpp b/Core/Inc/BLCU/Comms.hpp index 71a8957..56db7e7 100644 --- a/Core/Inc/BLCU/Comms.hpp +++ b/Core/Inc/BLCU/Comms.hpp @@ -5,6 +5,9 @@ class BLCU; class Comms{ private: + static Comms* self; + static void cb_write_program(); + static void cb_reset_all(); BLCU* board=nullptr; HeapOrder* write_program_order=nullptr; HeapOrder* reset_all_order=nullptr; diff --git a/Core/Src/BLCU/Comms.cpp b/Core/Src/BLCU/Comms.cpp index bc1f7c8..85ca350 100644 --- a/Core/Src/BLCU/Comms.cpp +++ b/Core/Src/BLCU/Comms.cpp @@ -2,22 +2,19 @@ #include "BLCU/BLCU.hpp" +Comms* Comms::self = nullptr; Comms::Comms(BLCU* board): board(board){ + self = this; tcp_socket = new ServerSocket(board->ip, board->port,1000,500,10); - write_program_order = new HeapOrder{ - 700, - write_program, - &board->current_target, - }; - - reset_all_order = new HeapOrder{ - 704, - reset_all - }; + write_program_order = new HeapOrder(700, cb_write_program, &board->current_target); + reset_all_order = new HeapOrder(704, cb_reset_all); } +void Comms::cb_write_program() { self->write_program(); } +void Comms::cb_reset_all() { self->reset_all(); } + void Comms::read_program(){ if(board->specific_state_machine.current_state != board->SpecificStates::READY){ tcp_socket->send_order(nack); @@ -79,3 +76,7 @@ void Comms::get_version(){ board->specific_state_machine.force_change_state(board->SpecificStates::READY); } +void Comms::reset_all() { + board->reset_all(); +} + diff --git a/Core/Src/Runes/generated_metadata.cpp b/Core/Src/Runes/generated_metadata.cpp index 084d4e8..9eb34a0 100644 --- a/Core/Src/Runes/generated_metadata.cpp +++ b/Core/Src/Runes/generated_metadata.cpp @@ -5,11 +5,11 @@ extern "C"{ const char DESCRIPTION[255] __attribute__((section(".metadata_pool")))= "****************" // placeholder for beggining - "20250712T231721" // DateTime using ISO-8601 format + "20250712T233133" // DateTime using ISO-8601 format " " // alignment "f73885a9" // STLIB commit "--------" // ADJ commit - "726aac5a" // Board commit + "860e5b52" // Board commit // the '=' is used for unparsing ; } \ No newline at end of file From 49ec81caad37c0e723055a3da9b2975eb4c2697d Mon Sep 17 00:00:00 2001 From: Cantonplas Date: Sun, 13 Jul 2025 17:54:12 +0200 Subject: [PATCH 05/11] Cosas --- Core/Inc/BLCU/BLCU.hpp | 21 ---------- Core/Src/FDCBootloader/BootloaderTFTP.cpp | 48 +++++++++++------------ Core/Src/Runes/generated_metadata.cpp | 4 +- Core/Src/main.cpp | 6 +-- 4 files changed, 27 insertions(+), 52 deletions(-) diff --git a/Core/Inc/BLCU/BLCU.hpp b/Core/Inc/BLCU/BLCU.hpp index 878cf58..2e6762e 100644 --- a/Core/Inc/BLCU/BLCU.hpp +++ b/Core/Inc/BLCU/BLCU.hpp @@ -92,24 +92,3 @@ }; - // void set_up() - // { - // BLCU::__set_up_peripherals(); - // BLCU::__set_up_state_machine(); - // ProtectionManager::link_state_machine(BLCU::general_state_machine, BLCU_ID); - // BLCU::__set_up_protections(); - // } - - // void start(){ - // STLIB::start("00:80:e1:06:07:10",ip, mask, gateway, UART::uart2); - // BLCU::__tcp_start(); - - // // BLCU::__resets_start(); - // // BLCU::__boots_start(); - // // BLCU::__leds_start(); - // } - - // void update(){ - // STLIB::update(); - // } - diff --git a/Core/Src/FDCBootloader/BootloaderTFTP.cpp b/Core/Src/FDCBootloader/BootloaderTFTP.cpp index fc98ba6..c0b5114 100644 --- a/Core/Src/FDCBootloader/BootloaderTFTP.cpp +++ b/Core/Src/FDCBootloader/BootloaderTFTP.cpp @@ -70,22 +70,22 @@ void* BTFTP::open(const char* fname, const char* mode, u8_t write){ } printf("File opened in %s mode.\n", accepted_mode); - uint8_t version = 0x0; - - if (not FDCB::get_version(version)){ - //TODO: WARNING: Bootloader not respondig, unable to start read/write operation! - ErrorHandler("Bootloader not respondig, unable to start read/write operation!"); - BLCU::finish_write_read_order(false); - return nullptr; - } - - if (version != FDCB_CURRENT_VERSION) { - //TODO: WARNING? - ErrorHandler("Mismatch in bootloader version, current version in host: 0x%X in target: 0x%X.", FDCB_CURRENT_VERSION, version); - // ErrorHandler("Mismatch in bootloader version, current version in host: 0x%X in target: 0x%X.", FDCB_CURRENT_VERSION, version); - BLCU::finish_write_read_order(false); - return nullptr; - } + // uint8_t version = 0x0; + + // if (not FDCB::get_version(version)){ + // //TODO: WARNING: Bootloader not respondig, unable to start read/write operation! + // ErrorHandler("Bootloader not respondig, unable to start read/write operation!"); + // BLCU::finish_write_read_order(false); + // return nullptr; + // } + + // if (version != FDCB_CURRENT_VERSION) { + // //TODO: WARNING? + // ErrorHandler("Mismatch in bootloader version, current version in host: 0x%X in target: 0x%X.", FDCB_CURRENT_VERSION, version); + // // ErrorHandler("Mismatch in bootloader version, current version in host: 0x%X in target: 0x%X.", FDCB_CURRENT_VERSION, version); + // BLCU::finish_write_read_order(false); + // return nullptr; + // } uint32_t address = FLASH_SECTOR0_START_ADDRESS; BTFTP::BHandle* handle = new BTFTP::BHandle(string(fname), string(mode), write, address); @@ -147,10 +147,10 @@ int BTFTP::write(void* handle, struct pbuf* p){ if (btftp_handle->current_sector > 5) { return 1; }else{ - if (not FDCB::write_memory(btftp_handle->current_sector, btftp_handle->file->payload,(btftp_handle->file->pointer))) { - error_ok = false; - return -1; - } + // if (not FDCB::write_memory(btftp_handle->current_sector, btftp_handle->file->payload,(btftp_handle->file->pointer))) { + // error_ok = false; + // return -1; + // } btftp_handle->file->pointer = 0; btftp_handle->current_sector++; } @@ -165,10 +165,10 @@ int BTFTP::write(void* handle, struct pbuf* p){ btftp_handle->file->payload[btftp_handle->file->pointer] = 255; btftp_handle->file->pointer++; } - if (not FDCB::write_memory(btftp_handle->current_sector, btftp_handle->file->payload,btftp_handle->file->pointer)) { - error_ok = false; - return -1; - } + // if (not FDCB::write_memory(btftp_handle->current_sector, btftp_handle->file->payload,btftp_handle->file->pointer)) { + // error_ok = false; + // return -1; + // } //Deberia de cambiar de estado aqui, para quitar de boot a la otra pagina return 0; }else{ diff --git a/Core/Src/Runes/generated_metadata.cpp b/Core/Src/Runes/generated_metadata.cpp index 9eb34a0..c1d7458 100644 --- a/Core/Src/Runes/generated_metadata.cpp +++ b/Core/Src/Runes/generated_metadata.cpp @@ -5,11 +5,11 @@ extern "C"{ const char DESCRIPTION[255] __attribute__((section(".metadata_pool")))= "****************" // placeholder for beggining - "20250712T233133" // DateTime using ISO-8601 format + "20250713T175251" // DateTime using ISO-8601 format " " // alignment "f73885a9" // STLIB commit "--------" // ADJ commit - "860e5b52" // Board commit + "2ed77dfe" // Board commit // the '=' is used for unparsing ; } \ No newline at end of file diff --git a/Core/Src/main.cpp b/Core/Src/main.cpp index 3541c35..ea495eb 100644 --- a/Core/Src/main.cpp +++ b/Core/Src/main.cpp @@ -13,11 +13,7 @@ int main(void) { SharedMemory::start(); #endif - - - - - BLCU board; + BLCU board{}; board.init(); From 0f3bf1989b79f25e3786fab91531269a027721d2 Mon Sep 17 00:00:00 2001 From: Cantonplas Date: Wed, 16 Jul 2025 12:44:21 +0200 Subject: [PATCH 06/11] TFTP working, now need to check CAN --- Core/Inc/BLCU/BLCU.hpp | 9 ++++- Core/Inc/BLCU/Comms.hpp | 6 ++- Core/Src/BLCU/BLCU.cpp | 38 +++++++++++------- Core/Src/BLCU/Comms.cpp | 35 ++++++++++------ Core/Src/FDCBootloader/BootloaderTFTP.cpp | 49 +++++++++-------------- Core/Src/Runes/generated_metadata.cpp | 4 +- 6 files changed, 79 insertions(+), 62 deletions(-) diff --git a/Core/Inc/BLCU/BLCU.hpp b/Core/Inc/BLCU/BLCU.hpp index 2e6762e..5b2206f 100644 --- a/Core/Inc/BLCU/BLCU.hpp +++ b/Core/Inc/BLCU/BLCU.hpp @@ -45,6 +45,8 @@ bool programming_error = false; + + void finish_write_read_order(bool error_ok); void turn_off_all_boards(); void turn_on_all_boards(); @@ -81,8 +83,10 @@ //PUBLIC VARIABLES: Target current_target; - StateMachine general_state_machine; - StateMachine specific_state_machine; + bool ready_flag = false; + bool booting_flag = false; + StateMachine general_state_machine{ GeneralStates::INITIAL }; + StateMachine specific_state_machine{ SpecificStates::READY }; static constexpr string ip{"192.168.0.27"}; static constexpr string mask{"255.255.0.0"}; @@ -92,3 +96,4 @@ }; + diff --git a/Core/Inc/BLCU/Comms.hpp b/Core/Inc/BLCU/Comms.hpp index 56db7e7..cf3b604 100644 --- a/Core/Inc/BLCU/Comms.hpp +++ b/Core/Inc/BLCU/Comms.hpp @@ -8,11 +8,12 @@ class Comms{ static Comms* self; static void cb_write_program(); static void cb_reset_all(); + static void cb_read_program(); BLCU* board=nullptr; HeapOrder* write_program_order=nullptr; HeapOrder* reset_all_order=nullptr; // static HeapOrder* get_version_order; - // static HeapOrder* read_program_order; + HeapOrder* read_program_order; // static HeapOrder* erase_program_order; public: @@ -25,6 +26,9 @@ class Comms{ void get_version(); void write_program(); void erase_program(); + + void send_ack(); + void send_nack(); HeapOrder ack = { 3, diff --git a/Core/Src/BLCU/BLCU.cpp b/Core/Src/BLCU/BLCU.cpp index 3d6cd83..0938c87 100644 --- a/Core/Src/BLCU/BLCU.cpp +++ b/Core/Src/BLCU/BLCU.cpp @@ -1,15 +1,13 @@ #include "BLCU/BLCU.hpp" -BLCU::BLCU(){ +BLCU::BLCU(): general_state_machine(GeneralStates::INITIAL), +specific_state_machine(SpecificStates::READY){ //-------STATE MACHINE------// setup_state_machine(); setup_specific_state_machine(); general_state_machine.add_state_machine(specific_state_machine, GeneralStates::OPERATIONAL); ProtectionManager::link_state_machine(BLCU::general_state_machine, BLCU_ID); - Time::register_low_precision_alarm(1, [&](){ - general_state_machine.check_transitions(); - }); //-------FDCAN------// fdcan = FDCAN::inscribe< @@ -52,6 +50,10 @@ void BLCU::init(){ tcp = new Comms(this); BTFTP::start(); + Time::register_low_precision_alarm(1, [&](){ + general_state_machine.check_transitions(); + }); + for (auto& [target, reset_pin]: BLCU::resets){ reset_pin.turn_on(); @@ -67,7 +69,6 @@ void BLCU::update(){ } void BLCU::setup_state_machine(){ - general_state_machine = {GeneralStates::INITIAL}; general_state_machine.add_state(GeneralStates::OPERATIONAL); general_state_machine.add_state(GeneralStates::FAULT); @@ -127,7 +128,6 @@ void BLCU::setup_state_machine(){ } void BLCU::setup_specific_state_machine(){ - specific_state_machine = StateMachine(SpecificStates::READY); specific_state_machine.add_state(SpecificStates::BOOTING); @@ -153,23 +153,31 @@ void BLCU::setup_specific_state_machine(){ LED_CAN.turn_off(); }, SpecificStates::BOOTING); + specific_state_machine.add_transition(SpecificStates::READY, SpecificStates::BOOTING,[&](){ + return ready_flag; + }); + + specific_state_machine.add_transition(SpecificStates::BOOTING, SpecificStates::READY,[&](){ + return !ready_flag; + }); + //Cyclic actions general_state_machine.add_low_precision_cyclic_action([&](){LED_CAN.toggle();}, (chrono::milliseconds)75, SpecificStates::BOOTING); } -void BLCU::finish_write_read_order(bool error_ok){ +void BLCU::finish_write_read_order(bool error_okey){ BTFTP::off(); + ready_flag = true; - if(not error_ok){ - tcp->tcp_socket->send_order(tcp->nack); - abort_booting(); - return; - }else{ - tcp->tcp_socket->send_order(tcp->ack); - } + // if(error_okey==false){ + // tcp->tcp_socket->send_order(tcp->nack); + // abort_booting(); + // return; + // }else{ + // tcp->send_ack(); + // } - specific_state_machine.force_change_state(SpecificStates::READY); } void BLCU::abort_booting(){ diff --git a/Core/Src/BLCU/Comms.cpp b/Core/Src/BLCU/Comms.cpp index 85ca350..448a928 100644 --- a/Core/Src/BLCU/Comms.cpp +++ b/Core/Src/BLCU/Comms.cpp @@ -6,52 +6,61 @@ Comms* Comms::self = nullptr; Comms::Comms(BLCU* board): board(board){ self = this; - tcp_socket = new ServerSocket(board->ip, board->port,1000,500,10); + tcp_socket = new ServerSocket(board->ip, board->port); write_program_order = new HeapOrder(700, cb_write_program, &board->current_target); - reset_all_order = new HeapOrder(704, cb_reset_all); + reset_all_order = new HeapOrder(701, cb_reset_all); + read_program_order = new HeapOrder(702, cb_read_program, &board->current_target); } void Comms::cb_write_program() { self->write_program(); } void Comms::cb_reset_all() { self->reset_all(); } +void Comms::cb_read_program() { self->read_program(); } +void Comms::send_ack() { + tcp_socket->send_order(ack); +} + +void Comms::send_nack() { + tcp_socket->send_order(nack); +} void Comms::read_program(){ if(board->specific_state_machine.current_state != board->SpecificStates::READY){ - tcp_socket->send_order(nack); + send_nack(); return; } board->specific_state_machine.force_change_state(board->SpecificStates::BOOTING); BTFTP::on(BTFTP::Mode::READ); - tcp_socket->send_order(ack); + send_ack(); } void Comms::write_program(){ if(board->specific_state_machine.current_state != board->SpecificStates::READY){ - tcp_socket->send_order(nack); + send_nack(); return; } - board->specific_state_machine.force_change_state(board->SpecificStates::BOOTING); + board->booting_flag = false; BTFTP::on(BTFTP::Mode::WRITE); - tcp_socket->send_order(ack); + send_ack(); } void Comms::erase_program(){ if(board->specific_state_machine.current_state != board->SpecificStates::READY){ - tcp_socket->send_order(nack); + send_nack(); return; } board->specific_state_machine.force_change_state(board->SpecificStates::BOOTING); if(not FDCB::erase_memory()){ - tcp_socket->send_order(nack); + send_nack(); board->abort_booting(); return; }else{ - tcp_socket->send_order(ack); + send_ack(); } board->specific_state_machine.force_change_state(board->SpecificStates::READY); @@ -59,19 +68,19 @@ void Comms::erase_program(){ void Comms::get_version(){ if(board->specific_state_machine.current_state != board->SpecificStates::READY){ - tcp_socket->send_order(nack); + send_ack(); return; } board->specific_state_machine.force_change_state(board->SpecificStates::BOOTING); uint8_t temporal_value = 0; if (not FDCB::get_version(temporal_value)){ - tcp_socket->send_order(nack); + send_nack(); board->abort_booting(); return; } - tcp_socket->send_order(ack); + send_ack(); board->specific_state_machine.force_change_state(board->SpecificStates::READY); } diff --git a/Core/Src/FDCBootloader/BootloaderTFTP.cpp b/Core/Src/FDCBootloader/BootloaderTFTP.cpp index c0b5114..5a55bb7 100644 --- a/Core/Src/FDCBootloader/BootloaderTFTP.cpp +++ b/Core/Src/FDCBootloader/BootloaderTFTP.cpp @@ -36,9 +36,6 @@ void BTFTP::start(){ const tftp_context* context = new tftp_context(&BTFTP::open, &BTFTP::close, &BTFTP::read, &BTFTP::write, &BTFTP::re); err_t error = tftp_init(context); - //TODO: estaria bien comprobar antes que no esta ya ready - // ya sea aqui o denstro del set_up - if (error != ERR_OK) { ErrorHandler("Unable to start TFTP server, error code: %lu.", error); @@ -53,7 +50,6 @@ void BTFTP::start(){ BTFTP::on(BTFTP::Mode::WRITE); //TODO: Remove debug print - printf("Servidor tftp inciado con éxito! Error code: %u\n", error); } //Private: @@ -68,24 +64,20 @@ void* BTFTP::open(const char* fname, const char* mode, u8_t write){ BLCU::finish_write_read_order(false); return nullptr; } - printf("File opened in %s mode.\n", accepted_mode); - // uint8_t version = 0x0; + uint8_t version = 0x0; - // if (not FDCB::get_version(version)){ - // //TODO: WARNING: Bootloader not respondig, unable to start read/write operation! - // ErrorHandler("Bootloader not respondig, unable to start read/write operation!"); - // BLCU::finish_write_read_order(false); - // return nullptr; - // } + if (not FDCB::get_version(version)){ + ErrorHandler("Bootloader not respondig, unable to start read/write operation!"); + BLCU::finish_write_read_order(false); + return nullptr; + } - // if (version != FDCB_CURRENT_VERSION) { - // //TODO: WARNING? - // ErrorHandler("Mismatch in bootloader version, current version in host: 0x%X in target: 0x%X.", FDCB_CURRENT_VERSION, version); - // // ErrorHandler("Mismatch in bootloader version, current version in host: 0x%X in target: 0x%X.", FDCB_CURRENT_VERSION, version); - // BLCU::finish_write_read_order(false); - // return nullptr; - // } + if (version != FDCB_CURRENT_VERSION) { + ErrorHandler("Mismatch in bootloader version, current version in host: 0x%X in target: 0x%X.", FDCB_CURRENT_VERSION, version); + BLCU::finish_write_read_order(false); + return nullptr; + } uint32_t address = FLASH_SECTOR0_START_ADDRESS; BTFTP::BHandle* handle = new BTFTP::BHandle(string(fname), string(mode), write, address); @@ -103,7 +95,7 @@ void* BTFTP::open(const char* fname, const char* mode, u8_t write){ } void BTFTP::close(void* handle){ - free(handle); + delete static_cast(handle); BLCU::finish_write_read_order(error_ok); } @@ -147,10 +139,10 @@ int BTFTP::write(void* handle, struct pbuf* p){ if (btftp_handle->current_sector > 5) { return 1; }else{ - // if (not FDCB::write_memory(btftp_handle->current_sector, btftp_handle->file->payload,(btftp_handle->file->pointer))) { - // error_ok = false; - // return -1; - // } + if (not FDCB::write_memory(btftp_handle->current_sector, btftp_handle->file->payload,(btftp_handle->file->pointer))) { + error_ok = false; + return -1; + } btftp_handle->file->pointer = 0; btftp_handle->current_sector++; } @@ -165,11 +157,10 @@ int BTFTP::write(void* handle, struct pbuf* p){ btftp_handle->file->payload[btftp_handle->file->pointer] = 255; btftp_handle->file->pointer++; } - // if (not FDCB::write_memory(btftp_handle->current_sector, btftp_handle->file->payload,btftp_handle->file->pointer)) { - // error_ok = false; - // return -1; - // } - //Deberia de cambiar de estado aqui, para quitar de boot a la otra pagina + if (not FDCB::write_memory(btftp_handle->current_sector, btftp_handle->file->payload,btftp_handle->file->pointer)) { + error_ok = false; + return -1; + } return 0; }else{ btftp_handle->file->pointer += TFTP_MAX_DATA_SIZE; diff --git a/Core/Src/Runes/generated_metadata.cpp b/Core/Src/Runes/generated_metadata.cpp index c1d7458..8ac3c57 100644 --- a/Core/Src/Runes/generated_metadata.cpp +++ b/Core/Src/Runes/generated_metadata.cpp @@ -5,11 +5,11 @@ extern "C"{ const char DESCRIPTION[255] __attribute__((section(".metadata_pool")))= "****************" // placeholder for beggining - "20250713T175251" // DateTime using ISO-8601 format + "20250715T175732" // DateTime using ISO-8601 format " " // alignment "f73885a9" // STLIB commit "--------" // ADJ commit - "2ed77dfe" // Board commit + "49ec81ca" // Board commit // the '=' is used for unparsing ; } \ No newline at end of file From c368d05fb7a2d69750167adb91e3a848458abc19 Mon Sep 17 00:00:00 2001 From: Cantonplas Date: Wed, 16 Jul 2025 13:46:04 +0200 Subject: [PATCH 07/11] It compiles --- Core/Inc/BLCU/BLCU.hpp | 148 ++++++++++++-------------- Core/Inc/BLCU/Comms.hpp | 50 ++++----- Core/Src/BLCU/BLCU.cpp | 57 +++++----- Core/Src/BLCU/Comms.cpp | 48 ++++----- Core/Src/Runes/generated_metadata.cpp | 4 +- Core/Src/main.cpp | 5 +- 6 files changed, 139 insertions(+), 173 deletions(-) diff --git a/Core/Inc/BLCU/BLCU.hpp b/Core/Inc/BLCU/BLCU.hpp index 5b2206f..a608eea 100644 --- a/Core/Inc/BLCU/BLCU.hpp +++ b/Core/Inc/BLCU/BLCU.hpp @@ -12,88 +12,74 @@ #include "FDCBootloader/BootloaderTFTP.hpp" #include "Comms.hpp" -#define BLCU_ID ((uint8_t)1) - - enum Target{ - NOTARGET, - VCU, // 1 - HVSCU, // 2 - BCU, // 3 - BMSL, // 4 - LCU, // 5 - PCU, // 6 - - }; - class BLCU{ - private: - - static constexpr uint16_t max_tcp_connection_timeout = 30000; - - unordered_map resets; - unordered_map boots; - - uint8_t fdcan; - DigitalOutput LED_OPERATIONAL; - DigitalOutput LED_FAULT; - DigitalOutput LED_CAN; - DigitalOutput LED_FLASH; - DigitalOutput LED_SLEEP; - - - static ServerSocket* tcp_socket;//Hacer nullptr - bool tcp_timeout = false; - - bool programming_error = false; - - - - void finish_write_read_order(bool error_ok); - void turn_off_all_boards(); - void turn_on_all_boards(); - void send_to_bootmode(); - void stop_booting(); - - - void setup_state_machine(); - void setup_specific_state_machine(); - - Comms* tcp =nullptr; - - public: - - enum GeneralStates{ - INITIAL, - OPERATIONAL, - FAULT, - }; - - - enum SpecificStates{ - READY, - BOOTING, - }; - - BLCU(); - void init(); - void update(); - - void abort_booting(); - void reset_all(); - - //PUBLIC VARIABLES: - Target current_target; - - bool ready_flag = false; - bool booting_flag = false; - StateMachine general_state_machine{ GeneralStates::INITIAL }; - StateMachine specific_state_machine{ SpecificStates::READY }; - - static constexpr string ip{"192.168.0.27"}; - static constexpr string mask{"255.255.0.0"}; - static constexpr string gateway{"192.168.1.1"}; - static constexpr uint32_t port{50500}; - +// Macro for BLCU id +#define BLCU_ID ((uint8_t)1) + +enum Target { + NOTARGET, + VCU, // 1 + HVSCU, // 2 + BCU, // 3 + BMSL, // 4 + LCU, // 5 + PCU // 6 +}; + +class BLCU { +private: + BLCU() = delete; + + static constexpr uint16_t max_tcp_connection_timeout = 30000; + + static unordered_map resets; + static unordered_map boots; + + static uint8_t fdcan; + static DigitalOutput LED_OPERATIONAL; + static DigitalOutput LED_FAULT; + static DigitalOutput LED_CAN; + static DigitalOutput LED_FLASH; + static DigitalOutput LED_SLEEP; + + static bool tcp_timeout; + static bool programming_error; + + + static void finish_write_read_order(bool error_ok); + static void turn_off_all_boards(); + static void turn_on_all_boards(); + static void send_to_bootmode(); + static void stop_booting(); + + static void setup_state_machine(); + static void setup_specific_state_machine(); + +public: + enum GeneralStates { + INITIAL, + OPERATIONAL, + FAULT, + }; + enum SpecificStates { + READY, + BOOTING, }; + static void init(); + static void update(); + static void abort_booting(); + static void reset_all(); + + static Target current_target; + static bool ready_flag; + static StateMachine general_state_machine; + static StateMachine specific_state_machine; + + static constexpr string ip{"192.168.0.27"}; + static constexpr string mask{"255.255.0.0"}; + static constexpr string gateway{"192.168.1.1"}; + static constexpr uint32_t port{50500}; +}; + diff --git a/Core/Inc/BLCU/Comms.hpp b/Core/Inc/BLCU/Comms.hpp index cf3b604..cfea217 100644 --- a/Core/Inc/BLCU/Comms.hpp +++ b/Core/Inc/BLCU/Comms.hpp @@ -1,43 +1,29 @@ #pragma once #include "ST-LIB.hpp" +#include "BLCU/BLCU.hpp" // Ensure BLCU is fully defined -class BLCU; - -class Comms{ +class Comms { private: - static Comms* self; - static void cb_write_program(); - static void cb_reset_all(); - static void cb_read_program(); - BLCU* board=nullptr; - HeapOrder* write_program_order=nullptr; - HeapOrder* reset_all_order=nullptr; - // static HeapOrder* get_version_order; - HeapOrder* read_program_order; - // static HeapOrder* erase_program_order; - + Comms() = delete; public: - Comms(BLCU* board); - - ServerSocket* tcp_socket=nullptr; + static void init(); + static ServerSocket* tcp_socket; - void read_program(); - void reset_all(); - void get_version(); - void write_program(); - void erase_program(); - - void send_ack(); - void send_nack(); + inline static HeapOrder* write_program_order=nullptr; + inline static HeapOrder* reset_all_order=nullptr; + inline static HeapOrder* read_program_order=nullptr; - HeapOrder ack = { - 3, - }; + static void read_program(); + static void reset_all(); + static void get_version(); + static void write_program(); + static void erase_program(); + + static void send_ack(); + static void send_nack(); - HeapOrder nack = { - 4, - }; - + inline static HeapOrder ack = HeapOrder(3); + inline static HeapOrder nack = HeapOrder(4); }; diff --git a/Core/Src/BLCU/BLCU.cpp b/Core/Src/BLCU/BLCU.cpp index 0938c87..2b630ea 100644 --- a/Core/Src/BLCU/BLCU.cpp +++ b/Core/Src/BLCU/BLCU.cpp @@ -1,23 +1,36 @@ #include "BLCU/BLCU.hpp" +StateMachine BLCU::general_state_machine{ BLCU::GeneralStates::INITIAL }; +StateMachine BLCU::specific_state_machine{ BLCU::SpecificStates::READY }; +DigitalOutput BLCU::LED_OPERATIONAL; +DigitalOutput BLCU::LED_FAULT; +DigitalOutput BLCU::LED_CAN; +DigitalOutput BLCU::LED_FLASH; +DigitalOutput BLCU::LED_SLEEP; +unordered_map BLCU::resets; +unordered_map BLCU::boots; +uint8_t BLCU::fdcan = 0; +Target BLCU::current_target = NOTARGET; +bool BLCU::ready_flag = false; +bool BLCU::tcp_timeout = false; +bool BLCU::programming_error = false; -BLCU::BLCU(): general_state_machine(GeneralStates::INITIAL), -specific_state_machine(SpecificStates::READY){ -//-------STATE MACHINE------// - setup_state_machine(); +void BLCU::init(){ + setup_state_machine(); setup_specific_state_machine(); general_state_machine.add_state_machine(specific_state_machine, GeneralStates::OPERATIONAL); ProtectionManager::link_state_machine(BLCU::general_state_machine, BLCU_ID); -//-------FDCAN------// + //FDCAN: fdcan = FDCAN::inscribe< CANBitRatesSpeed::CAN_1_Mbit, CANFormat::CAN_FDCAN_FORMAT, CANIdentifier::CAN_29_BIT_IDENTIFIER, CANMode::CAN_MODE_NORMAL >(FDCAN::fdcan1); + STLIB::start("00:80:e1:06:07:10",ip, mask, gateway, UART::uart2); -//-------RESETS------// + //-------RESETS------// resets[VCU] = DigitalOutput(PA12); resets[HVSCU] = DigitalOutput(PG3); resets[BCU] = DigitalOutput(PD11); @@ -25,7 +38,7 @@ specific_state_machine(SpecificStates::READY){ resets[LCU] = DigitalOutput(PB12); resets[PCU] = DigitalOutput(PG1); -//-------BOOTS------// + //-------BOOTS------// boots[VCU] = DigitalOutput(PA11); boots[HVSCU] = DigitalOutput(PG2); boots[BCU] = DigitalOutput(PD10); @@ -33,21 +46,13 @@ specific_state_machine(SpecificStates::READY){ boots[LCU] = DigitalOutput(PE15); boots[PCU] = DigitalOutput(PE7); - - -//-------LEDS------// + //-------LEDS------// LED_OPERATIONAL = DigitalOutput(PG8); LED_FAULT = DigitalOutput(PG7); LED_CAN = DigitalOutput(PG6); LED_FLASH = DigitalOutput(PG5); LED_SLEEP = DigitalOutput(PG4); -} - -void BLCU::init(){ - STLIB::start("00:80:e1:06:07:10",ip, mask, gateway, UART::uart2); - - tcp = new Comms(this); BTFTP::start(); Time::register_low_precision_alarm(1, [&](){ @@ -78,7 +83,7 @@ void BLCU::setup_state_machine(){ //Enter actions general_state_machine.add_enter_action([&](){ Time::set_timeout(max_tcp_connection_timeout, [&](){ - if(not (tcp->tcp_socket->state == ServerSocket::ServerState::ACCEPTED )){ + if(not (Comms::tcp_socket->state == ServerSocket::ServerState::ACCEPTED )){ tcp_timeout = true; } }); @@ -107,14 +112,14 @@ void BLCU::setup_state_machine(){ //Transitions general_state_machine.add_transition(GeneralStates::INITIAL, GeneralStates::OPERATIONAL, [&](){ - return tcp->tcp_socket->state == ServerSocket::ServerState::ACCEPTED; + return Comms::tcp_socket->state == ServerSocket::ServerState::ACCEPTED; }); general_state_machine.add_transition(GeneralStates::INITIAL, GeneralStates::FAULT, [&](){ if(tcp_timeout) ErrorHandler("TCP connections could not be established in time and timed out"); return tcp_timeout; }); general_state_machine.add_transition(GeneralStates::OPERATIONAL, GeneralStates::FAULT, [&](){ - if(tcp->tcp_socket->state!= ServerSocket::ServerState::ACCEPTED){ + if(Comms::tcp_socket->state!= ServerSocket::ServerState::ACCEPTED){ ErrorHandler("TCP connections fell"); return true; } @@ -170,13 +175,13 @@ void BLCU::finish_write_read_order(bool error_okey){ BTFTP::off(); ready_flag = true; - // if(error_okey==false){ - // tcp->tcp_socket->send_order(tcp->nack); - // abort_booting(); - // return; - // }else{ - // tcp->send_ack(); - // } + if(error_okey==false){ + Comms::send_nack(); + abort_booting(); + return; + }else{ + Comms::send_ack(); + } } diff --git a/Core/Src/BLCU/Comms.cpp b/Core/Src/BLCU/Comms.cpp index 448a928..67ee384 100644 --- a/Core/Src/BLCU/Comms.cpp +++ b/Core/Src/BLCU/Comms.cpp @@ -1,21 +1,15 @@ #include "BLCU/Comms.hpp" -#include "BLCU/BLCU.hpp" +#include "BLCU/BLCU.hpp" +ServerSocket* Comms::tcp_socket = nullptr; -Comms* Comms::self = nullptr; - -Comms::Comms(BLCU* board): board(board){ - self = this; - tcp_socket = new ServerSocket(board->ip, board->port); - write_program_order = new HeapOrder(700, cb_write_program, &board->current_target); - reset_all_order = new HeapOrder(701, cb_reset_all); - read_program_order = new HeapOrder(702, cb_read_program, &board->current_target); - +void Comms::init(){ + tcp_socket = new ServerSocket(BLCU::ip, BLCU::port); + write_program_order = new HeapOrder(700, Comms::write_program, &BLCU::current_target); + reset_all_order = new HeapOrder(701, Comms::reset_all, &BLCU::current_target); + read_program_order = new HeapOrder(702, Comms::read_program, &BLCU::current_target); } -void Comms::cb_write_program() { self->write_program(); } -void Comms::cb_reset_all() { self->reset_all(); } -void Comms::cb_read_program() { self->read_program(); } void Comms::send_ack() { tcp_socket->send_order(ack); } @@ -25,11 +19,11 @@ void Comms::send_nack() { } void Comms::read_program(){ - if(board->specific_state_machine.current_state != board->SpecificStates::READY){ + if(BLCU::specific_state_machine.current_state != BLCU::SpecificStates::READY){ send_nack(); return; } - board->specific_state_machine.force_change_state(board->SpecificStates::BOOTING); + BLCU::specific_state_machine.force_change_state(BLCU::SpecificStates::BOOTING); BTFTP::on(BTFTP::Mode::READ); @@ -37,11 +31,11 @@ void Comms::read_program(){ } void Comms::write_program(){ - if(board->specific_state_machine.current_state != board->SpecificStates::READY){ + if(BLCU::specific_state_machine.current_state != BLCU::SpecificStates::READY){ send_nack(); return; } - board->booting_flag = false; + BLCU::ready_flag = false; BTFTP::on(BTFTP::Mode::WRITE); @@ -49,43 +43,39 @@ void Comms::write_program(){ } void Comms::erase_program(){ - if(board->specific_state_machine.current_state != board->SpecificStates::READY){ + if(BLCU::specific_state_machine.current_state != BLCU::SpecificStates::READY){ send_nack(); return; } - board->specific_state_machine.force_change_state(board->SpecificStates::BOOTING); + BLCU::specific_state_machine.force_change_state(BLCU::SpecificStates::BOOTING); if(not FDCB::erase_memory()){ send_nack(); - board->abort_booting(); + BLCU::abort_booting(); return; }else{ send_ack(); } - board->specific_state_machine.force_change_state(board->SpecificStates::READY); + BLCU::specific_state_machine.force_change_state(BLCU::SpecificStates::READY); } void Comms::get_version(){ - if(board->specific_state_machine.current_state != board->SpecificStates::READY){ + if(BLCU::specific_state_machine.current_state != BLCU::SpecificStates::READY){ send_ack(); return; } - board->specific_state_machine.force_change_state(board->SpecificStates::BOOTING); + BLCU::specific_state_machine.force_change_state(BLCU::SpecificStates::BOOTING); uint8_t temporal_value = 0; if (not FDCB::get_version(temporal_value)){ send_nack(); - board->abort_booting(); + BLCU::abort_booting(); return; } send_ack(); - board->specific_state_machine.force_change_state(board->SpecificStates::READY); -} - -void Comms::reset_all() { - board->reset_all(); + BLCU::specific_state_machine.force_change_state(BLCU::SpecificStates::READY); } diff --git a/Core/Src/Runes/generated_metadata.cpp b/Core/Src/Runes/generated_metadata.cpp index 8ac3c57..12acd34 100644 --- a/Core/Src/Runes/generated_metadata.cpp +++ b/Core/Src/Runes/generated_metadata.cpp @@ -5,11 +5,11 @@ extern "C"{ const char DESCRIPTION[255] __attribute__((section(".metadata_pool")))= "****************" // placeholder for beggining - "20250715T175732" // DateTime using ISO-8601 format + "20250716T134534" // DateTime using ISO-8601 format " " // alignment "f73885a9" // STLIB commit "--------" // ADJ commit - "49ec81ca" // Board commit + "0f3bf198" // Board commit // the '=' is used for unparsing ; } \ No newline at end of file diff --git a/Core/Src/main.cpp b/Core/Src/main.cpp index ea495eb..3126e2b 100644 --- a/Core/Src/main.cpp +++ b/Core/Src/main.cpp @@ -13,12 +13,11 @@ int main(void) { SharedMemory::start(); #endif - BLCU board{}; - board.init(); + BLCU::init(); while (1) { - board.update(); + BLCU::update(); } } From 34199cf12fed58b893b6bd7127151344f659b0e2 Mon Sep 17 00:00:00 2001 From: Cantonplas Date: Thu, 17 Jul 2025 17:22:06 +0200 Subject: [PATCH 08/11] Working code(CAN to be tested) --- Core/Inc/BLCU/Comms.hpp | 1 - Core/Src/BLCU/BLCU.cpp | 10 ++++++---- Core/Src/BLCU/Comms.cpp | 7 +++---- Core/Src/FDCBootloader/BootloaderTFTP.cpp | 3 --- Core/Src/Runes/generated_metadata.cpp | 4 ++-- 5 files changed, 11 insertions(+), 14 deletions(-) diff --git a/Core/Inc/BLCU/Comms.hpp b/Core/Inc/BLCU/Comms.hpp index cfea217..f70db44 100644 --- a/Core/Inc/BLCU/Comms.hpp +++ b/Core/Inc/BLCU/Comms.hpp @@ -14,7 +14,6 @@ class Comms { inline static HeapOrder* read_program_order=nullptr; static void read_program(); - static void reset_all(); static void get_version(); static void write_program(); static void erase_program(); diff --git a/Core/Src/BLCU/BLCU.cpp b/Core/Src/BLCU/BLCU.cpp index 2b630ea..44affd6 100644 --- a/Core/Src/BLCU/BLCU.cpp +++ b/Core/Src/BLCU/BLCU.cpp @@ -19,7 +19,6 @@ void BLCU::init(){ setup_state_machine(); setup_specific_state_machine(); general_state_machine.add_state_machine(specific_state_machine, GeneralStates::OPERATIONAL); - ProtectionManager::link_state_machine(BLCU::general_state_machine, BLCU_ID); //FDCAN: fdcan = FDCAN::inscribe< @@ -28,7 +27,6 @@ void BLCU::init(){ CANIdentifier::CAN_29_BIT_IDENTIFIER, CANMode::CAN_MODE_NORMAL >(FDCAN::fdcan1); - STLIB::start("00:80:e1:06:07:10",ip, mask, gateway, UART::uart2); //-------RESETS------// resets[VCU] = DigitalOutput(PA12); @@ -53,6 +51,10 @@ void BLCU::init(){ LED_FLASH = DigitalOutput(PG5); LED_SLEEP = DigitalOutput(PG4); + STLIB::start("00:80:e1:06:07:10",ip, mask, gateway, UART::uart2); + + Comms::init(); + BTFTP::start(); Time::register_low_precision_alarm(1, [&](){ @@ -129,7 +131,7 @@ void BLCU::setup_state_machine(){ //Cyclic actions general_state_machine.add_low_precision_cyclic_action([&](){LED_OPERATIONAL.toggle();}, (chrono::milliseconds)150, INITIAL); - // general_state_machine.add_low_precision_cyclic_action(ProtectionManager::check_protections, (chrono::milliseconds)1 , OPERATIONAL); + general_state_machine.add_low_precision_cyclic_action(ProtectionManager::check_protections, (chrono::milliseconds)1 , OPERATIONAL); } void BLCU::setup_specific_state_machine(){ @@ -173,7 +175,7 @@ void BLCU::setup_specific_state_machine(){ void BLCU::finish_write_read_order(bool error_okey){ BTFTP::off(); - ready_flag = true; + specific_state_machine.force_change_state(BOOTING); if(error_okey==false){ Comms::send_nack(); diff --git a/Core/Src/BLCU/Comms.cpp b/Core/Src/BLCU/Comms.cpp index 67ee384..0f7368f 100644 --- a/Core/Src/BLCU/Comms.cpp +++ b/Core/Src/BLCU/Comms.cpp @@ -5,9 +5,8 @@ ServerSocket* Comms::tcp_socket = nullptr; void Comms::init(){ tcp_socket = new ServerSocket(BLCU::ip, BLCU::port); - write_program_order = new HeapOrder(700, Comms::write_program, &BLCU::current_target); - reset_all_order = new HeapOrder(701, Comms::reset_all, &BLCU::current_target); - read_program_order = new HeapOrder(702, Comms::read_program, &BLCU::current_target); + write_program_order = new HeapOrder(700, &Comms::write_program, &BLCU::current_target); + reset_all_order = new HeapOrder(701, &BLCU::reset_all); } void Comms::send_ack() { @@ -35,7 +34,7 @@ void Comms::write_program(){ send_nack(); return; } - BLCU::ready_flag = false; + BLCU::specific_state_machine.force_change_state(BLCU::SpecificStates::BOOTING); BTFTP::on(BTFTP::Mode::WRITE); diff --git a/Core/Src/FDCBootloader/BootloaderTFTP.cpp b/Core/Src/FDCBootloader/BootloaderTFTP.cpp index 5a55bb7..6cb8728 100644 --- a/Core/Src/FDCBootloader/BootloaderTFTP.cpp +++ b/Core/Src/FDCBootloader/BootloaderTFTP.cpp @@ -8,8 +8,6 @@ #include "FDCBootloader/BootloaderTFTP.hpp" -//#ifdef HAL_ETH_MODULE_ENABLED -//Variables: namespace BLCU { extern void finish_write_read_order(bool error_ok); } @@ -175,4 +173,3 @@ void BTFTP::re(void* handle){ } -//#endif diff --git a/Core/Src/Runes/generated_metadata.cpp b/Core/Src/Runes/generated_metadata.cpp index 12acd34..ceb76b3 100644 --- a/Core/Src/Runes/generated_metadata.cpp +++ b/Core/Src/Runes/generated_metadata.cpp @@ -5,11 +5,11 @@ extern "C"{ const char DESCRIPTION[255] __attribute__((section(".metadata_pool")))= "****************" // placeholder for beggining - "20250716T134534" // DateTime using ISO-8601 format + "20250717T164957" // DateTime using ISO-8601 format " " // alignment "f73885a9" // STLIB commit "--------" // ADJ commit - "0f3bf198" // Board commit + "c368d05f" // Board commit // the '=' is used for unparsing ; } \ No newline at end of file From a58b46cb6b43d5f8e4289cdefd7782429c71b210 Mon Sep 17 00:00:00 2001 From: Cantonplas Date: Thu, 17 Jul 2025 22:21:37 +0200 Subject: [PATCH 09/11] Deleted useless comment --- Core/Inc/BLCU/Comms.hpp | 2 +- Core/Src/Runes/generated_metadata.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Core/Inc/BLCU/Comms.hpp b/Core/Inc/BLCU/Comms.hpp index f70db44..8822e8a 100644 --- a/Core/Inc/BLCU/Comms.hpp +++ b/Core/Inc/BLCU/Comms.hpp @@ -1,6 +1,6 @@ #pragma once #include "ST-LIB.hpp" -#include "BLCU/BLCU.hpp" // Ensure BLCU is fully defined +#include "BLCU/BLCU.hpp" class Comms { private: diff --git a/Core/Src/Runes/generated_metadata.cpp b/Core/Src/Runes/generated_metadata.cpp index ceb76b3..7f3dbd3 100644 --- a/Core/Src/Runes/generated_metadata.cpp +++ b/Core/Src/Runes/generated_metadata.cpp @@ -5,11 +5,11 @@ extern "C"{ const char DESCRIPTION[255] __attribute__((section(".metadata_pool")))= "****************" // placeholder for beggining - "20250717T164957" // DateTime using ISO-8601 format + "20250717T172821" // DateTime using ISO-8601 format " " // alignment "f73885a9" // STLIB commit "--------" // ADJ commit - "c368d05f" // Board commit + "34199cf1" // Board commit // the '=' is used for unparsing ; } \ No newline at end of file From c919bb0dd73cc8f8428f59b04b041a20be594844 Mon Sep 17 00:00:00 2001 From: Cantonplas Date: Mon, 21 Jul 2025 11:23:18 +0200 Subject: [PATCH 10/11] Bootloader version changed --- Core/Inc/FDCBootloader/FDCBootloader.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/Inc/FDCBootloader/FDCBootloader.hpp b/Core/Inc/FDCBootloader/FDCBootloader.hpp index 3b7ba10..3caf7f4 100644 --- a/Core/Inc/FDCBootloader/FDCBootloader.hpp +++ b/Core/Inc/FDCBootloader/FDCBootloader.hpp @@ -12,7 +12,7 @@ #include "C++Utilities/CppUtils.hpp" -#define FDCB_CURRENT_VERSION ((uint8_t)0x05) +#define FDCB_CURRENT_VERSION ((uint8_t)0x06) #define FDCB_VERBOSE_LEVEL 1 #define FDCB_TIMEOUT_MS 5000 From bc8f597f173929f494124c1ec716d4b5bc21a743 Mon Sep 17 00:00:00 2001 From: Cantonplas Date: Mon, 21 Jul 2025 11:39:16 +0200 Subject: [PATCH 11/11] Deps changes --- deps/ST-LIB | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deps/ST-LIB b/deps/ST-LIB index f73885a..1af648a 160000 --- a/deps/ST-LIB +++ b/deps/ST-LIB @@ -1 +1 @@ -Subproject commit f73885a99171d3e75c6453efc6d9c9c958849538 +Subproject commit 1af648a011e4fb0f87031ef1b6b24f9a62c76955