blob: 06ee51312e695f567b70f386545e993b19c1b9c2 [file] [log] [blame]
Parker Schuhd3b7a8872018-02-19 16:42:27 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2008-2017. 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 "frc971/wpilib/ahal/InterruptableSensorBase.h"
9
Austin Schuh9950f682021-11-06 15:27:58 -070010#include "glog/logging.h"
Philipp Schrader790cb542023-07-05 21:06:52 -070011
12#include "frc971/wpilib/ahal/WPIErrors.h"
Austin Schuh9950f682021-11-06 15:27:58 -070013#include "hal/HAL.h"
Parker Schuhd3b7a8872018-02-19 16:42:27 -080014
15using namespace frc;
16
Austin Schuhf6b94632019-02-02 22:11:27 -080017InterruptableSensorBase::InterruptableSensorBase() {}
18
Parker Schuhd3b7a8872018-02-19 16:42:27 -080019void InterruptableSensorBase::RequestInterrupts() {
20 if (StatusIsFatal()) return;
21
Austin Schuh9950f682021-11-06 15:27:58 -070022 CHECK_EQ(m_interrupt, HAL_kInvalidHandle);
23 AllocateInterrupts();
Parker Schuhd3b7a8872018-02-19 16:42:27 -080024 if (StatusIsFatal()) return; // if allocate failed, out of interrupts
25
26 int32_t status = 0;
27 HAL_RequestInterrupts(
28 m_interrupt, GetPortHandleForRouting(),
29 static_cast<HAL_AnalogTriggerType>(GetAnalogTriggerTypeForRouting()),
30 &status);
31 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
32 SetUpSourceEdge(true, false);
33}
34
Austin Schuh9950f682021-11-06 15:27:58 -070035void InterruptableSensorBase::AllocateInterrupts() {
36 CHECK_EQ(m_interrupt, HAL_kInvalidHandle);
Parker Schuhd3b7a8872018-02-19 16:42:27 -080037 // Expects the calling leaf class to allocate an interrupt index.
38 int32_t status = 0;
Austin Schuh9950f682021-11-06 15:27:58 -070039 m_interrupt = HAL_InitializeInterrupts(&status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -080040 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
41}
42
Parker Schuhd3b7a8872018-02-19 16:42:27 -080043void InterruptableSensorBase::CancelInterrupts() {
44 if (StatusIsFatal()) return;
Austin Schuh9950f682021-11-06 15:27:58 -070045 CHECK_NE(m_interrupt, HAL_kInvalidHandle);
46 HAL_CleanInterrupts(m_interrupt);
Parker Schuhd3b7a8872018-02-19 16:42:27 -080047 // ignore status, as an invalid handle just needs to be ignored.
48 m_interrupt = HAL_kInvalidHandle;
49}
50
Parker Schuhd3b7a8872018-02-19 16:42:27 -080051InterruptableSensorBase::WaitResult InterruptableSensorBase::WaitForInterrupt(
52 double timeout, bool ignorePrevious) {
53 if (StatusIsFatal()) return InterruptableSensorBase::kTimeout;
Austin Schuh9950f682021-11-06 15:27:58 -070054 CHECK_NE(m_interrupt, HAL_kInvalidHandle);
Parker Schuhd3b7a8872018-02-19 16:42:27 -080055 int32_t status = 0;
56 int result;
57
58 result = HAL_WaitForInterrupt(m_interrupt, timeout, ignorePrevious, &status);
59 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
60
61 // Rising edge result is the interrupt bit set in the byte 0xFF
62 // Falling edge result is the interrupt bit set in the byte 0xFF00
63 // Set any bit set to be true for that edge, and AND the 2 results
64 // together to match the existing enum for all interrupts
65 int32_t rising = (result & 0xFF) ? 0x1 : 0x0;
66 int32_t falling = ((result & 0xFF00) ? 0x0100 : 0x0);
67 return static_cast<WaitResult>(falling | rising);
68}
69
Austin Schuhf6b94632019-02-02 22:11:27 -080070hal::fpga_clock::time_point InterruptableSensorBase::ReadRisingTimestamp() {
71 if (StatusIsFatal()) return hal::fpga_clock::min_time;
Austin Schuh9950f682021-11-06 15:27:58 -070072 CHECK_NE(m_interrupt, HAL_kInvalidHandle);
Parker Schuhd3b7a8872018-02-19 16:42:27 -080073 int32_t status = 0;
Austin Schuhf6b94632019-02-02 22:11:27 -080074 uint64_t timestamp = HAL_ReadInterruptRisingTimestamp(m_interrupt, &status);
75 timestamp = HAL_ExpandFPGATime(timestamp, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -080076 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
Austin Schuhf6b94632019-02-02 22:11:27 -080077 return hal::fpga_clock::time_point(hal::fpga_clock::duration(timestamp));
Parker Schuhd3b7a8872018-02-19 16:42:27 -080078}
79
Austin Schuhf6b94632019-02-02 22:11:27 -080080hal::fpga_clock::time_point InterruptableSensorBase::ReadFallingTimestamp() {
81 if (StatusIsFatal()) return hal::fpga_clock::min_time;
Austin Schuh9950f682021-11-06 15:27:58 -070082 CHECK_NE(m_interrupt, HAL_kInvalidHandle);
Parker Schuhd3b7a8872018-02-19 16:42:27 -080083 int32_t status = 0;
Austin Schuhf6b94632019-02-02 22:11:27 -080084 uint64_t timestamp = HAL_ReadInterruptFallingTimestamp(m_interrupt, &status);
85 timestamp = HAL_ExpandFPGATime(timestamp, &status);
Parker Schuhd3b7a8872018-02-19 16:42:27 -080086 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
Austin Schuhf6b94632019-02-02 22:11:27 -080087 return hal::fpga_clock::time_point(hal::fpga_clock::duration(timestamp));
Parker Schuhd3b7a8872018-02-19 16:42:27 -080088}
89
Parker Schuhd3b7a8872018-02-19 16:42:27 -080090void InterruptableSensorBase::SetUpSourceEdge(bool risingEdge,
91 bool fallingEdge) {
92 if (StatusIsFatal()) return;
93 if (m_interrupt == HAL_kInvalidHandle) {
94 wpi_setWPIErrorWithContext(
95 NullParameter,
96 "You must call RequestInterrupts before SetUpSourceEdge");
97 return;
98 }
99 if (m_interrupt != HAL_kInvalidHandle) {
100 int32_t status = 0;
101 HAL_SetInterruptUpSourceEdge(m_interrupt, risingEdge, fallingEdge, &status);
102 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
103 }
104}