Brian Silverman | 26e4e52 | 2015-12-17 01:56:40 -0500 | [diff] [blame^] | 1 | #include "HAL/Interrupts.hpp" |
| 2 | #include "ChipObject.h" |
| 3 | |
| 4 | extern void remapDigitalSource(bool analogTrigger, uint32_t &pin, uint8_t &module); |
| 5 | |
| 6 | struct Interrupt // FIXME: why is this internal? |
| 7 | { |
| 8 | tInterrupt *anInterrupt; |
| 9 | tInterruptManager *manager; |
| 10 | }; |
| 11 | |
| 12 | void* initializeInterrupts(uint32_t interruptIndex, bool watcher, int32_t *status) |
| 13 | { |
| 14 | Interrupt* anInterrupt = new Interrupt(); |
| 15 | // Expects the calling leaf class to allocate an interrupt index. |
| 16 | anInterrupt->anInterrupt = tInterrupt::create(interruptIndex, status); |
| 17 | anInterrupt->anInterrupt->writeConfig_WaitForAck(false, status); |
| 18 | anInterrupt->manager = new tInterruptManager( |
| 19 | (1 << interruptIndex) | (1 << (interruptIndex + 8)), watcher, status); |
| 20 | return anInterrupt; |
| 21 | } |
| 22 | |
| 23 | void cleanInterrupts(void* interrupt_pointer, int32_t *status) |
| 24 | { |
| 25 | Interrupt* anInterrupt = (Interrupt*)interrupt_pointer; |
| 26 | delete anInterrupt->anInterrupt; |
| 27 | delete anInterrupt->manager; |
| 28 | anInterrupt->anInterrupt = NULL; |
| 29 | anInterrupt->manager = NULL; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * In synchronous mode, wait for the defined interrupt to occur. |
| 34 | * @param timeout Timeout in seconds |
| 35 | * @param ignorePrevious If true, ignore interrupts that happened before |
| 36 | * waitForInterrupt was called. |
| 37 | * @return The mask of interrupts that fired. |
| 38 | */ |
| 39 | uint32_t waitForInterrupt(void* interrupt_pointer, double timeout, bool ignorePrevious, int32_t *status) |
| 40 | { |
| 41 | uint32_t result; |
| 42 | Interrupt* anInterrupt = (Interrupt*)interrupt_pointer; |
| 43 | |
| 44 | result = anInterrupt->manager->watch((int32_t)(timeout * 1e3), ignorePrevious, status); |
| 45 | |
| 46 | // Don't report a timeout as an error - the return code is enough to tell |
| 47 | // that a timeout happened. |
| 48 | if(*status == -NiFpga_Status_IrqTimeout) { |
| 49 | *status = NiFpga_Status_Success; |
| 50 | } |
| 51 | |
| 52 | return result; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Enable interrupts to occur on this input. |
| 57 | * Interrupts are disabled when the RequestInterrupt call is made. This gives time to do the |
| 58 | * setup of the other options before starting to field interrupts. |
| 59 | */ |
| 60 | void enableInterrupts(void* interrupt_pointer, int32_t *status) |
| 61 | { |
| 62 | Interrupt* anInterrupt = (Interrupt*)interrupt_pointer; |
| 63 | anInterrupt->manager->enable(status); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Disable Interrupts without without deallocating structures. |
| 68 | */ |
| 69 | void disableInterrupts(void* interrupt_pointer, int32_t *status) |
| 70 | { |
| 71 | Interrupt* anInterrupt = (Interrupt*)interrupt_pointer; |
| 72 | anInterrupt->manager->disable(status); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Return the timestamp for the rising interrupt that occurred most recently. |
| 77 | * This is in the same time domain as GetClock(). |
| 78 | * @return Timestamp in seconds since boot. |
| 79 | */ |
| 80 | double readRisingTimestamp(void* interrupt_pointer, int32_t *status) |
| 81 | { |
| 82 | Interrupt* anInterrupt = (Interrupt*)interrupt_pointer; |
| 83 | uint32_t timestamp = anInterrupt->anInterrupt->readRisingTimeStamp(status); |
| 84 | return timestamp * 1e-6; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Return the timestamp for the falling interrupt that occurred most recently. |
| 89 | * This is in the same time domain as GetClock(). |
| 90 | * @return Timestamp in seconds since boot. |
| 91 | */ |
| 92 | double readFallingTimestamp(void* interrupt_pointer, int32_t *status) |
| 93 | { |
| 94 | Interrupt* anInterrupt = (Interrupt*)interrupt_pointer; |
| 95 | uint32_t timestamp = anInterrupt->anInterrupt->readFallingTimeStamp(status); |
| 96 | return timestamp * 1e-6; |
| 97 | } |
| 98 | |
| 99 | void requestInterrupts(void* interrupt_pointer, uint8_t routing_module, uint32_t routing_pin, |
| 100 | bool routing_analog_trigger, int32_t *status) |
| 101 | { |
| 102 | Interrupt* anInterrupt = (Interrupt*)interrupt_pointer; |
| 103 | anInterrupt->anInterrupt->writeConfig_WaitForAck(false, status); |
| 104 | remapDigitalSource(routing_analog_trigger, routing_pin, routing_module); |
| 105 | anInterrupt->anInterrupt->writeConfig_Source_AnalogTrigger(routing_analog_trigger, status); |
| 106 | anInterrupt->anInterrupt->writeConfig_Source_Channel(routing_pin, status); |
| 107 | anInterrupt->anInterrupt->writeConfig_Source_Module(routing_module, status); |
| 108 | } |
| 109 | |
| 110 | void attachInterruptHandler(void* interrupt_pointer, InterruptHandlerFunction handler, void* param, |
| 111 | int32_t *status) |
| 112 | { |
| 113 | Interrupt* anInterrupt = (Interrupt*)interrupt_pointer; |
| 114 | anInterrupt->manager->registerHandler(handler, param, status); |
| 115 | } |
| 116 | |
| 117 | void setInterruptUpSourceEdge(void* interrupt_pointer, bool risingEdge, bool fallingEdge, |
| 118 | int32_t *status) |
| 119 | { |
| 120 | Interrupt* anInterrupt = (Interrupt*)interrupt_pointer; |
| 121 | anInterrupt->anInterrupt->writeConfig_RisingEdge(risingEdge, status); |
| 122 | anInterrupt->anInterrupt->writeConfig_FallingEdge(fallingEdge, status); |
| 123 | } |