2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2018-01-02 09:20:21 -08:00
|
|
|
/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "InterruptableSensorBase.h"
|
2016-05-25 22:38:11 -07:00
|
|
|
|
2017-08-27 00:11:52 -07:00
|
|
|
#include <HAL/HAL.h>
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
#include "Utility.h"
|
2014-10-08 14:52:24 -04:00
|
|
|
#include "WPIErrors.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void InterruptableSensorBase::RequestInterrupts(
|
2016-09-05 07:31:51 -07:00
|
|
|
HAL_InterruptHandlerFunction handler, void* param) {
|
2015-06-25 15:07:55 -04:00
|
|
|
if (StatusIsFatal()) return;
|
|
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_assert(m_interrupt == HAL_kInvalidHandle);
|
2015-06-25 15:07:55 -04:00
|
|
|
AllocateInterrupts(false);
|
2016-06-20 23:22:49 -07:00
|
|
|
if (StatusIsFatal()) return; // if allocate failed, out of interrupts
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
int32_t status = 0;
|
2016-08-24 21:39:16 -07:00
|
|
|
HAL_RequestInterrupts(
|
|
|
|
|
m_interrupt, GetPortHandleForRouting(),
|
|
|
|
|
static_cast<HAL_AnalogTriggerType>(GetAnalogTriggerTypeForRouting()),
|
|
|
|
|
&status);
|
2015-06-25 15:07:55 -04:00
|
|
|
SetUpSourceEdge(true, false);
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_AttachInterruptHandler(m_interrupt, handler, param, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2014-10-08 14:52:24 -04:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void InterruptableSensorBase::RequestInterrupts() {
|
|
|
|
|
if (StatusIsFatal()) return;
|
|
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_assert(m_interrupt == HAL_kInvalidHandle);
|
2015-06-25 15:07:55 -04:00
|
|
|
AllocateInterrupts(true);
|
2016-06-20 23:22:49 -07:00
|
|
|
if (StatusIsFatal()) return; // if allocate failed, out of interrupts
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
int32_t status = 0;
|
2016-08-24 21:39:16 -07:00
|
|
|
HAL_RequestInterrupts(
|
|
|
|
|
m_interrupt, GetPortHandleForRouting(),
|
|
|
|
|
static_cast<HAL_AnalogTriggerType>(GetAnalogTriggerTypeForRouting()),
|
|
|
|
|
&status);
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2015-06-25 15:07:55 -04:00
|
|
|
SetUpSourceEdge(true, false);
|
2014-10-08 14:52:24 -04:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void InterruptableSensorBase::CancelInterrupts() {
|
|
|
|
|
if (StatusIsFatal()) return;
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_assert(m_interrupt != HAL_kInvalidHandle);
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_CleanInterrupts(m_interrupt, &status);
|
2017-11-16 00:33:51 -08:00
|
|
|
// Ignore status, as an invalid handle just needs to be ignored.
|
2016-07-09 00:24:26 -07:00
|
|
|
m_interrupt = HAL_kInvalidHandle;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
InterruptableSensorBase::WaitResult InterruptableSensorBase::WaitForInterrupt(
|
2016-11-20 07:25:03 -08:00
|
|
|
double timeout, bool ignorePrevious) {
|
2015-06-25 15:07:55 -04:00
|
|
|
if (StatusIsFatal()) return InterruptableSensorBase::kTimeout;
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_assert(m_interrupt != HAL_kInvalidHandle);
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-09-06 00:01:45 -07:00
|
|
|
int result;
|
2014-10-05 17:17:59 -04:00
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
result = HAL_WaitForInterrupt(m_interrupt, timeout, ignorePrevious, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2014-10-05 17:17:59 -04:00
|
|
|
|
2017-05-07 09:07:14 -07:00
|
|
|
// Rising edge result is the interrupt bit set in the byte 0xFF
|
|
|
|
|
// Falling edge result is the interrupt bit set in the byte 0xFF00
|
|
|
|
|
// Set any bit set to be true for that edge, and AND the 2 results
|
|
|
|
|
// together to match the existing enum for all interrupts
|
|
|
|
|
int32_t rising = (result & 0xFF) ? 0x1 : 0x0;
|
|
|
|
|
int32_t falling = ((result & 0xFF00) ? 0x0100 : 0x0);
|
|
|
|
|
return static_cast<WaitResult>(falling | rising);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void InterruptableSensorBase::EnableInterrupts() {
|
|
|
|
|
if (StatusIsFatal()) return;
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_assert(m_interrupt != HAL_kInvalidHandle);
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_EnableInterrupts(m_interrupt, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void InterruptableSensorBase::DisableInterrupts() {
|
|
|
|
|
if (StatusIsFatal()) return;
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_assert(m_interrupt != HAL_kInvalidHandle);
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_DisableInterrupts(m_interrupt, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
double InterruptableSensorBase::ReadRisingTimestamp() {
|
|
|
|
|
if (StatusIsFatal()) return 0.0;
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_assert(m_interrupt != HAL_kInvalidHandle);
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-07-09 01:12:37 -07:00
|
|
|
double timestamp = HAL_ReadInterruptRisingTimestamp(m_interrupt, &status);
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2015-06-25 15:07:55 -04:00
|
|
|
return timestamp;
|
2014-10-05 17:17:59 -04:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
double InterruptableSensorBase::ReadFallingTimestamp() {
|
|
|
|
|
if (StatusIsFatal()) return 0.0;
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_assert(m_interrupt != HAL_kInvalidHandle);
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-07-09 01:12:37 -07:00
|
|
|
double timestamp = HAL_ReadInterruptFallingTimestamp(m_interrupt, &status);
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2015-06-25 15:07:55 -04:00
|
|
|
return timestamp;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
2014-10-08 14:52:24 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void InterruptableSensorBase::SetUpSourceEdge(bool risingEdge,
|
|
|
|
|
bool fallingEdge) {
|
|
|
|
|
if (StatusIsFatal()) return;
|
2016-07-09 00:24:26 -07:00
|
|
|
if (m_interrupt == HAL_kInvalidHandle) {
|
2015-06-25 15:07:55 -04:00
|
|
|
wpi_setWPIErrorWithContext(
|
|
|
|
|
NullParameter,
|
|
|
|
|
"You must call RequestInterrupts before SetUpSourceEdge");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-07-09 00:24:26 -07:00
|
|
|
if (m_interrupt != HAL_kInvalidHandle) {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_SetInterruptUpSourceEdge(m_interrupt, risingEdge, fallingEdge, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2014-10-08 14:52:24 -04:00
|
|
|
}
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
void InterruptableSensorBase::AllocateInterrupts(bool watcher) {
|
|
|
|
|
wpi_assert(m_interrupt == HAL_kInvalidHandle);
|
|
|
|
|
// Expects the calling leaf class to allocate an interrupt index.
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
m_interrupt = HAL_InitializeInterrupts(watcher, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
|
|
|
|
}
|