Skip to content

Commit

Permalink
refactor platform specific functions and restart request (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
PonomarevDA authored Sep 25, 2024
1 parent 937de75 commit 2e63e03
Show file tree
Hide file tree
Showing 22 changed files with 75 additions and 65 deletions.
9 changes: 2 additions & 7 deletions Cyphal/cyphal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
/// Author: Dmitry Ponomarev <[email protected]>

#include "cyphal.hpp"
#include "main.h"
#include "cyphal_subscribers.hpp"
#include "cyphal_registers.hpp"
#include "uavcan/node/Heartbeat_1_0.h"
Expand All @@ -18,7 +17,6 @@ static const constexpr size_t TX_QUEUE_FRAME_SIZE = 320; ///< we need 314 bytes
///< wrappers
static void* memAllocate(CanardInstance* const canard, const size_t amount);
static void memFree(CanardInstance* const canard, void* const pointer);
static uint32_t getCurrentMicroseconds();


O1HeapInstance* Cyphal::my_allocator;
Expand Down Expand Up @@ -58,7 +56,7 @@ int Cyphal::init(uint8_t id) {

void Cyphal::process() {
// 1. spin recv
uint32_t crnt_time_ms = HAL_GetTick();
uint32_t crnt_time_ms = platformSpecificGetTimeMs();
for (uint_fast8_t frame_idx = 0; frame_idx < transport.get_rx_queue_size(); frame_idx++) {
CanardFrame rx_frame;
if (transport.receive(&rx_frame)) {
Expand Down Expand Up @@ -132,7 +130,7 @@ void Cyphal::processReceivedTransfer([[maybe_unused]] const uint8_t redundant_in
}

bool Cyphal::isTxQueueItemFresh(const CanardTxQueueItem* ti) const {
return (0U == ti->tx_deadline_usec) || (ti->tx_deadline_usec > getCurrentMicroseconds());
return (0U == ti->tx_deadline_usec) || (ti->tx_deadline_usec > platformSpecificGetTimeMs());
}

void Cyphal::spinTransmit() {
Expand Down Expand Up @@ -201,8 +199,5 @@ static void memFree(CanardInstance* const canard, void* const pointer) {
(void) canard;
o1heapFree(Cyphal::my_allocator, pointer);
}
static uint32_t getCurrentMicroseconds() {
return HAL_GetTick();
}

} // namespace cyphal
1 change: 1 addition & 0 deletions Cyphal/cyphal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "cyphal_subscribers.hpp"
#include "cyphal_registers.hpp"
#include "cyphal_publishers.hpp"
#include "platform_specific.h"
#include "canard.h"
#include "o1heap.h"
#include "uavcan/node/GetInfo_1_0.h"
Expand Down
3 changes: 1 addition & 2 deletions Cyphal/cyphal_publishers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include "cyphal_publishers.hpp"
#include "cyphal.hpp"
#include "main.h"
#include "storage.h"

namespace cyphal {
Expand Down Expand Up @@ -101,7 +100,7 @@ size_t PortListPublisher::uavcan_node_port_List_1_0_create() {
}

void PortListPublisher::publish() {
auto crnt_time_ms = HAL_GetTick();
auto crnt_time_ms = platformSpecificGetTimeMs();
if (crnt_time_ms < next_pub_time_ms && !driver->ports_updated) {
return;
}
Expand Down
24 changes: 3 additions & 21 deletions Cyphal/cyphal_subscribers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

#include "cyphal_subscribers.hpp"
#include "cyphal.hpp"
#include "main.h"
#include "params.hpp"
#include "algorithms.hpp"
#include "storage.h"
#include "platform_specific.h"

#ifndef GIT_HASH
#define GIT_HASH 0xBADC0FFEE000
Expand Down Expand Up @@ -56,24 +56,7 @@ void NodeGetInfoSubscriber::init() {
get_info_response.hardware_version.minor = hw_version.minor;
get_info_response.certificate_of_authenticity.count = 0;

auto uid_u32 = HAL_GetUIDw0();
get_info_response.unique_id[0] = uid_u32 & 0xFF;
get_info_response.unique_id[1] = (uid_u32 >> 8) & 0xFF;
get_info_response.unique_id[2] = (uid_u32 >> 16) & 0xFF;
get_info_response.unique_id[3] = (uid_u32 >> 24) & 0xFF;

uid_u32 = HAL_GetUIDw1();
get_info_response.unique_id[4] = uid_u32 & 0xFF;
get_info_response.unique_id[5] = (uid_u32 >> 8) & 0xFF;
get_info_response.unique_id[6] = (uid_u32 >> 16) & 0xFF;
get_info_response.unique_id[7] = (uid_u32 >> 24) & 0xFF;

uid_u32 = HAL_GetUIDw2();
get_info_response.unique_id[8] = uid_u32 & 0xFF;
get_info_response.unique_id[9] = (uid_u32 >> 8) & 0xFF;
get_info_response.unique_id[10] = (uid_u32 >> 16) & 0xFF;
get_info_response.unique_id[11] = (uid_u32 >> 24) & 0xFF;

platformSpecificReadUniqueID(get_info_response.unique_id);
updateNodeName();
}

Expand Down Expand Up @@ -135,8 +118,7 @@ void ExecuteCommandSubscriber::callback(const CanardRxTransfer& transfer) {

switch (msg.command) {
case uavcan_node_ExecuteCommand_Request_1_0_COMMAND_RESTART:
HAL_NVIC_SystemReset();
cmd_response.status = uavcan_node_ExecuteCommand_Response_1_0_STATUS_SUCCESS;
cmd_response.status = platformSpecificRequestRestart() ? 0 : 1;
break;

case uavcan_node_ExecuteCommand_Request_1_0_COMMAND_STORE_PERSISTENT_STATES:
Expand Down
41 changes: 41 additions & 0 deletions Cyphal/platform_specific.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2024 Dmitry Ponomarev <[email protected]>
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

#ifndef CYPHAL_PLATFORM_SPECIFIC_H_
#define CYPHAL_PLATFORM_SPECIFIC_H_

#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
* @return the time in milliseconds since the application started.
* @note This function must be provided by a user!
*/
uint32_t platformSpecificGetTimeMs();

/**
* @return whether the request will be processed
* True - the application will be restarted soon.
* False - the restarted is not supported or can't be handled at the moment.
* @note Implementation is recommended, but optional.
*/
bool platformSpecificRequestRestart();

/**
* @param[out] out_id - hardware Unique ID
* @note Implementation is recommended, but optional.
*/
void platformSpecificReadUniqueID(uint8_t out_uid[16]);

#ifdef __cplusplus
}
#endif

#endif // CYPHAL_PLATFORM_SPECIFIC_H_
16 changes: 9 additions & 7 deletions Cyphal/weak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
/// Copyright (c) 2022 Dmitry Ponomarev.
/// Author: Dmitry Ponomarev <[email protected]>

#include <stdint.h>
#include "platform_specific.h"
#include <string.h>

/// Functions below are auxiliary.
/// You may provide their implementation outside if you need.
__attribute__ ((weak)) uint32_t HAL_GetUIDw0() { return 0; }
__attribute__ ((weak)) uint32_t HAL_GetUIDw1() { return 0; }
__attribute__ ((weak)) uint32_t HAL_GetUIDw2() { return 0; }
__attribute__ ((weak)) void HAL_NVIC_SystemReset() { }
__attribute__((weak)) bool platformSpecificRequestRestart() {
return false; // because not implemented
}

__attribute__((weak)) void platformSpecificReadUniqueID(uint8_t out_uid[16]) {
memset(out_uid, 0x00, 16);
}
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
# Distributed under the MIT License, available in the file LICENSE.
# Author: Dmitry Ponomarev <[email protected]>

ROOT_DIR:=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
LIBPARAMS_DIR:=${ROOT_DIR}/Libs/libparams

all: ubuntu_minimal ubuntu_publisher_example

define build_example
Expand All @@ -22,6 +25,10 @@ generage_dsdl:
mkdir -p build/nunavut_out
./scripts/nnvg_generate_c_headers.sh build/nunavut_out

clone_dependencies:
mkdir -p build
if [ ! -d "${LIBPARAMS_DIR}" ]; then git clone --depth 1 --branch v0.8.4 https://github.com/PonomarevDA/libparams.git ${LIBPARAMS_DIR}; fi

clean:
rm -rf build/examples/

Expand Down
5 changes: 2 additions & 3 deletions Udral/actuator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
/// Author: Dmitry Ponomarev <[email protected]>

#include "actuator.hpp"
#include "main.h"
#include "params.hpp"

namespace cyphal {
Expand Down Expand Up @@ -61,7 +60,7 @@ int8_t ReadinessSubscriber::init() {
}

uint8_t ReadinessSubscriber::get_readiness() {
if (_last_recv_time_ms == 0 || HAL_GetTick() > _last_recv_time_ms + 500) {
if (_last_recv_time_ms == 0 || platformSpecificGetTimeMs() > _last_recv_time_ms + 500) {
return reg_udral_service_common_Readiness_0_1_SLEEP;
}

Expand All @@ -72,7 +71,7 @@ void ReadinessSubscriber::callback(const CanardRxTransfer& transfer) {
auto payload = static_cast<const uint8_t*>(transfer.payload);
size_t payload_len = transfer.payload_size;
reg_udral_service_common_Readiness_0_1_deserialize_(&msg, payload, &payload_len);
_last_recv_time_ms = HAL_GetTick();
_last_recv_time_ms = platformSpecificGetTimeMs();
}

} // namespace cyphal
1 change: 0 additions & 1 deletion Udral/airspeed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
/// Author: Dmitry Ponomarev <[email protected]>

#include "airspeed.hpp"
#include "main.h"
#include "params.hpp"

namespace cyphal {
Expand Down
1 change: 0 additions & 1 deletion Udral/barometer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
/// Author: Dmitry Ponomarev <[email protected]>

#include "barometer.hpp"
#include "main.h"
#include "cyphal.hpp"
#include "params.hpp"

Expand Down
3 changes: 1 addition & 2 deletions Udral/battery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
/// Author: Dmitry Ponomarev <[email protected]>

#include "battery.hpp"
#include "main.h"
#include "params.hpp"

#define WH_TO_JOULE 3600
Expand All @@ -16,7 +15,7 @@ void UdralBatteryPublisher::set_nominal_data(float design_capacity_ah, uint64_t
}

void UdralBatteryPublisher::publish(float voltage, float current, float temperature_kelvin, float full_capacity_ah, float remaining_capacity_ah) {
uint32_t crnt_time_ms = HAL_GetTick();
uint32_t crnt_time_ms = platformSpecificGetTimeMs();

if (crnt_time_ms > _next_source_pub_time_ms) {
_next_source_pub_time_ms = crnt_time_ms + 10;
Expand Down
1 change: 0 additions & 1 deletion Udral/imu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
/// Author: Dmitry Ponomarev <[email protected]>

#include "imu.hpp"
#include "main.h"
#include "params.hpp"

namespace cyphal {
Expand Down
1 change: 0 additions & 1 deletion Udral/rangefinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
/// Author: Dmitry Ponomarev <[email protected]>

#include "rangefinder.hpp"
#include "main.h"

namespace cyphal {

Expand Down
3 changes: 1 addition & 2 deletions Udral/rgbled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
/// Author: Dmitry Ponomarev <[email protected]>

#include "rgbled.hpp"
#include "main.h"
#include "params.hpp"

namespace cyphal {
Expand Down Expand Up @@ -35,7 +34,7 @@ void HighColorSubscriber::callback(const CanardRxTransfer& transfer) {
auto payload = static_cast<const uint8_t*>(transfer.payload);
size_t payload_len = transfer.payload_size;
reg_udral_physics_optics_HighColor_0_1_deserialize_(&_msg, payload, &payload_len);
_last_recv_ts_ms = HAL_GetTick();
_last_recv_ts_ms = platformSpecificGetTimeMs();
}

} // namespace cyphal
6 changes: 0 additions & 6 deletions examples/main.h → examples/_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,4 @@

#include <stdint.h>

uint32_t HAL_GetTick();
uint32_t HAL_GetUIDw0();
uint32_t HAL_GetUIDw1();
uint32_t HAL_GetUIDw2();
void HAL_NVIC_SystemReset();

#endif // UBUNTU_MAIN_H_
2 changes: 1 addition & 1 deletion examples/ubuntu_minimal/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <chrono>
#include <thread>

uint32_t HAL_GetTick() {
uint32_t platformSpecificGetTimeMs() {
static auto time_start = std::chrono::steady_clock::now();
auto time_now = std::chrono::steady_clock::now();
auto elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(time_now - time_start).count();
Expand Down
3 changes: 0 additions & 3 deletions examples/ubuntu_minimal/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
#ifndef UBUNTU_MAIN_H_
#define UBUNTU_MAIN_H_


uint32_t HAL_GetTick();
uint32_t HAL_GetUIDw0();
uint32_t HAL_GetUIDw1();
uint32_t HAL_GetUIDw2();
void HAL_NVIC_SystemReset();

#endif // UBUNTU_MAIN_H_
3 changes: 1 addition & 2 deletions examples/ubuntu_publisher_example/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
#include <chrono>
#include <thread>
#include "reg/udral/service/actuator/common/Feedback_0_1.h"
#include "main.h"
#include "params.hpp"

void FeedbackPublisher::process() {
static uint32_t prev_pub_time_ms = 1000;
uint32_t crnt_time_ms = HAL_GetTick();
uint32_t crnt_time_ms = platformSpecificGetTimeMs();
if (crnt_time_ms < prev_pub_time_ms + 1000) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/ubuntu_publisher_example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <chrono>
#include <thread>

uint32_t HAL_GetTick() {
uint32_t platformSpecificGetTimeMs() {
static auto time_start = std::chrono::steady_clock::now();
auto time_now = std::chrono::steady_clock::now();
auto elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(time_now - time_start).count();
Expand Down
3 changes: 2 additions & 1 deletion platform_specific/bxcan/cyphal_transport_can.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <string.h>
#include "bxcan.h"
#include "main.h"
#include "platform_specific.h"

namespace cyphal {

Expand Down Expand Up @@ -42,7 +43,7 @@ bool CyphalTransportCan::transmit(const CanardTxQueueItem* transfer) {
return false;
}

const uint64_t current_time = HAL_GetTick() * 1000;
const uint64_t current_time = platformSpecificGetTimeMs() * 1000;
const uint64_t deadline = current_time + 100 * 1000;
const uint8_t iface_index = 0;

Expand Down
4 changes: 2 additions & 2 deletions platform_specific/socketcan/cyphal_transport_can.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "cyphal_transport_can.hpp"
#include <string.h>
#include "socketcan.h"
#include "main.h"
#include "platform_specific.h"

namespace cyphal {

Expand Down Expand Up @@ -47,7 +47,7 @@ bool CyphalTransportCan::transmit(const CanardTxQueueItem* transfer) {
return false;
}

const uint64_t current_time_us = HAL_GetTick() * 1000;
const uint64_t current_time_us = platformSpecificGetTimeMs() * 1000;
int16_t res = socketcanPush(_instance, &transfer->frame, current_time_us);
return (res > 0) ? true : false;
}
Expand Down
1 change: 0 additions & 1 deletion zubax/compact_feedback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
/// Author: Dmitry Ponomarev <[email protected]>

#include "compact_feedback.hpp"
#include "main.h"
#include "params.hpp"

namespace zubax::telega {
Expand Down

0 comments on commit 2e63e03

Please sign in to comment.