2016-01-02 03:02:34 -08:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
/* Copyright (c) FIRST 2016. All Rights Reserved. */
|
|
|
|
|
/* 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/Interrupts.h"
|
2016-05-25 22:38:11 -07:00
|
|
|
|
2016-06-20 23:22:49 -07:00
|
|
|
#include <memory>
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
#include "ChipObject.h"
|
|
|
|
|
|
2016-05-26 12:56:39 -07:00
|
|
|
#include "DigitalInternal.h"
|
2016-06-20 23:22:49 -07:00
|
|
|
#include "HAL/Errors.h"
|
2016-07-02 23:19:14 -07:00
|
|
|
#include "PortsInternal.h"
|
2016-07-07 21:43:55 -07:00
|
|
|
#include "handles/HandlesInternal.h"
|
2016-06-20 23:22:49 -07:00
|
|
|
#include "handles/LimitedHandleResource.h"
|
2016-05-26 12:56:39 -07:00
|
|
|
|
|
|
|
|
using namespace hal;
|
2015-02-11 20:37:11 -05:00
|
|
|
|
2016-06-20 23:22:49 -07:00
|
|
|
namespace {
|
2016-07-10 17:47:44 -07:00
|
|
|
// FIXME: why is this internal?
|
|
|
|
|
struct Interrupt {
|
2016-05-20 17:30:37 -07:00
|
|
|
tInterrupt* anInterrupt;
|
|
|
|
|
tInterruptManager* manager;
|
2013-12-15 18:30:16 -05:00
|
|
|
};
|
2016-06-20 23:22:49 -07:00
|
|
|
}
|
|
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
static LimitedHandleResource<HAL_InterruptHandle, Interrupt, kNumInterrupts,
|
|
|
|
|
HAL_HandleEnum::Interrupt>
|
2016-06-20 23:22:49 -07:00
|
|
|
interruptHandles;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-11-26 00:08:32 -08:00
|
|
|
extern "C" {
|
|
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_InterruptHandle HAL_InitializeInterrupts(bool watcher, int32_t* status) {
|
|
|
|
|
HAL_InterruptHandle handle = interruptHandles.Allocate();
|
|
|
|
|
if (handle == HAL_kInvalidHandle) {
|
2016-06-20 23:22:49 -07:00
|
|
|
*status = NO_AVAILABLE_RESOURCES;
|
2016-07-09 00:24:26 -07:00
|
|
|
return HAL_kInvalidHandle;
|
2016-06-20 23:22:49 -07:00
|
|
|
}
|
|
|
|
|
auto anInterrupt = interruptHandles.Get(handle);
|
|
|
|
|
uint32_t interruptIndex = static_cast<uint32_t>(getHandleIndex(handle));
|
2016-05-20 17:30:37 -07:00
|
|
|
// Expects the calling leaf class to allocate an interrupt index.
|
|
|
|
|
anInterrupt->anInterrupt = tInterrupt::create(interruptIndex, status);
|
|
|
|
|
anInterrupt->anInterrupt->writeConfig_WaitForAck(false, status);
|
|
|
|
|
anInterrupt->manager = new tInterruptManager(
|
|
|
|
|
(1 << interruptIndex) | (1 << (interruptIndex + 8)), watcher, status);
|
2016-06-20 23:22:49 -07:00
|
|
|
return handle;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
void HAL_CleanInterrupts(HAL_InterruptHandle interrupt_handle,
|
|
|
|
|
int32_t* status) {
|
2016-06-20 23:22:49 -07:00
|
|
|
auto anInterrupt = interruptHandles.Get(interrupt_handle);
|
|
|
|
|
if (anInterrupt == nullptr) {
|
2016-07-03 17:27:06 -07:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
2016-06-20 23:22:49 -07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
interruptHandles.Free(interrupt_handle);
|
2016-05-20 17:30:37 -07:00
|
|
|
delete anInterrupt->anInterrupt;
|
|
|
|
|
delete anInterrupt->manager;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* In synchronous mode, wait for the defined interrupt to occur.
|
|
|
|
|
* @param timeout Timeout in seconds
|
2014-10-05 17:17:59 -04:00
|
|
|
* @param ignorePrevious If true, ignore interrupts that happened before
|
|
|
|
|
* waitForInterrupt was called.
|
|
|
|
|
* @return The mask of interrupts that fired.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2016-07-09 00:24:26 -07:00
|
|
|
uint32_t HAL_WaitForInterrupt(HAL_InterruptHandle interrupt_handle,
|
|
|
|
|
double timeout, bool ignorePrevious,
|
|
|
|
|
int32_t* status) {
|
2016-05-20 17:30:37 -07:00
|
|
|
uint32_t result;
|
2016-06-20 23:22:49 -07:00
|
|
|
auto anInterrupt = interruptHandles.Get(interrupt_handle);
|
|
|
|
|
if (anInterrupt == nullptr) {
|
2016-07-03 17:27:06 -07:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
2016-06-20 23:22:49 -07:00
|
|
|
return 0;
|
|
|
|
|
}
|
2014-10-05 17:17:59 -04:00
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
result = anInterrupt->manager->watch((int32_t)(timeout * 1e3), ignorePrevious,
|
|
|
|
|
status);
|
2014-10-05 17:17:59 -04:00
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
// Don't report a timeout as an error - the return code is enough to tell
|
|
|
|
|
// that a timeout happened.
|
|
|
|
|
if (*status == -NiFpga_Status_IrqTimeout) {
|
|
|
|
|
*status = NiFpga_Status_Success;
|
|
|
|
|
}
|
2014-10-05 17:17:59 -04:00
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
return result;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Enable interrupts to occur on this input.
|
2016-05-20 17:30:37 -07:00
|
|
|
* Interrupts are disabled when the RequestInterrupt call is made. This gives
|
|
|
|
|
* time to do the setup of the other options before starting to field
|
|
|
|
|
* interrupts.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2016-07-09 00:24:26 -07:00
|
|
|
void HAL_EnableInterrupts(HAL_InterruptHandle interrupt_handle,
|
|
|
|
|
int32_t* status) {
|
2016-06-20 23:22:49 -07:00
|
|
|
auto anInterrupt = interruptHandles.Get(interrupt_handle);
|
|
|
|
|
if (anInterrupt == nullptr) {
|
2016-07-03 17:27:06 -07:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
2016-06-20 23:22:49 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2016-05-20 17:30:37 -07:00
|
|
|
anInterrupt->manager->enable(status);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Disable Interrupts without without deallocating structures.
|
|
|
|
|
*/
|
2016-07-09 00:24:26 -07:00
|
|
|
void HAL_DisableInterrupts(HAL_InterruptHandle interrupt_handle,
|
|
|
|
|
int32_t* status) {
|
2016-06-20 23:22:49 -07:00
|
|
|
auto anInterrupt = interruptHandles.Get(interrupt_handle);
|
|
|
|
|
if (anInterrupt == nullptr) {
|
2016-07-03 17:27:06 -07:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
2016-06-20 23:22:49 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2016-05-20 17:30:37 -07:00
|
|
|
anInterrupt->manager->disable(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.
|
2013-12-15 18:30:16 -05:00
|
|
|
* This is in the same time domain as GetClock().
|
|
|
|
|
* @return Timestamp in seconds since boot.
|
|
|
|
|
*/
|
2016-07-09 01:12:37 -07:00
|
|
|
double HAL_ReadInterruptRisingTimestamp(HAL_InterruptHandle interrupt_handle,
|
|
|
|
|
int32_t* status) {
|
2016-06-20 23:22:49 -07:00
|
|
|
auto anInterrupt = interruptHandles.Get(interrupt_handle);
|
|
|
|
|
if (anInterrupt == nullptr) {
|
2016-07-03 17:27:06 -07:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
2016-06-20 23:22:49 -07:00
|
|
|
return 0;
|
|
|
|
|
}
|
2016-05-20 17:30:37 -07:00
|
|
|
uint32_t timestamp = anInterrupt->anInterrupt->readRisingTimeStamp(status);
|
|
|
|
|
return timestamp * 1e-6;
|
2014-10-05 17:17:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the timestamp for the falling interrupt that occurred most recently.
|
|
|
|
|
* This is in the same time domain as GetClock().
|
|
|
|
|
* @return Timestamp in seconds since boot.
|
|
|
|
|
*/
|
2016-07-09 01:12:37 -07:00
|
|
|
double HAL_ReadInterruptFallingTimestamp(HAL_InterruptHandle interrupt_handle,
|
|
|
|
|
int32_t* status) {
|
2016-06-20 23:22:49 -07:00
|
|
|
auto anInterrupt = interruptHandles.Get(interrupt_handle);
|
|
|
|
|
if (anInterrupt == nullptr) {
|
2016-07-03 17:27:06 -07:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
2016-06-20 23:22:49 -07:00
|
|
|
return 0;
|
|
|
|
|
}
|
2016-05-20 17:30:37 -07:00
|
|
|
uint32_t timestamp = anInterrupt->anInterrupt->readFallingTimeStamp(status);
|
|
|
|
|
return timestamp * 1e-6;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
void HAL_RequestInterrupts(HAL_InterruptHandle interrupt_handle,
|
|
|
|
|
HAL_Handle digitalSourceHandle,
|
|
|
|
|
HAL_AnalogTriggerType analogTriggerType,
|
|
|
|
|
int32_t* status) {
|
2016-06-20 23:22:49 -07:00
|
|
|
auto anInterrupt = interruptHandles.Get(interrupt_handle);
|
|
|
|
|
if (anInterrupt == nullptr) {
|
2016-07-03 17:27:06 -07:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
2016-06-20 23:22:49 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2016-05-20 17:30:37 -07:00
|
|
|
anInterrupt->anInterrupt->writeConfig_WaitForAck(false, status);
|
2016-07-07 21:43:55 -07:00
|
|
|
bool routingAnalogTrigger = false;
|
|
|
|
|
uint32_t routingPin = 0;
|
|
|
|
|
uint8_t routingModule = 0;
|
|
|
|
|
bool success =
|
|
|
|
|
remapDigitalSource(digitalSourceHandle, analogTriggerType, routingPin,
|
|
|
|
|
routingModule, routingAnalogTrigger);
|
|
|
|
|
if (!success) {
|
|
|
|
|
*status = HAL_HANDLE_ERROR;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-05-20 17:30:37 -07:00
|
|
|
anInterrupt->anInterrupt->writeConfig_Source_AnalogTrigger(
|
2016-07-07 21:43:55 -07:00
|
|
|
routingAnalogTrigger, status);
|
|
|
|
|
anInterrupt->anInterrupt->writeConfig_Source_Channel(routingPin, status);
|
|
|
|
|
anInterrupt->anInterrupt->writeConfig_Source_Module(routingModule, status);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
void HAL_AttachInterruptHandler(HAL_InterruptHandle interrupt_handle,
|
|
|
|
|
InterruptHandlerFunction handler, void* param,
|
|
|
|
|
int32_t* status) {
|
2016-06-20 23:22:49 -07:00
|
|
|
auto anInterrupt = interruptHandles.Get(interrupt_handle);
|
|
|
|
|
if (anInterrupt == nullptr) {
|
2016-07-03 17:27:06 -07:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
2016-06-20 23:22:49 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2016-05-20 17:30:37 -07:00
|
|
|
anInterrupt->manager->registerHandler(handler, param, status);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
2014-05-02 17:54:01 -04:00
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
void HAL_SetInterruptUpSourceEdge(HAL_InterruptHandle interrupt_handle,
|
|
|
|
|
bool risingEdge, bool fallingEdge,
|
|
|
|
|
int32_t* status) {
|
2016-06-20 23:22:49 -07:00
|
|
|
auto anInterrupt = interruptHandles.Get(interrupt_handle);
|
|
|
|
|
if (anInterrupt == nullptr) {
|
2016-07-03 17:27:06 -07:00
|
|
|
*status = HAL_HANDLE_ERROR;
|
2016-06-20 23:22:49 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2016-05-20 17:30:37 -07:00
|
|
|
anInterrupt->anInterrupt->writeConfig_RisingEdge(risingEdge, status);
|
|
|
|
|
anInterrupt->anInterrupt->writeConfig_FallingEdge(fallingEdge, status);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
2015-11-26 00:08:32 -08:00
|
|
|
|
|
|
|
|
} // extern "C"
|