2016-01-02 03:02:34 -08:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2017-08-23 22:06:13 -07:00
|
|
|
/* Copyright (c) 2016-2017 FIRST. All Rights Reserved. */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* 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. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2016-05-22 21:41:22 -07:00
|
|
|
#include "HAL/HAL.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2017-08-18 21:35:53 -07:00
|
|
|
#include "ErrorsInternal.h"
|
2016-12-21 21:55:31 -08:00
|
|
|
#include "HAL/DriverStation.h"
|
2016-05-22 21:41:22 -07:00
|
|
|
#include "HAL/Errors.h"
|
2017-10-18 02:27:55 -05:00
|
|
|
#include "HAL/Extensions.h"
|
2016-07-13 20:29:28 -07:00
|
|
|
#include "HAL/handles/HandlesInternal.h"
|
2017-08-18 21:35:53 -07:00
|
|
|
#include "MockData/RoboRioDataInternal.h"
|
|
|
|
|
#include "MockHooksInternal.h"
|
2015-12-30 19:06:47 -08:00
|
|
|
|
2016-06-05 15:23:58 -07:00
|
|
|
using namespace hal;
|
|
|
|
|
|
2015-11-26 00:08:32 -08:00
|
|
|
extern "C" {
|
|
|
|
|
|
2016-08-12 13:45:28 -07:00
|
|
|
HAL_PortHandle HAL_GetPort(int32_t channel) {
|
2016-07-12 10:45:14 -07:00
|
|
|
// Dont allow a number that wouldn't fit in a uint8_t
|
2016-08-12 13:45:28 -07:00
|
|
|
if (channel < 0 || channel >= 255) return HAL_kInvalidHandle;
|
|
|
|
|
return createPortHandle(channel, 1);
|
2016-07-12 10:45:14 -07:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @deprecated Uses module numbers
|
|
|
|
|
*/
|
2016-08-12 13:45:28 -07:00
|
|
|
HAL_PortHandle HAL_GetPortWithModule(int32_t module, int32_t channel) {
|
2016-07-12 10:45:14 -07:00
|
|
|
// Dont allow a number that wouldn't fit in a uint8_t
|
2016-08-12 13:45:28 -07:00
|
|
|
if (channel < 0 || channel >= 255) return HAL_kInvalidHandle;
|
2016-07-12 10:45:14 -07:00
|
|
|
if (module < 0 || module >= 255) return HAL_kInvalidHandle;
|
2016-08-12 13:45:28 -07:00
|
|
|
return createPortHandle(channel, module);
|
2014-05-02 17:54:01 -04:00
|
|
|
}
|
|
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
const char* HAL_GetErrorMessage(int32_t code) {
|
2016-05-20 17:30:37 -07:00
|
|
|
switch (code) {
|
|
|
|
|
case 0:
|
|
|
|
|
return "";
|
|
|
|
|
case CTR_RxTimeout:
|
|
|
|
|
return CTR_RxTimeout_MESSAGE;
|
|
|
|
|
case CTR_TxTimeout:
|
|
|
|
|
return CTR_TxTimeout_MESSAGE;
|
|
|
|
|
case CTR_InvalidParamValue:
|
|
|
|
|
return CTR_InvalidParamValue_MESSAGE;
|
|
|
|
|
case CTR_UnexpectedArbId:
|
|
|
|
|
return CTR_UnexpectedArbId_MESSAGE;
|
|
|
|
|
case CTR_TxFailed:
|
|
|
|
|
return CTR_TxFailed_MESSAGE;
|
|
|
|
|
case CTR_SigNotUpdated:
|
|
|
|
|
return CTR_SigNotUpdated_MESSAGE;
|
|
|
|
|
case NiFpga_Status_FifoTimeout:
|
|
|
|
|
return NiFpga_Status_FifoTimeout_MESSAGE;
|
|
|
|
|
case NiFpga_Status_TransferAborted:
|
|
|
|
|
return NiFpga_Status_TransferAborted_MESSAGE;
|
|
|
|
|
case NiFpga_Status_MemoryFull:
|
|
|
|
|
return NiFpga_Status_MemoryFull_MESSAGE;
|
|
|
|
|
case NiFpga_Status_SoftwareFault:
|
|
|
|
|
return NiFpga_Status_SoftwareFault_MESSAGE;
|
|
|
|
|
case NiFpga_Status_InvalidParameter:
|
|
|
|
|
return NiFpga_Status_InvalidParameter_MESSAGE;
|
|
|
|
|
case NiFpga_Status_ResourceNotFound:
|
|
|
|
|
return NiFpga_Status_ResourceNotFound_MESSAGE;
|
|
|
|
|
case NiFpga_Status_ResourceNotInitialized:
|
|
|
|
|
return NiFpga_Status_ResourceNotInitialized_MESSAGE;
|
|
|
|
|
case NiFpga_Status_HardwareFault:
|
|
|
|
|
return NiFpga_Status_HardwareFault_MESSAGE;
|
|
|
|
|
case NiFpga_Status_IrqTimeout:
|
|
|
|
|
return NiFpga_Status_IrqTimeout_MESSAGE;
|
|
|
|
|
case SAMPLE_RATE_TOO_HIGH:
|
|
|
|
|
return SAMPLE_RATE_TOO_HIGH_MESSAGE;
|
|
|
|
|
case VOLTAGE_OUT_OF_RANGE:
|
|
|
|
|
return VOLTAGE_OUT_OF_RANGE_MESSAGE;
|
|
|
|
|
case LOOP_TIMING_ERROR:
|
|
|
|
|
return LOOP_TIMING_ERROR_MESSAGE;
|
|
|
|
|
case SPI_WRITE_NO_MOSI:
|
|
|
|
|
return SPI_WRITE_NO_MOSI_MESSAGE;
|
|
|
|
|
case SPI_READ_NO_MISO:
|
|
|
|
|
return SPI_READ_NO_MISO_MESSAGE;
|
|
|
|
|
case SPI_READ_NO_DATA:
|
|
|
|
|
return SPI_READ_NO_DATA_MESSAGE;
|
|
|
|
|
case INCOMPATIBLE_STATE:
|
|
|
|
|
return INCOMPATIBLE_STATE_MESSAGE;
|
|
|
|
|
case NO_AVAILABLE_RESOURCES:
|
|
|
|
|
return NO_AVAILABLE_RESOURCES_MESSAGE;
|
|
|
|
|
case RESOURCE_IS_ALLOCATED:
|
|
|
|
|
return RESOURCE_IS_ALLOCATED_MESSAGE;
|
2016-07-13 20:29:28 -07:00
|
|
|
case RESOURCE_OUT_OF_RANGE:
|
|
|
|
|
return RESOURCE_OUT_OF_RANGE_MESSAGE;
|
|
|
|
|
case HAL_INVALID_ACCUMULATOR_CHANNEL:
|
|
|
|
|
return HAL_INVALID_ACCUMULATOR_CHANNEL_MESSAGE;
|
2016-07-03 17:27:06 -07:00
|
|
|
case HAL_HANDLE_ERROR:
|
|
|
|
|
return HAL_HANDLE_ERROR_MESSAGE;
|
2016-05-20 17:30:37 -07:00
|
|
|
case NULL_PARAMETER:
|
|
|
|
|
return NULL_PARAMETER_MESSAGE;
|
|
|
|
|
case ANALOG_TRIGGER_LIMIT_ORDER_ERROR:
|
|
|
|
|
return ANALOG_TRIGGER_LIMIT_ORDER_ERROR_MESSAGE;
|
|
|
|
|
case ANALOG_TRIGGER_PULSE_OUTPUT_ERROR:
|
|
|
|
|
return ANALOG_TRIGGER_PULSE_OUTPUT_ERROR_MESSAGE;
|
|
|
|
|
case PARAMETER_OUT_OF_RANGE:
|
|
|
|
|
return PARAMETER_OUT_OF_RANGE_MESSAGE;
|
2016-07-03 15:22:22 -07:00
|
|
|
case HAL_COUNTER_NOT_SUPPORTED:
|
|
|
|
|
return HAL_COUNTER_NOT_SUPPORTED_MESSAGE;
|
2017-09-07 21:40:30 -07:00
|
|
|
case HAL_ERR_CANSessionMux_InvalidBuffer:
|
2016-05-20 17:30:37 -07:00
|
|
|
return ERR_CANSessionMux_InvalidBuffer_MESSAGE;
|
2017-09-07 21:40:30 -07:00
|
|
|
case HAL_ERR_CANSessionMux_MessageNotFound:
|
2016-05-20 17:30:37 -07:00
|
|
|
return ERR_CANSessionMux_MessageNotFound_MESSAGE;
|
2017-09-07 21:40:30 -07:00
|
|
|
case HAL_WARN_CANSessionMux_NoToken:
|
2016-05-20 17:30:37 -07:00
|
|
|
return WARN_CANSessionMux_NoToken_MESSAGE;
|
2017-09-07 21:40:30 -07:00
|
|
|
case HAL_ERR_CANSessionMux_NotAllowed:
|
2016-05-20 17:30:37 -07:00
|
|
|
return ERR_CANSessionMux_NotAllowed_MESSAGE;
|
2017-09-07 21:40:30 -07:00
|
|
|
case HAL_ERR_CANSessionMux_NotInitialized:
|
2016-05-20 17:30:37 -07:00
|
|
|
return ERR_CANSessionMux_NotInitialized_MESSAGE;
|
|
|
|
|
case VI_ERROR_SYSTEM_ERROR:
|
|
|
|
|
return VI_ERROR_SYSTEM_ERROR_MESSAGE;
|
|
|
|
|
case VI_ERROR_INV_OBJECT:
|
|
|
|
|
return VI_ERROR_INV_OBJECT_MESSAGE;
|
|
|
|
|
case VI_ERROR_RSRC_LOCKED:
|
|
|
|
|
return VI_ERROR_RSRC_LOCKED_MESSAGE;
|
|
|
|
|
case VI_ERROR_RSRC_NFOUND:
|
|
|
|
|
return VI_ERROR_RSRC_NFOUND_MESSAGE;
|
|
|
|
|
case VI_ERROR_INV_RSRC_NAME:
|
|
|
|
|
return VI_ERROR_INV_RSRC_NAME_MESSAGE;
|
|
|
|
|
case VI_ERROR_QUEUE_OVERFLOW:
|
|
|
|
|
return VI_ERROR_QUEUE_OVERFLOW_MESSAGE;
|
|
|
|
|
case VI_ERROR_IO:
|
|
|
|
|
return VI_ERROR_IO_MESSAGE;
|
|
|
|
|
case VI_ERROR_ASRL_PARITY:
|
|
|
|
|
return VI_ERROR_ASRL_PARITY_MESSAGE;
|
|
|
|
|
case VI_ERROR_ASRL_FRAMING:
|
|
|
|
|
return VI_ERROR_ASRL_FRAMING_MESSAGE;
|
|
|
|
|
case VI_ERROR_ASRL_OVERRUN:
|
|
|
|
|
return VI_ERROR_ASRL_OVERRUN_MESSAGE;
|
|
|
|
|
case VI_ERROR_RSRC_BUSY:
|
|
|
|
|
return VI_ERROR_RSRC_BUSY_MESSAGE;
|
|
|
|
|
case VI_ERROR_INV_PARAMETER:
|
|
|
|
|
return VI_ERROR_INV_PARAMETER_MESSAGE;
|
2016-07-08 21:29:29 -07:00
|
|
|
case HAL_PWM_SCALE_ERROR:
|
|
|
|
|
return HAL_PWM_SCALE_ERROR_MESSAGE;
|
2016-05-20 17:30:37 -07:00
|
|
|
default:
|
|
|
|
|
return "Unknown error status";
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-09-13 21:21:57 -07:00
|
|
|
/**
|
|
|
|
|
* Returns the runtime type of this HAL
|
|
|
|
|
*/
|
2017-08-18 21:35:53 -07:00
|
|
|
HAL_RuntimeType HAL_GetRuntimeType() { return HAL_Mock; }
|
2016-09-13 21:21:57 -07:00
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
/**
|
|
|
|
|
* Return the FPGA Version number.
|
|
|
|
|
* For now, expect this to be competition year.
|
|
|
|
|
* @return FPGA Version number.
|
|
|
|
|
*/
|
2016-07-12 10:45:14 -07:00
|
|
|
int32_t HAL_GetFPGAVersion(int32_t* status) {
|
2017-08-18 21:35:53 -07:00
|
|
|
return 2018; // Automatically script this at some point
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the FPGA Revision number.
|
|
|
|
|
* The format of the revision is 3 numbers.
|
|
|
|
|
* The 12 most significant bits are the Major Revision.
|
|
|
|
|
* the next 8 bits are the Minor Revision.
|
|
|
|
|
* The 12 least significant bits are the Build Number.
|
|
|
|
|
* @return FPGA Revision number.
|
|
|
|
|
*/
|
2016-07-12 10:45:14 -07:00
|
|
|
int64_t HAL_GetFPGARevision(int32_t* status) {
|
2017-08-18 21:35:53 -07:00
|
|
|
return 0; // TODO: Find a better number to return;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read the microsecond-resolution timer on the FPGA.
|
2014-07-24 10:05:24 -04:00
|
|
|
*
|
2016-05-20 17:30:37 -07:00
|
|
|
* @return The current time in microseconds according to the FPGA (since FPGA
|
|
|
|
|
* reset).
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2017-08-18 21:35:53 -07:00
|
|
|
uint64_t HAL_GetFPGATime(int32_t* status) { return hal::GetFPGATime(); }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2014-07-29 14:34:52 -04:00
|
|
|
/**
|
2016-05-29 09:19:43 -07:00
|
|
|
* Get the state of the "USER" button on the roboRIO
|
2014-07-29 14:34:52 -04:00
|
|
|
* @return true if the button is currently pressed down
|
|
|
|
|
*/
|
2016-07-12 10:45:14 -07:00
|
|
|
HAL_Bool HAL_GetFPGAButton(int32_t* status) {
|
2017-08-18 21:35:53 -07:00
|
|
|
return SimRoboRioData[0].GetFPGAButton();
|
2014-07-29 14:34:52 -04:00
|
|
|
}
|
|
|
|
|
|
2016-07-12 10:45:14 -07:00
|
|
|
HAL_Bool HAL_GetSystemActive(int32_t* status) {
|
2017-08-18 21:35:53 -07:00
|
|
|
return true; // Figure out if we need to handle this
|
2014-11-18 10:56:25 -05:00
|
|
|
}
|
2015-08-19 11:12:54 -04:00
|
|
|
|
2016-07-12 10:45:14 -07:00
|
|
|
HAL_Bool HAL_GetBrownedOut(int32_t* status) {
|
2017-08-18 21:35:53 -07:00
|
|
|
return false; // Figure out if we need to detect a brownout condition
|
2016-12-02 00:32:01 -08:00
|
|
|
}
|
|
|
|
|
|
2017-07-01 00:43:06 -07:00
|
|
|
HAL_Bool HAL_Initialize(int32_t timeout, int32_t mode) {
|
2017-10-16 20:00:32 -07:00
|
|
|
static std::atomic_bool initialized{false};
|
|
|
|
|
static std::mutex initializeMutex;
|
|
|
|
|
// Initial check, as if it's true initialization has finished
|
|
|
|
|
if (initialized) return true;
|
|
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(initializeMutex);
|
|
|
|
|
// Second check in case another thread was waiting
|
|
|
|
|
if (initialized) return true;
|
|
|
|
|
|
2017-10-18 02:27:55 -05:00
|
|
|
if (HAL_LoadExtensions() < 0) return false;
|
2017-08-18 21:35:53 -07:00
|
|
|
hal::RestartTiming();
|
2016-07-17 19:42:33 -07:00
|
|
|
HAL_InitializeDriverStation();
|
2017-10-16 20:00:32 -07:00
|
|
|
|
|
|
|
|
initialized = true;
|
2017-08-18 21:35:53 -07:00
|
|
|
return true; // Add initialization if we need to at a later point
|
2014-01-06 10:12:21 -05:00
|
|
|
}
|
|
|
|
|
|
2016-07-12 10:45:14 -07:00
|
|
|
int64_t HAL_Report(int32_t resource, int32_t instanceNumber, int32_t context,
|
|
|
|
|
const char* feature) {
|
2017-08-18 21:35:53 -07:00
|
|
|
return 0; // Do nothing for now
|
2014-05-02 17:54:01 -04:00
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-11-26 00:08:32 -08:00
|
|
|
} // extern "C"
|