Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
module;
#include <array>
#include <cstdint>
#include <cstddef>
#include <functional>
#include <utility>
#include <atomic>
#include "stm32f1xx_hal_gpio.h"
module Driver.PulseCounterDriver;
import Driver.PulseCounterId;
namespace
{
static constexpr std::uint8_t PULSE_COUNTER_COUNT =
std::to_underlying(Driver::PulseCounterId::LastNotUsed);
// Modified in interruption, shared by all instances of PulseCounterDriver.
// Because this is at namespace scope, it has static storage duration and
// is zero-initialized before any other initialization.
alignas(std::uint32_t) std::array<std::atomic<Driver::PulseCount>, PULSE_COUNTER_COUNT> rawPulseCounters;
}
// We are the client of PulseCounterId; verify enum values because
// the program will fail at runtime if the values are incorrect.
static_assert(std::to_underlying(Driver::PulseCounterId::bncA) == 0U,
"PulseCounterId::bncA must be 0");
static_assert(std::to_underlying(Driver::PulseCounterId::bncB) == 1U,
"PulseCounterId::bncB must be 1");
static_assert(std::to_underlying(Driver::PulseCounterId::bncC) == 2U,
"PulseCounterId::bncC must be 2");
static_assert(std::to_underlying(Driver::PulseCounterId::bncD) == 3U,
"PulseCounterId::bncD must be 3");
static_assert(std::to_underlying(Driver::PulseCounterId::LastNotUsed) == 4U,
"PulseCounterId::LastNotUsed must be 4 (count of valid BNC connectors)");
// Global HAL EXTI callback for the entire MCU.
// CubeMX provides a weak default; this definition overrides it and therefore becomes the
// central dispatch point for all GPIO EXTI lines in the system.
// It is implemented here because EXTI events are owned by this driver in this project;
// if more EXTI users are added later, consider moving this to a dedicated IRQ dispatcher.
extern "C" void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
static constexpr std::uint8_t COUNTER_1 = std::to_underlying(Driver::PulseCounterId::bncA);
static constexpr std::uint8_t COUNTER_2 = std::to_underlying(Driver::PulseCounterId::bncB);
static constexpr std::uint8_t COUNTER_3 = std::to_underlying(Driver::PulseCounterId::bncC);
static constexpr std::uint8_t COUNTER_4 = std::to_underlying(Driver::PulseCounterId::bncD);
switch (GPIO_Pin)
{
case GPIO_PIN_6:
rawPulseCounters[COUNTER_1].fetch_add(1, std::memory_order_relaxed);
break;
case GPIO_PIN_7:
rawPulseCounters[COUNTER_2].fetch_add(1, std::memory_order_relaxed);
break;
case GPIO_PIN_8:
rawPulseCounters[COUNTER_3].fetch_add(1, std::memory_order_relaxed);
break;
case GPIO_PIN_9:
rawPulseCounters[COUNTER_4].fetch_add(1, std::memory_order_relaxed);
break;
default:
break;
}
}
namespace Driver
{
PulseCounterDriver::PulseCounterDriver(PulseCounterId deviceId) noexcept
: deviceId(deviceId)
{
}
auto PulseCounterDriver::onStart() noexcept -> bool
{
clear();
return true;
}
auto PulseCounterDriver::fetchAndReset() const noexcept -> PulseCount
{
return rawPulseCounters[std::to_underlying(deviceId)].exchange(
0,
std::memory_order_relaxed);
}
auto PulseCounterDriver::clear() noexcept -> void
{
rawPulseCounters[std::to_underlying(deviceId)].store(0U, std::memory_order_relaxed);
}
} // namespace Driverd4cbf8c64This file is pinned to the selected release. Download the original to open it in your design tool.