Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / firmware / qorvo-uwb / src / qplatform_zephyr.c
qplatform_zephyr.c123 lines · 4.7 KiBRaw
| 1 | /* |
| 2 | * Platform instance definitions for Qorvo's qplatform on Zephyr. |
| 3 | * |
| 4 | * Replaces qplatform/qm33_qhal_zephyr/src/qplatform.c, which cannot be used as |
| 5 | * shipped: it dereferences `spi_config.cs` as a pointer (it has been a by-value |
| 6 | * struct since Zephyr 3.5) and hard-#errors unless the devicetree happens to label |
| 7 | * its UWB node `dw35720` — a DW3572, where this board carries a DW3110. |
| 8 | * |
| 9 | * The four externs below are what qm33_qhal_common/src/qplatform.c consumes; see |
| 10 | * qplatform_internal.h. Everything comes from the devicetree so the pin mapping |
| 11 | * lives in the board overlay rather than in compile definitions. |
| 12 | */ |
| 13 | #include "qplatform_internal.h" |
| 14 | |
| 15 | #include <qgpio.h> |
| 16 | #include <qspi.h> |
| 17 | |
| 18 | #include <zephyr/devicetree.h> |
| 19 | #include <zephyr/drivers/gpio.h> |
| 20 | #include <zephyr/drivers/spi.h> |
| 21 | #include <zephyr/sys/byteorder.h> |
| 22 | |
| 23 | #define UWB_NODE DT_NODELABEL(dw3110) |
| 24 | |
| 25 | #if !DT_NODE_HAS_STATUS(UWB_NODE, okay) |
| 26 | #error "Enable a 'dw3110' SPI node with irq-gpios and rstn-gpios in the board overlay. \ |
| 27 | See firmware/findfamily-tracker/boards/decawave_dwm3001cdk_nrf52833.overlay." |
| 28 | #endif |
| 29 | |
| 30 | static const struct gpio_dt_spec rstn_pin_spec = GPIO_DT_SPEC_GET(UWB_NODE, rstn_gpios); |
| 31 | static const struct gpio_dt_spec irq_pin_spec = GPIO_DT_SPEC_GET(UWB_NODE, irq_gpios); |
| 32 | static const struct gpio_dt_spec cs_pin_spec = GPIO_DT_SPEC_GET(DT_BUS(UWB_NODE), cs_gpios); |
| 33 | |
| 34 | /* |
| 35 | * qhal's Zephyr qgpio backend keys entirely off `dev` (a gpio_dt_spec); port and |
| 36 | * pin_number are unused there and left zero. |
| 37 | */ |
| 38 | const struct qgpio qm33_rstn = { |
| 39 | .dev = (void *)&rstn_pin_spec, |
| 40 | .port = 0, |
| 41 | .pin_number = 0, |
| 42 | }; |
| 43 | |
| 44 | const struct qgpio qm33_irq = { |
| 45 | .dev = (void *)&irq_pin_spec, |
| 46 | .port = 0, |
| 47 | .pin_number = 0, |
| 48 | }; |
| 49 | |
| 50 | const struct qspi_instance qm33_qspi_instance = { |
| 51 | .instance_number = CONFIG_UWB_SPI_INSTANCE, |
| 52 | .dev = DEVICE_DT_GET(DT_BUS(UWB_NODE)), |
| 53 | }; |
| 54 | |
| 55 | struct qspi_config qm33_qspi_config = { |
| 56 | /* SCK/MOSI/MISO are owned by Zephyr's pinctrl, so qhal needs no descriptors. */ |
| 57 | .sck_pin = { .port = 0, .pin_number = 0, .dev = NULL }, |
| 58 | .mosi_pin = { .port = 0, .pin_number = 0, .dev = NULL }, |
| 59 | .miso_pin = { .port = 0, .pin_number = 0, .dev = NULL }, |
| 60 | .cs_pin = { .port = 0, .pin_number = 0, .dev = (void *)&cs_pin_spec }, |
| 61 | /* Slow until the device is identified; the driver raises it afterwards. */ |
| 62 | .freq_hz = CONFIG_SPI_UWB_SLOW_RATE_FREQ, |
| 63 | .irq_priority = CONFIG_SPI_UWB_IRQ_PRIORITY, |
| 64 | /* |
| 65 | * SPI mode 0. Deliberately not the SDK's QSPI_OP_FLAGS, which sets QSPI_CPOL: |
| 66 | * the DW3000 datasheet specifies mode 0 (CPOL=0, CPHA=0), and mode 2 is what |
| 67 | * the nrfx backend's flag handling happens to paper over. |
| 68 | */ |
| 69 | .op_flags = QSPI_MASTER | QSPI_MSB_FIRST | QSPI_MISO_SINGLE | |
| 70 | QSPI_SET_FRAME_LEN(CONFIG_UWB_SPI_FRAME_SIZE), |
| 71 | }; |
| 72 | |
| 73 | /* |
| 74 | * Bring-up diagnostic: read the transceiver's DEV_ID register straight through Zephyr's |
| 75 | * SPI API, bypassing the Qorvo driver entirely. A DW3110 answers 0xDECA0302. |
| 76 | * |
| 77 | * This exists because "qplatform_init failed" is ambiguous — it cannot distinguish a |
| 78 | * miswired bus or a wrong SPI mode from the driver being unhappy about something it read. |
| 79 | * A correct id here means the bus is fine and the problem is above it. |
| 80 | * |
| 81 | * The transaction is a DW3000 short-addressed read: one header byte (bit 7 clear = read, |
| 82 | * register file 0 offset 0) followed by four bytes clocked out to shift the value in. |
| 83 | */ |
| 84 | static const struct spi_dt_spec uwb_bus = |
| 85 | SPI_DT_SPEC_GET(UWB_NODE, SPI_WORD_SET(8) | SPI_OP_MODE_MASTER | SPI_TRANSFER_MSB); |
| 86 | |
| 87 | /* |
| 88 | * The devicetree-derived bus spec, shared with the qspi backend. |
| 89 | * |
| 90 | * Hand-assembling an equivalent spi_config produced transfers that clocked out correctly |
| 91 | * but read back all zeros, while this spec worked on the same bus with what logged as an |
| 92 | * identical frequency, operation word and chip-select. Rather than keep guessing at the |
| 93 | * discrepancy, the backend uses Zephyr's own DT plumbing and only overrides the clock. |
| 94 | */ |
| 95 | const struct spi_dt_spec *ff_qorvo_spi_spec(void) |
| 96 | { |
| 97 | return &uwb_bus; |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Bring-up diagnostic: the current level of the transceiver IRQ line, read through the same |
| 102 | * devicetree spec the stack itself uses. The overlay declares irq-gpios GPIO_ACTIVE_HIGH, so |
| 103 | * this logical read equals the physical level on P1.02. |
| 104 | */ |
| 105 | int ff_qorvo_irq_pin_level(void) |
| 106 | { |
| 107 | return gpio_pin_get_dt(&irq_pin_spec); |
| 108 | } |
| 109 | |
| 110 | uint32_t ff_qorvo_read_dev_id(void) |
| 111 | { |
| 112 | uint8_t tx[5] = { 0x00, 0x00, 0x00, 0x00, 0x00 }; |
| 113 | uint8_t rx[5] = { 0 }; |
| 114 | const struct spi_buf tx_buf = { .buf = tx, .len = sizeof(tx) }; |
| 115 | const struct spi_buf rx_buf = { .buf = rx, .len = sizeof(rx) }; |
| 116 | const struct spi_buf_set tx_set = { .buffers = &tx_buf, .count = 1 }; |
| 117 | const struct spi_buf_set rx_set = { .buffers = &rx_buf, .count = 1 }; |
| 118 | |
| 119 | if (spi_transceive_dt(&uwb_bus, &tx_set, &rx_set) < 0) { |
| 120 | return 0; |
| 121 | } |
| 122 | return sys_get_le32(&rx[1]); |
| 123 | } |