|
| 1 | +/* |
| 2 | +* This file is part of the MicroPython project, http://micropython.org/ |
| 3 | +* |
| 4 | +* The MIT License (MIT) |
| 5 | +* |
| 6 | +* Copyright (c) 2016 Damien P. George |
| 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 <stdint.h> |
| 29 | +#include <string.h> |
| 30 | + |
| 31 | + |
| 32 | +#include "py/runtime.h" |
| 33 | +#include "py/stream.h" |
| 34 | +#include "py/mphal.h" |
| 35 | +#include "extmod/machine_spi.h" |
| 36 | +#include "machine_hw_spi.h" |
| 37 | +#include "modmachine.h" |
| 38 | + |
| 39 | + |
| 40 | +// if a port didn't define MSB/LSB constants then provide them |
| 41 | +#ifndef MICROPY_PY_MACHINE_SPI_MSB |
| 42 | +#define MICROPY_PY_MACHINE_SPI_MSB (0) |
| 43 | +#define MICROPY_PY_MACHINE_SPI_LSB (1) |
| 44 | +#endif |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +STATIC void machine_hw_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) { |
| 49 | + machine_hw_spi_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 50 | + int bits_to_send = len * self->bits; |
| 51 | + if (self->deinitialized) { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + struct spi_transaction_t transaction = { |
| 56 | + .flags = 0, |
| 57 | + .length = bits_to_send, |
| 58 | + .tx_buffer = NULL, |
| 59 | + .rx_buffer = NULL, |
| 60 | + }; |
| 61 | + bool shortMsg = len <= 4; |
| 62 | + |
| 63 | + |
| 64 | + if(shortMsg) { |
| 65 | + if (src != NULL) { |
| 66 | + memcpy(&transaction.tx_data, src, len); |
| 67 | + transaction.flags |= SPI_TRANS_USE_TXDATA; |
| 68 | + } |
| 69 | + if (dest != NULL) { |
| 70 | + transaction.flags |= SPI_TRANS_USE_RXDATA; |
| 71 | + } |
| 72 | + } else { |
| 73 | + transaction.tx_buffer = src; |
| 74 | + transaction.rx_buffer = dest; |
| 75 | + } |
| 76 | + |
| 77 | + spi_device_transmit(self->spi, &transaction); |
| 78 | + |
| 79 | + if (shortMsg && dest != NULL) { |
| 80 | + memcpy(dest, &transaction.rx_data, len); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +/******************************************************************************/ |
| 85 | +// MicroPython bindings for hw_spi |
| 86 | + |
| 87 | +STATIC void machine_hw_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { |
| 88 | + machine_hw_spi_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 89 | + mp_printf(print, "hw_spi(id=%u, baudrate=%u, polarity=%u, phase=%u, bits=%u, firstbit=%u, sck=%d, mosi=%d, miso=%d)", |
| 90 | + self->host, self->baudrate, self->polarity, |
| 91 | + self->phase, self->bits, self->firstbit, |
| 92 | + self->sck, self->mosi, self->miso ); |
| 93 | +} |
| 94 | + |
| 95 | +STATIC void machine_hw_spi_init(mp_obj_base_t *self_in, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 96 | + machine_hw_spi_obj_t *self = (machine_hw_spi_obj_t*)self_in; |
| 97 | + esp_err_t ret; |
| 98 | + |
| 99 | + enum { ARG_id, ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_sck, ARG_mosi, ARG_miso }; |
| 100 | + static const mp_arg_t allowed_args[] = { |
| 101 | + { MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_INT , {.u_int = 1} }, |
| 102 | + { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 500000} }, |
| 103 | + { MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, |
| 104 | + { MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, |
| 105 | + { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} }, |
| 106 | + { MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = MICROPY_PY_MACHINE_SPI_MSB} }, |
| 107 | + { MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, |
| 108 | + { MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, |
| 109 | + { MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, |
| 110 | + }; |
| 111 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 112 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), |
| 113 | + allowed_args, args); |
| 114 | + |
| 115 | + int host = args[ARG_id].u_int; |
| 116 | + if (host != HSPI_HOST && host != VSPI_HOST) { |
| 117 | + mp_raise_ValueError("SPI ID must be either hw_spi(1) or VSPI(2)"); |
| 118 | + } |
| 119 | + |
| 120 | + self->host = host; |
| 121 | + self->baudrate = args[ARG_baudrate].u_int; |
| 122 | + self->polarity = args[ARG_polarity].u_int ? 1 : 0; |
| 123 | + self->phase = args[ARG_phase].u_int ? 1 : 0; |
| 124 | + self->bits = args[ARG_bits].u_int; |
| 125 | + self->firstbit = args[ARG_firstbit].u_int; |
| 126 | + self->sck = args[ARG_sck].u_obj == MP_OBJ_NULL ? -1 : machine_pin_get_id(args[ARG_sck].u_obj); |
| 127 | + self->miso = args[ARG_miso].u_obj == MP_OBJ_NULL ? -1 : machine_pin_get_id(args[ARG_miso].u_obj); |
| 128 | + self->mosi = args[ARG_mosi].u_obj == MP_OBJ_NULL ? -1 : machine_pin_get_id(args[ARG_mosi].u_obj); |
| 129 | + self->deinitialized = false; |
| 130 | + |
| 131 | + spi_bus_config_t buscfg = { |
| 132 | + .miso_io_num = self->miso, |
| 133 | + .mosi_io_num = self->mosi, |
| 134 | + .sclk_io_num = self->sck, |
| 135 | + .quadwp_io_num = -1, |
| 136 | + .quadhd_io_num = -1 |
| 137 | + }; |
| 138 | + spi_device_interface_config_t devcfg = { |
| 139 | + .clock_speed_hz = self->baudrate, |
| 140 | + .mode = self->phase | (self->polarity << 1), |
| 141 | + .spics_io_num = -1, // No CS pin |
| 142 | + .queue_size = 1, |
| 143 | + .flags = self->firstbit == MICROPY_PY_MACHINE_SPI_LSB ? SPI_DEVICE_TXBIT_LSBFIRST | SPI_DEVICE_RXBIT_LSBFIRST : 0, |
| 144 | + .pre_cb = NULL |
| 145 | + }; |
| 146 | + |
| 147 | + //Initialize the SPI bus |
| 148 | + // FIXME: Does the DMA matter? There are two |
| 149 | + ret = spi_bus_initialize(self->host, &buscfg, 1); |
| 150 | + assert(ret == ESP_OK); |
| 151 | + ret = spi_bus_add_device(self->host, &devcfg, &self->spi); |
| 152 | + assert(ret == ESP_OK); |
| 153 | +} |
| 154 | + |
| 155 | +STATIC void machine_hw_spi_deinit(mp_obj_base_t *self_in) { |
| 156 | + machine_hw_spi_obj_t *self = (machine_hw_spi_obj_t*)self_in; |
| 157 | + esp_err_t ret; |
| 158 | + if (!self->deinitialized) { |
| 159 | + self->deinitialized = true; |
| 160 | + ret = spi_bus_remove_device(self->spi); |
| 161 | + assert(ret == ESP_OK); |
| 162 | + ret = spi_bus_free(self->host); |
| 163 | + assert(ret == ESP_OK); |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +mp_obj_t machine_hw_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { |
| 168 | + // args[0] holds the id of the peripheral |
| 169 | + machine_hw_spi_obj_t *self = m_new_obj(machine_hw_spi_obj_t); |
| 170 | + self->base.type = &machine_hw_spi_type; |
| 171 | + // set defaults |
| 172 | + mp_map_t kw_args; |
| 173 | + mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); |
| 174 | + machine_hw_spi_init((mp_obj_base_t*)self, n_args, args, &kw_args); |
| 175 | + return MP_OBJ_FROM_PTR(self); |
| 176 | +} |
| 177 | + |
| 178 | +STATIC const mp_machine_spi_p_t machine_hw_spi_p = { |
| 179 | + .init = machine_hw_spi_init, |
| 180 | + .deinit = machine_hw_spi_deinit, |
| 181 | + .transfer = machine_hw_spi_transfer, |
| 182 | +}; |
| 183 | + |
| 184 | +const mp_obj_type_t machine_hw_spi_type = { |
| 185 | + { &mp_type_type }, |
| 186 | + .name = MP_QSTR_hw_spi, |
| 187 | + .print = machine_hw_spi_print, |
| 188 | + .make_new = machine_hw_spi_make_new, |
| 189 | + .protocol = &machine_hw_spi_p, |
| 190 | + .locals_dict = (mp_obj_dict_t*)&mp_machine_spi_locals_dict, |
| 191 | +}; |
0 commit comments