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/Counter.h"
|
|
|
|
|
|
2025-01-28 08:58:34 -08:00
|
|
|
#include <cstdio>
|
2024-11-30 18:04:00 +00:00
|
|
|
#include <limits>
|
|
|
|
|
#include <memory>
|
2025-01-28 08:58:34 -08:00
|
|
|
#include <thread>
|
2024-11-30 18:04:00 +00:00
|
|
|
|
|
|
|
|
#include <fmt/format.h>
|
|
|
|
|
|
|
|
|
|
#include "HALInitializer.h"
|
|
|
|
|
#include "HALInternal.h"
|
|
|
|
|
#include "PortsInternal.h"
|
2025-01-28 08:58:34 -08:00
|
|
|
#include "SmartIo.h"
|
2024-11-30 18:04:00 +00:00
|
|
|
#include "hal/HAL.h"
|
2025-01-28 08:58:34 -08:00
|
|
|
#include "hal/cpp/fpga_clock.h"
|
2024-11-30 18:04:00 +00:00
|
|
|
#include "hal/handles/LimitedHandleResource.h"
|
|
|
|
|
|
|
|
|
|
using namespace hal;
|
|
|
|
|
|
|
|
|
|
namespace hal::init {
|
|
|
|
|
void InitializeCounter() {}
|
|
|
|
|
} // namespace hal::init
|
|
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
2025-01-28 08:58:34 -08:00
|
|
|
HAL_CounterHandle HAL_InitializeCounter(int channel, HAL_Bool risingEdge,
|
|
|
|
|
const char* allocationLocation,
|
2024-11-30 18:04:00 +00:00
|
|
|
int32_t* status) {
|
|
|
|
|
hal::init::CheckInit();
|
2025-01-28 08:58:34 -08:00
|
|
|
if (channel == InvalidHandleIndex || channel >= kNumSmartIo) {
|
|
|
|
|
*status = RESOURCE_OUT_OF_RANGE;
|
|
|
|
|
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Counter", 0,
|
|
|
|
|
kNumSmartIo, channel);
|
|
|
|
|
return HAL_kInvalidHandle;
|
|
|
|
|
}
|
2024-11-30 18:04:00 +00:00
|
|
|
|
2025-01-28 08:58:34 -08:00
|
|
|
HAL_CounterHandle handle;
|
2024-11-30 18:04:00 +00:00
|
|
|
|
2025-01-28 08:58:34 -08:00
|
|
|
auto port = smartIoHandles->Allocate(channel, HAL_HandleEnum::Counter,
|
|
|
|
|
&handle, status);
|
2024-11-30 18:04:00 +00:00
|
|
|
|
2025-01-28 08:58:34 -08:00
|
|
|
if (*status != 0) {
|
|
|
|
|
if (port) {
|
|
|
|
|
hal::SetLastErrorPreviouslyAllocated(status, "SmartIo", channel,
|
|
|
|
|
port->previousAllocation);
|
|
|
|
|
} else {
|
|
|
|
|
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Counter", 0,
|
|
|
|
|
kNumSmartIo, channel);
|
|
|
|
|
}
|
|
|
|
|
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
|
|
|
|
|
}
|
2024-11-30 18:04:00 +00:00
|
|
|
|
2025-01-28 08:58:34 -08:00
|
|
|
port->channel = channel;
|
2024-11-30 18:04:00 +00:00
|
|
|
|
2025-01-28 08:58:34 -08:00
|
|
|
*status =
|
|
|
|
|
port->InitializeMode(risingEdge ? SmartIoMode::SingleCounterRising
|
|
|
|
|
: SmartIoMode::SingleCounterFalling);
|
|
|
|
|
if (*status != 0) {
|
|
|
|
|
smartIoHandles->Free(handle, HAL_HandleEnum::Counter);
|
|
|
|
|
return HAL_kInvalidHandle;
|
|
|
|
|
}
|
2024-11-30 18:04:00 +00:00
|
|
|
|
2025-01-28 08:58:34 -08:00
|
|
|
port->previousAllocation = allocationLocation ? allocationLocation : "";
|
2024-11-30 18:04:00 +00:00
|
|
|
|
2025-01-28 08:58:34 -08:00
|
|
|
return handle;
|
2024-11-30 18:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
2025-01-28 08:58:34 -08:00
|
|
|
void HAL_FreeCounter(HAL_CounterHandle counterHandle) {
|
|
|
|
|
auto port = smartIoHandles->Get(counterHandle, HAL_HandleEnum::Counter);
|
|
|
|
|
if (port == nullptr) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-11-30 18:04:00 +00:00
|
|
|
|
2025-01-28 08:58:34 -08:00
|
|
|
smartIoHandles->Free(counterHandle, HAL_HandleEnum::Counter);
|
2024-11-30 18:04:00 +00:00
|
|
|
|
2025-01-28 08:58:34 -08:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
2025-01-28 08:58:34 -08:00
|
|
|
void HAL_SetCounterEdgeConfiguration(HAL_CounterHandle counterHandle,
|
|
|
|
|
HAL_Bool risingEdge, int32_t* status) {
|
2024-11-30 18:04:00 +00:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HAL_ResetCounter(HAL_CounterHandle counterHandle, int32_t* status) {
|
2025-01-28 08:58:34 -08:00
|
|
|
// TODO
|
2024-11-30 18:04:00 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t HAL_GetCounter(HAL_CounterHandle counterHandle, int32_t* status) {
|
2025-01-28 08:58:34 -08:00
|
|
|
auto port = smartIoHandles->Get(counterHandle, HAL_HandleEnum::Counter);
|
|
|
|
|
if (port == nullptr) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t ret = 0;
|
|
|
|
|
*status = port->GetCounter(&ret);
|
|
|
|
|
return ret;
|
2024-11-30 18:04:00 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double HAL_GetCounterPeriod(HAL_CounterHandle counterHandle, int32_t* status) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HAL_SetCounterMaxPeriod(HAL_CounterHandle counterHandle, double maxPeriod,
|
|
|
|
|
int32_t* status) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HAL_Bool HAL_GetCounterStopped(HAL_CounterHandle counterHandle,
|
|
|
|
|
int32_t* status) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HAL_Bool HAL_GetCounterDirection(HAL_CounterHandle counterHandle,
|
|
|
|
|
int32_t* status) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-28 08:58:34 -08:00
|
|
|
void HAL_SetCounterDirection(HAL_CounterHandle counterHandle, HAL_Bool countUp,
|
|
|
|
|
int32_t* status) {
|
2024-11-30 18:04:00 +00:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // extern "C"
|