2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2016-01-02 03:02:34 -08:00
|
|
|
/* Copyright (c) FIRST 2008-2016. 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
|
|
|
|
|
|
|
|
#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-05-20 17:30:37 -07:00
|
|
|
InterruptableSensorBase::InterruptableSensorBase() {}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2014-10-29 15:23:55 -04:00
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Request one of the 8 interrupts asynchronously on this digital input.
|
|
|
|
|
*
|
|
|
|
|
* Request interrupts in asynchronous mode where the user's interrupt handler
|
|
|
|
|
* will be called when the interrupt fires. Users that want control over the
|
|
|
|
|
* thread priority should use the synchronous method with their own spawned
|
|
|
|
|
* thread. The default is interrupt on rising edges only.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void InterruptableSensorBase::RequestInterrupts(
|
2016-05-20 17:30:37 -07:00
|
|
|
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-07-09 00:24:26 -07:00
|
|
|
HAL_RequestInterrupts(m_interrupt, GetPortHandleForRouting(),
|
|
|
|
|
(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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Request one of the 8 interrupts synchronously on this digital input.
|
|
|
|
|
*
|
|
|
|
|
* Request interrupts in synchronous mode where the user program will have to
|
|
|
|
|
* explicitly wait for the interrupt to occur using WaitForInterrupt.
|
|
|
|
|
* The default is interrupt on rising edges only.
|
|
|
|
|
*/
|
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-07-09 00:24:26 -07:00
|
|
|
HAL_RequestInterrupts(m_interrupt, GetPortHandleForRouting(),
|
|
|
|
|
(HAL_AnalogTriggerType)GetAnalogTriggerTypeForRouting(),
|
|
|
|
|
&status);
|
|
|
|
|
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::AllocateInterrupts(bool watcher) {
|
2016-07-09 00:24:26 -07:00
|
|
|
wpi_assert(m_interrupt == HAL_kInvalidHandle);
|
2015-06-25 15:07:55 -04:00
|
|
|
// Expects the calling leaf class to allocate an interrupt index.
|
|
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
m_interrupt = HAL_InitializeInterrupts(watcher, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Cancel interrupts on this device.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* This deallocates all the chipobject structures and disables any interrupts.
|
|
|
|
|
*/
|
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);
|
2016-06-20 23:22:49 -07: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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* In synchronous mode, wait for the defined interrupt to occur.
|
|
|
|
|
*
|
|
|
|
|
* You should <b>NOT</b> attempt to read the sensor from another thread while
|
|
|
|
|
* waiting for an interrupt. This is not threadsafe, and can cause memory
|
|
|
|
|
* corruption
|
|
|
|
|
*
|
|
|
|
|
* @param timeout Timeout in seconds
|
2014-10-05 17:17:59 -04:00
|
|
|
* @param ignorePrevious If true, ignore interrupts that happened before
|
2016-05-20 17:30:37 -07:00
|
|
|
* WaitForInterrupt was called.
|
2014-10-05 17:17:59 -04:00
|
|
|
* @return What interrupts fired
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
InterruptableSensorBase::WaitResult InterruptableSensorBase::WaitForInterrupt(
|
|
|
|
|
float timeout, bool ignorePrevious) {
|
|
|
|
|
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;
|
|
|
|
|
uint32_t 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
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
return static_cast<WaitResult>(result);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Enable interrupts to occur on this input.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* Interrupts are disabled when the RequestInterrupt call is made. This gives
|
2016-05-20 17:30:37 -07:00
|
|
|
* time to do the setup of the other options before starting to field
|
|
|
|
|
* interrupts.
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Disable Interrupts without without deallocating structures.
|
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-10-05 17:17:59 -04:00
|
|
|
* Return the timestamp for the rising interrupt that occurred most recently.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* This is in the same time domain as GetClock().
|
2014-10-05 17:17:59 -04:00
|
|
|
* The rising-edge interrupt should be enabled with
|
|
|
|
|
* {@link #DigitalInput.SetUpSourceEdge}
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return Timestamp in seconds since boot.
|
|
|
|
|
*/
|
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 00:24:26 -07:00
|
|
|
double timestamp = HAL_ReadRisingTimestamp(m_interrupt, &status);
|
|
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2015-06-25 15:07:55 -04:00
|
|
|
return timestamp;
|
2014-10-05 17:17:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the timestamp for the falling interrupt that occurred most recently.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2014-10-05 17:17:59 -04:00
|
|
|
* This is in the same time domain as GetClock().
|
|
|
|
|
* The falling-edge interrupt should be enabled with
|
|
|
|
|
* {@link #DigitalInput.SetUpSourceEdge}
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2014-10-05 17:17:59 -04:00
|
|
|
* @return Timestamp in seconds since boot.
|
|
|
|
|
*/
|
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 00:24:26 -07:00
|
|
|
double timestamp = HAL_ReadFallingTimestamp(m_interrupt, &status);
|
|
|
|
|
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
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set which edge to trigger interrupts on
|
|
|
|
|
*
|
2016-05-20 17:30:37 -07:00
|
|
|
* @param risingEdge true to interrupt on rising edge
|
|
|
|
|
* @param fallingEdge true to interrupt on falling edge
|
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
|
|
|
}
|