mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-30 02:31:44 +00:00
Make the HAL self initialize when ever any initialization function is called (#1012)
This commit is contained in:
committed by
Peter Johnson
parent
59a8e9da57
commit
e21a246a4d
@@ -15,6 +15,7 @@
|
||||
|
||||
#include "HAL/ChipObject.h"
|
||||
#include "HAL/HAL.h"
|
||||
#include "HALInitializer.h"
|
||||
|
||||
using namespace hal;
|
||||
|
||||
@@ -92,6 +93,7 @@ static uint8_t readRegister(Register reg);
|
||||
* Initialize the accelerometer.
|
||||
*/
|
||||
static void initializeAccelerometer() {
|
||||
hal::init::CheckInit();
|
||||
int32_t status;
|
||||
|
||||
if (!accel) {
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include "HAL/AnalogAccumulator.h"
|
||||
#include "HAL/AnalogInput.h"
|
||||
#include "HAL/handles/IndexedHandleResource.h"
|
||||
#include "HALInitializer.h"
|
||||
|
||||
namespace {
|
||||
|
||||
@@ -56,6 +57,7 @@ extern "C" {
|
||||
|
||||
HAL_GyroHandle HAL_InitializeAnalogGyro(HAL_AnalogInputHandle analogHandle,
|
||||
int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
if (!HAL_IsAccumulatorChannel(analogHandle, status)) {
|
||||
if (*status == 0) {
|
||||
*status = HAL_INVALID_ACCUMULATOR_CHANNEL;
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#include "HAL/AnalogAccumulator.h"
|
||||
#include "HAL/HAL.h"
|
||||
#include "HAL/handles/HandlesInternal.h"
|
||||
#include "HALInitializer.h"
|
||||
#include "PortsInternal.h"
|
||||
|
||||
namespace hal {
|
||||
@@ -33,6 +34,7 @@ extern "C" {
|
||||
*/
|
||||
HAL_AnalogInputHandle HAL_InitializeAnalogInputPort(HAL_PortHandle portHandle,
|
||||
int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
initializeAnalog(status);
|
||||
|
||||
if (*status != 0) return HAL_kInvalidHandle;
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
|
||||
#include "HAL/AnalogInput.h"
|
||||
#include "HAL/ChipObject.h"
|
||||
#include "HALInitializer.h"
|
||||
#include "PortsInternal.h"
|
||||
|
||||
namespace hal {
|
||||
@@ -44,6 +45,7 @@ void InitializeAnalogInternal() {
|
||||
* Initialize the analog System.
|
||||
*/
|
||||
void initializeAnalog(int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
if (analogSystemInitialized) return;
|
||||
std::lock_guard<wpi::mutex> lock(analogRegisterWindowMutex);
|
||||
if (analogSystemInitialized) return;
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "HAL/Errors.h"
|
||||
#include "HAL/handles/HandlesInternal.h"
|
||||
#include "HAL/handles/IndexedHandleResource.h"
|
||||
#include "HALInitializer.h"
|
||||
#include "PortsInternal.h"
|
||||
|
||||
using namespace hal;
|
||||
@@ -45,6 +46,7 @@ extern "C" {
|
||||
*/
|
||||
HAL_AnalogOutputHandle HAL_InitializeAnalogOutputPort(HAL_PortHandle portHandle,
|
||||
int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
initializeAnalog(status);
|
||||
|
||||
if (*status != 0) return HAL_kInvalidHandle;
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include "HAL/Errors.h"
|
||||
#include "HAL/handles/HandlesInternal.h"
|
||||
#include "HAL/handles/LimitedHandleResource.h"
|
||||
#include "HALInitializer.h"
|
||||
#include "PortsInternal.h"
|
||||
|
||||
using namespace hal;
|
||||
@@ -46,6 +47,7 @@ extern "C" {
|
||||
|
||||
HAL_AnalogTriggerHandle HAL_InitializeAnalogTrigger(
|
||||
HAL_AnalogInputHandle portHandle, int32_t* index, int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
// ensure we are given a valid and active AnalogInput handle
|
||||
auto analog_port = analogInputHandles->Get(portHandle);
|
||||
if (analog_port == nullptr) {
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
#include "HAL/Errors.h"
|
||||
#include "HAL/handles/HandlesInternal.h"
|
||||
#include "HALInitializer.h"
|
||||
#include "PCMInternal.h"
|
||||
#include "PortsInternal.h"
|
||||
#include "ctre/PCM.h"
|
||||
@@ -24,6 +25,7 @@ void InitializeCompressor() {}
|
||||
extern "C" {
|
||||
|
||||
HAL_CompressorHandle HAL_InitializeCompressor(int32_t module, int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
// Use status to check for invalid index
|
||||
initializePCM(module, status);
|
||||
if (*status != 0) {
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "DigitalInternal.h"
|
||||
#include "HAL/HAL.h"
|
||||
#include "HAL/handles/LimitedHandleResource.h"
|
||||
#include "HALInitializer.h"
|
||||
#include "PortsInternal.h"
|
||||
|
||||
using namespace hal;
|
||||
@@ -42,6 +43,7 @@ extern "C" {
|
||||
|
||||
HAL_CounterHandle HAL_InitializeCounter(HAL_Counter_Mode mode, int32_t* index,
|
||||
int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
auto handle = counterHandles->Allocate();
|
||||
if (handle == HAL_kInvalidHandle) { // out of resources
|
||||
*status = NO_AVAILABLE_RESOURCES;
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#include "HAL/cpp/fpga_clock.h"
|
||||
#include "HAL/handles/HandlesInternal.h"
|
||||
#include "HAL/handles/LimitedHandleResource.h"
|
||||
#include "HALInitializer.h"
|
||||
#include "PortsInternal.h"
|
||||
|
||||
using namespace hal;
|
||||
@@ -46,6 +47,7 @@ extern "C" {
|
||||
*/
|
||||
HAL_DigitalHandle HAL_InitializeDIOPort(HAL_PortHandle portHandle,
|
||||
HAL_Bool input, int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
initializeDigital(status);
|
||||
|
||||
if (*status != 0) return HAL_kInvalidHandle;
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include "HAL/HAL.h"
|
||||
#include "HAL/Ports.h"
|
||||
#include "HAL/cpp/UnsafeDIO.h"
|
||||
#include "HALInitializer.h"
|
||||
#include "PortsInternal.h"
|
||||
|
||||
namespace hal {
|
||||
@@ -70,6 +71,7 @@ int32_t ComputeDigitalMask(HAL_DigitalHandle handle, int32_t* status) {
|
||||
* Initialize the digital system.
|
||||
*/
|
||||
void initializeDigital(int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
static std::atomic_bool initialized{false};
|
||||
static wpi::mutex initializeMutex;
|
||||
// Initial check, as if it's true initialization has finished
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include "HAL/Counter.h"
|
||||
#include "HAL/Errors.h"
|
||||
#include "HAL/handles/LimitedClassedHandleResource.h"
|
||||
#include "HALInitializer.h"
|
||||
#include "PortsInternal.h"
|
||||
|
||||
using namespace hal;
|
||||
@@ -243,6 +244,7 @@ HAL_EncoderHandle HAL_InitializeEncoder(
|
||||
HAL_Handle digitalSourceHandleB, HAL_AnalogTriggerType analogTriggerTypeB,
|
||||
HAL_Bool reverseDirection, HAL_EncoderEncodingType encodingType,
|
||||
int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
auto encoder = std::make_shared<Encoder>(
|
||||
digitalSourceHandleA, analogTriggerTypeA, digitalSourceHandleB,
|
||||
analogTriggerTypeB, reverseDirection, encodingType, status);
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
|
||||
#include "DigitalInternal.h"
|
||||
#include "HAL/handles/LimitedHandleResource.h"
|
||||
#include "HALInitializer.h"
|
||||
#include "PortsInternal.h"
|
||||
|
||||
using namespace hal;
|
||||
@@ -46,6 +47,7 @@ HAL_FPGAEncoderHandle HAL_InitializeFPGAEncoder(
|
||||
HAL_Handle digitalSourceHandleA, HAL_AnalogTriggerType analogTriggerTypeA,
|
||||
HAL_Handle digitalSourceHandleB, HAL_AnalogTriggerType analogTriggerTypeB,
|
||||
HAL_Bool reverseDirection, int32_t* index, int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
bool routingAnalogTriggerA = false;
|
||||
uint8_t routingChannelA = 0;
|
||||
uint8_t routingModuleA = 0;
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include <wpi/raw_ostream.h>
|
||||
|
||||
#include "HAL/DriverStation.h"
|
||||
#include "HALInitializer.h"
|
||||
|
||||
static_assert(sizeof(int32_t) >= sizeof(int),
|
||||
"FRC_NetworkComm status variable is larger than 32 bits");
|
||||
@@ -380,6 +381,7 @@ static void newDataOccur(uint32_t refNum) {
|
||||
* that interfaces with LabVIEW.
|
||||
*/
|
||||
void HAL_InitializeDriverStation(void) {
|
||||
hal::init::CheckInit();
|
||||
static std::atomic_bool initialized{false};
|
||||
static wpi::mutex initializeMutex;
|
||||
// Initial check, as if it's true initialization has finished
|
||||
|
||||
@@ -372,6 +372,8 @@ HAL_Bool HAL_Initialize(int32_t timeout, int32_t mode) {
|
||||
|
||||
hal::init::InitializeHAL();
|
||||
|
||||
hal::init::HAL_IsInitialized.store(true);
|
||||
|
||||
setlinebuf(stdin);
|
||||
setlinebuf(stdout);
|
||||
wpi::outs().SetUnbuffered();
|
||||
|
||||
17
hal/src/main/native/athena/HALInitializer.cpp
Normal file
17
hal/src/main/native/athena/HALInitializer.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2018 FIRST. 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#include "HALInitializer.h"
|
||||
|
||||
#include "HAL/HAL.h"
|
||||
|
||||
namespace hal {
|
||||
namespace init {
|
||||
std::atomic_bool HAL_IsInitialized{false};
|
||||
void RunInitialize() { HAL_Initialize(500, 0); }
|
||||
} // namespace init
|
||||
} // namespace hal
|
||||
@@ -7,8 +7,17 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <atomic>
|
||||
|
||||
namespace hal {
|
||||
namespace init {
|
||||
extern std::atomic_bool HAL_IsInitialized;
|
||||
extern void RunInitialize();
|
||||
static inline void CheckInit() {
|
||||
if (HAL_IsInitialized.load(std::memory_order_relaxed)) return;
|
||||
RunInitialize();
|
||||
}
|
||||
|
||||
extern void InitializeAccelerometer();
|
||||
extern void InitializeAnalogAccumulator();
|
||||
extern void InitializeAnalogGyro();
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include "DigitalInternal.h"
|
||||
#include "HAL/DIO.h"
|
||||
#include "HAL/HAL.h"
|
||||
#include "HALInitializer.h"
|
||||
|
||||
using namespace hal;
|
||||
|
||||
@@ -46,6 +47,7 @@ extern "C" {
|
||||
* @param port The port to open, 0 for the on-board, 1 for the MXP.
|
||||
*/
|
||||
void HAL_InitializeI2C(HAL_I2CPort port, int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
initializeDigital(status);
|
||||
if (*status != 0) return;
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#include "HAL/Errors.h"
|
||||
#include "HAL/handles/HandlesInternal.h"
|
||||
#include "HAL/handles/LimitedHandleResource.h"
|
||||
#include "HALInitializer.h"
|
||||
#include "PortsInternal.h"
|
||||
|
||||
using namespace hal;
|
||||
@@ -93,6 +94,7 @@ extern "C" {
|
||||
|
||||
HAL_InterruptHandle HAL_InitializeInterrupts(HAL_Bool watcher,
|
||||
int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
HAL_InterruptHandle handle = interruptHandles->Allocate();
|
||||
if (handle == HAL_kInvalidHandle) {
|
||||
*status = NO_AVAILABLE_RESOURCES;
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
#include "HAL/Errors.h"
|
||||
#include "HAL/HAL.h"
|
||||
#include "HAL/handles/UnlimitedHandleResource.h"
|
||||
#include "HALInitializer.h"
|
||||
|
||||
using namespace hal;
|
||||
|
||||
@@ -113,6 +114,7 @@ void InitializeNotifier() {
|
||||
extern "C" {
|
||||
|
||||
HAL_NotifierHandle HAL_InitializeNotifier(int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
if (!notifierAtexitRegistered.test_and_set())
|
||||
std::atexit(cleanupNotifierAtExit);
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
|
||||
#include "HAL/Errors.h"
|
||||
#include "HAL/cpp/SerialHelper.h"
|
||||
#include "HALInitializer.h"
|
||||
|
||||
static int portHandles[4]{-1, -1, -1, -1};
|
||||
static std::chrono::milliseconds portTimeouts[4]{
|
||||
@@ -38,6 +39,7 @@ void InitializeOSSerialPort() {
|
||||
extern "C" {
|
||||
|
||||
void HAL_InitializeOSSerialPort(HAL_SerialPort port, int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
std::string portName;
|
||||
|
||||
hal::SerialHelper serialHelper;
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
#include "HAL/Errors.h"
|
||||
#include "HAL/Solenoid.h"
|
||||
#include "HALInitializer.h"
|
||||
#include "PortsInternal.h"
|
||||
|
||||
namespace hal {
|
||||
@@ -24,6 +25,7 @@ void InitializePCMInternal() {
|
||||
} // namespace init
|
||||
|
||||
void initializePCM(int32_t module, int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
if (!HAL_CheckSolenoidModule(module)) {
|
||||
*status = RESOURCE_OUT_OF_RANGE;
|
||||
return;
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
|
||||
#include "HAL/Errors.h"
|
||||
#include "HAL/Ports.h"
|
||||
#include "HALInitializer.h"
|
||||
#include "PortsInternal.h"
|
||||
#include "ctre/PDP.h"
|
||||
|
||||
@@ -43,6 +44,7 @@ void InitializePDP() {
|
||||
extern "C" {
|
||||
|
||||
void HAL_InitializePDP(int32_t module, int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
if (!HAL_CheckPDPModule(module)) {
|
||||
*status = RESOURCE_OUT_OF_RANGE;
|
||||
return;
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#include "DigitalInternal.h"
|
||||
#include "HAL/cpp/fpga_clock.h"
|
||||
#include "HAL/handles/HandlesInternal.h"
|
||||
#include "HALInitializer.h"
|
||||
#include "PortsInternal.h"
|
||||
|
||||
using namespace hal;
|
||||
@@ -70,6 +71,7 @@ extern "C" {
|
||||
|
||||
HAL_DigitalHandle HAL_InitializePWMPort(HAL_PortHandle portHandle,
|
||||
int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
initializeDigital(status);
|
||||
|
||||
if (*status != 0) return HAL_kInvalidHandle;
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <memory>
|
||||
|
||||
#include "HAL/ChipObject.h"
|
||||
#include "HALInitializer.h"
|
||||
|
||||
using namespace hal;
|
||||
|
||||
@@ -18,6 +19,7 @@ namespace hal {
|
||||
static std::unique_ptr<tPower> power{nullptr};
|
||||
|
||||
static void initializePower(int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
if (power == nullptr) {
|
||||
power.reset(tPower::create(status));
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
#include "DigitalInternal.h"
|
||||
#include "HAL/handles/IndexedHandleResource.h"
|
||||
#include "HALInitializer.h"
|
||||
#include "PortsInternal.h"
|
||||
|
||||
using namespace hal;
|
||||
@@ -43,6 +44,7 @@ extern "C" {
|
||||
|
||||
HAL_RelayHandle HAL_InitializeRelayPort(HAL_PortHandle portHandle, HAL_Bool fwd,
|
||||
int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
initializeDigital(status);
|
||||
|
||||
if (*status != 0) return HAL_kInvalidHandle;
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#include "HAL/DIO.h"
|
||||
#include "HAL/HAL.h"
|
||||
#include "HAL/handles/HandlesInternal.h"
|
||||
#include "HALInitializer.h"
|
||||
|
||||
using namespace hal;
|
||||
|
||||
@@ -105,6 +106,7 @@ static void CommonSPIPortFree(void) {
|
||||
* @param port The number of the port to use. 0-3 for Onboard CS0-CS3, 4 for MXP
|
||||
*/
|
||||
void HAL_InitializeSPI(HAL_SPIPort port, int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
if (port < 0 || port >= kSpiMaxHandles) {
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
return;
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <string>
|
||||
|
||||
#include "HAL/cpp/SerialHelper.h"
|
||||
#include "HALInitializer.h"
|
||||
#include "visa/visa.h"
|
||||
|
||||
static int32_t resourceManagerHandle{0};
|
||||
@@ -24,6 +25,7 @@ void InitializeSerialPort() {}
|
||||
extern "C" {
|
||||
|
||||
void HAL_InitializeSerialPort(HAL_SerialPort port, int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
std::string portName;
|
||||
|
||||
if (resourceManagerHandle == 0)
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#include "HAL/Ports.h"
|
||||
#include "HAL/handles/HandlesInternal.h"
|
||||
#include "HAL/handles/IndexedHandleResource.h"
|
||||
#include "HALInitializer.h"
|
||||
#include "PCMInternal.h"
|
||||
#include "PortsInternal.h"
|
||||
#include "ctre/PCM.h"
|
||||
@@ -49,6 +50,7 @@ extern "C" {
|
||||
|
||||
HAL_SolenoidHandle HAL_InitializeSolenoidPort(HAL_PortHandle portHandle,
|
||||
int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
int16_t channel = getPortHandleChannel(portHandle);
|
||||
int16_t module = getPortHandleModule(portHandle);
|
||||
if (channel == InvalidHandleIndex) {
|
||||
|
||||
Reference in New Issue
Block a user