Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) 2016-2019 FIRST. All Rights Reserved. */ |
| 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include "hal/HAL.h" |
| 9 | |
| 10 | #include <signal.h> // linux for kill |
| 11 | #include <sys/prctl.h> |
| 12 | #include <unistd.h> |
| 13 | |
| 14 | #include <atomic> |
| 15 | #include <cstdlib> |
| 16 | #include <fstream> |
| 17 | #include <thread> |
| 18 | |
| 19 | #include <FRC_NetworkCommunication/FRCComm.h> |
| 20 | #include <FRC_NetworkCommunication/LoadOut.h> |
| 21 | #include <FRC_NetworkCommunication/UsageReporting.h> |
| 22 | #include <wpi/mutex.h> |
| 23 | #include <wpi/raw_ostream.h> |
| 24 | #include <wpi/timestamp.h> |
| 25 | |
| 26 | #include "HALInitializer.h" |
| 27 | #include "ctre/ctre.h" |
| 28 | #include "hal/ChipObject.h" |
| 29 | #include "hal/DriverStation.h" |
| 30 | #include "hal/Errors.h" |
| 31 | #include "hal/Notifier.h" |
| 32 | #include "hal/handles/HandlesInternal.h" |
| 33 | |
| 34 | using namespace hal; |
| 35 | |
| 36 | static std::unique_ptr<tGlobal> global; |
| 37 | static std::unique_ptr<tSysWatchdog> watchdog; |
| 38 | |
| 39 | using namespace hal; |
| 40 | |
| 41 | namespace hal { |
| 42 | namespace init { |
| 43 | void InitializeHAL() { |
| 44 | InitializeAddressableLED(); |
| 45 | InitializeAccelerometer(); |
| 46 | InitializeAnalogAccumulator(); |
| 47 | InitializeAnalogInput(); |
| 48 | InitializeAnalogInternal(); |
| 49 | InitializeAnalogOutput(); |
| 50 | InitializeAnalogTrigger(); |
| 51 | InitializeCAN(); |
| 52 | InitializeCANAPI(); |
| 53 | InitializeCompressor(); |
| 54 | InitializeConstants(); |
| 55 | InitializeCounter(); |
| 56 | InitializeDigitalInternal(); |
| 57 | InitializeDIO(); |
| 58 | InitializeDMA(); |
| 59 | InitializeDutyCycle(); |
| 60 | InitializeEncoder(); |
| 61 | InitializeFPGAEncoder(); |
| 62 | InitializeFRCDriverStation(); |
| 63 | InitializeI2C(); |
| 64 | InitialzeInterrupts(); |
| 65 | InitializeMain(); |
| 66 | InitializeNotifier(); |
| 67 | InitializePCMInternal(); |
| 68 | InitializePDP(); |
| 69 | InitializePorts(); |
| 70 | InitializePower(); |
| 71 | InitializePWM(); |
| 72 | InitializeRelay(); |
| 73 | InitializeSolenoid(); |
| 74 | InitializeSPI(); |
| 75 | InitializeThreads(); |
| 76 | } |
| 77 | } // namespace init |
| 78 | } // namespace hal |
| 79 | |
| 80 | extern "C" { |
| 81 | |
| 82 | HAL_PortHandle HAL_GetPort(int32_t channel) { |
| 83 | // Dont allow a number that wouldn't fit in a uint8_t |
| 84 | if (channel < 0 || channel >= 255) return HAL_kInvalidHandle; |
| 85 | return createPortHandle(channel, 1); |
| 86 | } |
| 87 | |
| 88 | HAL_PortHandle HAL_GetPortWithModule(int32_t module, int32_t channel) { |
| 89 | // Dont allow a number that wouldn't fit in a uint8_t |
| 90 | if (channel < 0 || channel >= 255) return HAL_kInvalidHandle; |
| 91 | if (module < 0 || module >= 255) return HAL_kInvalidHandle; |
| 92 | return createPortHandle(channel, module); |
| 93 | } |
| 94 | |
| 95 | const char* HAL_GetErrorMessage(int32_t code) { |
| 96 | switch (code) { |
| 97 | case 0: |
| 98 | return ""; |
| 99 | case CTR_RxTimeout: |
| 100 | return CTR_RxTimeout_MESSAGE; |
| 101 | case CTR_TxTimeout: |
| 102 | return CTR_TxTimeout_MESSAGE; |
| 103 | case CTR_InvalidParamValue: |
| 104 | return CTR_InvalidParamValue_MESSAGE; |
| 105 | case CTR_UnexpectedArbId: |
| 106 | return CTR_UnexpectedArbId_MESSAGE; |
| 107 | case CTR_TxFailed: |
| 108 | return CTR_TxFailed_MESSAGE; |
| 109 | case CTR_SigNotUpdated: |
| 110 | return CTR_SigNotUpdated_MESSAGE; |
| 111 | case NiFpga_Status_FifoTimeout: |
| 112 | return NiFpga_Status_FifoTimeout_MESSAGE; |
| 113 | case NiFpga_Status_TransferAborted: |
| 114 | return NiFpga_Status_TransferAborted_MESSAGE; |
| 115 | case NiFpga_Status_MemoryFull: |
| 116 | return NiFpga_Status_MemoryFull_MESSAGE; |
| 117 | case NiFpga_Status_SoftwareFault: |
| 118 | return NiFpga_Status_SoftwareFault_MESSAGE; |
| 119 | case NiFpga_Status_InvalidParameter: |
| 120 | return NiFpga_Status_InvalidParameter_MESSAGE; |
| 121 | case NiFpga_Status_ResourceNotFound: |
| 122 | return NiFpga_Status_ResourceNotFound_MESSAGE; |
| 123 | case NiFpga_Status_ResourceNotInitialized: |
| 124 | return NiFpga_Status_ResourceNotInitialized_MESSAGE; |
| 125 | case NiFpga_Status_HardwareFault: |
| 126 | return NiFpga_Status_HardwareFault_MESSAGE; |
| 127 | case NiFpga_Status_IrqTimeout: |
| 128 | return NiFpga_Status_IrqTimeout_MESSAGE; |
| 129 | case SAMPLE_RATE_TOO_HIGH: |
| 130 | return SAMPLE_RATE_TOO_HIGH_MESSAGE; |
| 131 | case VOLTAGE_OUT_OF_RANGE: |
| 132 | return VOLTAGE_OUT_OF_RANGE_MESSAGE; |
| 133 | case LOOP_TIMING_ERROR: |
| 134 | return LOOP_TIMING_ERROR_MESSAGE; |
| 135 | case SPI_WRITE_NO_MOSI: |
| 136 | return SPI_WRITE_NO_MOSI_MESSAGE; |
| 137 | case SPI_READ_NO_MISO: |
| 138 | return SPI_READ_NO_MISO_MESSAGE; |
| 139 | case SPI_READ_NO_DATA: |
| 140 | return SPI_READ_NO_DATA_MESSAGE; |
| 141 | case INCOMPATIBLE_STATE: |
| 142 | return INCOMPATIBLE_STATE_MESSAGE; |
| 143 | case NO_AVAILABLE_RESOURCES: |
| 144 | return NO_AVAILABLE_RESOURCES_MESSAGE; |
| 145 | case RESOURCE_IS_ALLOCATED: |
| 146 | return RESOURCE_IS_ALLOCATED_MESSAGE; |
| 147 | case RESOURCE_OUT_OF_RANGE: |
| 148 | return RESOURCE_OUT_OF_RANGE_MESSAGE; |
| 149 | case HAL_INVALID_ACCUMULATOR_CHANNEL: |
| 150 | return HAL_INVALID_ACCUMULATOR_CHANNEL_MESSAGE; |
| 151 | case HAL_HANDLE_ERROR: |
| 152 | return HAL_HANDLE_ERROR_MESSAGE; |
| 153 | case NULL_PARAMETER: |
| 154 | return NULL_PARAMETER_MESSAGE; |
| 155 | case ANALOG_TRIGGER_LIMIT_ORDER_ERROR: |
| 156 | return ANALOG_TRIGGER_LIMIT_ORDER_ERROR_MESSAGE; |
| 157 | case ANALOG_TRIGGER_PULSE_OUTPUT_ERROR: |
| 158 | return ANALOG_TRIGGER_PULSE_OUTPUT_ERROR_MESSAGE; |
| 159 | case PARAMETER_OUT_OF_RANGE: |
| 160 | return PARAMETER_OUT_OF_RANGE_MESSAGE; |
| 161 | case HAL_COUNTER_NOT_SUPPORTED: |
| 162 | return HAL_COUNTER_NOT_SUPPORTED_MESSAGE; |
| 163 | case HAL_ERR_CANSessionMux_InvalidBuffer: |
| 164 | return ERR_CANSessionMux_InvalidBuffer_MESSAGE; |
| 165 | case HAL_ERR_CANSessionMux_MessageNotFound: |
| 166 | return ERR_CANSessionMux_MessageNotFound_MESSAGE; |
| 167 | case HAL_WARN_CANSessionMux_NoToken: |
| 168 | return WARN_CANSessionMux_NoToken_MESSAGE; |
| 169 | case HAL_ERR_CANSessionMux_NotAllowed: |
| 170 | return ERR_CANSessionMux_NotAllowed_MESSAGE; |
| 171 | case HAL_ERR_CANSessionMux_NotInitialized: |
| 172 | return ERR_CANSessionMux_NotInitialized_MESSAGE; |
| 173 | case HAL_PWM_SCALE_ERROR: |
| 174 | return HAL_PWM_SCALE_ERROR_MESSAGE; |
| 175 | case HAL_SERIAL_PORT_NOT_FOUND: |
| 176 | return HAL_SERIAL_PORT_NOT_FOUND_MESSAGE; |
| 177 | case HAL_THREAD_PRIORITY_ERROR: |
| 178 | return HAL_THREAD_PRIORITY_ERROR_MESSAGE; |
| 179 | case HAL_THREAD_PRIORITY_RANGE_ERROR: |
| 180 | return HAL_THREAD_PRIORITY_RANGE_ERROR_MESSAGE; |
| 181 | case HAL_SERIAL_PORT_OPEN_ERROR: |
| 182 | return HAL_SERIAL_PORT_OPEN_ERROR_MESSAGE; |
| 183 | case HAL_SERIAL_PORT_ERROR: |
| 184 | return HAL_SERIAL_PORT_ERROR_MESSAGE; |
| 185 | case HAL_CAN_TIMEOUT: |
| 186 | return HAL_CAN_TIMEOUT_MESSAGE; |
| 187 | case ERR_FRCSystem_NetCommNotResponding: |
| 188 | return ERR_FRCSystem_NetCommNotResponding_MESSAGE; |
| 189 | case ERR_FRCSystem_NoDSConnection: |
| 190 | return ERR_FRCSystem_NoDSConnection_MESSAGE; |
| 191 | case HAL_CAN_BUFFER_OVERRUN: |
| 192 | return HAL_CAN_BUFFER_OVERRUN_MESSAGE; |
| 193 | case HAL_LED_CHANNEL_ERROR: |
| 194 | return HAL_LED_CHANNEL_ERROR_MESSAGE; |
| 195 | default: |
| 196 | return "Unknown error status"; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | HAL_RuntimeType HAL_GetRuntimeType(void) { return HAL_Athena; } |
| 201 | |
| 202 | int32_t HAL_GetFPGAVersion(int32_t* status) { |
| 203 | if (!global) { |
| 204 | *status = NiFpga_Status_ResourceNotInitialized; |
| 205 | return 0; |
| 206 | } |
| 207 | return global->readVersion(status); |
| 208 | } |
| 209 | |
| 210 | int64_t HAL_GetFPGARevision(int32_t* status) { |
| 211 | if (!global) { |
| 212 | *status = NiFpga_Status_ResourceNotInitialized; |
| 213 | return 0; |
| 214 | } |
| 215 | return global->readRevision(status); |
| 216 | } |
| 217 | |
| 218 | uint64_t HAL_GetFPGATime(int32_t* status) { |
| 219 | if (!global) { |
| 220 | *status = NiFpga_Status_ResourceNotInitialized; |
| 221 | return 0; |
| 222 | } |
| 223 | *status = 0; |
| 224 | uint64_t upper1 = global->readLocalTimeUpper(status); |
| 225 | uint32_t lower = global->readLocalTime(status); |
| 226 | uint64_t upper2 = global->readLocalTimeUpper(status); |
| 227 | if (*status != 0) return 0; |
| 228 | if (upper1 != upper2) { |
| 229 | // Rolled over between the lower call, reread lower |
| 230 | lower = global->readLocalTime(status); |
| 231 | if (*status != 0) return 0; |
| 232 | } |
| 233 | return (upper2 << 32) + lower; |
| 234 | } |
| 235 | |
| 236 | uint64_t HAL_ExpandFPGATime(uint32_t unexpanded_lower, int32_t* status) { |
| 237 | // Capture the current FPGA time. This will give us the upper half of the |
| 238 | // clock. |
| 239 | uint64_t fpga_time = HAL_GetFPGATime(status); |
| 240 | if (*status != 0) return 0; |
| 241 | |
| 242 | // Now, we need to detect the case where the lower bits rolled over after we |
| 243 | // sampled. In that case, the upper bits will be 1 bigger than they should |
| 244 | // be. |
| 245 | |
| 246 | // Break it into lower and upper portions. |
| 247 | uint32_t lower = fpga_time & 0xffffffffull; |
| 248 | uint64_t upper = (fpga_time >> 32) & 0xffffffff; |
| 249 | |
| 250 | // The time was sampled *before* the current time, so roll it back. |
| 251 | if (lower < unexpanded_lower) { |
| 252 | --upper; |
| 253 | } |
| 254 | |
| 255 | return (upper << 32) + static_cast<uint64_t>(unexpanded_lower); |
| 256 | } |
| 257 | |
| 258 | HAL_Bool HAL_GetFPGAButton(int32_t* status) { |
| 259 | if (!global) { |
| 260 | *status = NiFpga_Status_ResourceNotInitialized; |
| 261 | return false; |
| 262 | } |
| 263 | return global->readUserButton(status); |
| 264 | } |
| 265 | |
| 266 | HAL_Bool HAL_GetSystemActive(int32_t* status) { |
| 267 | if (!watchdog) { |
| 268 | *status = NiFpga_Status_ResourceNotInitialized; |
| 269 | return false; |
| 270 | } |
| 271 | return watchdog->readStatus_SystemActive(status); |
| 272 | } |
| 273 | |
| 274 | HAL_Bool HAL_GetBrownedOut(int32_t* status) { |
| 275 | if (!watchdog) { |
| 276 | *status = NiFpga_Status_ResourceNotInitialized; |
| 277 | return false; |
| 278 | } |
| 279 | return !(watchdog->readStatus_PowerAlive(status)); |
| 280 | } |
| 281 | |
| 282 | static bool killExistingProgram(int timeout, int mode) { |
| 283 | // Kill any previous robot programs |
| 284 | std::fstream fs; |
| 285 | // By making this both in/out, it won't give us an error if it doesnt exist |
| 286 | fs.open("/var/lock/frc.pid", std::fstream::in | std::fstream::out); |
| 287 | if (fs.bad()) return false; |
| 288 | |
| 289 | pid_t pid = 0; |
| 290 | if (!fs.eof() && !fs.fail()) { |
| 291 | fs >> pid; |
| 292 | // see if the pid is around, but we don't want to mess with init id=1, or |
| 293 | // ourselves |
| 294 | if (pid >= 2 && kill(pid, 0) == 0 && pid != getpid()) { |
| 295 | wpi::outs() << "Killing previously running FRC program...\n"; |
| 296 | kill(pid, SIGTERM); // try to kill it |
| 297 | std::this_thread::sleep_for(std::chrono::milliseconds(timeout)); |
| 298 | if (kill(pid, 0) == 0) { |
| 299 | // still not successfull |
| 300 | wpi::outs() << "FRC pid " << pid << " did not die within " << timeout |
| 301 | << "ms. Force killing with kill -9\n"; |
| 302 | // Force kill -9 |
| 303 | auto forceKill = kill(pid, SIGKILL); |
| 304 | if (forceKill != 0) { |
| 305 | auto errorMsg = std::strerror(forceKill); |
| 306 | wpi::outs() << "Kill -9 error: " << errorMsg << "\n"; |
| 307 | } |
| 308 | // Give a bit of time for the kill to take place |
| 309 | std::this_thread::sleep_for(std::chrono::milliseconds(250)); |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | fs.close(); |
| 314 | // we will re-open it write only to truncate the file |
| 315 | fs.open("/var/lock/frc.pid", std::fstream::out | std::fstream::trunc); |
| 316 | fs.seekp(0); |
| 317 | pid = getpid(); |
| 318 | fs << pid << std::endl; |
| 319 | fs.close(); |
| 320 | return true; |
| 321 | } |
| 322 | |
| 323 | HAL_Bool HAL_Initialize(int32_t timeout, int32_t mode) { |
| 324 | static std::atomic_bool initialized{false}; |
| 325 | static wpi::mutex initializeMutex; |
| 326 | // Initial check, as if it's true initialization has finished |
| 327 | if (initialized) return true; |
| 328 | |
| 329 | std::scoped_lock lock(initializeMutex); |
| 330 | // Second check in case another thread was waiting |
| 331 | if (initialized) return true; |
| 332 | |
| 333 | hal::init::InitializeHAL(); |
| 334 | |
| 335 | hal::init::HAL_IsInitialized.store(true); |
| 336 | |
| 337 | setlinebuf(stdin); |
| 338 | setlinebuf(stdout); |
| 339 | wpi::outs().SetUnbuffered(); |
| 340 | |
| 341 | prctl(PR_SET_PDEATHSIG, SIGTERM); |
| 342 | |
| 343 | // Return false if program failed to kill an existing program |
| 344 | if (!killExistingProgram(timeout, mode)) { |
| 345 | return false; |
| 346 | } |
| 347 | |
| 348 | FRC_NetworkCommunication_Reserve(nullptr); |
| 349 | |
| 350 | std::atexit([]() { |
| 351 | // Unregister our new data condition variable. |
| 352 | setNewDataSem(nullptr); |
| 353 | }); |
| 354 | |
| 355 | // image 4; Fixes errors caused by multiple processes. Talk to NI about this |
| 356 | nFPGA::nRoboRIO_FPGANamespace::g_currentTargetClass = |
| 357 | nLoadOut::kTargetClass_RoboRIO; |
| 358 | |
| 359 | int32_t status = 0; |
| 360 | global.reset(tGlobal::create(&status)); |
| 361 | watchdog.reset(tSysWatchdog::create(&status)); |
| 362 | |
| 363 | if (status != 0) return false; |
| 364 | |
| 365 | HAL_InitializeDriverStation(); |
| 366 | |
| 367 | // Set WPI_Now to use FPGA timestamp |
| 368 | wpi::SetNowImpl([]() -> uint64_t { |
| 369 | int32_t status = 0; |
| 370 | uint64_t rv = HAL_GetFPGATime(&status); |
| 371 | if (status != 0) { |
| 372 | wpi::errs() |
| 373 | << "Call to HAL_GetFPGATime failed in wpi::Now() with status " |
| 374 | << status |
| 375 | << ". Initialization might have failed. Time will not be correct\n"; |
| 376 | wpi::errs().flush(); |
| 377 | return 0u; |
| 378 | } |
| 379 | return rv; |
| 380 | }); |
| 381 | |
| 382 | initialized = true; |
| 383 | return true; |
| 384 | } |
| 385 | |
| 386 | int64_t HAL_Report(int32_t resource, int32_t instanceNumber, int32_t context, |
| 387 | const char* feature) { |
| 388 | if (feature == nullptr) { |
| 389 | feature = ""; |
| 390 | } |
| 391 | |
| 392 | return FRC_NetworkCommunication_nUsageReporting_report( |
| 393 | resource, instanceNumber, context, feature); |
| 394 | } |
| 395 | |
| 396 | // TODO: HACKS |
| 397 | // No need for header definitions, as we should not run from user code. |
| 398 | void NumericArrayResize(void) {} |
| 399 | void RTSetCleanupProc(void) {} |
| 400 | void EDVR_CreateReference(void) {} |
| 401 | |
| 402 | } // extern "C" |