From fc515f457214d6eb42051db4e6530834ff4590b2 Mon Sep 17 00:00:00 2001 From: Thad House Date: Sun, 5 Jun 2016 15:23:58 -0700 Subject: [PATCH] Changes HAL Port from a pointer to a handle (#93) HAL Port is using a special handle, where the module and pin are bit shifted straight into the handle. This is one of the few special cases we have, but for the way port is used it is much cleaner and uses much less memory. Plus it is generic and not specific to one type. --- hal/include/HAL/AnalogInput.h | 4 +- hal/include/HAL/AnalogOutput.h | 4 +- hal/include/HAL/AnalogTrigger.h | 4 +- hal/include/HAL/DIO.h | 4 +- hal/include/HAL/HAL.h | 7 +- hal/include/HAL/Handles.h | 2 + hal/include/HAL/Port.h | 13 --- hal/include/HAL/Solenoid.h | 4 +- hal/lib/athena/AnalogAccumulator.cpp | 2 +- hal/lib/athena/AnalogInput.cpp | 39 +++++---- hal/lib/athena/AnalogInternal.cpp | 1 - hal/lib/athena/AnalogInternal.h | 3 +- hal/lib/athena/AnalogOutput.cpp | 19 +++-- hal/lib/athena/AnalogTrigger.cpp | 13 +-- hal/lib/athena/DIO.cpp | 85 ++++++++++--------- hal/lib/athena/DigitalInternal.cpp | 3 +- hal/lib/athena/DigitalInternal.h | 3 +- hal/lib/athena/HALAthena.cpp | 23 ++--- hal/lib/athena/I2C.cpp | 10 ++- hal/lib/athena/PWM.cpp | 49 ++++++----- hal/lib/athena/Relay.cpp | 14 +-- hal/lib/athena/SPI.cpp | 14 ++- hal/lib/athena/Solenoid.cpp | 20 +++-- hal/lib/athena/handles/HandlesInternal.cpp | 13 +++ hal/lib/athena/handles/HandlesInternal.h | 25 ++++++ wpilibc/athena/src/AnalogInput.cpp | 3 +- wpilibc/athena/src/AnalogOutput.cpp | 3 +- wpilibc/athena/src/AnalogTrigger.cpp | 3 +- wpilibc/athena/src/SensorBase.cpp | 7 +- wpilibc/athena/src/SolenoidBase.cpp | 2 +- wpilibj/src/athena/cpp/lib/AnalogJNI.cpp | 24 +++--- wpilibj/src/athena/cpp/lib/DIOJNI.cpp | 8 +- wpilibj/src/athena/cpp/lib/JNIWrapper.cpp | 28 +++--- wpilibj/src/athena/cpp/lib/SolenoidJNI.cpp | 23 ++--- .../edu/wpi/first/wpilibj/AnalogInput.java | 4 +- .../edu/wpi/first/wpilibj/AnalogOutput.java | 4 +- .../edu/wpi/first/wpilibj/AnalogTrigger.java | 4 +- .../edu/wpi/first/wpilibj/DigitalSource.java | 4 +- .../java/edu/wpi/first/wpilibj/Solenoid.java | 4 +- .../edu/wpi/first/wpilibj/SolenoidBase.java | 4 +- .../edu/wpi/first/wpilibj/hal/AnalogJNI.java | 6 +- .../edu/wpi/first/wpilibj/hal/DIOJNI.java | 2 +- .../edu/wpi/first/wpilibj/hal/JNIWrapper.java | 6 +- .../wpi/first/wpilibj/hal/SolenoidJNI.java | 4 +- 44 files changed, 288 insertions(+), 233 deletions(-) delete mode 100644 hal/include/HAL/Port.h diff --git a/hal/include/HAL/AnalogInput.h b/hal/include/HAL/AnalogInput.h index 53f43143a4..62876fb178 100644 --- a/hal/include/HAL/AnalogInput.h +++ b/hal/include/HAL/AnalogInput.h @@ -9,9 +9,11 @@ #include +#include "Handles.h" + extern "C" { // Analog input functions -void* initializeAnalogInputPort(void* port_pointer, int32_t* status); +void* initializeAnalogInputPort(HalPortHandle port_handle, int32_t* status); void freeAnalogInputPort(void* analog_port_pointer); bool checkAnalogModule(uint8_t module); bool checkAnalogInputChannel(uint32_t pin); diff --git a/hal/include/HAL/AnalogOutput.h b/hal/include/HAL/AnalogOutput.h index c86d27ee85..752917bf29 100644 --- a/hal/include/HAL/AnalogOutput.h +++ b/hal/include/HAL/AnalogOutput.h @@ -9,8 +9,10 @@ #include +#include "Handles.h" + extern "C" { -void* initializeAnalogOutputPort(void* port_pointer, int32_t* status); +void* initializeAnalogOutputPort(HalPortHandle port_handle, int32_t* status); void freeAnalogOutputPort(void* analog_port_pointer); void setAnalogOutput(void* analog_port_pointer, double voltage, int32_t* status); diff --git a/hal/include/HAL/AnalogTrigger.h b/hal/include/HAL/AnalogTrigger.h index 63446d88c3..0c09210420 100644 --- a/hal/include/HAL/AnalogTrigger.h +++ b/hal/include/HAL/AnalogTrigger.h @@ -9,6 +9,8 @@ #include +#include "Handles.h" + enum AnalogTriggerType { kInWindow = 0, kState = 1, @@ -17,7 +19,7 @@ enum AnalogTriggerType { }; extern "C" { -void* initializeAnalogTrigger(void* port_pointer, uint32_t* index, +void* initializeAnalogTrigger(HalPortHandle port_handle, uint32_t* index, int32_t* status); void cleanAnalogTrigger(void* analog_trigger_pointer, int32_t* status); void setAnalogTriggerLimitsRaw(void* analog_trigger_pointer, int32_t lower, diff --git a/hal/include/HAL/DIO.h b/hal/include/HAL/DIO.h index 0ec2c14cd5..22cbd0ea78 100644 --- a/hal/include/HAL/DIO.h +++ b/hal/include/HAL/DIO.h @@ -9,10 +9,12 @@ #include +#include "Handles.h" + extern "C" { // the following 2 functions are here as they will be changed with // the handle changes to be DIO exclusive. -void* initializeDigitalPort(void* port_pointer, int32_t* status); +void* initializeDigitalPort(HalPortHandle port_handle, int32_t* status); void freeDigitalPort(void* digital_port_pointer); void* allocatePWM(int32_t* status); diff --git a/hal/include/HAL/HAL.h b/hal/include/HAL/HAL.h index 6df4d5b970..fea749cd66 100644 --- a/hal/include/HAL/HAL.h +++ b/hal/include/HAL/HAL.h @@ -19,6 +19,7 @@ #include "DIO.h" #include "Encoder.h" #include "Errors.h" +#include "Handles.h" #include "I2C.h" #include "Interrupts.h" #include "Notifier.h" @@ -211,9 +212,9 @@ extern const uint32_t solenoid_kNumDO7_0Elements; extern const uint32_t interrupt_kNumSystems; extern const uint32_t kSystemClockTicksPerMicrosecond; -void* getPort(uint8_t pin); -void* getPortWithModule(uint8_t module, uint8_t pin); -void freePort(void* port); +HalPortHandle getPort(uint8_t pin); +HalPortHandle getPortWithModule(uint8_t module, uint8_t pin); +void freePort(HalPortHandle port); const char* getHALErrorMessage(int32_t code); uint16_t getFPGAVersion(int32_t* status); diff --git a/hal/include/HAL/Handles.h b/hal/include/HAL/Handles.h index 2110b5c22b..f269d5bc98 100644 --- a/hal/include/HAL/Handles.h +++ b/hal/include/HAL/Handles.h @@ -17,3 +17,5 @@ #define HAL_HANDLE_NEGATIVE_INDEX -5 typedef int32_t HalHandle; + +typedef HalHandle HalPortHandle; diff --git a/hal/include/HAL/Port.h b/hal/include/HAL/Port.h deleted file mode 100644 index 9e5aa1720b..0000000000 --- a/hal/include/HAL/Port.h +++ /dev/null @@ -1,13 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/* Copyright (c) FIRST 2016. All Rights Reserved. */ -/* Open Source Software - may be modified and shared by FRC teams. The code */ -/* must be accompanied by the FIRST BSD license file in the root directory of */ -/* the project. */ -/*----------------------------------------------------------------------------*/ - -#pragma once - -typedef struct port_t { - uint8_t pin; - uint8_t module; -} Port; diff --git a/hal/include/HAL/Solenoid.h b/hal/include/HAL/Solenoid.h index b6309b4713..41f4ccb4d1 100644 --- a/hal/include/HAL/Solenoid.h +++ b/hal/include/HAL/Solenoid.h @@ -9,8 +9,10 @@ #include +#include "Handles.h" + extern "C" { -void* initializeSolenoidPort(void* port_pointer, int32_t* status); +void* initializeSolenoidPort(HalPortHandle port_handle, int32_t* status); void freeSolenoidPort(void* solenoid_port_pointer); bool checkSolenoidModule(uint8_t module); diff --git a/hal/lib/athena/AnalogAccumulator.cpp b/hal/lib/athena/AnalogAccumulator.cpp index a758e7380d..3181c220c5 100644 --- a/hal/lib/athena/AnalogAccumulator.cpp +++ b/hal/lib/athena/AnalogAccumulator.cpp @@ -21,7 +21,7 @@ extern "C" { bool isAccumulatorChannel(void* analog_port_pointer, int32_t* status) { AnalogPort* port = (AnalogPort*)analog_port_pointer; for (uint32_t i = 0; i < kAccumulatorNumChannels; i++) { - if (port->port.pin == kAccumulatorChannels[i]) return true; + if (port->pin == kAccumulatorChannels[i]) return true; } return false; } diff --git a/hal/lib/athena/AnalogInput.cpp b/hal/lib/athena/AnalogInput.cpp index c2ab7c85b3..d222c46e95 100644 --- a/hal/lib/athena/AnalogInput.cpp +++ b/hal/lib/athena/AnalogInput.cpp @@ -14,6 +14,7 @@ #include "HAL/AnalogAccumulator.h" #include "HAL/HAL.h" #include "HAL/cpp/priority_mutex.h" +#include "handles/HandlesInternal.h" using namespace hal; @@ -23,20 +24,27 @@ extern "C" { /** * Initialize the analog input port using the given port object. */ -void* initializeAnalogInputPort(void* port_pointer, int32_t* status) { +void* initializeAnalogInputPort(HalPortHandle port_handle, int32_t* status) { initializeAnalog(status); - Port* port = (Port*)port_pointer; + + if (*status != 0) return nullptr; + + int16_t pin = getPortHandlePin(port_handle); + if (pin == HAL_HANDLE_INVALID_TYPE) { + *status = PARAMETER_OUT_OF_RANGE; + return nullptr; + } // Initialize port structure AnalogPort* analog_port = new AnalogPort(); - analog_port->port = *port; + analog_port->pin = (uint8_t)pin; if (isAccumulatorChannel(analog_port, status)) { - analog_port->accumulator = tAccumulator::create(port->pin, status); + analog_port->accumulator = tAccumulator::create(pin, status); } else analog_port->accumulator = nullptr; // Set default configuration - analogInputSystem->writeScanList(port->pin, port->pin, status); + analogInputSystem->writeScanList(pin, pin, status); setAnalogAverageBits(analog_port, kDefaultAverageBits, status); setAnalogOversampleBits(analog_port, kDefaultOversampleBits, status); return analog_port; @@ -130,7 +138,7 @@ float getAnalogSampleRate(int32_t* status) { void setAnalogAverageBits(void* analog_port_pointer, uint32_t bits, int32_t* status) { AnalogPort* port = (AnalogPort*)analog_port_pointer; - analogInputSystem->writeAverageBits(port->port.pin, bits, status); + analogInputSystem->writeAverageBits(port->pin, bits, status); } /** @@ -144,7 +152,7 @@ void setAnalogAverageBits(void* analog_port_pointer, uint32_t bits, */ uint32_t getAnalogAverageBits(void* analog_port_pointer, int32_t* status) { AnalogPort* port = (AnalogPort*)analog_port_pointer; - uint32_t result = analogInputSystem->readAverageBits(port->port.pin, status); + uint32_t result = analogInputSystem->readAverageBits(port->pin, status); return result; } @@ -162,7 +170,7 @@ uint32_t getAnalogAverageBits(void* analog_port_pointer, int32_t* status) { void setAnalogOversampleBits(void* analog_port_pointer, uint32_t bits, int32_t* status) { AnalogPort* port = (AnalogPort*)analog_port_pointer; - analogInputSystem->writeOversampleBits(port->port.pin, bits, status); + analogInputSystem->writeOversampleBits(port->pin, bits, status); } /** @@ -177,8 +185,7 @@ void setAnalogOversampleBits(void* analog_port_pointer, uint32_t bits, */ uint32_t getAnalogOversampleBits(void* analog_port_pointer, int32_t* status) { AnalogPort* port = (AnalogPort*)analog_port_pointer; - uint32_t result = - analogInputSystem->readOversampleBits(port->port.pin, status); + uint32_t result = analogInputSystem->readOversampleBits(port->pin, status); return result; } @@ -195,12 +202,12 @@ uint32_t getAnalogOversampleBits(void* analog_port_pointer, int32_t* status) { int16_t getAnalogValue(void* analog_port_pointer, int32_t* status) { AnalogPort* port = (AnalogPort*)analog_port_pointer; int16_t value; - if (!checkAnalogInputChannel(port->port.pin)) { + if (!checkAnalogInputChannel(port->pin)) { return 0; } tAI::tReadSelect readSelect; - readSelect.Channel = port->port.pin; + readSelect.Channel = port->pin; readSelect.Averaged = false; { @@ -230,12 +237,12 @@ int16_t getAnalogValue(void* analog_port_pointer, int32_t* status) { int32_t getAnalogAverageValue(void* analog_port_pointer, int32_t* status) { AnalogPort* port = (AnalogPort*)analog_port_pointer; int32_t value; - if (!checkAnalogInputChannel(port->port.pin)) { + if (!checkAnalogInputChannel(port->pin)) { return 0; } tAI::tReadSelect readSelect; - readSelect.Channel = port->port.pin; + readSelect.Channel = port->pin; readSelect.Averaged = true; { @@ -331,7 +338,7 @@ int32_t getAnalogVoltsToValue(void* analog_port_pointer, double voltage, uint32_t getAnalogLSBWeight(void* analog_port_pointer, int32_t* status) { AnalogPort* port = (AnalogPort*)analog_port_pointer; uint32_t lsbWeight = FRC_NetworkCommunication_nAICalibration_getLSBWeight( - 0, port->port.pin, status); // XXX: aiSystemIndex == 0? + 0, port->pin, status); // XXX: aiSystemIndex == 0? return lsbWeight; } @@ -348,7 +355,7 @@ uint32_t getAnalogLSBWeight(void* analog_port_pointer, int32_t* status) { int32_t getAnalogOffset(void* analog_port_pointer, int32_t* status) { AnalogPort* port = (AnalogPort*)analog_port_pointer; int32_t offset = FRC_NetworkCommunication_nAICalibration_getOffset( - 0, port->port.pin, status); // XXX: aiSystemIndex == 0? + 0, port->pin, status); // XXX: aiSystemIndex == 0? return offset; } } diff --git a/hal/lib/athena/AnalogInternal.cpp b/hal/lib/athena/AnalogInternal.cpp index 2ca5bc2045..88e65677b6 100644 --- a/hal/lib/athena/AnalogInternal.cpp +++ b/hal/lib/athena/AnalogInternal.cpp @@ -9,7 +9,6 @@ #include "ChipObject.h" #include "HAL/AnalogInput.h" -#include "HAL/Port.h" #include "HAL/cpp/priority_mutex.h" namespace hal { diff --git a/hal/lib/athena/AnalogInternal.h b/hal/lib/athena/AnalogInternal.h index a9ae1e00fe..7088625d62 100644 --- a/hal/lib/athena/AnalogInternal.h +++ b/hal/lib/athena/AnalogInternal.h @@ -10,7 +10,6 @@ #include #include "ChipObject.h" -#include "HAL/Port.h" #include "HAL/cpp/priority_mutex.h" namespace hal { @@ -28,7 +27,7 @@ extern tAO* analogOutputSystem; extern priority_recursive_mutex analogRegisterWindowMutex; struct AnalogPort { - Port port; + uint8_t pin; tAccumulator* accumulator; }; diff --git a/hal/lib/athena/AnalogOutput.cpp b/hal/lib/athena/AnalogOutput.cpp index d50392cd2c..626a800967 100644 --- a/hal/lib/athena/AnalogOutput.cpp +++ b/hal/lib/athena/AnalogOutput.cpp @@ -8,6 +8,8 @@ #include "HAL/AnalogOutput.h" #include "AnalogInternal.h" +#include "HAL/Errors.h" +#include "handles/HandlesInternal.h" using namespace hal; @@ -16,13 +18,20 @@ extern "C" { /** * Initialize the analog output port using the given port object. */ -void* initializeAnalogOutputPort(void* port_pointer, int32_t* status) { +void* initializeAnalogOutputPort(HalPortHandle port_handle, int32_t* status) { initializeAnalog(status); - Port* port = (Port*)port_pointer; + + if (*status != 0) return nullptr; + + int16_t pin = getPortHandlePin(port_handle); + if (pin == HAL_HANDLE_INVALID_TYPE) { + *status = PARAMETER_OUT_OF_RANGE; + return nullptr; + } // Initialize port structure AnalogPort* analog_port = new AnalogPort(); - analog_port->port = *port; + analog_port->pin = (uint8_t)pin; analog_port->accumulator = nullptr; return analog_port; } @@ -57,13 +66,13 @@ void setAnalogOutput(void* analog_port_pointer, double voltage, else if (voltage > 5.0) rawValue = 0x1000; - analogOutputSystem->writeMXP(port->port.pin, rawValue, status); + analogOutputSystem->writeMXP(port->pin, rawValue, status); } double getAnalogOutput(void* analog_port_pointer, int32_t* status) { AnalogPort* port = (AnalogPort*)analog_port_pointer; - uint16_t rawValue = analogOutputSystem->readMXP(port->port.pin, status); + uint16_t rawValue = analogOutputSystem->readMXP(port->pin, status); return rawValue * 5.0 / 0x1000; } diff --git a/hal/lib/athena/AnalogTrigger.cpp b/hal/lib/athena/AnalogTrigger.cpp index a3d6239625..0bb9566322 100644 --- a/hal/lib/athena/AnalogTrigger.cpp +++ b/hal/lib/athena/AnalogTrigger.cpp @@ -9,7 +9,9 @@ #include "AnalogInternal.h" #include "HAL/AnalogInput.h" +#include "HAL/Errors.h" #include "HAL/cpp/Resource.h" +#include "handles/HandlesInternal.h" using namespace hal; @@ -23,20 +25,21 @@ typedef struct trigger_t AnalogTrigger; static hal::Resource* triggers = nullptr; -void* initializeAnalogTrigger(void* port_pointer, uint32_t* index, +void* initializeAnalogTrigger(HalPortHandle port_handle, uint32_t* index, int32_t* status) { - Port* port = (Port*)port_pointer; hal::Resource::CreateResourceObject(&triggers, tAnalogTrigger::kNumSystems); AnalogTrigger* trigger = new AnalogTrigger(); - trigger->port = (AnalogPort*)initializeAnalogInputPort(port, status); + trigger->port = (AnalogPort*)initializeAnalogInputPort(port_handle, status); + if (*status != 0) { + return nullptr; + } trigger->index = triggers->Allocate("Analog Trigger"); *index = trigger->index; // TODO: if (index == ~0ul) { CloneError(triggers); return; } trigger->trigger = tAnalogTrigger::create(trigger->index, status); - trigger->trigger->writeSourceSelect_Channel(port->pin, status); - + trigger->trigger->writeSourceSelect_Channel(trigger->port->pin, status); return trigger; } diff --git a/hal/lib/athena/DIO.cpp b/hal/lib/athena/DIO.cpp index 65465a104f..ea97f233a2 100644 --- a/hal/lib/athena/DIO.cpp +++ b/hal/lib/athena/DIO.cpp @@ -10,6 +10,7 @@ #include #include "DigitalInternal.h" +#include "handles/HandlesInternal.h" static_assert(sizeof(uint32_t) <= sizeof(void*), "This file shoves uint32_ts into pointers."); @@ -24,13 +25,20 @@ extern "C" { /** * Create a new instance of a digital port. */ -void* initializeDigitalPort(void* port_pointer, int32_t* status) { +void* initializeDigitalPort(HalPortHandle port_handle, int32_t* status) { initializeDigital(status); - Port* port = (Port*)port_pointer; + + if (*status != 0) return nullptr; + + int16_t pin = getPortHandlePin(port_handle); + if (pin == HAL_HANDLE_INVALID_TYPE) { + *status = PARAMETER_OUT_OF_RANGE; + return nullptr; + } // Initialize port structure DigitalPort* digital_port = new DigitalPort(); - digital_port->port = *port; + digital_port->pin = (uint8_t)pin; return digital_port; } @@ -132,9 +140,13 @@ void setPWMOutputChannel(void* pwmGenerator, uint32_t pin, int32_t* status) { */ bool allocateDIO(void* digital_port_pointer, bool input, int32_t* status) { DigitalPort* port = (DigitalPort*)digital_port_pointer; + if (port == nullptr) { + *status = NULL_PARAMETER; + return false; + } char buf[64]; - snprintf(buf, 64, "DIO %d", port->port.pin); - if (DIOChannels->Allocate(port->port.pin, buf) == ~0ul) { + snprintf(buf, 64, "DIO %d", port->pin); + if (DIOChannels->Allocate(port->pin, buf) == ~0ul) { *status = RESOURCE_IS_ALLOCATED; return false; } @@ -144,8 +156,8 @@ bool allocateDIO(void* digital_port_pointer, bool input, int32_t* status) { tDIO::tOutputEnable outputEnable = digitalSystem->readOutputEnable(status); - if (port->port.pin < kNumHeaders) { - uint32_t bitToSet = 1 << port->port.pin; + if (port->pin < kNumHeaders) { + uint32_t bitToSet = 1 << port->pin; if (input) { outputEnable.Headers = outputEnable.Headers & (~bitToSet); // clear the bit for read @@ -154,7 +166,7 @@ bool allocateDIO(void* digital_port_pointer, bool input, int32_t* status) { outputEnable.Headers | bitToSet; // set the bit for write } } else { - uint32_t bitToSet = 1 << remapMXPChannel(port->port.pin); + uint32_t bitToSet = 1 << remapMXPChannel(port->pin); // Disable special functions on this pin short specialFunctions = @@ -184,7 +196,7 @@ bool allocateDIO(void* digital_port_pointer, bool input, int32_t* status) { void freeDIO(void* digital_port_pointer, int32_t* status) { DigitalPort* port = (DigitalPort*)digital_port_pointer; if (!port) return; - DIOChannels->Free(port->port.pin); + DIOChannels->Free(port->pin); } /** @@ -204,22 +216,20 @@ void setDIO(void* digital_port_pointer, short value, int32_t* status) { std::lock_guard sync(digitalDIOMutex); tDIO::tDO currentDIO = digitalSystem->readDO(status); - if (port->port.pin < kNumHeaders) { + if (port->pin < kNumHeaders) { if (value == 0) { - currentDIO.Headers = currentDIO.Headers & ~(1 << port->port.pin); + currentDIO.Headers = currentDIO.Headers & ~(1 << port->pin); } else if (value == 1) { - currentDIO.Headers = currentDIO.Headers | (1 << port->port.pin); + currentDIO.Headers = currentDIO.Headers | (1 << port->pin); } } else { if (value == 0) { - currentDIO.MXP = - currentDIO.MXP & ~(1 << remapMXPChannel(port->port.pin)); + currentDIO.MXP = currentDIO.MXP & ~(1 << remapMXPChannel(port->pin)); } else if (value == 1) { - currentDIO.MXP = - currentDIO.MXP | (1 << remapMXPChannel(port->port.pin)); + currentDIO.MXP = currentDIO.MXP | (1 << remapMXPChannel(port->pin)); } - uint32_t bitToSet = 1 << remapMXPChannel(port->port.pin); + uint32_t bitToSet = 1 << remapMXPChannel(port->pin); short specialFunctions = digitalSystem->readEnableMXPSpecialFunction(status); digitalSystem->writeEnableMXPSpecialFunction(specialFunctions & ~bitToSet, @@ -244,17 +254,17 @@ bool getDIO(void* digital_port_pointer, int32_t* status) { // if it == 0, then return false // else return true - if (port->port.pin < kNumHeaders) { - return ((currentDIO.Headers >> port->port.pin) & 1) != 0; + if (port->pin < kNumHeaders) { + return ((currentDIO.Headers >> port->pin) & 1) != 0; } else { // Disable special functions - uint32_t bitToSet = 1 << remapMXPChannel(port->port.pin); + uint32_t bitToSet = 1 << remapMXPChannel(port->pin); short specialFunctions = digitalSystem->readEnableMXPSpecialFunction(status); digitalSystem->writeEnableMXPSpecialFunction(specialFunctions & ~bitToSet, status); - return ((currentDIO.MXP >> remapMXPChannel(port->port.pin)) & 1) != 0; + return ((currentDIO.MXP >> remapMXPChannel(port->pin)) & 1) != 0; } } @@ -269,16 +279,15 @@ bool getDIODirection(void* digital_port_pointer, int32_t* status) { DigitalPort* port = (DigitalPort*)digital_port_pointer; tDIO::tOutputEnable currentOutputEnable = digitalSystem->readOutputEnable(status); - // Shift 00000001 over port->port.pin-1 places. + // Shift 00000001 over port->pin-1 places. // AND it against the currentOutputEnable // if it == 0, then return false // else return true - if (port->port.pin < kNumHeaders) { - return ((currentOutputEnable.Headers >> port->port.pin) & 1) != 0; + if (port->pin < kNumHeaders) { + return ((currentOutputEnable.Headers >> port->pin) & 1) != 0; } else { - return ((currentOutputEnable.MXP >> remapMXPChannel(port->port.pin)) & 1) != - 0; + return ((currentOutputEnable.MXP >> remapMXPChannel(port->pin)) & 1) != 0; } } @@ -294,10 +303,10 @@ void pulse(void* digital_port_pointer, double pulseLength, int32_t* status) { DigitalPort* port = (DigitalPort*)digital_port_pointer; tDIO::tPulse pulse; - if (port->port.pin < kNumHeaders) { - pulse.Headers = 1 << port->port.pin; + if (port->pin < kNumHeaders) { + pulse.Headers = 1 << port->pin; } else { - pulse.MXP = 1 << remapMXPChannel(port->port.pin); + pulse.MXP = 1 << remapMXPChannel(port->pin); } digitalSystem->writePulseLength( @@ -315,10 +324,10 @@ bool isPulsing(void* digital_port_pointer, int32_t* status) { DigitalPort* port = (DigitalPort*)digital_port_pointer; tDIO::tPulse pulseRegister = digitalSystem->readPulse(status); - if (port->port.pin < kNumHeaders) { - return (pulseRegister.Headers & (1 << port->port.pin)) != 0; + if (port->pin < kNumHeaders) { + return (pulseRegister.Headers & (1 << port->pin)) != 0; } else { - return (pulseRegister.MXP & (1 << remapMXPChannel(port->port.pin))) != 0; + return (pulseRegister.MXP & (1 << remapMXPChannel(port->pin))) != 0; } } @@ -345,10 +354,10 @@ void setFilterSelect(void* digital_port_pointer, int filter_index, DigitalPort* port = (DigitalPort*)digital_port_pointer; std::lock_guard sync(digitalDIOMutex); - if (port->port.pin < kNumHeaders) { - digitalSystem->writeFilterSelectHdr(port->port.pin, filter_index, status); + if (port->pin < kNumHeaders) { + digitalSystem->writeFilterSelectHdr(port->pin, filter_index, status); } else { - digitalSystem->writeFilterSelectMXP(remapMXPChannel(port->port.pin), + digitalSystem->writeFilterSelectMXP(remapMXPChannel(port->pin), filter_index, status); } } @@ -365,10 +374,10 @@ int getFilterSelect(void* digital_port_pointer, int32_t* status) { DigitalPort* port = (DigitalPort*)digital_port_pointer; std::lock_guard sync(digitalDIOMutex); - if (port->port.pin < kNumHeaders) { - return digitalSystem->readFilterSelectHdr(port->port.pin, status); + if (port->pin < kNumHeaders) { + return digitalSystem->readFilterSelectHdr(port->pin, status); } else { - return digitalSystem->readFilterSelectMXP(remapMXPChannel(port->port.pin), + return digitalSystem->readFilterSelectMXP(remapMXPChannel(port->pin), status); } } diff --git a/hal/lib/athena/DigitalInternal.cpp b/hal/lib/athena/DigitalInternal.cpp index 97124b1fc3..2fc24d6c5d 100644 --- a/hal/lib/athena/DigitalInternal.cpp +++ b/hal/lib/athena/DigitalInternal.cpp @@ -13,7 +13,6 @@ #include "ChipObject.h" #include "FRC_NetworkCommunication/LoadOut.h" #include "HAL/HAL.h" -#include "HAL/Port.h" #include "HAL/cpp/Resource.h" #include "HAL/cpp/priority_mutex.h" @@ -78,7 +77,7 @@ void initializeDigital(int32_t* status) { for (uint32_t pwm_index = 0; pwm_index < kPwmPins; pwm_index++) { // Initialize port structure DigitalPort digital_port; - digital_port.port.pin = pwm_index; + digital_port.pin = pwm_index; setPWM(&digital_port, kPwmDisabled, status); setPWMPeriodScale(&digital_port, 3, status); // Set all to 4x by default. diff --git a/hal/lib/athena/DigitalInternal.h b/hal/lib/athena/DigitalInternal.h index e9609fa2a4..44be4d3d5e 100644 --- a/hal/lib/athena/DigitalInternal.h +++ b/hal/lib/athena/DigitalInternal.h @@ -10,7 +10,6 @@ #include #include "ChipObject.h" -#include "HAL/Port.h" #include "HAL/cpp/Resource.h" namespace hal { @@ -62,7 +61,7 @@ extern hal::Resource* PWMChannels; extern bool digitalSystemsInitialized; struct DigitalPort { - Port port; + uint8_t pin; uint32_t PWMGeneratorID; }; diff --git a/hal/lib/athena/HALAthena.cpp b/hal/lib/athena/HALAthena.cpp index eda8af4190..4e37dfd68c 100644 --- a/hal/lib/athena/HALAthena.cpp +++ b/hal/lib/athena/HALAthena.cpp @@ -24,8 +24,8 @@ #include "FRC_NetworkCommunication/LoadOut.h" #include "FRC_NetworkCommunication/UsageReporting.h" #include "HAL/Errors.h" -#include "HAL/Port.h" #include "ctre/ctre.h" +#include "handles/HandlesInternal.h" #include "visa/visa.h" const uint32_t solenoid_kNumDO7_0Elements = 8; @@ -42,28 +42,21 @@ static uint32_t timeEpoch = 0; static uint32_t prevFPGATime = 0; static HalNotifierHandle rolloverNotifier = 0; +using namespace hal; + extern "C" { -void* getPort(uint8_t pin) { - Port* port = new Port(); - port->pin = pin; - port->module = 1; - return port; -} +HalPortHandle getPort(uint8_t pin) { return createPortHandle(pin, 1); } /** * @deprecated Uses module numbers */ -void* getPortWithModule(uint8_t module, uint8_t pin) { - Port* port = new Port(); - port->pin = pin; - port->module = module; - return port; +HalPortHandle getPortWithModule(uint8_t module, uint8_t pin) { + return createPortHandle(pin, module); } -void freePort(void* port_pointer) { - Port* port = (Port*)port_pointer; - delete port; +void freePort(HalPortHandle port_handle) { + // noop } const char* getHALErrorMessage(int32_t code) { diff --git a/hal/lib/athena/I2C.cpp b/hal/lib/athena/I2C.cpp index b6383cd56f..9dd4e527f3 100644 --- a/hal/lib/athena/I2C.cpp +++ b/hal/lib/athena/I2C.cpp @@ -8,6 +8,7 @@ #include "HAL/I2C.h" #include "DigitalInternal.h" +#include "HAL/DIO.h" #include "HAL/HAL.h" #include "i2clib/i2c-lib.h" @@ -32,6 +33,7 @@ extern "C" { */ void i2CInitialize(uint8_t port, int32_t* status) { initializeDigital(status); + if (*status != 0) return; if (port > 1) { // Set port out of range error here @@ -49,8 +51,12 @@ void i2CInitialize(uint8_t port, int32_t* status) { } else if (port == 1) { i2CMXPObjCount++; if (i2CMXPHandle > 0) return; - if (!allocateDIO(getPort(24), false, status)) return; - if (!allocateDIO(getPort(25), false, status)) return; + if (!allocateDIO(initializeDigitalPort(getPort(24), status), false, + status)) + return; + if (!allocateDIO(initializeDigitalPort(getPort(25), status), false, + status)) + return; digitalSystem->writeEnableMXPSpecialFunction( digitalSystem->readEnableMXPSpecialFunction(status) | 0xC000, status); i2CMXPHandle = i2clib_open("/dev/i2c-1"); diff --git a/hal/lib/athena/PWM.cpp b/hal/lib/athena/PWM.cpp index 53b39aac9e..055b032a08 100644 --- a/hal/lib/athena/PWM.cpp +++ b/hal/lib/athena/PWM.cpp @@ -17,7 +17,7 @@ using namespace hal; extern "C" { bool checkPWMChannel(void* digital_port_pointer) { DigitalPort* port = (DigitalPort*)digital_port_pointer; - return port->port.pin < kPwmPins; + return port->pin < kPwmPins; } /** @@ -53,10 +53,10 @@ void setPWM(void* digital_port_pointer, unsigned short value, int32_t* status) { return; } - if (port->port.pin < tPWM::kNumHdrRegisters) { - pwmSystem->writeHdr(port->port.pin, value, status); + if (port->pin < tPWM::kNumHdrRegisters) { + pwmSystem->writeHdr(port->pin, value, status); } else { - pwmSystem->writeMXP(port->port.pin - tPWM::kNumHdrRegisters, value, status); + pwmSystem->writeMXP(port->pin - tPWM::kNumHdrRegisters, value, status); } } @@ -72,10 +72,10 @@ unsigned short getPWM(void* digital_port_pointer, int32_t* status) { return 0; } - if (port->port.pin < tPWM::kNumHdrRegisters) { - return pwmSystem->readHdr(port->port.pin, status); + if (port->pin < tPWM::kNumHdrRegisters) { + return pwmSystem->readHdr(port->pin, status); } else { - return pwmSystem->readMXP(port->port.pin - tPWM::kNumHdrRegisters, status); + return pwmSystem->readMXP(port->pin - tPWM::kNumHdrRegisters, status); } } @@ -85,8 +85,8 @@ void latchPWMZero(void* digital_port_pointer, int32_t* status) { return; } - pwmSystem->writeZeroLatch(port->port.pin, true, status); - pwmSystem->writeZeroLatch(port->port.pin, false, status); + pwmSystem->writeZeroLatch(port->pin, true, status); + pwmSystem->writeZeroLatch(port->pin, false, status); } /** @@ -102,11 +102,11 @@ void setPWMPeriodScale(void* digital_port_pointer, uint32_t squelchMask, return; } - if (port->port.pin < tPWM::kNumPeriodScaleHdrElements) { - pwmSystem->writePeriodScaleHdr(port->port.pin, squelchMask, status); + if (port->pin < tPWM::kNumPeriodScaleHdrElements) { + pwmSystem->writePeriodScaleHdr(port->pin, squelchMask, status); } else { - pwmSystem->writePeriodScaleMXP( - port->port.pin - tPWM::kNumPeriodScaleHdrElements, squelchMask, status); + pwmSystem->writePeriodScaleMXP(port->pin - tPWM::kNumPeriodScaleHdrElements, + squelchMask, status); } } @@ -117,19 +117,18 @@ bool allocatePWMChannel(void* digital_port_pointer, int32_t* status) { } char buf[64]; - snprintf(buf, 64, "PWM %d", port->port.pin); - if (PWMChannels->Allocate(port->port.pin, buf) == ~0ul) { + snprintf(buf, 64, "PWM %d", port->pin); + if (PWMChannels->Allocate(port->pin, buf) == ~0ul) { *status = RESOURCE_IS_ALLOCATED; return false; } - if (port->port.pin > tPWM::kNumHdrRegisters - 1) { - snprintf(buf, 64, "PWM %d and DIO %d", port->port.pin, - remapMXPPWMChannel(port->port.pin) + 10); - if (DIOChannels->Allocate(remapMXPPWMChannel(port->port.pin) + 10, buf) == - ~0ul) + if (port->pin > tPWM::kNumHdrRegisters - 1) { + snprintf(buf, 64, "PWM %d and DIO %d", port->pin, + remapMXPPWMChannel(port->pin) + 10); + if (DIOChannels->Allocate(remapMXPPWMChannel(port->pin) + 10, buf) == ~0ul) return false; - uint32_t bitToSet = 1 << remapMXPPWMChannel(port->port.pin); + uint32_t bitToSet = 1 << remapMXPPWMChannel(port->pin); short specialFunctions = digitalSystem->readEnableMXPSpecialFunction(status); digitalSystem->writeEnableMXPSpecialFunction(specialFunctions | bitToSet, @@ -145,10 +144,10 @@ void freePWMChannel(void* digital_port_pointer, int32_t* status) { return; } - PWMChannels->Free(port->port.pin); - if (port->port.pin > tPWM::kNumHdrRegisters - 1) { - DIOChannels->Free(remapMXPPWMChannel(port->port.pin) + 10); - uint32_t bitToUnset = 1 << remapMXPPWMChannel(port->port.pin); + PWMChannels->Free(port->pin); + if (port->pin > tPWM::kNumHdrRegisters - 1) { + DIOChannels->Free(remapMXPPWMChannel(port->pin) + 10); + uint32_t bitToUnset = 1 << remapMXPPWMChannel(port->pin); short specialFunctions = digitalSystem->readEnableMXPSpecialFunction(status); digitalSystem->writeEnableMXPSpecialFunction(specialFunctions & ~bitToUnset, diff --git a/hal/lib/athena/Relay.cpp b/hal/lib/athena/Relay.cpp index 2ed8834e0c..4cd3a3fb5c 100644 --- a/hal/lib/athena/Relay.cpp +++ b/hal/lib/athena/Relay.cpp @@ -22,7 +22,7 @@ constexpr uint32_t kRelayPins = 8; extern "C" { bool checkRelayChannel(void* digital_port_pointer) { DigitalPort* port = (DigitalPort*)digital_port_pointer; - return port->port.pin < kRelayPins; + return port->pin < kRelayPins; } /** @@ -60,9 +60,9 @@ void setRelayForward(void* digital_port_pointer, bool on, int32_t* status) { std::lock_guard sync(digitalRelayMutex); uint8_t forwardRelays = relaySystem->readValue_Forward(status); if (on) - forwardRelays |= 1 << port->port.pin; + forwardRelays |= 1 << port->pin; else - forwardRelays &= ~(1 << port->port.pin); + forwardRelays &= ~(1 << port->pin); relaySystem->writeValue_Forward(forwardRelays, status); } } @@ -83,9 +83,9 @@ void setRelayReverse(void* digital_port_pointer, bool on, int32_t* status) { std::lock_guard sync(digitalRelayMutex); uint8_t reverseRelays = relaySystem->readValue_Reverse(status); if (on) - reverseRelays |= 1 << port->port.pin; + reverseRelays |= 1 << port->pin; else - reverseRelays &= ~(1 << port->port.pin); + reverseRelays &= ~(1 << port->pin); relaySystem->writeValue_Reverse(reverseRelays, status); } } @@ -100,7 +100,7 @@ bool getRelayForward(void* digital_port_pointer, int32_t* status) { } uint8_t forwardRelays = relaySystem->readValue_Forward(status); - return (forwardRelays & (1 << port->port.pin)) != 0; + return (forwardRelays & (1 << port->pin)) != 0; } /** @@ -113,6 +113,6 @@ bool getRelayReverse(void* digital_port_pointer, int32_t* status) { } uint8_t reverseRelays = relaySystem->readValue_Reverse(status); - return (reverseRelays & (1 << port->port.pin)) != 0; + return (reverseRelays & (1 << port->pin)) != 0; } } diff --git a/hal/lib/athena/SPI.cpp b/hal/lib/athena/SPI.cpp index 4866eda12f..9c06acc03d 100644 --- a/hal/lib/athena/SPI.cpp +++ b/hal/lib/athena/SPI.cpp @@ -10,6 +10,7 @@ #include #include "DigitalInternal.h" +#include "HAL/DIO.h" #include "HAL/HAL.h" #include "spilib/spi-lib.h" @@ -78,19 +79,24 @@ void spiInitialize(uint8_t port, int32_t* status) { break; case 4: initializeDigital(status); - if (!allocateDIO(getPort(14), false, status)) { + if (*status != 0) return; + if (!allocateDIO(initializeDigitalPort(getPort(14), status), false, + status)) { printf("Failed to allocate DIO 14\n"); return; } - if (!allocateDIO(getPort(15), false, status)) { + if (!allocateDIO(initializeDigitalPort(getPort(15), status), false, + status)) { printf("Failed to allocate DIO 15\n"); return; } - if (!allocateDIO(getPort(16), true, status)) { + if (!allocateDIO(initializeDigitalPort(getPort(16), status), true, + status)) { printf("Failed to allocate DIO 16\n"); return; } - if (!allocateDIO(getPort(17), false, status)) { + if (!allocateDIO(initializeDigitalPort(getPort(17), status), false, + status)) { printf("Failed to allocate DIO 17\n"); return; } diff --git a/hal/lib/athena/Solenoid.cpp b/hal/lib/athena/Solenoid.cpp index 0fd2c40724..caf8966ed1 100644 --- a/hal/lib/athena/Solenoid.cpp +++ b/hal/lib/athena/Solenoid.cpp @@ -10,8 +10,8 @@ #include "ChipObject.h" #include "FRC_NetworkCommunication/LoadOut.h" #include "HAL/Errors.h" -#include "HAL/Port.h" #include "ctre/PCM.h" +#include "handles/HandlesInternal.h" static const int NUM_MODULE_NUMBERS = 63; @@ -28,15 +28,23 @@ void initializePCM(int module) { } } +using namespace hal; + extern "C" { -void* initializeSolenoidPort(void* port_pointer, int32_t* status) { - Port* port = (Port*)port_pointer; - initializePCM(port->module); +void* initializeSolenoidPort(HalPortHandle port_handle, int32_t* status) { + int16_t pin = getPortHandlePin(port_handle); + int16_t module = getPortHandleModule(port_handle); + if (pin == HAL_HANDLE_INVALID_TYPE) { + *status = PARAMETER_OUT_OF_RANGE; + return nullptr; + } + + initializePCM(module); solenoid_port_t* solenoid_port = new solenoid_port_t; - solenoid_port->module = PCM_modules[port->module]; - solenoid_port->pin = port->pin; + solenoid_port->module = PCM_modules[module]; + solenoid_port->pin = pin; return solenoid_port; } diff --git a/hal/lib/athena/handles/HandlesInternal.cpp b/hal/lib/athena/handles/HandlesInternal.cpp index 08963cf6b1..c8730387e1 100644 --- a/hal/lib/athena/handles/HandlesInternal.cpp +++ b/hal/lib/athena/handles/HandlesInternal.cpp @@ -11,6 +11,19 @@ #include "UnlimitedHandleResource.h" namespace hal { +HalPortHandle createPortHandle(uint8_t pin, uint8_t module) { + // set last 8 bits, then shift to first 8 bits + HalPortHandle handle = static_cast(HalHandleEnum::Port); + handle = handle << 24; + // shift module and add to 3rd set of 8 bits + int32_t temp = module; + temp = (temp << 8) & 0xff00; + handle += temp; + // add pin to last 8 bits + handle += pin; + return handle; +} + HalHandle createHandle(int16_t index, HalHandleEnum handleType) { if (index < 0) return HAL_HANDLE_NEGATIVE_INDEX; uint8_t hType = static_cast(handleType); diff --git a/hal/lib/athena/handles/HandlesInternal.h b/hal/lib/athena/handles/HandlesInternal.h index a2633f9f4c..37936f4c06 100644 --- a/hal/lib/athena/handles/HandlesInternal.h +++ b/hal/lib/athena/handles/HandlesInternal.h @@ -42,5 +42,30 @@ static inline int16_t getHandleTypedIndex(HalHandle handle, return getHandleIndex(handle); } +/* specialized functions for Port handle + * Port Handle Data Layout + * Bits 0-7: Pin Number + * Bits 8-15: Module Number + * Bits 16-23: Unused + * Bits 24-30: Handle Type + * Bit 31: 1 if handle error, 0 if no error + */ + +// using a 16 bit value so we can store 0-255 and still report error +static inline int16_t getPortHandlePin(HalPortHandle handle) { + if (!isHandleType(handle, HalHandleEnum::Port)) + return HAL_HANDLE_INVALID_TYPE; + return (uint8_t)(handle & 0xff); +} + +// using a 16 bit value so we can store 0-255 and still report error +static inline int16_t getPortHandleModule(HalPortHandle handle) { + if (!isHandleType(handle, HalHandleEnum::Port)) + return HAL_HANDLE_INVALID_TYPE; + return (uint8_t)((handle >> 8) & 0xff); +} + +HalPortHandle createPortHandle(uint8_t pin, uint8_t module); + HalHandle createHandle(int16_t index, HalHandleEnum handleType); } diff --git a/wpilibc/athena/src/AnalogInput.cpp b/wpilibc/athena/src/AnalogInput.cpp index 69d21a046e..801b29cfa4 100644 --- a/wpilibc/athena/src/AnalogInput.cpp +++ b/wpilibc/athena/src/AnalogInput.cpp @@ -7,7 +7,6 @@ #include "AnalogInput.h" #include "HAL/HAL.h" -#include "HAL/Port.h" #include "LiveWindow/LiveWindow.h" #include "Resource.h" #include "Timer.h" @@ -45,7 +44,7 @@ AnalogInput::AnalogInput(uint32_t channel) { m_channel = channel; - void* port = getPort(channel); + HalPortHandle port = getPort(channel); int32_t status = 0; m_port = initializeAnalogInputPort(port, &status); wpi_setErrorWithContext(status, getHALErrorMessage(status)); diff --git a/wpilibc/athena/src/AnalogOutput.cpp b/wpilibc/athena/src/AnalogOutput.cpp index a1756249c8..4b16ad3a98 100644 --- a/wpilibc/athena/src/AnalogOutput.cpp +++ b/wpilibc/athena/src/AnalogOutput.cpp @@ -7,7 +7,6 @@ #include "AnalogOutput.h" #include "HAL/HAL.h" -#include "HAL/Port.h" #include "LiveWindow/LiveWindow.h" #include "Resource.h" #include "WPIErrors.h" @@ -47,7 +46,7 @@ AnalogOutput::AnalogOutput(uint32_t channel) { m_channel = channel; - void* port = getPort(m_channel); + HalPortHandle port = getPort(m_channel); int32_t status = 0; m_port = initializeAnalogOutputPort(port, &status); wpi_setErrorWithContext(status, getHALErrorMessage(status)); diff --git a/wpilibc/athena/src/AnalogTrigger.cpp b/wpilibc/athena/src/AnalogTrigger.cpp index 67788cdbfe..51a52879fb 100644 --- a/wpilibc/athena/src/AnalogTrigger.cpp +++ b/wpilibc/athena/src/AnalogTrigger.cpp @@ -9,7 +9,6 @@ #include "AnalogInput.h" #include "HAL/HAL.h" -#include "HAL/Port.h" #include "Resource.h" #include "WPIErrors.h" @@ -22,7 +21,7 @@ * on-board 4-7 are on the MXP port. */ AnalogTrigger::AnalogTrigger(int32_t channel) { - void* port = getPort(channel); + HalPortHandle port = getPort(channel); int32_t status = 0; uint32_t index = 0; m_trigger = initializeAnalogTrigger(port, &index, &status); diff --git a/wpilibc/athena/src/SensorBase.cpp b/wpilibc/athena/src/SensorBase.cpp index a55c60b5e6..8c578e3114 100644 --- a/wpilibc/athena/src/SensorBase.cpp +++ b/wpilibc/athena/src/SensorBase.cpp @@ -9,7 +9,6 @@ #include "FRC_NetworkCommunication/LoadOut.h" #include "HAL/HAL.h" -#include "HAL/Port.h" #include "WPIErrors.h" const uint32_t SensorBase::kDigitalChannels; @@ -33,7 +32,7 @@ void* SensorBase::m_pwm_ports[kPwmChannels]; SensorBase::SensorBase() { if (!portsInitialized) { for (uint32_t i = 0; i < kDigitalChannels; i++) { - void* port = getPort(i); + HalPortHandle port = getPort(i); int32_t status = 0; m_digital_ports[i] = initializeDigitalPort(port, &status); wpi_setErrorWithContext(status, getHALErrorMessage(status)); @@ -41,7 +40,7 @@ SensorBase::SensorBase() { } for (uint32_t i = 0; i < kRelayChannels; i++) { - void* port = getPort(i); + HalPortHandle port = getPort(i); int32_t status = 0; m_relay_ports[i] = initializeDigitalPort(port, &status); wpi_setErrorWithContext(status, getHALErrorMessage(status)); @@ -49,7 +48,7 @@ SensorBase::SensorBase() { } for (uint32_t i = 0; i < kPwmChannels; i++) { - void* port = getPort(i); + HalPortHandle port = getPort(i); int32_t status = 0; m_pwm_ports[i] = initializeDigitalPort(port, &status); wpi_setErrorWithContext(status, getHALErrorMessage(status)); diff --git a/wpilibc/athena/src/SolenoidBase.cpp b/wpilibc/athena/src/SolenoidBase.cpp index c83e0928d7..c1696fcb3e 100644 --- a/wpilibc/athena/src/SolenoidBase.cpp +++ b/wpilibc/athena/src/SolenoidBase.cpp @@ -20,7 +20,7 @@ std::unique_ptr SolenoidBase::m_allocated; SolenoidBase::SolenoidBase(uint8_t moduleNumber) : m_moduleNumber(moduleNumber) { for (uint32_t i = 0; i < kSolenoidChannels; i++) { - void* port = getPortWithModule(moduleNumber, i); + HalPortHandle port = getPortWithModule(moduleNumber, i); int32_t status = 0; SolenoidBase::m_ports[moduleNumber][i] = initializeSolenoidPort(port, &status); diff --git a/wpilibj/src/athena/cpp/lib/AnalogJNI.cpp b/wpilibj/src/athena/cpp/lib/AnalogJNI.cpp index 7db511c7c7..7bcb676c00 100644 --- a/wpilibj/src/athena/cpp/lib/AnalogJNI.cpp +++ b/wpilibj/src/athena/cpp/lib/AnalogJNI.cpp @@ -31,14 +31,14 @@ extern "C" { /* * Class: edu_wpi_first_wpilibj_hal_AnalogJNI * Method: initializeAnalogInputPort - * Signature: (J)J + * Signature: (I)J */ JNIEXPORT jlong JNICALL Java_edu_wpi_first_wpilibj_hal_AnalogJNI_initializeAnalogInputPort( - JNIEnv *env, jclass, jlong id) { - ANALOGJNI_LOG(logDEBUG) << "Port Ptr = " << (void *)id; + JNIEnv *env, jclass, jint id) { + ANALOGJNI_LOG(logDEBUG) << "Port Handle = " << (HalPortHandle)id; int32_t status = 0; - void *analog = initializeAnalogInputPort((void *)id, &status); + void *analog = initializeAnalogInputPort((HalPortHandle)id, &status); ANALOGJNI_LOG(logDEBUG) << "Status = " << status; ANALOGJNI_LOG(logDEBUG) << "Analog Ptr = " << analog; CheckStatus(env, status); @@ -60,14 +60,14 @@ Java_edu_wpi_first_wpilibj_hal_AnalogJNI_freeAnalogInputPort( /* * Class: edu_wpi_first_wpilibj_hal_AnalogJNI * Method: initializeAnalogOutputPort - * Signature: (J)J + * Signature: (I)J */ JNIEXPORT jlong JNICALL Java_edu_wpi_first_wpilibj_hal_AnalogJNI_initializeAnalogOutputPort( - JNIEnv *env, jclass, jlong id) { - ANALOGJNI_LOG(logDEBUG) << "Port Ptr = " << (void *)id; + JNIEnv *env, jclass, jint id) { + ANALOGJNI_LOG(logDEBUG) << "Port Handle = " << (HalPortHandle)id; int32_t status = 0; - void *analog = initializeAnalogOutputPort((void *)id, &status); + void *analog = initializeAnalogOutputPort((HalPortHandle)id, &status); ANALOGJNI_LOG(logDEBUG) << "Status = " << status; ANALOGJNI_LOG(logDEBUG) << "Analog Ptr = " << analog; CheckStatus(env, status); @@ -521,19 +521,19 @@ Java_edu_wpi_first_wpilibj_hal_AnalogJNI_getAccumulatorOutput( /* * Class: edu_wpi_first_wpilibj_hal_AnalogJNI * Method: initializeAnalogTrigger - * Signature: (JLjava/nio/IntBuffer;)J + * Signature: (ILjava/nio/IntBuffer;)J */ JNIEXPORT jlong JNICALL Java_edu_wpi_first_wpilibj_hal_AnalogJNI_initializeAnalogTrigger( - JNIEnv *env, jclass, jlong id, jobject index) { - ANALOGJNI_LOG(logDEBUG) << "Port Ptr = " << (void *)id; + JNIEnv *env, jclass, jint id, jobject index) { + ANALOGJNI_LOG(logDEBUG) << "Port Ptr = " << (HalPortHandle)id; jint *indexPtr = (jint *)env->GetDirectBufferAddress(index); ANALOGJNI_LOG(logDEBUG) << "Index Ptr = " << indexPtr; int32_t status = 0; void *analogTrigger = - initializeAnalogTrigger((void *)id, (uint32_t *)indexPtr, &status); + initializeAnalogTrigger((HalPortHandle)id, (uint32_t *)indexPtr, &status); ANALOGJNI_LOG(logDEBUG) << "Status = " << status; ANALOGJNI_LOG(logDEBUG) << "AnalogTrigger Ptr = " << analogTrigger; CheckStatus(env, status); diff --git a/wpilibj/src/athena/cpp/lib/DIOJNI.cpp b/wpilibj/src/athena/cpp/lib/DIOJNI.cpp index 7ec7acba75..76b76825c3 100644 --- a/wpilibj/src/athena/cpp/lib/DIOJNI.cpp +++ b/wpilibj/src/athena/cpp/lib/DIOJNI.cpp @@ -29,15 +29,15 @@ extern "C" { /* * Class: edu_wpi_first_wpilibj_hal_DIOJNI * Method: initializeDigitalPort - * Signature: (J)J; + * Signature: (I)J; */ JNIEXPORT jlong JNICALL Java_edu_wpi_first_wpilibj_hal_DIOJNI_initializeDigitalPort( - JNIEnv *env, jclass, jlong id) { + JNIEnv *env, jclass, jint id) { DIOJNI_LOG(logDEBUG) << "Calling DIOJNI initializeDigitalPort"; - DIOJNI_LOG(logDEBUG) << "Port Ptr = " << (void *)id; + DIOJNI_LOG(logDEBUG) << "Port Handle = " << (HalPortHandle)id; int32_t status = 0; - void *dio = initializeDigitalPort((void *)id, &status); + void *dio = initializeDigitalPort((HalPortHandle)id, &status); DIOJNI_LOG(logDEBUG) << "Status = " << status; DIOJNI_LOG(logDEBUG) << "DIO Ptr = " << dio; CheckStatus(env, status); diff --git a/wpilibj/src/athena/cpp/lib/JNIWrapper.cpp b/wpilibj/src/athena/cpp/lib/JNIWrapper.cpp index c20a27e7f3..fed3679d9c 100644 --- a/wpilibj/src/athena/cpp/lib/JNIWrapper.cpp +++ b/wpilibj/src/athena/cpp/lib/JNIWrapper.cpp @@ -18,46 +18,46 @@ extern "C" { /* * Class: edu_wpi_first_wpilibj_hal_JNIWrapper * Method: getPortWithModule - * Signature: (BB)J + * Signature: (BB)I */ -JNIEXPORT jlong JNICALL +JNIEXPORT jint JNICALL Java_edu_wpi_first_wpilibj_hal_JNIWrapper_getPortWithModule( JNIEnv* env, jclass, jbyte module, jbyte pin) { // FILE_LOG(logDEBUG) << "Calling JNIWrapper getPortWithModlue"; // FILE_LOG(logDEBUG) << "Module = " << (jint)module; // FILE_LOG(logDEBUG) << "Pin = " << (jint)pin; - void* port = getPortWithModule(module, pin); - // FILE_LOG(logDEBUG) << "Port Ptr = " << port; - return (jlong)port; + HalPortHandle port = getPortWithModule(module, pin); + // FILE_LOG(logDEBUG) << "Port Handle = " << port; + return (jint)port; } /* * Class: edu_wpi_first_wpilibj_hal_JNIWrapper * Method: getPort - * Signature: (BB)J + * Signature: (B)I */ -JNIEXPORT jlong JNICALL Java_edu_wpi_first_wpilibj_hal_JNIWrapper_getPort( +JNIEXPORT jint JNICALL Java_edu_wpi_first_wpilibj_hal_JNIWrapper_getPort( JNIEnv* env, jclass, jbyte pin) { // FILE_LOG(logDEBUG) << "Calling JNIWrapper getPortWithModlue"; // FILE_LOG(logDEBUG) << "Module = " << (jint)module; // FILE_LOG(logDEBUG) << "Pin = " << (jint)pin; - void* port = getPort(pin); - // FILE_LOG(logDEBUG) << "Port Ptr = " << port; - return (jlong)port; + HalPortHandle port = getPort(pin); + // FILE_LOG(logDEBUG) << "Port Handle = " << port; + return (jint)port; } /* * Class: edu_wpi_first_wpilibj_hal_JNIWrapper * Method: freePort - * Signature: (J)V + * Signature: (I)V */ JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_JNIWrapper_freePort( - JNIEnv* env, jclass, jlong id) { + JNIEnv* env, jclass, jint id) { // FILE_LOG(logDEBUG) << "Calling JNIWrapper getPortWithModlue"; // FILE_LOG(logDEBUG) << "Module = " << (jint)module; // FILE_LOG(logDEBUG) << "Pin = " << (jint)pin; - freePort((void*)id); - // FILE_LOG(logDEBUG) << "Port Ptr = " << port; + freePort((HalPortHandle)id); + // FILE_LOG(logDEBUG) << "Port Handle = " << port; } } // extern "C" diff --git a/wpilibj/src/athena/cpp/lib/SolenoidJNI.cpp b/wpilibj/src/athena/cpp/lib/SolenoidJNI.cpp index 5a9a030b1c..2a85ea9e56 100644 --- a/wpilibj/src/athena/cpp/lib/SolenoidJNI.cpp +++ b/wpilibj/src/athena/cpp/lib/SolenoidJNI.cpp @@ -26,21 +26,21 @@ extern "C" { /* * Class: edu_wpi_first_wpilibj_hal_SolenoidJNI * Method: initializeSolenoidPort - * Signature: (J)J + * Signature: (I)J */ JNIEXPORT jlong JNICALL Java_edu_wpi_first_wpilibj_hal_SolenoidJNI_initializeSolenoidPort( - JNIEnv *env, jclass, jlong port_pointer) { + JNIEnv *env, jclass, jint port_handle) { SOLENOIDJNI_LOG(logDEBUG) << "Calling SolenoidJNI initializeSolenoidPort"; - SOLENOIDJNI_LOG(logDEBUG) << "Port Ptr = " << (void *)port_pointer; - char *aschars = (char *)port_pointer; + SOLENOIDJNI_LOG(logDEBUG) << "Port Handle = " << (HalPortHandle)port_handle; + char *aschars = (char *)port_handle; SOLENOIDJNI_LOG(logDEBUG) << '\t' << (int)aschars[0] << '\t' << (int)aschars[1] << std::endl; int32_t status = 0; void *solenoid_port_pointer = - initializeSolenoidPort((void *)port_pointer, &status); + initializeSolenoidPort((HalPortHandle)port_handle, &status); SOLENOIDJNI_LOG(logDEBUG) << "Status = " << status; SOLENOIDJNI_LOG(logDEBUG) << "Solenoid Port Pointer = " @@ -68,19 +68,6 @@ Java_edu_wpi_first_wpilibj_hal_SolenoidJNI_freeSolenoidPort( freeSolenoidPort((void *)id); } -/* - * Class: edu_wpi_first_wpilibj_hal_SolenoidJNI - * Method: getPortWithModule - * Signature: (BB)J - */ -JNIEXPORT jlong JNICALL -Java_edu_wpi_first_wpilibj_hal_SolenoidJNI_getPortWithModule( - JNIEnv *env, jclass, jbyte module, jbyte channel) { - void *port_pointer = getPortWithModule(module, channel); - - return (jlong)port_pointer; -} - /* * Class: edu_wpi_first_wpilibj_hal_SolenoidJNI * Method: setSolenoid diff --git a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/AnalogInput.java b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/AnalogInput.java index b943bf5e26..a583102699 100644 --- a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/AnalogInput.java +++ b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/AnalogInput.java @@ -59,8 +59,8 @@ public class AnalogInput extends SensorBase implements PIDSource, LiveWindowSend throw new AllocationException("Analog input channel " + m_channel + " is already allocated"); } - final long portPointer = AnalogJNI.getPort((byte) channel); - m_port = AnalogJNI.initializeAnalogInputPort(portPointer); + final int portHandle = AnalogJNI.getPort((byte) channel); + m_port = AnalogJNI.initializeAnalogInputPort(portHandle); LiveWindow.addSensor("AnalogInput", channel, this); UsageReporting.report(tResourceType.kResourceType_AnalogChannel, channel); diff --git a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/AnalogOutput.java b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/AnalogOutput.java index 101daace9f..3ccf2a8933 100644 --- a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/AnalogOutput.java +++ b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/AnalogOutput.java @@ -42,8 +42,8 @@ public class AnalogOutput extends SensorBase implements LiveWindowSendable { throw new AllocationException("Analog output channel " + m_channel + " is already allocated"); } - final long portPointer = AnalogJNI.getPort((byte) channel); - m_port = AnalogJNI.initializeAnalogOutputPort(portPointer); + final int portHandle = AnalogJNI.getPort((byte) channel); + m_port = AnalogJNI.initializeAnalogOutputPort(portHandle); LiveWindow.addSensor("AnalogOutput", channel, this); UsageReporting.report(tResourceType.kResourceType_AnalogOutput, channel); diff --git a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/AnalogTrigger.java b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/AnalogTrigger.java index 80dfa6ff10..a61fb95028 100644 --- a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/AnalogTrigger.java +++ b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/AnalogTrigger.java @@ -51,12 +51,12 @@ public class AnalogTrigger { * @param channel the port to use for the analog trigger */ public AnalogTrigger(final int channel) { - final long portPointer = AnalogJNI.getPort((byte) channel); + final int portHandle = AnalogJNI.getPort((byte) channel); ByteBuffer index = ByteBuffer.allocateDirect(4); index.order(ByteOrder.LITTLE_ENDIAN); m_port = - AnalogJNI.initializeAnalogTrigger(portPointer, index.asIntBuffer()); + AnalogJNI.initializeAnalogTrigger(portHandle, index.asIntBuffer()); m_index = index.asIntBuffer().get(0); UsageReporting.report(tResourceType.kResourceType_AnalogTrigger, channel); diff --git a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/DigitalSource.java b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/DigitalSource.java index 1f9224db18..3d18f75d0b 100644 --- a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/DigitalSource.java +++ b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/DigitalSource.java @@ -35,8 +35,8 @@ public abstract class DigitalSource extends InterruptableSensorBase { throw new AllocationException("Digital input " + m_channel + " is already allocated"); } - long portPointer = DIOJNI.getPort((byte) channel); - m_port = DIOJNI.initializeDigitalPort(portPointer); + int portHandle = DIOJNI.getPort((byte) channel); + m_port = DIOJNI.initializeDigitalPort(portHandle); DIOJNI.allocateDIO(m_port, input); } diff --git a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/Solenoid.java b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/Solenoid.java index e89fb70d43..9113f5feba 100644 --- a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/Solenoid.java +++ b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/Solenoid.java @@ -57,8 +57,8 @@ public class Solenoid extends SolenoidBase implements LiveWindowSendable { + m_moduleNumber + " is already allocated"); } - long port = SolenoidJNI.getPortWithModule((byte) m_moduleNumber, (byte) m_channel); - m_solenoidPort = SolenoidJNI.initializeSolenoidPort(port); + int portHandle = SolenoidJNI.getPortWithModule((byte) m_moduleNumber, (byte) m_channel); + m_solenoidPort = SolenoidJNI.initializeSolenoidPort(portHandle); LiveWindow.addActuator("Solenoid", m_moduleNumber, m_channel, this); UsageReporting.report(tResourceType.kResourceType_Solenoid, m_channel, m_moduleNumber); diff --git a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/SolenoidBase.java b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/SolenoidBase.java index 15c9e8bd7f..50b3eb055f 100644 --- a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/SolenoidBase.java +++ b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/SolenoidBase.java @@ -28,8 +28,8 @@ public abstract class SolenoidBase extends SensorBase { m_moduleNumber = moduleNumber; m_ports = new long[SensorBase.kSolenoidChannels]; for (int i = 0; i < SensorBase.kSolenoidChannels; i++) { - long port = SolenoidJNI.getPortWithModule((byte) moduleNumber, (byte) i); - m_ports[i] = SolenoidJNI.initializeSolenoidPort(port); + int portHandle = SolenoidJNI.getPortWithModule((byte) moduleNumber, (byte) i); + m_ports[i] = SolenoidJNI.initializeSolenoidPort(portHandle); } } diff --git a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/hal/AnalogJNI.java b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/hal/AnalogJNI.java index 3a68b207ff..f53990f4f7 100644 --- a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/hal/AnalogJNI.java +++ b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/hal/AnalogJNI.java @@ -33,11 +33,11 @@ public class AnalogJNI extends JNIWrapper { int kFallingPulse = 3; } - public static native long initializeAnalogInputPort(long portPointer); + public static native long initializeAnalogInputPort(int halPortHandle); public static native void freeAnalogInputPort(long portPointer); - public static native long initializeAnalogOutputPort(long portPointer); + public static native long initializeAnalogOutputPort(int halPortHandle); public static native void freeAnalogOutputPort(long portPointer); @@ -94,7 +94,7 @@ public class AnalogJNI extends JNIWrapper { public static native void getAccumulatorOutput(long analogPortPointer, LongBuffer value, IntBuffer count); - public static native long initializeAnalogTrigger(long portPointer, IntBuffer index); + public static native long initializeAnalogTrigger(int halPortHandle, IntBuffer index); public static native void cleanAnalogTrigger(long analogTriggerPointer); diff --git a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/hal/DIOJNI.java b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/hal/DIOJNI.java index bb6a5cfb50..54c558f500 100644 --- a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/hal/DIOJNI.java +++ b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/hal/DIOJNI.java @@ -9,7 +9,7 @@ package edu.wpi.first.wpilibj.hal; @SuppressWarnings("AbbreviationAsWordInName") public class DIOJNI extends JNIWrapper { - public static native long initializeDigitalPort(long portPointer); + public static native long initializeDigitalPort(int halPortHandle); public static native void freeDigitalPort(long portPointer); diff --git a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/hal/JNIWrapper.java b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/hal/JNIWrapper.java index 56f1c14cdc..eda418763a 100644 --- a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/hal/JNIWrapper.java +++ b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/hal/JNIWrapper.java @@ -59,9 +59,9 @@ public class JNIWrapper { } } - public static native long getPortWithModule(byte module, byte pin); + public static native int getPortWithModule(byte module, byte pin); - public static native long getPort(byte pin); + public static native int getPort(byte pin); - public static native void freePort(long portPointer); + public static native void freePort(int halPortHandle); } diff --git a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/hal/SolenoidJNI.java b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/hal/SolenoidJNI.java index 3d8845c361..d660c33a03 100644 --- a/wpilibj/src/athena/java/edu/wpi/first/wpilibj/hal/SolenoidJNI.java +++ b/wpilibj/src/athena/java/edu/wpi/first/wpilibj/hal/SolenoidJNI.java @@ -8,12 +8,10 @@ package edu.wpi.first.wpilibj.hal; public class SolenoidJNI extends JNIWrapper { - public static native long initializeSolenoidPort(long portPointer); + public static native long initializeSolenoidPort(int halPortHandle); public static native void freeSolenoidPort(long portPointer); - public static native long getPortWithModule(byte module, byte channel); - public static native void setSolenoid(long port, boolean on); public static native boolean getSolenoid(long port);