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/PWM.h"
|
2024-11-30 18:04:00 +00:00
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
#include <cstdio>
|
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
|
|
#include <fmt/format.h>
|
|
|
|
|
|
2026-01-04 00:41:53 -08:00
|
|
|
#include "HALInitializer.hpp"
|
|
|
|
|
#include "HALInternal.hpp"
|
|
|
|
|
#include "PortsInternal.hpp"
|
|
|
|
|
#include "SmartIo.hpp"
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/hal/Errors.h"
|
2026-01-04 00:41:53 -08:00
|
|
|
#include "wpi/hal/handles/HandlesInternal.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 InitializePWM() {}
|
2025-11-07 20:00:05 -05:00
|
|
|
} // namespace wpi::hal::init
|
2024-11-30 18:04:00 +00:00
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
2025-01-30 18:59:34 -08:00
|
|
|
HAL_DigitalHandle HAL_InitializePWMPort(int32_t channel,
|
2024-11-30 18:04:00 +00:00
|
|
|
const char* allocationLocation,
|
|
|
|
|
int32_t* status) {
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::hal::init::CheckInit();
|
2024-12-08 12:02:22 -08:00
|
|
|
|
2025-01-30 18:59:34 -08:00
|
|
|
if (channel < 0 || channel >= kNumSmartIo) {
|
2024-12-08 12:02:22 -08:00
|
|
|
*status = RESOURCE_OUT_OF_RANGE;
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for PWM", 0,
|
2025-11-07 20:01:58 -05:00
|
|
|
kNumSmartIo, channel);
|
2026-03-21 00:34:46 -07:00
|
|
|
return HAL_INVALID_HANDLE;
|
2024-12-08 12:02:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HAL_DigitalHandle handle;
|
|
|
|
|
|
|
|
|
|
auto port =
|
|
|
|
|
smartIoHandles->Allocate(channel, HAL_HandleEnum::PWM, &handle, status);
|
|
|
|
|
|
|
|
|
|
if (*status != 0) {
|
|
|
|
|
if (port) {
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::hal::SetLastErrorPreviouslyAllocated(status, "SmartIo", channel,
|
2025-11-07 20:01:58 -05:00
|
|
|
port->previousAllocation);
|
2024-12-08 12:02:22 -08:00
|
|
|
} else {
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for PWM", 0,
|
2025-11-07 20:01:58 -05:00
|
|
|
kNumSmartIo, channel);
|
2024-12-08 12:02:22 -08:00
|
|
|
}
|
2026-03-21 00:34:46 -07:00
|
|
|
return HAL_INVALID_HANDLE; // failed to allocate. Pass error back.
|
2024-12-08 12:02:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
port->channel = channel;
|
|
|
|
|
|
2025-01-12 16:58:46 -08:00
|
|
|
*status = port->InitializeMode(SmartIoMode::PwmOutput);
|
2024-12-08 12:02:22 -08:00
|
|
|
if (*status != 0) {
|
|
|
|
|
smartIoHandles->Free(handle, HAL_HandleEnum::PWM);
|
2026-03-21 00:34:46 -07:00
|
|
|
return HAL_INVALID_HANDLE;
|
2024-12-08 12:02:22 -08:00
|
|
|
}
|
|
|
|
|
|
2025-03-20 19:23:22 -07:00
|
|
|
// Disable the PWM output.
|
|
|
|
|
HAL_SetPWMPulseTimeMicroseconds(handle, 0, status);
|
2024-12-08 12:02:22 -08:00
|
|
|
if (*status != 0) {
|
|
|
|
|
smartIoHandles->Free(handle, HAL_HandleEnum::PWM);
|
2026-03-21 00:34:46 -07:00
|
|
|
return HAL_INVALID_HANDLE;
|
2024-12-08 12:02:22 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
port->previousAllocation = allocationLocation ? allocationLocation : "";
|
|
|
|
|
|
|
|
|
|
return handle;
|
2024-11-30 18:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
2024-12-08 12:02:22 -08:00
|
|
|
void HAL_FreePWMPort(HAL_DigitalHandle pwmPortHandle) {
|
|
|
|
|
auto port = smartIoHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM);
|
|
|
|
|
if (port == nullptr) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
smartIoHandles->Free(pwmPortHandle, HAL_HandleEnum::PWM);
|
|
|
|
|
|
|
|
|
|
// Wait for no other object to hold this handle.
|
2026-03-15 15:08:41 -07:00
|
|
|
auto start = wpi::hal::monotonic_clock::now();
|
2024-12-08 12:02:22 -08:00
|
|
|
while (port.use_count() != 1) {
|
2026-03-15 15:08:41 -07:00
|
|
|
auto current = wpi::hal::monotonic_clock::now();
|
2024-12-08 12:02:22 -08:00
|
|
|
if (start + std::chrono::seconds(1) < current) {
|
|
|
|
|
std::puts("PWM handle free timeout");
|
|
|
|
|
std::fflush(stdout);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
std::this_thread::yield();
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-30 18:04:00 +00:00
|
|
|
|
2025-03-20 19:23:22 -07:00
|
|
|
void HAL_SetPWMSimDevice(HAL_DigitalHandle handle, HAL_SimDeviceHandle device) {
|
2024-11-30 18:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
2025-03-20 19:23:22 -07:00
|
|
|
HAL_Bool HAL_CheckPWMChannel(int32_t channel) {
|
|
|
|
|
return channel < kNumSmartIo && channel >= 0;
|
2024-11-30 18:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HAL_SetPWMPulseTimeMicroseconds(HAL_DigitalHandle pwmPortHandle,
|
|
|
|
|
int32_t microsecondPulseTime,
|
|
|
|
|
int32_t* status) {
|
2024-12-08 12:02:22 -08:00
|
|
|
auto port = smartIoHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM);
|
|
|
|
|
if (port == nullptr) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (microsecondPulseTime < 0 ||
|
|
|
|
|
(microsecondPulseTime != 0xFFFF && microsecondPulseTime >= 4096)) {
|
|
|
|
|
*status = PARAMETER_OUT_OF_RANGE;
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::hal::SetLastError(
|
2024-12-08 12:02:22 -08:00
|
|
|
status,
|
|
|
|
|
fmt::format("Pulse time {} out of range. Expect [0-4096) or 0xFFFF",
|
|
|
|
|
microsecondPulseTime));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*status = port->SetPwmMicroseconds(microsecondPulseTime);
|
2024-11-30 18:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t HAL_GetPWMPulseTimeMicroseconds(HAL_DigitalHandle pwmPortHandle,
|
|
|
|
|
int32_t* status) {
|
2024-12-08 12:02:22 -08:00
|
|
|
auto port = smartIoHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM);
|
|
|
|
|
if (port == nullptr) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint16_t microseconds = 0;
|
|
|
|
|
*status = port->GetPwmMicroseconds(µseconds);
|
|
|
|
|
return microseconds;
|
2024-11-30 18:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
2025-03-20 19:23:22 -07:00
|
|
|
void HAL_SetPWMOutputPeriod(HAL_DigitalHandle pwmPortHandle, int32_t period,
|
|
|
|
|
int32_t* status) {
|
2025-01-12 16:58:46 -08:00
|
|
|
auto port = smartIoHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM);
|
|
|
|
|
if (port == nullptr) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-20 19:23:22 -07:00
|
|
|
switch (period) {
|
2025-01-12 16:58:46 -08:00
|
|
|
case 0:
|
2025-11-07 20:00:05 -05:00
|
|
|
*status = port->SetPwmOutputPeriod(wpi::hal::PwmOutputPeriod::k5ms);
|
2025-01-12 16:58:46 -08:00
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
case 2:
|
2025-11-07 20:00:05 -05:00
|
|
|
*status = port->SetPwmOutputPeriod(wpi::hal::PwmOutputPeriod::k10ms);
|
2025-01-12 16:58:46 -08:00
|
|
|
break;
|
|
|
|
|
case 3:
|
2025-11-07 20:00:05 -05:00
|
|
|
*status = port->SetPwmOutputPeriod(wpi::hal::PwmOutputPeriod::k20ms);
|
2025-01-12 16:58:46 -08:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
*status = PARAMETER_OUT_OF_RANGE;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-11-30 18:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // extern "C"
|