|
| 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 | +#include "py/runtime.h" |
| 28 | +#include "py/mphal.h" |
| 29 | +#include "py/mperrno.h" |
| 30 | +#include "extmod/modmachine.h" |
| 31 | + |
| 32 | +#if MICROPY_PY_MACHINE_SPI |
| 33 | +#include "clk.h" |
| 34 | +#include "spi.h" |
| 35 | +#include "sys_ctrl_spi.h" |
| 36 | + |
| 37 | +typedef struct _machine_spi_obj_t { |
| 38 | + mp_obj_base_t base; |
| 39 | + uint8_t id; |
| 40 | + SPI_Type *inst; |
| 41 | + bool is_lp; |
| 42 | +} machine_spi_obj_t; |
| 43 | + |
| 44 | +static machine_spi_obj_t machine_spi_obj[] = { |
| 45 | + #if defined(MICROPY_HW_SPI0_SCK) |
| 46 | + [0] = {{&machine_spi_type}, 0, (SPI_Type *)SPI0_BASE, false}, |
| 47 | + #endif |
| 48 | + #if defined(MICROPY_HW_SPI1_SCK) |
| 49 | + [1] = {{&machine_spi_type}, 1, (SPI_Type *)SPI1_BASE, false}, |
| 50 | + #endif |
| 51 | + #if defined(MICROPY_HW_SPI2_SCK) |
| 52 | + [2] = {{&machine_spi_type}, 2, (SPI_Type *)SPI2_BASE, false}, |
| 53 | + #endif |
| 54 | + #if defined(MICROPY_HW_SPI3_SCK) |
| 55 | + [3] = {{&machine_spi_type}, 3, (SPI_Type *)SPI3_BASE, false}, |
| 56 | + #endif |
| 57 | + #if defined(MICROPY_HW_LPSPI0_SCK) |
| 58 | + [4] = {{&machine_spi_type}, 4, (SPI_Type *)LPSPI0_BASE, true}, |
| 59 | + #endif |
| 60 | + |
| 61 | +}; |
| 62 | + |
| 63 | +static inline uint32_t spi_get_clk(machine_spi_obj_t *spi) { |
| 64 | + return spi->is_lp ? GetSystemCoreClock() : GetSystemAHBClock(); |
| 65 | +} |
| 66 | + |
| 67 | +static void spi_init(machine_spi_obj_t *spi, uint32_t baudrate, |
| 68 | + uint32_t polarity, uint32_t phase, uint32_t bits, uint32_t firstbit) { |
| 69 | + const machine_pin_obj_t *pins[4] = { NULL, NULL, NULL, NULL }; |
| 70 | + switch (spi->id) { |
| 71 | + #if defined(MICROPY_HW_SPI0_SCK) |
| 72 | + case 0: |
| 73 | + pins[0] = MICROPY_HW_SPI0_SCK; |
| 74 | + pins[1] = MICROPY_HW_SPI0_MISO; |
| 75 | + pins[2] = MICROPY_HW_SPI0_MOSI; |
| 76 | + #if defined(MICROPY_HW_SPI0_NSS) |
| 77 | + pins[3] = MICROPY_HW_SPI0_NSS; |
| 78 | + #endif |
| 79 | + break; |
| 80 | + #endif |
| 81 | + #if defined(MICROPY_HW_SPI1_SCK) |
| 82 | + case 1: |
| 83 | + pins[0] = MICROPY_HW_SPI1_SCK; |
| 84 | + pins[1] = MICROPY_HW_SPI1_MISO; |
| 85 | + pins[2] = MICROPY_HW_SPI1_MOSI; |
| 86 | + #if defined(MICROPY_HW_SPI1_NSS) |
| 87 | + pins[3] = MICROPY_HW_SPI1_NSS; |
| 88 | + #endif |
| 89 | + break; |
| 90 | + #endif |
| 91 | + #if defined(MICROPY_HW_SPI2_SCK) |
| 92 | + case 2: |
| 93 | + pins[0] = MICROPY_HW_SPI2_SCK; |
| 94 | + pins[1] = MICROPY_HW_SPI2_MISO; |
| 95 | + pins[2] = MICROPY_HW_SPI2_MOSI; |
| 96 | + #if defined(MICROPY_HW_SPI2_NSS) |
| 97 | + pins[3] = MICROPY_HW_SPI2_NSS; |
| 98 | + #endif |
| 99 | + break; |
| 100 | + #endif |
| 101 | + #if defined(MICROPY_HW_SPI3_SCK) |
| 102 | + case 3: |
| 103 | + pins[0] = MICROPY_HW_SPI3_SCK; |
| 104 | + pins[1] = MICROPY_HW_SPI3_MISO; |
| 105 | + pins[2] = MICROPY_HW_SPI3_MOSI; |
| 106 | + #if defined(MICROPY_HW_SPI3_NSS) |
| 107 | + pins[3] = MICROPY_HW_SPI3_NSS; |
| 108 | + #endif |
| 109 | + break; |
| 110 | + #endif |
| 111 | + #if defined(MICROPY_HW_LPSPI0_SCK) |
| 112 | + case 4: // LPSPI0 |
| 113 | + pins[0] = MICROPY_HW_LPSPI0_SCK; |
| 114 | + pins[1] = MICROPY_HW_LPSPI0_MISO; |
| 115 | + pins[2] = MICROPY_HW_LPSPI0_MOSI; |
| 116 | + #if defined(MICROPY_HW_LPSPI0_NSS) |
| 117 | + pins[3] = MICROPY_HW_LPSPI0_NSS; |
| 118 | + #endif |
| 119 | + break; |
| 120 | + #endif |
| 121 | + default: |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + // Disable SPI. |
| 126 | + spi_disable(spi->inst); |
| 127 | + |
| 128 | + // Enable clocks. |
| 129 | + if (spi->is_lp) { |
| 130 | + enable_lpspi_clk(); |
| 131 | + } |
| 132 | + |
| 133 | + // Configure SPI pins. |
| 134 | + for (size_t i = 0; i < MP_ARRAY_SIZE(pins) && pins[i]; i++) { |
| 135 | + mp_hal_pin_config(pins[i], MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, |
| 136 | + MP_HAL_PIN_SPEED_HIGH, MP_HAL_PIN_DRIVE_8MA, MP_HAL_PIN_ALT_SPI, true); |
| 137 | + } |
| 138 | + |
| 139 | + // Disable all interrupts. |
| 140 | + spi_mask_interrupts(spi->inst); |
| 141 | + |
| 142 | + // Configure baudrate clock |
| 143 | + spi_set_bus_speed(spi->inst, baudrate, spi_get_clk(spi)); |
| 144 | + |
| 145 | + // Configure FIFOs |
| 146 | + spi_set_tx_threshold(spi->inst, 0); |
| 147 | + spi_set_rx_threshold(spi->inst, 0); |
| 148 | + if (!spi->is_lp) { |
| 149 | + spi_set_rx_sample_delay(spi->inst, 0); |
| 150 | + spi_set_tx_fifo_start_level(spi->inst, 0); |
| 151 | + } |
| 152 | + |
| 153 | + // Configure SPI bus mode. |
| 154 | + uint32_t spi_mode = (polarity << 1) | phase; |
| 155 | + if (!spi->is_lp) { |
| 156 | + spi_set_mode(spi->inst, spi_mode); |
| 157 | + } else { |
| 158 | + lpspi_set_mode(spi->inst, spi_mode); |
| 159 | + } |
| 160 | + |
| 161 | + // Configure SPI bus protocol. |
| 162 | + uint32_t spi_proto = SPI_PROTO_SPI; |
| 163 | + if (!spi->is_lp) { |
| 164 | + spi_set_protocol(spi->inst, spi_proto); |
| 165 | + } else { |
| 166 | + lpspi_set_protocol(spi->inst, spi_proto); |
| 167 | + } |
| 168 | + |
| 169 | + // Configure SPI transfer mode. |
| 170 | + if (!spi->is_lp) { |
| 171 | + spi_mode_master(spi->inst); |
| 172 | + } |
| 173 | + |
| 174 | + // Configure frame size. |
| 175 | + if (!spi->is_lp) { |
| 176 | + spi_set_dfs(spi->inst, bits); |
| 177 | + } else { |
| 178 | + lpspi_set_dfs(spi->inst, bits); |
| 179 | + } |
| 180 | + |
| 181 | + // Configure slave select pin |
| 182 | + spi_control_ss(spi->inst, 0, true); |
| 183 | + if (!spi->is_lp) { |
| 184 | + spi_set_sste(spi->inst, false); |
| 185 | + } else { |
| 186 | + lpspi_set_sste(spi->inst, false); |
| 187 | + } |
| 188 | + |
| 189 | + // Clear IRQs. |
| 190 | + (void)spi->inst->SPI_ICR; |
| 191 | + |
| 192 | + // Enable SPI. |
| 193 | + spi_enable(spi->inst); |
| 194 | +} |
| 195 | + |
| 196 | +static void machine_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { |
| 197 | + machine_spi_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 198 | + uint32_t baudrate = spi_get_bus_speed(self->inst, spi_get_clk(self)); |
| 199 | + mp_printf(print, "SPI(%u, baudrate=%u, lp=%u)", self->id, baudrate, self->is_lp); |
| 200 | +} |
| 201 | + |
| 202 | +mp_obj_t machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { |
| 203 | + enum { ARG_id, ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_sck, ARG_mosi, ARG_miso }; |
| 204 | + static const mp_arg_t allowed_args[] = { |
| 205 | + { MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ }, |
| 206 | + { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 500000} }, |
| 207 | + { MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, |
| 208 | + { MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, |
| 209 | + { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} }, |
| 210 | + { MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, |
| 211 | + { MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, |
| 212 | + { MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, |
| 213 | + { MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, |
| 214 | + }; |
| 215 | + |
| 216 | + // Parse args. |
| 217 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 218 | + mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 219 | + |
| 220 | + // Get spi bus. |
| 221 | + int spi_id = mp_obj_get_int(args[ARG_id].u_obj); |
| 222 | + if (spi_id < 0 || spi_id >= MP_ARRAY_SIZE(machine_spi_obj) || !machine_spi_obj[spi_id].inst) { |
| 223 | + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("SPI(%d) doesn't exist"), spi_id); |
| 224 | + } |
| 225 | + |
| 226 | + // Get static peripheral object. |
| 227 | + machine_spi_obj_t *self = &machine_spi_obj[spi_id]; |
| 228 | + |
| 229 | + // here we would check the sck/mosi/miso pins and configure them, but it's not implemented |
| 230 | + if (args[ARG_sck].u_obj != MP_OBJ_NULL || |
| 231 | + args[ARG_mosi].u_obj != MP_OBJ_NULL || |
| 232 | + args[ARG_miso].u_obj != MP_OBJ_NULL) { |
| 233 | + mp_raise_ValueError(MP_ERROR_TEXT("explicit choice of sck/mosi/miso is not implemented")); |
| 234 | + } |
| 235 | + |
| 236 | + // Initialize and configure SPI. |
| 237 | + spi_init(self, args[ARG_baudrate].u_int, args[ARG_polarity].u_int, |
| 238 | + args[ARG_phase].u_int, args[ARG_bits].u_int, args[ARG_firstbit].u_int); |
| 239 | + |
| 240 | + return MP_OBJ_FROM_PTR(self); |
| 241 | +} |
| 242 | + |
| 243 | +static void machine_spi_init(mp_obj_base_t *self_in, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 244 | + enum { ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit }; |
| 245 | + static const mp_arg_t allowed_args[] = { |
| 246 | + { MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, |
| 247 | + { MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, |
| 248 | + { MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, |
| 249 | + { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, |
| 250 | + { MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} }, |
| 251 | + }; |
| 252 | + |
| 253 | + // Parse the arguments. |
| 254 | + machine_spi_obj_t *self = (machine_spi_obj_t *)self_in; |
| 255 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 256 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 257 | + |
| 258 | + // Initialize and configure SPI. |
| 259 | + spi_init(self, args[ARG_baudrate].u_int, args[ARG_polarity].u_int, |
| 260 | + args[ARG_phase].u_int, args[ARG_bits].u_int, args[ARG_firstbit].u_int); |
| 261 | +} |
| 262 | + |
| 263 | +static void machine_spi_deinit(mp_obj_base_t *self_in) { |
| 264 | + machine_spi_obj_t *self = (machine_spi_obj_t *)self_in; |
| 265 | + // Disable all interrupts. |
| 266 | + spi_mask_interrupts(self->inst); |
| 267 | + // Disable SS pin. |
| 268 | + spi_control_ss(self->inst, 0, 0); |
| 269 | + // Disable SPI. |
| 270 | + spi_disable(self->inst); |
| 271 | + // Deinitialize GPIOs and clocks. |
| 272 | + if (self->is_lp) { |
| 273 | + disable_lpspi_clk(); |
| 274 | + } |
| 275 | +} |
| 276 | + |
| 277 | +static void machine_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) { |
| 278 | + machine_spi_obj_t *self = (machine_spi_obj_t *)self_in; |
| 279 | + spi_transfer_t spi_xfer = { |
| 280 | + .tx_buff = src, |
| 281 | + .tx_total_cnt = len, |
| 282 | + .rx_buff = dest, |
| 283 | + .rx_total_cnt = len, |
| 284 | + .tx_default_val = 0xFF, |
| 285 | + .tx_default_enable = true, |
| 286 | + .mode = SPI_TMOD_TX_AND_RX, |
| 287 | + }; |
| 288 | + // TODO redo transfer_blocking to timeout and poll events. |
| 289 | + if (!self->is_lp) { |
| 290 | + spi_transfer_blocking(self->inst, &spi_xfer); |
| 291 | + } else { |
| 292 | + lpspi_transfer_blocking(self->inst, &spi_xfer); |
| 293 | + } |
| 294 | +} |
| 295 | + |
| 296 | +static const mp_machine_spi_p_t machine_spi_p = { |
| 297 | + .init = machine_spi_init, |
| 298 | + .deinit = machine_spi_deinit, |
| 299 | + .transfer = machine_spi_transfer, |
| 300 | +}; |
| 301 | + |
| 302 | +MP_DEFINE_CONST_OBJ_TYPE( |
| 303 | + machine_spi_type, |
| 304 | + MP_QSTR_SPI, |
| 305 | + MP_TYPE_FLAG_NONE, |
| 306 | + make_new, machine_spi_make_new, |
| 307 | + print, machine_spi_print, |
| 308 | + protocol, &machine_spi_p, |
| 309 | + locals_dict, &mp_machine_spi_locals_dict |
| 310 | + ); |
| 311 | + |
| 312 | +#endif // MICROPY_PY_MACHINE_SPI |
0 commit comments