mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-24 01:31:46 +00:00
Moves Interrupts over to Handles instead of pointers (#99)
This commit is contained in:
committed by
Peter Johnson
parent
74fc10999b
commit
046e043c4e
@@ -7,37 +7,55 @@
|
||||
|
||||
#include "HAL/Interrupts.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "ChipObject.h"
|
||||
|
||||
#include "DigitalInternal.h"
|
||||
#include "HAL/Errors.h"
|
||||
#include "handles/LimitedHandleResource.h"
|
||||
|
||||
using namespace hal;
|
||||
|
||||
namespace {
|
||||
struct Interrupt // FIXME: why is this internal?
|
||||
{
|
||||
tInterrupt* anInterrupt;
|
||||
tInterruptManager* manager;
|
||||
};
|
||||
}
|
||||
|
||||
static LimitedHandleResource<HalInterruptHandle, Interrupt,
|
||||
tInterrupt::kNumSystems, HalHandleEnum::Interrupt>
|
||||
interruptHandles;
|
||||
|
||||
extern "C" {
|
||||
|
||||
void* initializeInterrupts(uint32_t interruptIndex, bool watcher,
|
||||
int32_t* status) {
|
||||
Interrupt* anInterrupt = new Interrupt();
|
||||
HalInterruptHandle initializeInterrupts(bool watcher, int32_t* status) {
|
||||
HalInterruptHandle handle = interruptHandles.Allocate();
|
||||
if (handle == HAL_INVALID_HANDLE) {
|
||||
*status = NO_AVAILABLE_RESOURCES;
|
||||
return HAL_INVALID_HANDLE;
|
||||
}
|
||||
auto anInterrupt = interruptHandles.Get(handle);
|
||||
uint32_t interruptIndex = static_cast<uint32_t>(getHandleIndex(handle));
|
||||
// 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);
|
||||
return anInterrupt;
|
||||
return handle;
|
||||
}
|
||||
|
||||
void cleanInterrupts(void* interrupt_pointer, int32_t* status) {
|
||||
Interrupt* anInterrupt = (Interrupt*)interrupt_pointer;
|
||||
void cleanInterrupts(HalInterruptHandle interrupt_handle, int32_t* status) {
|
||||
auto anInterrupt = interruptHandles.Get(interrupt_handle);
|
||||
if (anInterrupt == nullptr) {
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
return;
|
||||
}
|
||||
interruptHandles.Free(interrupt_handle);
|
||||
delete anInterrupt->anInterrupt;
|
||||
delete anInterrupt->manager;
|
||||
anInterrupt->anInterrupt = nullptr;
|
||||
anInterrupt->manager = nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -47,10 +65,14 @@ void cleanInterrupts(void* interrupt_pointer, int32_t* status) {
|
||||
* waitForInterrupt was called.
|
||||
* @return The mask of interrupts that fired.
|
||||
*/
|
||||
uint32_t waitForInterrupt(void* interrupt_pointer, double timeout,
|
||||
uint32_t waitForInterrupt(HalInterruptHandle interrupt_handle, double timeout,
|
||||
bool ignorePrevious, int32_t* status) {
|
||||
uint32_t result;
|
||||
Interrupt* anInterrupt = (Interrupt*)interrupt_pointer;
|
||||
auto anInterrupt = interruptHandles.Get(interrupt_handle);
|
||||
if (anInterrupt == nullptr) {
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
return 0;
|
||||
}
|
||||
|
||||
result = anInterrupt->manager->watch((int32_t)(timeout * 1e3), ignorePrevious,
|
||||
status);
|
||||
@@ -70,16 +92,24 @@ uint32_t waitForInterrupt(void* interrupt_pointer, double timeout,
|
||||
* time to do the setup of the other options before starting to field
|
||||
* interrupts.
|
||||
*/
|
||||
void enableInterrupts(void* interrupt_pointer, int32_t* status) {
|
||||
Interrupt* anInterrupt = (Interrupt*)interrupt_pointer;
|
||||
void enableInterrupts(HalInterruptHandle interrupt_handle, int32_t* status) {
|
||||
auto anInterrupt = interruptHandles.Get(interrupt_handle);
|
||||
if (anInterrupt == nullptr) {
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
return;
|
||||
}
|
||||
anInterrupt->manager->enable(status);
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable Interrupts without without deallocating structures.
|
||||
*/
|
||||
void disableInterrupts(void* interrupt_pointer, int32_t* status) {
|
||||
Interrupt* anInterrupt = (Interrupt*)interrupt_pointer;
|
||||
void disableInterrupts(HalInterruptHandle interrupt_handle, int32_t* status) {
|
||||
auto anInterrupt = interruptHandles.Get(interrupt_handle);
|
||||
if (anInterrupt == nullptr) {
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
return;
|
||||
}
|
||||
anInterrupt->manager->disable(status);
|
||||
}
|
||||
|
||||
@@ -88,8 +118,13 @@ void disableInterrupts(void* interrupt_pointer, int32_t* status) {
|
||||
* This is in the same time domain as GetClock().
|
||||
* @return Timestamp in seconds since boot.
|
||||
*/
|
||||
double readRisingTimestamp(void* interrupt_pointer, int32_t* status) {
|
||||
Interrupt* anInterrupt = (Interrupt*)interrupt_pointer;
|
||||
double readRisingTimestamp(HalInterruptHandle interrupt_handle,
|
||||
int32_t* status) {
|
||||
auto anInterrupt = interruptHandles.Get(interrupt_handle);
|
||||
if (anInterrupt == nullptr) {
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
return 0;
|
||||
}
|
||||
uint32_t timestamp = anInterrupt->anInterrupt->readRisingTimeStamp(status);
|
||||
return timestamp * 1e-6;
|
||||
}
|
||||
@@ -99,16 +134,25 @@ double readRisingTimestamp(void* interrupt_pointer, int32_t* status) {
|
||||
* This is in the same time domain as GetClock().
|
||||
* @return Timestamp in seconds since boot.
|
||||
*/
|
||||
double readFallingTimestamp(void* interrupt_pointer, int32_t* status) {
|
||||
Interrupt* anInterrupt = (Interrupt*)interrupt_pointer;
|
||||
double readFallingTimestamp(HalInterruptHandle interrupt_handle,
|
||||
int32_t* status) {
|
||||
auto anInterrupt = interruptHandles.Get(interrupt_handle);
|
||||
if (anInterrupt == nullptr) {
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
return 0;
|
||||
}
|
||||
uint32_t timestamp = anInterrupt->anInterrupt->readFallingTimeStamp(status);
|
||||
return timestamp * 1e-6;
|
||||
}
|
||||
|
||||
void requestInterrupts(void* interrupt_pointer, uint8_t routing_module,
|
||||
uint32_t routing_pin, bool routing_analog_trigger,
|
||||
int32_t* status) {
|
||||
Interrupt* anInterrupt = (Interrupt*)interrupt_pointer;
|
||||
void requestInterrupts(HalInterruptHandle interrupt_handle,
|
||||
uint8_t routing_module, uint32_t routing_pin,
|
||||
bool routing_analog_trigger, int32_t* status) {
|
||||
auto anInterrupt = interruptHandles.Get(interrupt_handle);
|
||||
if (anInterrupt == nullptr) {
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
return;
|
||||
}
|
||||
anInterrupt->anInterrupt->writeConfig_WaitForAck(false, status);
|
||||
remapDigitalSource(routing_analog_trigger, routing_pin, routing_module);
|
||||
anInterrupt->anInterrupt->writeConfig_Source_AnalogTrigger(
|
||||
@@ -117,16 +161,25 @@ void requestInterrupts(void* interrupt_pointer, uint8_t routing_module,
|
||||
anInterrupt->anInterrupt->writeConfig_Source_Module(routing_module, status);
|
||||
}
|
||||
|
||||
void attachInterruptHandler(void* interrupt_pointer,
|
||||
void attachInterruptHandler(HalInterruptHandle interrupt_handle,
|
||||
InterruptHandlerFunction handler, void* param,
|
||||
int32_t* status) {
|
||||
Interrupt* anInterrupt = (Interrupt*)interrupt_pointer;
|
||||
auto anInterrupt = interruptHandles.Get(interrupt_handle);
|
||||
if (anInterrupt == nullptr) {
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
return;
|
||||
}
|
||||
anInterrupt->manager->registerHandler(handler, param, status);
|
||||
}
|
||||
|
||||
void setInterruptUpSourceEdge(void* interrupt_pointer, bool risingEdge,
|
||||
bool fallingEdge, int32_t* status) {
|
||||
Interrupt* anInterrupt = (Interrupt*)interrupt_pointer;
|
||||
void setInterruptUpSourceEdge(HalInterruptHandle interrupt_handle,
|
||||
bool risingEdge, bool fallingEdge,
|
||||
int32_t* status) {
|
||||
auto anInterrupt = interruptHandles.Get(interrupt_handle);
|
||||
if (anInterrupt == nullptr) {
|
||||
*status = PARAMETER_OUT_OF_RANGE;
|
||||
return;
|
||||
}
|
||||
anInterrupt->anInterrupt->writeConfig_RisingEdge(risingEdge, status);
|
||||
anInterrupt->anInterrupt->writeConfig_FallingEdge(fallingEdge, status);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user