2024-11-30 18:04:00 +00:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
|
|
|
|
|
|
|
|
|
#include "hal/DutyCycle.h"
|
|
|
|
|
|
2025-01-15 11:57:31 -08:00
|
|
|
#include <cstdio>
|
2024-11-30 18:04:00 +00:00
|
|
|
#include <memory>
|
2025-01-15 11:57:31 -08:00
|
|
|
#include <thread>
|
2024-11-30 18:04:00 +00:00
|
|
|
|
|
|
|
|
#include "HALInitializer.h"
|
2025-01-15 11:57:31 -08:00
|
|
|
#include "HALInternal.h"
|
2024-11-30 18:04:00 +00:00
|
|
|
#include "PortsInternal.h"
|
2025-01-15 11:57:31 -08:00
|
|
|
#include "SmartIo.h"
|
2024-11-30 18:04:00 +00:00
|
|
|
#include "hal/Errors.h"
|
2025-01-15 11:57:31 -08:00
|
|
|
#include "hal/cpp/fpga_clock.h"
|
2024-11-30 18:04:00 +00:00
|
|
|
#include "hal/handles/HandlesInternal.h"
|
|
|
|
|
#include "hal/handles/LimitedHandleResource.h"
|
|
|
|
|
|
|
|
|
|
using namespace hal;
|
|
|
|
|
|
|
|
|
|
namespace hal::init {
|
|
|
|
|
void InitializeDutyCycle() {}
|
|
|
|
|
} // namespace hal::init
|
|
|
|
|
|
|
|
|
|
extern "C" {
|
2025-01-30 18:59:34 -08:00
|
|
|
HAL_DutyCycleHandle HAL_InitializeDutyCycle(int32_t channel,
|
2025-01-15 11:57:31 -08:00
|
|
|
const char* allocationLocation,
|
2024-11-30 18:04:00 +00:00
|
|
|
int32_t* status) {
|
|
|
|
|
hal::init::CheckInit();
|
|
|
|
|
|
2025-01-30 18:59:34 -08:00
|
|
|
if (channel < 0 || channel >= kNumSmartIo) {
|
2025-01-15 11:57:31 -08:00
|
|
|
*status = RESOURCE_OUT_OF_RANGE;
|
|
|
|
|
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for DutyCycle", 0,
|
|
|
|
|
kNumSmartIo, channel);
|
|
|
|
|
return HAL_kInvalidHandle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HAL_DigitalHandle handle;
|
|
|
|
|
|
|
|
|
|
auto port = smartIoHandles->Allocate(channel, HAL_HandleEnum::DutyCycle,
|
|
|
|
|
&handle, status);
|
|
|
|
|
|
|
|
|
|
if (*status != 0) {
|
|
|
|
|
if (port) {
|
|
|
|
|
hal::SetLastErrorPreviouslyAllocated(status, "SmartIo", channel,
|
|
|
|
|
port->previousAllocation);
|
|
|
|
|
} else {
|
|
|
|
|
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for DutyCycle", 0,
|
|
|
|
|
kNumSmartIo, channel);
|
|
|
|
|
}
|
|
|
|
|
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
port->channel = channel;
|
|
|
|
|
|
|
|
|
|
*status = port->InitializeMode(SmartIoMode::PwmInput);
|
|
|
|
|
if (*status != 0) {
|
|
|
|
|
smartIoHandles->Free(handle, HAL_HandleEnum::DutyCycle);
|
|
|
|
|
return HAL_kInvalidHandle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
port->previousAllocation = allocationLocation ? allocationLocation : "";
|
|
|
|
|
|
|
|
|
|
return handle;
|
|
|
|
|
}
|
|
|
|
|
void HAL_FreeDutyCycle(HAL_DutyCycleHandle dutyCycleHandle) {
|
|
|
|
|
auto port = smartIoHandles->Get(dutyCycleHandle, HAL_HandleEnum::DutyCycle);
|
|
|
|
|
if (port == nullptr) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
smartIoHandles->Free(dutyCycleHandle, HAL_HandleEnum::DutyCycle);
|
|
|
|
|
|
|
|
|
|
// Wait for no other object to hold this handle.
|
|
|
|
|
auto start = hal::fpga_clock::now();
|
|
|
|
|
while (port.use_count() != 1) {
|
|
|
|
|
auto current = hal::fpga_clock::now();
|
|
|
|
|
if (start + std::chrono::seconds(1) < current) {
|
|
|
|
|
std::puts("DIO handle free timeout");
|
|
|
|
|
std::fflush(stdout);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
std::this_thread::yield();
|
|
|
|
|
}
|
2024-11-30 18:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HAL_SetDutyCycleSimDevice(HAL_EncoderHandle handle,
|
|
|
|
|
HAL_SimDeviceHandle device) {}
|
|
|
|
|
|
|
|
|
|
int32_t HAL_GetDutyCycleFrequency(HAL_DutyCycleHandle dutyCycleHandle,
|
|
|
|
|
int32_t* status) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double HAL_GetDutyCycleOutput(HAL_DutyCycleHandle dutyCycleHandle,
|
|
|
|
|
int32_t* status) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t HAL_GetDutyCycleHighTime(HAL_DutyCycleHandle dutyCycleHandle,
|
|
|
|
|
int32_t* status) {
|
2025-01-15 11:57:31 -08:00
|
|
|
auto port = smartIoHandles->Get(dutyCycleHandle, HAL_HandleEnum::DutyCycle);
|
|
|
|
|
if (port == nullptr) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint16_t ret = false;
|
|
|
|
|
*status = port->GetPwmInputMicroseconds(&ret);
|
|
|
|
|
return ret;
|
2024-11-30 18:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t HAL_GetDutyCycleOutputScaleFactor(HAL_DutyCycleHandle dutyCycleHandle,
|
|
|
|
|
int32_t* status) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t HAL_GetDutyCycleFPGAIndex(HAL_DutyCycleHandle dutyCycleHandle,
|
|
|
|
|
int32_t* status) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
} // extern "C"
|