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.
|
|
|
|
|
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/hal/Counter.h"
|
2024-11-30 18:04:00 +00:00
|
|
|
|
2025-01-28 08:58:34 -08:00
|
|
|
#include <cstdio>
|
|
|
|
|
#include <thread>
|
2024-11-30 18:04:00 +00:00
|
|
|
|
|
|
|
|
#include <fmt/format.h>
|
|
|
|
|
|
2026-01-04 00:41:53 -08:00
|
|
|
#include "HALInitializer.hpp"
|
|
|
|
|
#include "PortsInternal.hpp"
|
|
|
|
|
#include "SmartIo.hpp"
|
2026-04-17 20:19:38 -07:00
|
|
|
#include "wpi/hal/ErrorHandling.hpp"
|
2026-03-15 15:08:41 -07:00
|
|
|
#include "wpi/hal/monotonic_clock.hpp"
|
2024-11-30 18:04:00 +00:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
using namespace wpi::hal;
|
2024-11-30 18:04:00 +00:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
namespace wpi::hal::init {
|
2024-11-30 18:04:00 +00:00
|
|
|
void InitializeCounter() {}
|
2025-11-07 20:00:05 -05:00
|
|
|
} // namespace wpi::hal::init
|
2024-11-30 18:04:00 +00:00
|
|
|
|
|
|
|
|
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) {
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::hal::init::CheckInit();
|
2026-03-16 21:49:21 -07:00
|
|
|
if (channel == INVALID_HANDLE_INDEX || channel >= kNumSmartIo) {
|
2026-04-17 20:19:38 -07:00
|
|
|
*status = MakeErrorIndexOutOfRange(RESOURCE_OUT_OF_RANGE,
|
|
|
|
|
"Invalid Index for Counter", 0,
|
|
|
|
|
kNumSmartIo, channel);
|
2026-03-21 00:34:46 -07:00
|
|
|
return HAL_INVALID_HANDLE;
|
2025-01-28 08:58:34 -08:00
|
|
|
}
|
2024-11-30 18:04:00 +00:00
|
|
|
|
2026-04-17 20:19:38 -07:00
|
|
|
auto resource =
|
|
|
|
|
smartIoHandles->Allocate(channel, HAL_HandleEnum::COUNTER, "Counter");
|
2024-11-30 18:04:00 +00:00
|
|
|
|
2026-04-17 20:19:38 -07:00
|
|
|
if (!resource) {
|
|
|
|
|
*status = resource.error();
|
2026-03-21 00:34:46 -07:00
|
|
|
return HAL_INVALID_HANDLE; // failed to allocate. Pass error back.
|
2025-01-28 08:58:34 -08:00
|
|
|
}
|
2024-11-30 18:04:00 +00:00
|
|
|
|
2026-04-17 20:19:38 -07:00
|
|
|
auto [handle, port] = *resource;
|
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) {
|
2026-03-16 21:49:21 -07:00
|
|
|
smartIoHandles->Free(handle, HAL_HandleEnum::COUNTER);
|
2026-03-21 00:34:46 -07:00
|
|
|
return HAL_INVALID_HANDLE;
|
2025-01-28 08:58:34 -08:00
|
|
|
}
|
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) {
|
2026-03-16 21:49:21 -07:00
|
|
|
auto port = smartIoHandles->Get(counterHandle, HAL_HandleEnum::COUNTER);
|
2025-01-28 08:58:34 -08:00
|
|
|
if (port == nullptr) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-11-30 18:04:00 +00:00
|
|
|
|
2026-03-16 21:49:21 -07: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.
|
2026-03-15 15:08:41 -07:00
|
|
|
auto start = wpi::hal::monotonic_clock::now();
|
2025-01-28 08:58:34 -08:00
|
|
|
while (port.use_count() != 1) {
|
2026-03-15 15:08:41 -07:00
|
|
|
auto current = wpi::hal::monotonic_clock::now();
|
2025-01-28 08:58:34 -08:00
|
|
|
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) {
|
2026-03-16 21:49:21 -07:00
|
|
|
auto port = smartIoHandles->Get(counterHandle, HAL_HandleEnum::COUNTER);
|
2025-01-28 08:58:34 -08:00
|
|
|
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"
|