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-20 22:05:17 -07:00
|
|
|
#include "HAL/cpp/make_unique.h"
|
2016-07-13 20:29:28 -07:00
|
|
|
#include "HAL/handles/HandlesInternal.h"
|
|
|
|
|
#include "HAL/handles/LimitedHandleResource.h"
|
2016-07-02 23:19:14 -07:00
|
|
|
#include "PortsInternal.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
|
|
|
struct Interrupt {
|
2016-07-20 22:05:17 -07:00
|
|
|
std::unique_ptr<tInterrupt> anInterrupt;
|
|
|
|
|
std::unique_ptr<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-12 10:45:14 -07:00
|
|
|
HAL_InterruptHandle HAL_InitializeInterrupts(HAL_Bool watcher,
|
|
|
|
|
int32_t* status) {
|
2016-07-09 00:24:26 -07:00
|
|
|
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.
|
2016-07-20 22:05:17 -07:00
|
|
|
anInterrupt->anInterrupt.reset(tInterrupt::create(interruptIndex, status));
|
2016-05-20 17:30:37 -07:00
|
|
|
anInterrupt->anInterrupt->writeConfig_WaitForAck(false, status);
|
2016-07-20 22:05:17 -07:00
|
|
|
anInterrupt->manager = std::make_unique<tInterruptManager>(
|
2016-07-12 10:45:14 -07:00
|
|
|
(1u << interruptIndex) | (1u << (interruptIndex + 8u)), watcher, status);
|
2016-06-20 23:22:49 -07:00
|
|
|
return handle;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-08-12 13:45:28 -07:00
|
|
|
void HAL_CleanInterrupts(HAL_InterruptHandle interruptHandle, int32_t* status) {
|
|
|
|
|
interruptHandles.Free(interruptHandle);
|
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-08-12 13:45:28 -07:00
|
|
|
int64_t HAL_WaitForInterrupt(HAL_InterruptHandle interruptHandle,
|
2016-07-12 10:45:14 -07:00
|
|
|
double timeout, HAL_Bool ignorePrevious,
|
|
|
|
|
int32_t* status) {
|
2016-05-20 17:30:37 -07:00
|
|
|
uint32_t result;
|
2016-08-12 13:45:28 -07:00
|
|
|
auto anInterrupt = interruptHandles.Get(interruptHandle);
|
2016-06-20 23:22:49 -07:00
|
|
|
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-08-11 23:38:45 -07:00
|
|
|
result = anInterrupt->manager->watch(static_cast<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-08-12 13:45:28 -07:00
|
|
|
void HAL_EnableInterrupts(HAL_InterruptHandle interruptHandle,
|
2016-07-09 00:24:26 -07:00
|
|
|
int32_t* status) {
|
2016-08-12 13:45:28 -07:00
|
|
|
auto anInterrupt = interruptHandles.Get(interruptHandle);
|
2016-06-20 23:22:49 -07:00
|
|
|
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-08-12 13:45:28 -07:00
|
|
|
void HAL_DisableInterrupts(HAL_InterruptHandle interruptHandle,
|
2016-07-09 00:24:26 -07:00
|
|
|
int32_t* status) {
|
2016-08-12 13:45:28 -07:00
|
|
|
auto anInterrupt = interruptHandles.Get(interruptHandle);
|
2016-06-20 23:22:49 -07:00
|
|
|
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-08-12 13:45:28 -07:00
|
|
|
double HAL_ReadInterruptRisingTimestamp(HAL_InterruptHandle interruptHandle,
|
2016-07-09 01:12:37 -07:00
|
|
|
int32_t* status) {
|
2016-08-12 13:45:28 -07:00
|
|
|
auto anInterrupt = interruptHandles.Get(interruptHandle);
|
2016-06-20 23:22:49 -07:00
|
|
|
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-08-12 13:45:28 -07:00
|
|
|
double HAL_ReadInterruptFallingTimestamp(HAL_InterruptHandle interruptHandle,
|
2016-07-09 01:12:37 -07:00
|
|
|
int32_t* status) {
|
2016-08-12 13:45:28 -07:00
|
|
|
auto anInterrupt = interruptHandles.Get(interruptHandle);
|
2016-06-20 23:22:49 -07:00
|
|
|
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-08-12 13:45:28 -07:00
|
|
|
void HAL_RequestInterrupts(HAL_InterruptHandle interruptHandle,
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_Handle digitalSourceHandle,
|
|
|
|
|
HAL_AnalogTriggerType analogTriggerType,
|
|
|
|
|
int32_t* status) {
|
2016-08-12 13:45:28 -07:00
|
|
|
auto anInterrupt = interruptHandles.Get(interruptHandle);
|
2016-06-20 23:22:49 -07:00
|
|
|
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;
|
2016-08-12 13:45:28 -07:00
|
|
|
uint8_t routingChannel = 0;
|
2016-07-07 21:43:55 -07:00
|
|
|
uint8_t routingModule = 0;
|
|
|
|
|
bool success =
|
2016-08-12 13:45:28 -07:00
|
|
|
remapDigitalSource(digitalSourceHandle, analogTriggerType, routingChannel,
|
2016-07-07 21:43:55 -07:00
|
|
|
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);
|
2016-08-12 13:45:28 -07:00
|
|
|
anInterrupt->anInterrupt->writeConfig_Source_Channel(routingChannel, status);
|
2016-07-07 21:43:55 -07:00
|
|
|
anInterrupt->anInterrupt->writeConfig_Source_Module(routingModule, status);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2016-08-12 13:45:28 -07:00
|
|
|
void HAL_AttachInterruptHandler(HAL_InterruptHandle interruptHandle,
|
2016-09-05 07:31:51 -07:00
|
|
|
HAL_InterruptHandlerFunction handler,
|
|
|
|
|
void* param, int32_t* status) {
|
2016-08-12 13:45:28 -07:00
|
|
|
auto anInterrupt = interruptHandles.Get(interruptHandle);
|
2016-06-20 23:22:49 -07:00
|
|
|
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-08-12 13:45:28 -07:00
|
|
|
void HAL_SetInterruptUpSourceEdge(HAL_InterruptHandle interruptHandle,
|
2016-07-12 10:45:14 -07:00
|
|
|
HAL_Bool risingEdge, HAL_Bool fallingEdge,
|
2016-07-09 00:24:26 -07:00
|
|
|
int32_t* status) {
|
2016-08-12 13:45:28 -07:00
|
|
|
auto anInterrupt = interruptHandles.Get(interruptHandle);
|
2016-06-20 23:22:49 -07:00
|
|
|
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"
|