diff --git a/Core/Inc/BLCU/BLCU.hpp b/Core/Inc/BLCU/BLCU.hpp index 3e5f2df..a608eea 100644 --- a/Core/Inc/BLCU/BLCU.hpp +++ b/Core/Inc/BLCU/BLCU.hpp @@ -7,37 +7,79 @@ #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" +#include "Comms.hpp" -namespace BLCU{ +// Macro for BLCU id +#define BLCU_ID ((uint8_t)1) - 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(); - } +enum Target { + NOTARGET, + VCU, // 1 + HVSCU, // 2 + BCU, // 3 + BMSL, // 4 + LCU, // 5 + PCU // 6 +}; - void start(){ - STLIB::start("00:80:e1:00:00:00",ip, mask, gateway, UART::uart2); - BLCU::__tcp_start(); - BTFTP::start(); +class BLCU { +private: + BLCU() = delete; - // BLCU::__resets_start(); - // BLCU::__boots_start(); - // BLCU::__leds_start(); - } + 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}; +}; - 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 7bd1fdb..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 1deb1ed..0000000 --- a/Core/Inc/BLCU/BLCU_Orders/BLCU_PublicFunctions.hpp +++ /dev/null @@ -1,95 +0,0 @@ -#pragma once - -#include "BLCU/BLCU_Definition/BLCU_Definition.hpp" - -namespace BLCU{ - void reset_all(){ - printf("Reset all order received\n"); - for (auto& [target, reset]: resets) - { - reset.turn_off(); - } - - for (auto& [target, reset]: resets) - { - reset.turn_on(); - } - - HAL_NVIC_SystemReset();//Good bye :) - } - - 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; - 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(){ - 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); - - BLCU::tcp_socket->send_order(ack); - } - - 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); - - BLCU::tcp_socket->send_order(ack); - } - - 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()){ - 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 164d96b..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 a8ccf98..0000000 --- a/Core/Inc/BLCU/BLCU_StateMachine/BLCU_StateMachine.hpp +++ /dev/null @@ -1,110 +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::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..8822e8a --- /dev/null +++ b/Core/Inc/BLCU/Comms.hpp @@ -0,0 +1,30 @@ +#pragma once +#include "ST-LIB.hpp" +#include "BLCU/BLCU.hpp" + +class Comms { +private: + Comms() = delete; +public: + static void init(); + static ServerSocket* tcp_socket; + + inline static HeapOrder* write_program_order=nullptr; + inline static HeapOrder* reset_all_order=nullptr; + inline static HeapOrder* read_program_order=nullptr; + + static void read_program(); + static void get_version(); + static void write_program(); + static void erase_program(); + + static void send_ack(); + static void send_nack(); + + inline static HeapOrder ack = HeapOrder(3); + inline static HeapOrder nack = HeapOrder(4); +}; + + + + 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/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 diff --git a/Core/Src/BLCU/BLCU.cpp b/Core/Src/BLCU/BLCU.cpp new file mode 100644 index 0000000..44affd6 --- /dev/null +++ b/Core/Src/BLCU/BLCU.cpp @@ -0,0 +1,234 @@ +#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; + +void BLCU::init(){ + setup_state_machine(); + setup_specific_state_machine(); + general_state_machine.add_state_machine(specific_state_machine, GeneralStates::OPERATIONAL); + + //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); + + STLIB::start("00:80:e1:06:07:10",ip, mask, gateway, UART::uart2); + + Comms::init(); + + BTFTP::start(); + + Time::register_low_precision_alarm(1, [&](){ + general_state_machine.check_transitions(); + }); + + + for (auto& [target, reset_pin]: BLCU::resets){ + reset_pin.turn_on(); + } + + for (auto& [target, boot_pin]: BLCU::boots){ + boot_pin.turn_off(); + } +} + +void BLCU::update(){ + STLIB::update(); +} + +void BLCU::setup_state_machine(){ + 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 (Comms::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 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(Comms::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.add_state(SpecificStates::BOOTING); + + + //Enter actions + specific_state_machine.add_enter_action([&](){ + 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); + + 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_okey){ + BTFTP::off(); + specific_state_machine.force_change_state(BOOTING); + + if(error_okey==false){ + Comms::send_nack(); + abort_booting(); + return; + }else{ + Comms::send_ack(); + } + +} + +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(); + } + + 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..0f7368f --- /dev/null +++ b/Core/Src/BLCU/Comms.cpp @@ -0,0 +1,80 @@ +#include "BLCU/Comms.hpp" +#include "BLCU/BLCU.hpp" + +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, &BLCU::reset_all); +} + +void Comms::send_ack() { + tcp_socket->send_order(ack); +} + +void Comms::send_nack() { + tcp_socket->send_order(nack); +} + +void Comms::read_program(){ + if(BLCU::specific_state_machine.current_state != BLCU::SpecificStates::READY){ + send_nack(); + return; + } + BLCU::specific_state_machine.force_change_state(BLCU::SpecificStates::BOOTING); + + BTFTP::on(BTFTP::Mode::READ); + + send_ack(); +} + +void Comms::write_program(){ + if(BLCU::specific_state_machine.current_state != BLCU::SpecificStates::READY){ + send_nack(); + return; + } + BLCU::specific_state_machine.force_change_state(BLCU::SpecificStates::BOOTING); + + BTFTP::on(BTFTP::Mode::WRITE); + + send_ack(); +} + +void Comms::erase_program(){ + if(BLCU::specific_state_machine.current_state != BLCU::SpecificStates::READY){ + send_nack(); + return; + } + BLCU::specific_state_machine.force_change_state(BLCU::SpecificStates::BOOTING); + + if(not FDCB::erase_memory()){ + send_nack(); + BLCU::abort_booting(); + return; + }else{ + send_ack(); + } + + BLCU::specific_state_machine.force_change_state(BLCU::SpecificStates::READY); +} + +void Comms::get_version(){ + if(BLCU::specific_state_machine.current_state != BLCU::SpecificStates::READY){ + send_ack(); + return; + } + BLCU::specific_state_machine.force_change_state(BLCU::SpecificStates::BOOTING); + + uint8_t temporal_value = 0; + if (not FDCB::get_version(temporal_value)){ + send_nack(); + BLCU::abort_booting(); + return; + } + + send_ack(); + + BLCU::specific_state_machine.force_change_state(BLCU::SpecificStates::READY); +} + diff --git a/Core/Src/FDCBootloader/BootloaderTFTP.cpp b/Core/Src/FDCBootloader/BootloaderTFTP.cpp index fc98ba6..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); } @@ -36,9 +34,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 +48,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,21 +62,17 @@ 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; 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; } @@ -103,7 +93,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); } @@ -169,7 +159,6 @@ int BTFTP::write(void* handle, struct pbuf* p){ error_ok = false; return -1; } - //Deberia de cambiar de estado aqui, para quitar de boot a la otra pagina return 0; }else{ btftp_handle->file->pointer += TFTP_MAX_DATA_SIZE; @@ -184,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 2bb322e..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 - "20250624T153513" // DateTime using ISO-8601 format + "20250717T172821" // DateTime using ISO-8601 format " " // alignment - "f8e647d0" // STLIB commit + "f73885a9" // STLIB commit "--------" // ADJ commit - "fa5dc1b1" // Board commit + "34199cf1" // 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..3126e2b 100644 --- a/Core/Src/main.cpp +++ b/Core/Src/main.cpp @@ -8,77 +8,14 @@ #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, - }; - + BLCU::init(); - 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(); - - - - // BootPCU.turn_on(); - // NReset.turn_off(); - // HAL_Delay(100); - // NReset.turn_on(); - // HAL_Delay(100); - // BootPCU.turn_off(); - - while (1) { BLCU::update(); } diff --git a/deps/ST-LIB b/deps/ST-LIB index f8e647d..1af648a 160000 --- a/deps/ST-LIB +++ b/deps/ST-LIB @@ -1 +1 @@ -Subproject commit f8e647d0c4f7d37dde4d6138719530086165ea76 +Subproject commit 1af648a011e4fb0f87031ef1b6b24f9a62c76955