Files
DCU_APP/GD32E508_DCU_APP/device/app_spi_cmd_ping.cpp
T

57 lines
2.0 KiB
C++
Raw Normal View History

#include "device/spi_command_handler.h"
#include "device/app_spi_command.h"
#include "device/app_spi_command_defs.h"
#include "sys/config.h"
#include "main.h"
#include <cstring>
namespace {
class SpiAppPingHandler final : public ISpiCommandHandler {
public:
bool canHandle(uint8_t frame_type) const override
{
return frame_type == SpiDefs::kTypeAppPing;
}
void handle(AppSpiCommand &bus, const SpiCommandContext &ctx) override
{
uint8_t alive_payload[19];
std::memset(alive_payload, 0, sizeof(alive_payload));
alive_payload[0] = 0xA1U; /* magic */
alive_payload[1] = 0x55U; /* APP version tag */
alive_payload[2] = (uint8_t)APP_FW_VERSION_MAJOR;
alive_payload[3] = (uint8_t)APP_FW_VERSION_MINOR;
alive_payload[4] = (uint8_t)APP_FW_VERSION_PATCH;
alive_payload[5] = (uint8_t)APP_FW_VERSION_BUILD;
alive_payload[6] = 0xAAU; /* BOOT version tag */
const volatile uint32_t *bv0 = (const volatile uint32_t *)CONFIG_BOOT_VERSION_ADDR0;
const volatile uint32_t *bv1 = (const volatile uint32_t *)CONFIG_BOOT_VERSION_ADDR1;
uint32_t bw0 = *bv0;
uint32_t bw1 = *bv1;
alive_payload[7] = (uint8_t)(bw0 & 0xFFU);
alive_payload[8] = (uint8_t)((bw0 >> 16) & 0xFFU);
alive_payload[9] = (uint8_t)(bw1 & 0xFFU);
alive_payload[10] = (uint8_t)((bw1 >> 16) & 0xFFU);
alive_payload[11] = (uint8_t)(g_app_uptime_sec & 0xFFU);
alive_payload[12] = (uint8_t)((g_app_uptime_sec >> 8) & 0xFFU);
alive_payload[13] = (uint8_t)((g_app_uptime_sec >> 16) & 0xFFU);
alive_payload[14] = (uint8_t)((g_app_uptime_sec >> 24) & 0xFFU);
bus.buildResponse(ctx.tx_buf, ctx.tx_buf_size, ctx.tx_len,
SpiDefs::kRspAck, alive_payload, sizeof(alive_payload));
}
};
static SpiAppPingHandler s_handler;
} // namespace
void registerSpiAppPingCommand(AppSpiCommand &bus)
{
bus.registerHandler(&s_handler);
}