|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2022 Ibrahim Abdelkader <iabdalkader@openmv.io> |
| 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 <stdio.h> |
| 28 | +#include <string.h> |
| 29 | + |
| 30 | +#include "py/runtime.h" |
| 31 | +#include "py/mphal.h" |
| 32 | + |
| 33 | +#if defined(MICROPY_PY_NETWORK_NINAW10) && defined(MICROPY_HW_PIN_EXT_COUNT) |
| 34 | + |
| 35 | +#include "modmachine.h" |
| 36 | +#include "machine_pin.h" |
| 37 | +#include "nina_wifi_drv.h" |
| 38 | + |
| 39 | +#define NINA_GPIO_INPUT (0x00) |
| 40 | +#define NINA_GPIO_OUTPUT (0x01) |
| 41 | +#define NINA_GPIO_INPUT_PULLUP (0x02) |
| 42 | + |
| 43 | +#define NINA_GPIO_MODE (0x50) |
| 44 | +#define NINA_GPIO_READ (0x53) |
| 45 | +#define NINA_GPIO_WRITE (0x51) |
| 46 | + |
| 47 | +static uint8_t pin_map[MICROPY_HW_PIN_EXT_COUNT] = { |
| 48 | + 27, // LEDR |
| 49 | + 25, // LEDG |
| 50 | + 26, // LEDB |
| 51 | + 36, // A6 |
| 52 | + 35, // A7 |
| 53 | +}; |
| 54 | + |
| 55 | +void machine_pin_ext_init(void) { |
| 56 | + nina_init(); |
| 57 | +} |
| 58 | + |
| 59 | +void machine_pin_ext_set(machine_pin_obj_t *self, bool value) { |
| 60 | + if (self->id >= 0 && self->id < MICROPY_HW_PIN_EXT_COUNT) { |
| 61 | + uint8_t buf[] = {pin_map[self->id], value}; |
| 62 | + nina_ioctl(NINA_GPIO_WRITE, sizeof(buf), buf, 0); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +bool machine_pin_ext_get(machine_pin_obj_t *self) { |
| 67 | + bool value = false; |
| 68 | + if (self->id >= 0 && self->id < MICROPY_HW_PIN_EXT_COUNT) { |
| 69 | + uint8_t buf[] = {pin_map[self->id]}; |
| 70 | + nina_ioctl(NINA_GPIO_READ, sizeof(buf), buf, 0); |
| 71 | + } |
| 72 | + return value; |
| 73 | +} |
| 74 | + |
| 75 | +void machine_pin_ext_config(machine_pin_obj_t *self, int mode, int value) { |
| 76 | + if (mode == MACHINE_PIN_MODE_IN) { |
| 77 | + mode = NINA_GPIO_INPUT; |
| 78 | + self->is_output = false; |
| 79 | + } else if (mode == MACHINE_PIN_MODE_OUT) { |
| 80 | + mode = NINA_GPIO_OUTPUT; |
| 81 | + self->is_output = true; |
| 82 | + machine_pin_ext_set(self, value); |
| 83 | + } else { |
| 84 | + mp_raise_ValueError("only Pin.OUT and Pin.IN are supported for this pin"); |
| 85 | + } |
| 86 | + if (self->id >= 0 && self->id < MICROPY_HW_PIN_EXT_COUNT) { |
| 87 | + uint8_t buf[] = {pin_map[self->id], mode}; |
| 88 | + nina_ioctl(NINA_GPIO_MODE, sizeof(buf), buf, 0); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +#endif // defined(MICROPY_PY_NETWORK_NINAW10) && defined(MICROPY_HW_PIN_EXT_COUNT) |
0 commit comments