[hal] Add frequency support to DutyCycle (#8076)

This commit is contained in:
Thad House
2025-07-14 23:46:17 -07:00
committed by GitHub
parent ef24c1df97
commit 3497a7d09f
31 changed files with 153 additions and 355 deletions

View File

@@ -4,11 +4,14 @@
#include "hal/DutyCycle.h"
#include <string>
#include "HALInitializer.h"
#include "HALInternal.h"
#include "PortsInternal.h"
#include "hal/Errors.h"
#include "hal/handles/HandlesInternal.h"
#include "hal/handles/LimitedHandleResource.h"
#include "hal/handles/IndexedHandleResource.h"
#include "mockdata/DutyCycleDataInternal.h"
using namespace hal;
@@ -16,16 +19,17 @@ using namespace hal;
namespace {
struct DutyCycle {
uint8_t index;
std::string previousAllocation;
};
struct Empty {};
} // namespace
static LimitedHandleResource<HAL_DutyCycleHandle, DutyCycle, kNumDutyCycles,
static IndexedHandleResource<HAL_DutyCycleHandle, DutyCycle, kNumDutyCycles,
HAL_HandleEnum::DutyCycle>* dutyCycleHandles;
namespace hal::init {
void InitializeDutyCycle() {
static LimitedHandleResource<HAL_DutyCycleHandle, DutyCycle, kNumDutyCycles,
static IndexedHandleResource<HAL_DutyCycleHandle, DutyCycle, kNumDutyCycles,
HAL_HandleEnum::DutyCycle>
dcH;
dutyCycleHandles = &dcH;
@@ -38,23 +42,25 @@ HAL_DutyCycleHandle HAL_InitializeDutyCycle(int32_t channel,
int32_t* status) {
hal::init::CheckInit();
HAL_DutyCycleHandle handle = dutyCycleHandles->Allocate();
if (handle == HAL_kInvalidHandle) {
*status = NO_AVAILABLE_RESOURCES;
return HAL_kInvalidHandle;
}
HAL_DutyCycleHandle handle = HAL_kInvalidHandle;
auto dutyCycle = dutyCycleHandles->Allocate(channel, &handle, status);
auto dutyCycle = dutyCycleHandles->Get(handle);
if (dutyCycle == nullptr) { // would only occur on thread issue
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
if (*status != 0) {
if (dutyCycle) {
hal::SetLastErrorPreviouslyAllocated(status, "SmartIo", channel,
dutyCycle->previousAllocation);
} else {
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Duty Cycle",
0, kNumDutyCycles, channel);
}
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
int16_t index = getHandleIndex(handle);
SimDutyCycleData[index].digitalChannel = channel;
SimDutyCycleData[index].initialized = true;
SimDutyCycleData[index].simDevice = 0;
dutyCycle->index = index;
dutyCycle->previousAllocation = allocationLocation ? allocationLocation : "";
return handle;
}
void HAL_FreeDutyCycle(HAL_DutyCycleHandle dutyCycleHandle) {
@@ -75,8 +81,8 @@ void HAL_SetDutyCycleSimDevice(HAL_EncoderHandle handle,
SimDutyCycleData[dutyCycle->index].simDevice = device;
}
int32_t HAL_GetDutyCycleFrequency(HAL_DutyCycleHandle dutyCycleHandle,
int32_t* status) {
double HAL_GetDutyCycleFrequency(HAL_DutyCycleHandle dutyCycleHandle,
int32_t* status) {
auto dutyCycle = dutyCycleHandles->Get(dutyCycleHandle);
if (dutyCycle == nullptr) {
*status = HAL_HANDLE_ERROR;
@@ -110,19 +116,4 @@ int32_t HAL_GetDutyCycleHighTime(HAL_DutyCycleHandle dutyCycleHandle,
double period = 1e9 / SimDutyCycleData[dutyCycle->index].frequency; // ns
return period * SimDutyCycleData[dutyCycle->index].output;
}
int32_t HAL_GetDutyCycleOutputScaleFactor(HAL_DutyCycleHandle dutyCycleHandle,
int32_t* status) {
return 4e7 - 1;
}
int32_t HAL_GetDutyCycleFPGAIndex(HAL_DutyCycleHandle dutyCycleHandle,
int32_t* status) {
auto dutyCycle = dutyCycleHandles->Get(dutyCycleHandle);
if (dutyCycle == nullptr) {
*status = HAL_HANDLE_ERROR;
return -1;
}
return dutyCycle->index;
}
} // extern "C"

View File

@@ -31,7 +31,7 @@ constexpr int32_t kNumREVPDHModules = 63;
constexpr int32_t kNumREVPDHChannels = 24;
constexpr int32_t kNumPDSimModules = kNumREVPDHModules;
constexpr int32_t kNumPDSimChannels = kNumREVPDHChannels;
constexpr int32_t kNumDutyCycles = 8;
constexpr int32_t kNumDutyCycles = 6;
constexpr int32_t kNumAddressableLEDs = 1;
constexpr int32_t kNumREVPHModules = 63;
constexpr int32_t kNumREVPHChannels = 16;

View File

@@ -17,7 +17,6 @@ void InitializeDutyCycleData() {
DutyCycleData* hal::SimDutyCycleData;
void DutyCycleData::ResetData() {
digitalChannel = 0;
initialized.Reset(false);
simDevice = 0;
frequency.Reset(0);
@@ -25,22 +24,10 @@ void DutyCycleData::ResetData() {
}
extern "C" {
int32_t HALSIM_FindDutyCycleForChannel(int32_t channel) {
for (int i = 0; i < kNumDutyCycles; ++i) {
if (SimDutyCycleData[i].initialized &&
SimDutyCycleData[i].digitalChannel == channel) {
return i;
}
}
return -1;
}
void HALSIM_ResetDutyCycleData(int32_t index) {
SimDutyCycleData[index].ResetData();
}
int32_t HALSIM_GetDutyCycleDigitalChannel(int32_t index) {
return SimDutyCycleData[index].digitalChannel;
}
HAL_SimDeviceHandle HALSIM_GetDutyCycleSimDevice(int32_t index) {
return SimDutyCycleData[index].simDevice;
@@ -51,7 +38,7 @@ HAL_SimDeviceHandle HALSIM_GetDutyCycleSimDevice(int32_t index) {
SimDutyCycleData, LOWERNAME)
DEFINE_CAPI(HAL_Bool, Initialized, initialized)
DEFINE_CAPI(int32_t, Frequency, frequency)
DEFINE_CAPI(double, Frequency, frequency)
DEFINE_CAPI(double, Output, output)
#define REGISTER(NAME) \

View File

@@ -17,11 +17,10 @@ class DutyCycleData {
HAL_SIMDATAVALUE_DEFINE_NAME(Frequency)
public:
std::atomic<int32_t> digitalChannel{0};
SimDataValue<HAL_Bool, HAL_MakeBoolean, GetInitializedName> initialized{
false};
std::atomic<HAL_SimDeviceHandle> simDevice;
SimDataValue<int32_t, HAL_MakeInt, GetFrequencyName> frequency{0};
SimDataValue<double, HAL_MakeDouble, GetFrequencyName> frequency{0};
SimDataValue<double, HAL_MakeDouble, GetOutputName> output{0};
virtual void ResetData();