mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-25 01:41:43 +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
@@ -14,6 +14,7 @@
|
||||
#include "HAL/AnalogAccumulator.h"
|
||||
#include "HAL/AnalogInput.h"
|
||||
#include "HAL/handles/IndexedHandleResource.h"
|
||||
#include "HALInitializer.h"
|
||||
#include "MockData/AnalogGyroDataInternal.h"
|
||||
|
||||
namespace {
|
||||
@@ -42,6 +43,7 @@ void InitializeAnalogGyro() {
|
||||
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;
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
#include "AnalogInternal.h"
|
||||
#include "HAL/handles/HandlesInternal.h"
|
||||
#include "HALInitializer.h"
|
||||
#include "MockData/AnalogInDataInternal.h"
|
||||
#include "PortsInternal.h"
|
||||
|
||||
@@ -23,6 +24,7 @@ void InitializeAnalogInput() {}
|
||||
extern "C" {
|
||||
HAL_AnalogInputHandle HAL_InitializeAnalogInputPort(HAL_PortHandle portHandle,
|
||||
int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
int16_t channel = getPortHandleChannel(portHandle);
|
||||
if (channel == InvalidHandleIndex) {
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include "HAL/Errors.h"
|
||||
#include "HAL/handles/HandlesInternal.h"
|
||||
#include "HAL/handles/IndexedHandleResource.h"
|
||||
#include "HALInitializer.h"
|
||||
#include "MockData/AnalogOutDataInternal.h"
|
||||
#include "PortsInternal.h"
|
||||
|
||||
@@ -39,6 +40,7 @@ void InitializeAnalogOutput() {
|
||||
extern "C" {
|
||||
HAL_AnalogOutputHandle HAL_InitializeAnalogOutputPort(HAL_PortHandle portHandle,
|
||||
int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
int16_t channel = getPortHandleChannel(portHandle);
|
||||
if (channel == InvalidHandleIndex) {
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include "HAL/Errors.h"
|
||||
#include "HAL/handles/HandlesInternal.h"
|
||||
#include "HAL/handles/LimitedHandleResource.h"
|
||||
#include "HALInitializer.h"
|
||||
#include "MockData/AnalogInDataInternal.h"
|
||||
#include "MockData/AnalogTriggerDataInternal.h"
|
||||
#include "PortsInternal.h"
|
||||
@@ -63,6 +64,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 "MockData/PCMDataInternal.h"
|
||||
#include "PortsInternal.h"
|
||||
|
||||
@@ -23,6 +24,7 @@ void InitializeCompressor() {}
|
||||
extern "C" {
|
||||
|
||||
HAL_CompressorHandle HAL_InitializeCompressor(int32_t module, int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
// As compressors can have unlimited objects, just create a
|
||||
// handle with the module number as the index.
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include "HAL/Errors.h"
|
||||
#include "HAL/handles/HandlesInternal.h"
|
||||
#include "HAL/handles/LimitedHandleResource.h"
|
||||
#include "HALInitializer.h"
|
||||
#include "PortsInternal.h"
|
||||
|
||||
namespace hal {
|
||||
@@ -33,6 +34,7 @@ void InitializeCounter() {
|
||||
extern "C" {
|
||||
HAL_CounterHandle HAL_InitializeCounter(HAL_Counter_Mode mode, int32_t* index,
|
||||
int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
return 0;
|
||||
}
|
||||
void HAL_FreeCounter(HAL_CounterHandle counterHandle, int32_t* status) {}
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include "DigitalInternal.h"
|
||||
#include "HAL/handles/HandlesInternal.h"
|
||||
#include "HAL/handles/LimitedHandleResource.h"
|
||||
#include "HALInitializer.h"
|
||||
#include "MockData/DIODataInternal.h"
|
||||
#include "MockData/DigitalPWMDataInternal.h"
|
||||
#include "PortsInternal.h"
|
||||
@@ -41,6 +42,7 @@ extern "C" {
|
||||
*/
|
||||
HAL_DigitalHandle HAL_InitializeDIOPort(HAL_PortHandle portHandle,
|
||||
HAL_Bool input, int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
if (*status != 0) return HAL_kInvalidHandle;
|
||||
|
||||
int16_t channel = getPortHandleChannel(portHandle);
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include <wpi/condition_variable.h>
|
||||
#include <wpi/mutex.h>
|
||||
|
||||
#include "HALInitializer.h"
|
||||
#include "MockData/DriverStationDataInternal.h"
|
||||
#include "MockData/MockHooks.h"
|
||||
|
||||
@@ -289,6 +290,7 @@ static int32_t 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
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include "HAL/Errors.h"
|
||||
#include "HAL/handles/HandlesInternal.h"
|
||||
#include "HAL/handles/LimitedHandleResource.h"
|
||||
#include "HALInitializer.h"
|
||||
#include "MockData/EncoderDataInternal.h"
|
||||
#include "PortsInternal.h"
|
||||
|
||||
@@ -56,6 +57,7 @@ HAL_EncoderHandle HAL_InitializeEncoder(
|
||||
HAL_Handle digitalSourceHandleB, HAL_AnalogTriggerType analogTriggerTypeB,
|
||||
HAL_Bool reverseDirection, HAL_EncoderEncodingType encodingType,
|
||||
int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
HAL_Handle nativeHandle = HAL_kInvalidHandle;
|
||||
if (encodingType == HAL_EncoderEncodingType::HAL_Encoder_k4X) {
|
||||
// k4x, allocate encoder
|
||||
|
||||
@@ -263,6 +263,8 @@ HAL_Bool HAL_Initialize(int32_t timeout, int32_t mode) {
|
||||
|
||||
hal::init::InitializeHAL();
|
||||
|
||||
hal::init::HAL_IsInitialized.store(true);
|
||||
|
||||
wpi::outs().SetUnbuffered();
|
||||
if (HAL_LoadExtensions() < 0) return false;
|
||||
hal::RestartTiming();
|
||||
|
||||
17
hal/src/main/native/sim/HALInitializer.cpp
Normal file
17
hal/src/main/native/sim/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 InitializeAccelerometerData();
|
||||
extern void InitializeAnalogGyroData();
|
||||
extern void InitializeAnalogInData();
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
#include "HAL/I2C.h"
|
||||
|
||||
#include "HALInitializer.h"
|
||||
#include "MockData/I2CDataInternal.h"
|
||||
|
||||
using namespace hal;
|
||||
@@ -19,6 +20,7 @@ void InitializeI2C() {}
|
||||
|
||||
extern "C" {
|
||||
void HAL_InitializeI2C(HAL_I2CPort port, int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
SimI2CData[port].SetInitialized(true);
|
||||
}
|
||||
int32_t HAL_TransactionI2C(HAL_I2CPort port, int32_t deviceAddress,
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include "HAL/handles/HandlesInternal.h"
|
||||
#include "HAL/handles/LimitedHandleResource.h"
|
||||
#include "HAL/handles/UnlimitedHandleResource.h"
|
||||
#include "HALInitializer.h"
|
||||
#include "MockData/AnalogInDataInternal.h"
|
||||
#include "MockData/DIODataInternal.h"
|
||||
#include "MockData/HAL_Value.h"
|
||||
@@ -85,6 +86,7 @@ void InitializeInterrupts() {
|
||||
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;
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#include "HAL/HAL.h"
|
||||
#include "HAL/cpp/fpga_clock.h"
|
||||
#include "HAL/handles/UnlimitedHandleResource.h"
|
||||
#include "HALInitializer.h"
|
||||
|
||||
namespace {
|
||||
struct Notifier {
|
||||
@@ -60,6 +61,7 @@ void InitializeNotifier() {
|
||||
extern "C" {
|
||||
|
||||
HAL_NotifierHandle HAL_InitializeNotifier(int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
std::shared_ptr<Notifier> notifier = std::make_shared<Notifier>();
|
||||
HAL_NotifierHandle handle = notifierHandles->Allocate(notifier);
|
||||
if (handle == HAL_kInvalidHandle) {
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#include "HAL/SerialPort.h"
|
||||
#include "HALInitializer.h"
|
||||
|
||||
namespace hal {
|
||||
namespace init {
|
||||
@@ -14,7 +15,9 @@ void InitializeOSSerialPort() {}
|
||||
} // namespace hal
|
||||
|
||||
extern "C" {
|
||||
void HAL_InitializeOSSerialPort(HAL_SerialPort port, int32_t* status) {}
|
||||
void HAL_InitializeOSSerialPort(HAL_SerialPort port, int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
}
|
||||
void HAL_SetOSSerialBaudRate(HAL_SerialPort port, int32_t baud,
|
||||
int32_t* status) {}
|
||||
void HAL_SetOSSerialDataBits(HAL_SerialPort port, int32_t bits,
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
#include "HAL/PDP.h"
|
||||
|
||||
#include "HALInitializer.h"
|
||||
#include "MockData/PDPDataInternal.h"
|
||||
#include "PortsInternal.h"
|
||||
|
||||
@@ -20,6 +21,7 @@ void InitializePDP() {}
|
||||
|
||||
extern "C" {
|
||||
void HAL_InitializePDP(int32_t module, int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
SimPDPData[module].SetInitialized(true);
|
||||
}
|
||||
HAL_Bool HAL_CheckPDPModule(int32_t module) {
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include "ConstantsInternal.h"
|
||||
#include "DigitalInternal.h"
|
||||
#include "HAL/handles/HandlesInternal.h"
|
||||
#include "HALInitializer.h"
|
||||
#include "MockData/PWMDataInternal.h"
|
||||
#include "PortsInternal.h"
|
||||
|
||||
@@ -25,6 +26,7 @@ extern "C" {
|
||||
|
||||
HAL_DigitalHandle HAL_InitializePWMPort(HAL_PortHandle portHandle,
|
||||
int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
if (*status != 0) return HAL_kInvalidHandle;
|
||||
|
||||
int16_t channel = getPortHandleChannel(portHandle);
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include "HAL/Relay.h"
|
||||
|
||||
#include "HAL/handles/IndexedHandleResource.h"
|
||||
#include "HALInitializer.h"
|
||||
#include "MockData/RelayDataInternal.h"
|
||||
#include "PortsInternal.h"
|
||||
|
||||
@@ -37,6 +38,7 @@ void InitializeRelay() {
|
||||
extern "C" {
|
||||
HAL_RelayHandle HAL_InitializeRelayPort(HAL_PortHandle portHandle, HAL_Bool fwd,
|
||||
int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
if (*status != 0) return HAL_kInvalidHandle;
|
||||
|
||||
int16_t channel = getPortHandleChannel(portHandle);
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
#include "HAL/SPI.h"
|
||||
|
||||
#include "HALInitializer.h"
|
||||
#include "MockData/SPIDataInternal.h"
|
||||
|
||||
using namespace hal;
|
||||
@@ -18,6 +19,7 @@ void InitializeSPI() {}
|
||||
} // namespace hal
|
||||
|
||||
void HAL_InitializeSPI(HAL_SPIPort port, int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
SimSPIData[port].SetInitialized(true);
|
||||
}
|
||||
int32_t HAL_TransactionSPI(HAL_SPIPort port, const uint8_t* dataToSend,
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
|
||||
#include "HAL/SerialPort.h"
|
||||
|
||||
#include "HALInitializer.h"
|
||||
|
||||
namespace hal {
|
||||
namespace init {
|
||||
void InitializeSerialPort() {}
|
||||
@@ -14,7 +16,9 @@ void InitializeSerialPort() {}
|
||||
} // namespace hal
|
||||
|
||||
extern "C" {
|
||||
void HAL_InitializeSerialPort(HAL_SerialPort port, int32_t* status) {}
|
||||
void HAL_InitializeSerialPort(HAL_SerialPort port, int32_t* status) {
|
||||
hal::init::CheckInit();
|
||||
}
|
||||
|
||||
void HAL_InitializeSerialPortDirect(HAL_SerialPort port, const char* portName,
|
||||
int32_t* status) {}
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include "HAL/Errors.h"
|
||||
#include "HAL/handles/HandlesInternal.h"
|
||||
#include "HAL/handles/IndexedHandleResource.h"
|
||||
#include "HALInitializer.h"
|
||||
#include "MockData/PCMDataInternal.h"
|
||||
#include "PortsInternal.h"
|
||||
|
||||
@@ -41,6 +42,7 @@ void InitializeSolenoid() {
|
||||
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