Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,7 @@ tools/*.bin

# Log files
*.log

# Generated packet headers
/Core/Inc/Communications/Packets/DataPackets.hpp
/Core/Inc/Communications/Packets/OrderPackets.hpp
14 changes: 13 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ include(CTest)
enable_testing()

set(EXECUTABLE ${PROJECT_NAME}.elf)
set(BOARD_NAME "VCU" CACHE STRING "Board key from Core/Inc/Code_generation/JSON_ADE/boards.json" FORCE)
set(BOARD_NAME "TEST" CACHE STRING "Board key from Core/Inc/Code_generation/JSON_ADE/boards.json")
if(BOARD_NAME STREQUAL "")
message(FATAL_ERROR "BOARD_NAME cannot be empty")
endif()
Expand Down Expand Up @@ -73,6 +73,7 @@ add_custom_command(
DEPENDS
${GENERATOR_SCRIPT}
${CMAKE_SOURCE_DIR}/Core/Inc/Code_generation/Packet_generation/Packet_generation.py
${CMAKE_SOURCE_DIR}/Core/Inc/Code_generation/Packet_generation/Packet_descriptions.py
${CMAKE_SOURCE_DIR}/Core/Inc/Code_generation/Packet_generation/DataTemplate.hpp
${CMAKE_SOURCE_DIR}/Core/Inc/Code_generation/Packet_generation/OrderTemplate.hpp
${GENERATOR_JSONS}
Expand Down Expand Up @@ -125,6 +126,16 @@ if(CMAKE_CROSSCOMPILING)
list(REMOVE_ITEM SOURCE_CPP ${EXAMPLE_CPP})
endif()

set(EXAMPLE_SELECTED OFF)
# If an EXAMPLE_* macro is injected through CXX flags, mark the build as
# example-selected so main.cpp can disable only the default main().
if(BUILD_EXAMPLES)
if(CMAKE_CXX_FLAGS MATCHES "(^|[ \t])-DEXAMPLE_[A-Za-z0-9_]+")
set(EXAMPLE_SELECTED ON)
message(STATUS "Template project: EXAMPLE_* detected, disabling default main()")
endif()
endif()

add_executable(${EXECUTABLE}
${SOURCE_C}
${SOURCE_CPP}
Expand All @@ -146,6 +157,7 @@ if(CMAKE_CROSSCOMPILING)
)

target_compile_definitions(${EXECUTABLE} PRIVATE
$<$<BOOL:${EXAMPLE_SELECTED}>:EXAMPLE_SELECTED>
$<$<BOOL:${USE_ETHERNET}>:STLIB_ETH>
$<IF:$<BOOL:${TARGET_NUCLEO}>,NUCLEO,BOARD>
$<IF:$<BOOL:${TARGET_NUCLEO}>,HSE_VALUE=8000000,HSE_VALUE=25000000>
Expand Down
2 changes: 1 addition & 1 deletion Core/Inc/Code_generation/JSON_ADE
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def __init__(self,measurements:list, variable:str, filename:str="Unknown"):
raise Exception(f"Measurement not found for variable: {variable} in file: {filename}")

self.name = measurement["name"]
self.type = (self._unsigned_int_correction(measurement["type"]).replace(" ", "_").replace("-", "_"))
self.type = (self._numeric_type_correction(measurement["type"]).replace(" ", "_").replace("-", "_"))
if self.type == "enum":
values = []
for value in measurement["enumValues"]:
Expand Down Expand Up @@ -175,9 +175,10 @@ def _MeasurementSearch(measurements:list, variable:str):


@staticmethod
def _unsigned_int_correction(type:str):
aux_type = type[:4]
if aux_type == "uint":
def _numeric_type_correction(type:str):
if type.startswith("uint") and not type.endswith("_t"):
type += "_t"
elif type.startswith("int") and not type.endswith("_t"):
type += "_t"
elif type == "float32":
type = "float"
Expand Down
233 changes: 233 additions & 0 deletions Core/Src/Examples/ExamplePackets.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
#ifdef EXAMPLE_PACKETS

#include "Communications/Packets/DataPackets.hpp"
#include "Communications/Packets/OrderPackets.hpp"
#include "ST-LIB.hpp"
#include "main.h"

using namespace ST_LIB;

#ifndef STLIB_ETH
#error "EXAMPLE_PACKETS requires STLIB_ETH"
#endif

constexpr auto led = ST_LIB::DigitalOutputDomain::DigitalOutput(ST_LIB::PB0);

#if defined(USE_PHY_LAN8742)
constexpr auto eth = EthernetDomain::Ethernet(
EthernetDomain::PINSET_H10,
"00:80:e1:00:01:08",
"192.168.1.7",
"255.255.0.0"
);
#elif defined(USE_PHY_LAN8700)
constexpr auto eth = EthernetDomain::Ethernet(
EthernetDomain::PINSET_H10,
"00:80:e1:00:01:08",
"192.168.1.7",
"255.255.0.0"
);
#elif defined(USE_PHY_KSZ8041)
constexpr auto eth = EthernetDomain::Ethernet(
EthernetDomain::PINSET_H11,
"00:80:e1:00:01:08",
"192.168.1.7",
"255.255.0.0"
);
#else
#error "No PHY selected for Ethernet pinset selection"
#endif

using ExamplePacketsBoard = ST_LIB::Board<eth, led>;

namespace {

constexpr uint16_t ORDER_ID_SET_SMALL_PROFILE = 0x5001;
constexpr uint16_t ORDER_ID_SET_LARGE_PROFILE = 0x5002;
constexpr uint16_t ORDER_ID_SET_EXTREMES = 0x5003;
constexpr uint16_t ORDER_ID_BUMP_STATE = 0x5004;
constexpr uint16_t ORDER_ID_SET_STATE_CODE = 0x5005;

constexpr uint32_t HEARTBEAT_PERIOD_MS = 250;
constexpr uint32_t SOCKET_RECONNECT_PERIOD_MS = 1000;

constexpr char BOARD_IP[] = "192.168.1.7";

bool enable_flag{false};
uint8_t small_counter{0};
uint16_t window_size{0};
uint32_t magic_value{0};
uint64_t big_counter{0};
int8_t trim_value{0};
int16_t offset_value{0};
int32_t position_value{0};
int64_t energy_value{0};
float ratio_value{0.0f};
double precise_value{0.0};

uint32_t tcp_order_count{0};
uint32_t udp_parse_count{0};
uint32_t heartbeat_ticks{0};
uint16_t last_order_code{0};

OrderPackets::order_mode order_mode{OrderPackets::order_mode::IDLE};
OrderPackets::order_state order_state{OrderPackets::order_state::BOOT};
DataPackets::mirror_mode mirror_mode{DataPackets::mirror_mode::IDLE};
DataPackets::mirror_state mirror_state{DataPackets::mirror_state::BOOT};

uint32_t probe_seq{0};
bool probe_toggle{false};
uint16_t probe_window{0};
float probe_ratio{0.0f};
DataPackets::probe_mode probe_mode{DataPackets::probe_mode::LOW};
uint32_t last_seen_probe_seq{0};

void sync_mirror_enums() {
mirror_mode = static_cast<DataPackets::mirror_mode>(static_cast<uint8_t>(order_mode));
mirror_state = static_cast<DataPackets::mirror_state>(static_cast<uint8_t>(order_state));
}

void process_orders() {
if (OrderPackets::set_small_profile_flag) {
OrderPackets::set_small_profile_flag = false;
++tcp_order_count;
last_order_code = ORDER_ID_SET_SMALL_PROFILE;
}

if (OrderPackets::set_large_profile_flag) {
OrderPackets::set_large_profile_flag = false;
++tcp_order_count;
last_order_code = ORDER_ID_SET_LARGE_PROFILE;
}

if (OrderPackets::set_extremes_flag) {
OrderPackets::set_extremes_flag = false;
++tcp_order_count;
last_order_code = ORDER_ID_SET_EXTREMES;
}

if (OrderPackets::bump_state_flag) {
OrderPackets::bump_state_flag = false;
++tcp_order_count;
last_order_code = ORDER_ID_BUMP_STATE;
const uint8_t next_state = (static_cast<uint8_t>(order_state) + 1U) % 4U;
order_state = static_cast<OrderPackets::order_state>(next_state);
}

if (OrderPackets::set_state_code_flag) {
OrderPackets::set_state_code_flag = false;
++tcp_order_count;
last_order_code = ORDER_ID_SET_STATE_CODE;
}

sync_mirror_enums();
}

void process_probe_packet() {
if (probe_seq == last_seen_probe_seq) {
return;
}
last_seen_probe_seq = probe_seq;
++udp_parse_count;
}

} // namespace

int main(void) {
Hard_fault_check();
ExamplePacketsBoard::init();
Scheduler::start();

OrderPackets::set_small_profile_init(enable_flag, small_counter, offset_value, order_mode);
OrderPackets::set_large_profile_init(
window_size,
magic_value,
position_value,
ratio_value,
precise_value
);
OrderPackets::set_extremes_init(trim_value, energy_value, big_counter);
OrderPackets::bump_state_init();
OrderPackets::set_state_code_init(order_state);

DataPackets::order_mirror_init(
tcp_order_count,
last_order_code,
enable_flag,
small_counter,
offset_value,
mirror_mode
);
DataPackets::numeric_mirror_init(
window_size,
magic_value,
position_value,
ratio_value,
precise_value
);
DataPackets::extremes_mirror_init(trim_value, energy_value, big_counter, mirror_state);
DataPackets::udp_probe_init(probe_seq, probe_toggle, probe_window, probe_ratio, probe_mode);
DataPackets::udp_probe_echo_init(
udp_parse_count,
probe_seq,
probe_toggle,
probe_window,
probe_ratio,
probe_mode
);
DataPackets::heartbeat_snapshot_init(
heartbeat_ticks,
tcp_order_count,
udp_parse_count,
mirror_state
);

DataPackets::start();
OrderPackets::start();

auto& eth_instance = ExamplePacketsBoard::instance_of<eth>();
auto& led_instance = ExamplePacketsBoard::instance_of<led>();

sync_mirror_enums();

uint32_t last_heartbeat_ms = HAL_GetTick();
uint32_t last_reconnect_ms = HAL_GetTick();
bool led_state = false;

while (1) {
eth_instance.update();
Scheduler::update();

process_orders();
process_probe_packet();

const uint32_t now = HAL_GetTick();
if ((now - last_reconnect_ms) >= SOCKET_RECONNECT_PERIOD_MS) {
last_reconnect_ms = now;
if (OrderPackets::control_test_client != nullptr &&
!OrderPackets::control_test_client->is_connected()) {
OrderPackets::control_test_client->reconnect();
}
if (OrderPackets::control_test_tcp != nullptr &&
!OrderPackets::control_test_tcp->is_connected() &&
!OrderPackets::control_test_tcp->is_listening()) {
delete OrderPackets::control_test_tcp;
OrderPackets::control_test_tcp = new ServerSocket(BOARD_IP, 41000);
}
}

if ((now - last_heartbeat_ms) >= HEARTBEAT_PERIOD_MS) {
last_heartbeat_ms = now;
++heartbeat_ticks;
sync_mirror_enums();
led_state = !led_state;
if (led_state) {
led_instance.turn_on();
} else {
led_instance.turn_off();
}
}
}
}

#endif // EXAMPLE_PACKETS
Loading