|
| 1 | +/* |
| 2 | +Copyright (c) 2020 SparkFun Electronics |
| 3 | +
|
| 4 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +of this software and associated documentation files (the "Software"), to deal |
| 6 | +in the Software without restriction, including without limitation the rights |
| 7 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +copies of the Software, and to permit persons to whom the Software is |
| 9 | +furnished to do so, subject to the following conditions: |
| 10 | +
|
| 11 | +The above copyright notice and this permission notice shall be included in all |
| 12 | +copies or substantial portions of the Software. |
| 13 | +
|
| 14 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | +SOFTWARE. |
| 21 | +*/ |
| 22 | + |
| 23 | +#include "mbed.h" |
| 24 | + |
| 25 | +#define PinMode Arduino_PinMode |
| 26 | +#include "core-api/api/Common.h" |
| 27 | +#undef PinMode |
| 28 | + |
| 29 | +#include "core-extend/Common.h" |
| 30 | +#include "bridge/pins.h" |
| 31 | + |
| 32 | +#define standInFunc() printf("Stand-In for '%s' [file: %s, line: %d]\n", __FUNCTION__, __FILE__, __LINE__) |
| 33 | + |
| 34 | +void indexAttachInterruptParam(pin_size_t index, voidFuncPtrParam callback, PinStatus mode, void* param){ |
| 35 | + indexDetachInterrupt(index); |
| 36 | + arduino::InterruptInParam* irq = pinIRQByIndex(index); |
| 37 | + if(!irq){ |
| 38 | + irq = new arduino::InterruptInParam(pinNameByIndex(index)); |
| 39 | + } |
| 40 | + pinIRQByIndex(index) = irq; |
| 41 | + switch (mode) { |
| 42 | + case CHANGE : |
| 43 | + irq->rise(mbed::callback(callback), param); |
| 44 | + irq->fall(mbed::callback(callback), param); |
| 45 | + break; |
| 46 | + case FALLING : |
| 47 | + irq->fall(mbed::callback(callback), param); |
| 48 | + break; |
| 49 | + case RISING : |
| 50 | + default : |
| 51 | + irq->rise(mbed::callback(callback), param); |
| 52 | + break; |
| 53 | + } |
| 54 | + if(!pinGPIOByIndex(index)){ // Give a default pullup for the pin, since calling InterruptIn with PinMode is impossible |
| 55 | + switch (mode) { |
| 56 | + case FALLING : |
| 57 | + indexPinMode(index, INPUT_PULLUP); |
| 58 | + break; |
| 59 | + case RISING : |
| 60 | + indexPinMode(index, INPUT_PULLDOWN); |
| 61 | + break; |
| 62 | + case CHANGE : |
| 63 | + default: |
| 64 | + indexPinMode(index, INPUT); |
| 65 | + break; |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +void attachInterruptParam(pin_size_t pinNumber, voidFuncPtrParam callback, PinStatus mode, void* param){ |
| 71 | + pin_size_t index = pinIndexByNumber(pinNumber); |
| 72 | + if( index == variantPinCount ){ return; } |
| 73 | + indexAttachInterruptParam(index, callback, mode, param); |
| 74 | +} |
| 75 | + |
| 76 | +void attachInterruptParam(PinName pinName, voidFuncPtrParam callback, PinStatus mode, void* param){ |
| 77 | + pin_size_t index = pinIndexByName(pinName); |
| 78 | + if( index == variantPinCount ){ return; } |
| 79 | + indexAttachInterruptParam(index, callback, mode, param); |
| 80 | +} |
| 81 | + |
| 82 | +void indexAttachInterrupt(pin_size_t index, voidFuncPtr callback, PinStatus mode){ |
| 83 | + indexAttachInterruptParam(index, (voidFuncPtrParam)callback, mode, NULL); |
| 84 | +} |
| 85 | + |
| 86 | +void attachInterrupt(pin_size_t pinNumber, voidFuncPtr callback, PinStatus mode){ |
| 87 | + pin_size_t index = pinIndexByNumber(pinNumber); |
| 88 | + if( index == variantPinCount ){ return; } |
| 89 | + indexAttachInterrupt(index, callback, mode); |
| 90 | +} |
| 91 | + |
| 92 | +void attachInterrupt(PinName pinName, voidFuncPtr callback, PinStatus mode){ |
| 93 | + pin_size_t index = pinIndexByName(pinName); |
| 94 | + if( index == variantPinCount ){ return; } |
| 95 | + indexAttachInterrupt(index, callback, mode); |
| 96 | +} |
| 97 | + |
| 98 | +void indexDetachInterrupt(pin_size_t index){ |
| 99 | + arduino::InterruptInParam* irq = pinIRQByIndex(index); |
| 100 | + if(!irq){ return; } |
| 101 | + irq->rise(NULL, NULL); |
| 102 | + irq->fall(NULL, NULL); |
| 103 | +} |
| 104 | + |
| 105 | +void detachInterrupt(pin_size_t pinNumber){ |
| 106 | + pin_size_t index = pinIndexByNumber(pinNumber); |
| 107 | + if( index == variantPinCount ){ return; } |
| 108 | + indexDetachInterrupt(index); |
| 109 | +} |
| 110 | + |
| 111 | +void detachInterrupt(PinName pinName){ |
| 112 | + pin_size_t index = pinIndexByName(pinName); |
| 113 | + if( index == variantPinCount ){ return; } |
| 114 | + indexDetachInterrupt(index); |
| 115 | +} |
| 116 | + |
| 117 | +// |
| 118 | +// InterruptInParam implementation |
| 119 | + |
| 120 | +// Note: This single-parameter constructor exists to maintain binary |
| 121 | +// compatibility. |
| 122 | +// If not for that, we could simplify by having only the 2-param |
| 123 | +// constructor, with a default value for the PinMode. |
| 124 | +InterruptInParam::InterruptInParam(PinName pin) : |
| 125 | + InterruptIn(pin) |
| 126 | +{ |
| 127 | + irq_init(pin); |
| 128 | +} |
| 129 | + |
| 130 | +InterruptInParam::InterruptInParam(PinName pin, PinMode mode) : |
| 131 | + InterruptIn(pin, mode) |
| 132 | +{ |
| 133 | + irq_init(pin); |
| 134 | +} |
| 135 | + |
| 136 | +InterruptInParam::~InterruptInParam() |
| 137 | +{ |
| 138 | + |
| 139 | +} |
| 140 | + |
| 141 | +void InterruptInParam::irq_init(PinName pin) |
| 142 | +{ |
| 143 | + gpio_irq_init(&gpio_irq, pin, (&InterruptInParam::_irq_handler), (uint32_t)this); |
| 144 | +} |
| 145 | + |
| 146 | +void InterruptInParam::rise(Callback<void(void*)> func, void* param) |
| 147 | +{ |
| 148 | + core_util_critical_section_enter(); |
| 149 | + if (func) { |
| 150 | + _rise = func; |
| 151 | + gpio_irq_set(&gpio_irq, IRQ_RISE, 1); |
| 152 | + } else { |
| 153 | + _rise = nullptr; |
| 154 | + gpio_irq_set(&gpio_irq, IRQ_RISE, 0); |
| 155 | + } |
| 156 | + if (param) { |
| 157 | + _rise_param = param; |
| 158 | + } else { |
| 159 | + _rise_param = nullptr; |
| 160 | + } |
| 161 | + core_util_critical_section_exit(); |
| 162 | +} |
| 163 | + |
| 164 | +void InterruptInParam::rise(Callback<void()> func){ |
| 165 | + error("InterruptInParam.rise called with 'void(void)' callback (should be 'void(void*)')\r\n"); |
| 166 | +} |
| 167 | + |
| 168 | +void InterruptInParam::fall(Callback<void(void*)> func, void* param) |
| 169 | +{ |
| 170 | + core_util_critical_section_enter(); |
| 171 | + if (func) { |
| 172 | + _fall = func; |
| 173 | + gpio_irq_set(&gpio_irq, IRQ_FALL, 1); |
| 174 | + } else { |
| 175 | + _fall = nullptr; |
| 176 | + gpio_irq_set(&gpio_irq, IRQ_FALL, 0); |
| 177 | + } |
| 178 | + if (param) { |
| 179 | + _fall_param = param; |
| 180 | + } else { |
| 181 | + _fall_param = nullptr; |
| 182 | + } |
| 183 | + core_util_critical_section_exit(); |
| 184 | +} |
| 185 | + |
| 186 | +void InterruptInParam::fall(Callback<void()> func){ |
| 187 | + error("InterruptInParam.fall called with 'void(void)' callback (should be 'void(void*)')\r\n"); |
| 188 | +} |
| 189 | + |
| 190 | +void InterruptInParam::_irq_handler(uint32_t id, gpio_irq_event event) |
| 191 | +{ |
| 192 | + InterruptInParam *handler = (InterruptInParam *)id; |
| 193 | + switch (event) { |
| 194 | + case IRQ_RISE: |
| 195 | + if (handler->_rise) { |
| 196 | + handler->_rise(handler->_rise_param); |
| 197 | + } |
| 198 | + break; |
| 199 | + case IRQ_FALL: |
| 200 | + if (handler->_fall) { |
| 201 | + handler->_fall(handler->_fall_param); |
| 202 | + } |
| 203 | + break; |
| 204 | + case IRQ_NONE: |
| 205 | + break; |
| 206 | + } |
| 207 | +} |
0 commit comments