2020-12-26 14:12:05 -08: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.
|
2016-05-26 12:56:39 -07:00
|
|
|
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/hal/PWM.h"
|
2016-05-26 12:56:39 -07:00
|
|
|
|
2023-07-09 21:28:50 -07:00
|
|
|
#include <algorithm>
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
2016-07-08 21:29:29 -07:00
|
|
|
#include "ConstantsInternal.h"
|
2016-05-26 12:56:39 -07:00
|
|
|
#include "DigitalInternal.h"
|
2018-05-13 22:02:47 -07:00
|
|
|
#include "HALInitializer.h"
|
2021-05-01 10:28:30 -07:00
|
|
|
#include "HALInternal.h"
|
2016-07-02 23:19:14 -07:00
|
|
|
#include "PortsInternal.h"
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/hal/handles/HandlesInternal.h"
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "mockdata/PWMDataInternal.h"
|
2016-05-26 12:56:39 -07:00
|
|
|
|
|
|
|
|
using namespace hal;
|
|
|
|
|
|
2020-12-28 01:19:59 -08:00
|
|
|
namespace hal::init {
|
2017-12-10 19:38:53 -08:00
|
|
|
void InitializePWM() {}
|
2020-12-28 01:19:59 -08:00
|
|
|
} // namespace hal::init
|
2017-12-10 19:38:53 -08:00
|
|
|
|
2016-05-26 12:56:39 -07:00
|
|
|
extern "C" {
|
|
|
|
|
|
2025-01-30 18:59:34 -08:00
|
|
|
HAL_DigitalHandle HAL_InitializePWMPort(int32_t channel,
|
2021-05-01 10:28:30 -07:00
|
|
|
const char* allocationLocation,
|
2016-07-09 00:24:26 -07:00
|
|
|
int32_t* status) {
|
2018-05-13 22:02:47 -07:00
|
|
|
hal::init::CheckInit();
|
2016-06-30 21:39:09 -07:00
|
|
|
|
2025-01-30 18:59:34 -08:00
|
|
|
if (channel < 0 || channel >= kNumPWMChannels) {
|
2021-05-01 10:28:30 -07:00
|
|
|
*status = RESOURCE_OUT_OF_RANGE;
|
|
|
|
|
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for PWM", 0,
|
|
|
|
|
kNumPWMChannels, channel);
|
2016-07-09 00:24:26 -07:00
|
|
|
return HAL_kInvalidHandle;
|
2016-06-30 21:39:09 -07:00
|
|
|
}
|
|
|
|
|
|
2016-08-12 13:45:28 -07:00
|
|
|
uint8_t origChannel = static_cast<uint8_t>(channel);
|
2016-06-30 21:39:09 -07:00
|
|
|
|
2016-08-12 13:45:28 -07:00
|
|
|
if (origChannel < kNumPWMHeaders) {
|
|
|
|
|
channel += kNumDigitalChannels; // remap Headers to end of allocations
|
2016-05-26 12:56:39 -07:00
|
|
|
} else {
|
2016-08-12 13:45:28 -07:00
|
|
|
channel = remapMXPPWMChannel(channel) + 10; // remap MXP to proper channel
|
2016-06-30 21:39:09 -07:00
|
|
|
}
|
|
|
|
|
|
2021-05-01 10:28:30 -07:00
|
|
|
HAL_DigitalHandle handle;
|
|
|
|
|
|
|
|
|
|
auto port = digitalChannelHandles->Allocate(channel, HAL_HandleEnum::PWM,
|
|
|
|
|
&handle, status);
|
2016-06-30 21:39:09 -07:00
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
if (*status != 0) {
|
2021-05-01 10:28:30 -07:00
|
|
|
if (port) {
|
|
|
|
|
hal::SetLastErrorPreviouslyAllocated(status, "PWM or DIO", channel,
|
|
|
|
|
port->previousAllocation);
|
|
|
|
|
} else {
|
|
|
|
|
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for PWM", 0,
|
|
|
|
|
kNumPWMChannels, channel);
|
|
|
|
|
}
|
2016-07-09 00:24:26 -07:00
|
|
|
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2016-06-30 21:39:09 -07:00
|
|
|
|
2016-08-12 13:45:28 -07:00
|
|
|
port->channel = origChannel;
|
2016-06-30 21:39:09 -07:00
|
|
|
|
2018-09-03 16:08:07 -07:00
|
|
|
SimPWMData[origChannel].initialized = true;
|
2016-06-30 21:39:09 -07:00
|
|
|
|
2025-03-20 19:23:22 -07:00
|
|
|
// Disable output.
|
|
|
|
|
HAL_SetPWMPulseTimeMicroseconds(handle, 0, status);
|
2018-03-03 01:56:49 -08:00
|
|
|
|
2021-05-01 10:28:30 -07:00
|
|
|
port->previousAllocation = allocationLocation ? allocationLocation : "";
|
|
|
|
|
|
2016-06-30 21:39:09 -07:00
|
|
|
return handle;
|
|
|
|
|
}
|
2024-09-07 13:58:15 -04:00
|
|
|
void HAL_FreePWMPort(HAL_DigitalHandle pwmPortHandle) {
|
2017-12-10 19:38:53 -08:00
|
|
|
auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM);
|
2016-06-30 21:39:09 -07:00
|
|
|
if (port == nullptr) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-03 16:08:07 -07:00
|
|
|
SimPWMData[port->channel].initialized = false;
|
2016-06-30 21:39:09 -07:00
|
|
|
|
2017-12-10 19:38:53 -08:00
|
|
|
digitalChannelHandles->Free(pwmPortHandle, HAL_HandleEnum::PWM);
|
2016-05-26 12:56:39 -07:00
|
|
|
}
|
|
|
|
|
|
2016-08-12 13:45:28 -07:00
|
|
|
HAL_Bool HAL_CheckPWMChannel(int32_t channel) {
|
|
|
|
|
return channel < kNumPWMChannels && channel >= 0;
|
2016-07-12 10:45:14 -07:00
|
|
|
}
|
2016-06-30 21:39:09 -07:00
|
|
|
|
2025-03-20 19:23:22 -07:00
|
|
|
void HAL_SetPWMSimDevice(HAL_DigitalHandle handle, HAL_SimDeviceHandle device) {
|
|
|
|
|
auto port = digitalChannelHandles->Get(handle, HAL_HandleEnum::PWM);
|
2016-07-08 21:29:29 -07:00
|
|
|
if (port == nullptr) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-03-20 19:23:22 -07:00
|
|
|
SimPWMData[port->channel].simDevice = device;
|
2016-07-08 21:29:29 -07:00
|
|
|
}
|
|
|
|
|
|
2023-06-22 19:43:16 -07:00
|
|
|
void HAL_SetPWMPulseTimeMicroseconds(HAL_DigitalHandle pwmPortHandle,
|
|
|
|
|
int32_t value, int32_t* status) {
|
2017-12-10 19:38:53 -08:00
|
|
|
auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM);
|
2016-06-30 21:39:09 -07:00
|
|
|
if (port == nullptr) {
|
2016-07-03 17:27:06 -07:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
2016-05-26 12:56:39 -07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-22 19:43:16 -07:00
|
|
|
SimPWMData[port->channel].pulseMicrosecond = value;
|
2016-07-08 21:29:29 -07:00
|
|
|
}
|
|
|
|
|
|
2023-06-22 19:43:16 -07:00
|
|
|
int32_t HAL_GetPWMPulseTimeMicroseconds(HAL_DigitalHandle pwmPortHandle,
|
|
|
|
|
int32_t* status) {
|
2017-12-10 19:38:53 -08:00
|
|
|
auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM);
|
2016-06-30 21:39:09 -07:00
|
|
|
if (port == nullptr) {
|
2016-07-03 17:27:06 -07:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
2016-05-26 12:56:39 -07:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-22 19:43:16 -07:00
|
|
|
return SimPWMData[port->channel].pulseMicrosecond;
|
2016-05-26 12:56:39 -07:00
|
|
|
}
|
|
|
|
|
|
2025-03-20 19:23:22 -07:00
|
|
|
void HAL_SetPWMOutputPeriod(HAL_DigitalHandle pwmPortHandle, int32_t period,
|
|
|
|
|
int32_t* status) {
|
2023-06-22 19:43:16 -07:00
|
|
|
auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM);
|
|
|
|
|
if (port == nullptr) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-20 19:23:22 -07:00
|
|
|
SimPWMData[port->channel].outputPeriod = period;
|
2023-06-22 19:43:16 -07:00
|
|
|
}
|
|
|
|
|
|
2017-11-16 01:05:20 -08:00
|
|
|
} // extern "C"
|