|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2024-2025 OpenMV LLC. |
| 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 | +#if MICROPY_PY_NETWORK_CYW43 |
| 28 | + |
| 29 | +#include "lib/cyw43-driver/src/cyw43.h" |
| 30 | +#include "lib/cyw43-driver/src/cyw43_internal.h" |
| 31 | +#include "lib/cyw43-driver/src/cyw43_spi.h" |
| 32 | + |
| 33 | +#include "spi.h" |
| 34 | +#include "sys_ctrl_spi.h" |
| 35 | + |
| 36 | +// CYW43 is connected to SPI3. |
| 37 | +#define HW_SPI ((SPI_Type *)SPI3_BASE) |
| 38 | +#define SPI_BAUDRATE (16000000) |
| 39 | +#define SPI_RX_FIFO_SIZE (16) |
| 40 | + |
| 41 | +// WL_IRQ is on P9_6. |
| 42 | +#define WL_IRQ_IRQN (GPIO9_IRQ6_IRQn) |
| 43 | +#define WL_IRQ_HANDLER GPIO9_IRQ6Handler |
| 44 | + |
| 45 | +// Must run at IRQ priority above PendSV so it can wake cyw43-driver when PendSV is disabled. |
| 46 | +void WL_IRQ_HANDLER(void) { |
| 47 | + if (gpio_read_int_rawstatus(pin_WL_IRQ->gpio, pin_WL_IRQ->pin)) { |
| 48 | + gpio_interrupt_eoi(pin_WL_IRQ->gpio, pin_WL_IRQ->pin); |
| 49 | + pendsv_schedule_dispatch(PENDSV_DISPATCH_CYW43, cyw43_poll); |
| 50 | + __SEV(); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +static void spi_bus_init(void) { |
| 55 | + // Configure pins. |
| 56 | + mp_hal_pin_output(pin_WL_CS); |
| 57 | + mp_hal_pin_high(pin_WL_CS); |
| 58 | + // NOTE: Alif recommends enabled input read for all SPI pins. |
| 59 | + mp_hal_pin_config(pin_WL_SCLK, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_HIGH, MP_HAL_PIN_DRIVE_8MA, MP_HAL_PIN_ALT_SPI, true); |
| 60 | + mp_hal_pin_config(pin_WL_MOSI, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_HIGH, MP_HAL_PIN_DRIVE_8MA, MP_HAL_PIN_ALT_SPI, true); |
| 61 | + mp_hal_pin_config(pin_WL_MISO, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, MP_HAL_PIN_SPEED_HIGH, MP_HAL_PIN_DRIVE_8MA, MP_HAL_PIN_ALT_SPI, true); |
| 62 | + |
| 63 | + // Starts out clock_polarity=1, clock_phase=0. |
| 64 | + spi_mode_master(HW_SPI); |
| 65 | + spi_set_bus_speed(HW_SPI, SPI_BAUDRATE, GetSystemAHBClock()); |
| 66 | + spi_set_mode(HW_SPI, SPI_MODE_2); |
| 67 | + spi_set_protocol(HW_SPI, SPI_PROTO_SPI); |
| 68 | + spi_set_dfs(HW_SPI, 8); |
| 69 | + spi_set_tmod(HW_SPI, SPI_TMOD_TX_AND_RX); |
| 70 | + spi_control_ss(HW_SPI, 0, SPI_SS_STATE_ENABLE); |
| 71 | +} |
| 72 | + |
| 73 | +int cyw43_spi_init(cyw43_int_t *self) { |
| 74 | + spi_bus_init(); |
| 75 | + |
| 76 | + // Configure IRQ for WL_IRQ (active low input). |
| 77 | + NVIC_SetPriority(WL_IRQ_IRQN, IRQ_PRI_CYW43); |
| 78 | + NVIC_ClearPendingIRQ(WL_IRQ_IRQN); |
| 79 | + NVIC_EnableIRQ(WL_IRQ_IRQN); |
| 80 | + |
| 81 | + return 0; |
| 82 | +} |
| 83 | + |
| 84 | +void cyw43_spi_deinit(cyw43_int_t *self) { |
| 85 | + // Disable clock, SS and SPI. |
| 86 | + spi_mask_interrupts(HW_SPI); |
| 87 | + spi_control_ss(HW_SPI, 0, 0); |
| 88 | + spi_disable(HW_SPI); |
| 89 | + |
| 90 | + // Disable SPI IRQ. |
| 91 | + NVIC_DisableIRQ(WL_IRQ_IRQN); |
| 92 | + NVIC_ClearPendingIRQ(WL_IRQ_IRQN); |
| 93 | +} |
| 94 | + |
| 95 | +void cyw43_spi_gpio_setup(void) { |
| 96 | +} |
| 97 | + |
| 98 | +void cyw43_spi_reset(void) { |
| 99 | +} |
| 100 | + |
| 101 | +void cyw43_spi_set_polarity(cyw43_int_t *self, int pol) { |
| 102 | + (void)self; |
| 103 | + |
| 104 | + if (pol == 0) { |
| 105 | + spi_set_mode(HW_SPI, SPI_MODE_0); |
| 106 | + } else { |
| 107 | + spi_set_mode(HW_SPI, SPI_MODE_2); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// tx must not be NULL. |
| 112 | +// rx_len must be 0, or the same as tx_len. |
| 113 | +int cyw43_spi_transfer(cyw43_int_t *self, const uint8_t *tx, size_t tx_len, uint8_t *rx, size_t rx_len) { |
| 114 | + (void)self; |
| 115 | + |
| 116 | + if (tx_len == 0 && rx_len == 0) { |
| 117 | + return 0; |
| 118 | + } |
| 119 | + |
| 120 | + mp_hal_pin_low(pin_WL_CS); |
| 121 | + |
| 122 | + // Must read the same amount of data that is written out to SPI. |
| 123 | + rx_len = tx_len; |
| 124 | + |
| 125 | + while (tx_len || rx_len) { |
| 126 | + // Only add data to the TX FIFO if: |
| 127 | + // - there's data to add |
| 128 | + // - and TX is not ahead of RX by more than the RX FIFO size |
| 129 | + // - and there's space in the TX FIFO |
| 130 | + if (tx_len && tx_len + SPI_RX_FIFO_SIZE > rx_len && (HW_SPI->SPI_SR & SPI_SR_TFNF) != 0) { |
| 131 | + HW_SPI->SPI_DR[0] = *tx++; |
| 132 | + --tx_len; |
| 133 | + } |
| 134 | + |
| 135 | + // Take data from the RX FIFO and store it into the output buffer (if given). |
| 136 | + if (rx_len && (HW_SPI->SPI_SR & SPI_SR_RFNE) != 0) { |
| 137 | + uint8_t data = HW_SPI->SPI_DR[0]; |
| 138 | + if (rx != NULL) { |
| 139 | + *rx++ = data; |
| 140 | + } |
| 141 | + --rx_len; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + mp_hal_pin_high(pin_WL_CS); |
| 146 | + |
| 147 | + return 0; |
| 148 | +} |
| 149 | + |
| 150 | +#endif |
0 commit comments