|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2024 Scott Shawcroft for Adafruit Industries |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "shared-module/max3421e/Max3421E.h" |
| 28 | + |
| 29 | +#include "common-hal/max3421e/Max3421E.h" |
| 30 | + |
| 31 | +#include "lib/tinyusb/src/host/usbh.h" |
| 32 | +#include "supervisor/usb.h" |
| 33 | + |
| 34 | +#include "eic_handler.h" |
| 35 | +#include "py/runtime.h" |
| 36 | +#include "samd/external_interrupts.h" |
| 37 | + |
| 38 | +#include "shared-bindings/microcontroller/Pin.h" |
| 39 | + |
| 40 | +void samd_max3421e_interrupt_handler(uint8_t channel) { |
| 41 | + max3421e_interrupt_handler((max3421e_max3421e_obj_t *)get_eic_channel_data(channel)); |
| 42 | +} |
| 43 | + |
| 44 | +// Setup irq on self->irq to call tuh_int_handler(rhport, true) on falling edge |
| 45 | +void common_hal_max3421e_max3421e_init_irq(max3421e_max3421e_obj_t *self) { |
| 46 | + const mcu_pin_obj_t *pin = self->irq.pin; |
| 47 | + if (!pin->has_extint) { |
| 48 | + raise_ValueError_invalid_pin(); |
| 49 | + } |
| 50 | + if (eic_get_enable()) { |
| 51 | + if (!eic_channel_free(pin->extint_channel)) { |
| 52 | + mp_raise_RuntimeError(MP_ERROR_TEXT("Internal resource(s) in use")); |
| 53 | + } |
| 54 | + } else { |
| 55 | + turn_on_external_interrupt_controller(); |
| 56 | + } |
| 57 | + |
| 58 | + set_eic_channel_data(pin->extint_channel, self); |
| 59 | + set_eic_handler(pin->extint_channel, EIC_HANDLER_MAX3421E); |
| 60 | + turn_on_eic_channel(pin->extint_channel, EIC_CONFIG_SENSE0_LOW_Val); |
| 61 | +} |
| 62 | + |
| 63 | +void common_hal_max3421e_max3421e_deinit_irq(max3421e_max3421e_obj_t *self) { |
| 64 | + const mcu_pin_obj_t *pin = self->irq.pin; |
| 65 | + set_eic_handler(pin->extint_channel, EIC_HANDLER_NO_INTERRUPT); |
| 66 | + turn_off_eic_channel(pin->extint_channel); |
| 67 | + reset_pin_number(pin->extint_channel); |
| 68 | +} |
| 69 | + |
| 70 | +// Enable or disable the irq interrupt. |
| 71 | +void common_hal_max3421e_max3421e_irq_enabled(max3421e_max3421e_obj_t *self, bool enabled) { |
| 72 | + const mcu_pin_obj_t *pin = self->irq.pin; |
| 73 | + uint32_t mask = 1 << pin->extint_channel; |
| 74 | + // Don't use the turn_(on|off) API because it may deinit the eic completely. |
| 75 | + if (!enabled) { |
| 76 | + EIC->INTENCLR.reg = mask << EIC_INTENSET_EXTINT_Pos; |
| 77 | + } else { |
| 78 | + EIC->INTENSET.reg = mask << EIC_INTENSET_EXTINT_Pos; |
| 79 | + } |
| 80 | +} |
0 commit comments