|
| 1 | +// This file is part of the CircuitPython project: https://circuitpython.org |
| 2 | +// |
| 3 | +// SPDX-FileCopyrightText: Copyright (c) 2024 Brandon Hurst, Analog Devices, Inc. |
| 4 | +// |
| 5 | +// SPDX-License-Identifier: MIT |
| 6 | + |
| 7 | +#include "peripherals/pins.h" |
| 8 | + |
| 9 | +#include "common-hal/busio/SPI.h" |
| 10 | +#include "max32_spi.h" |
| 11 | +#include "max32690.h" |
| 12 | + |
| 13 | +#include "py/runtime.h" |
| 14 | +#include "py/mperrno.h" |
| 15 | + |
| 16 | +// TODO Decouple from APARD board |
| 17 | +const mxc_gpio_cfg_t spi_maps[NUM_SPI] = { |
| 18 | + // DUMMY entry for SPI0 (not on APARD board) |
| 19 | + { MXC_GPIO0, 0xFFFFFFFF, 0, 0, 0, 0}, |
| 20 | + |
| 21 | + // SPI1A |
| 22 | + { MXC_GPIO1, (MXC_GPIO_PIN_23 | MXC_GPIO_PIN_26 | MXC_GPIO_PIN_28 | MXC_GPIO_PIN_29), |
| 23 | + MXC_GPIO_FUNC_ALT1, MXC_GPIO_PAD_NONE, MXC_GPIO_VSSEL_VDDIO, MXC_GPIO_DRVSTR_0 }, |
| 24 | + // SPI2A |
| 25 | + { MXC_GPIO2, (MXC_GPIO_PIN_2 | MXC_GPIO_PIN_3 | MXC_GPIO_PIN_4 | MXC_GPIO_PIN_5), |
| 26 | + MXC_GPIO_FUNC_ALT1, MXC_GPIO_PAD_NONE, MXC_GPIO_VSSEL_VDDIO, MXC_GPIO_DRVSTR_0 }, |
| 27 | + // SPI3A |
| 28 | + { MXC_GPIO0, (MXC_GPIO_PIN_16 | MXC_GPIO_PIN_19 | MXC_GPIO_PIN_20 | MXC_GPIO_PIN_21), |
| 29 | + MXC_GPIO_FUNC_ALT1, MXC_GPIO_PAD_NONE, MXC_GPIO_VSSEL_VDDIO, MXC_GPIO_DRVSTR_0 }, |
| 30 | + // SPI4A |
| 31 | + { MXC_GPIO1, (MXC_GPIO_PIN_0 | MXC_GPIO_PIN_1 | MXC_GPIO_PIN_2 | MXC_GPIO_PIN_3), |
| 32 | + MXC_GPIO_FUNC_ALT1, MXC_GPIO_PAD_NONE, MXC_GPIO_VSSEL_VDDIO, MXC_GPIO_DRVSTR_0 }, |
| 33 | +}; |
| 34 | + |
| 35 | +int pinsToSpi(const mcu_pin_obj_t *mosi, const mcu_pin_obj_t *miso, |
| 36 | + const mcu_pin_obj_t *sck, const mcu_pin_obj_t *cs) { |
| 37 | + for (int i = 0; i < NUM_SPI; i++) { |
| 38 | + if ((spi_maps[i].port == (MXC_GPIO_GET_GPIO(mosi->port))) |
| 39 | + && (spi_maps[i].mask == ((cs->mask) | (mosi->mask) | (miso->mask) | (sck->mask)))) { |
| 40 | + return i; |
| 41 | + } |
| 42 | + } |
| 43 | + mp_raise_RuntimeError_varg(MP_ERROR_TEXT("ERR: Unable to find an SPI matching pins... \ |
| 44 | + \nMOSI: port %d mask %d\nMISO: port %d mask %d\n \ |
| 45 | + \nSCK: port %d mask %d\nCS: port %d mask%d\n"), |
| 46 | + mosi->port, mosi->mask, miso->port, miso->mask, |
| 47 | + sck->port, sck->mask, cs->port, cs->mask); |
| 48 | + return -1; |
| 49 | +} |
0 commit comments