|
| 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 | +#include <errno.h> |
| 31 | + |
| 32 | +#include "ets_sys.h" |
| 33 | +#include "etshal.h" |
| 34 | +#include "ets_alt_task.h" |
| 35 | + |
| 36 | +#include "py/runtime.h" |
| 37 | +#include "py/stream.h" |
| 38 | +#include "modpyb.h" |
| 39 | + |
| 40 | +typedef struct _pyb_spi_obj_t { |
| 41 | + mp_obj_base_t base; |
| 42 | + uint32_t baudrate; |
| 43 | + uint8_t polarity; |
| 44 | + uint8_t phase; |
| 45 | + pyb_pin_obj_t *sck; |
| 46 | + pyb_pin_obj_t *mosi; |
| 47 | + pyb_pin_obj_t *miso; |
| 48 | +} pyb_spi_obj_t; |
| 49 | + |
| 50 | +STATIC void mp_hal_spi_transfer(pyb_spi_obj_t *self, size_t src_len, const uint8_t *src_buf, size_t dest_len, uint8_t *dest_buf) { |
| 51 | + // only MSB transfer is implemented |
| 52 | + uint32_t delay_half = 500000 / self->baudrate + 1; |
| 53 | + for (size_t i = 0; i < src_len || i < dest_len; ++i) { |
| 54 | + uint8_t data_out; |
| 55 | + if (src_len == 1) { |
| 56 | + data_out = src_buf[0]; |
| 57 | + } else { |
| 58 | + data_out = src_buf[i]; |
| 59 | + } |
| 60 | + uint8_t data_in = 0; |
| 61 | + for (int j = 0; j < 8; ++j, data_out <<= 1) { |
| 62 | + pin_set(self->mosi->phys_port, (data_out >> 7) & 1); |
| 63 | + if (self->phase == 0) { |
| 64 | + ets_delay_us(delay_half); |
| 65 | + pin_set(self->sck->phys_port, 1 - self->polarity); |
| 66 | + } else { |
| 67 | + pin_set(self->sck->phys_port, 1 - self->polarity); |
| 68 | + ets_delay_us(delay_half); |
| 69 | + } |
| 70 | + data_in = (data_in << 1) | pin_get(self->miso->phys_port); |
| 71 | + if (self->phase == 0) { |
| 72 | + ets_delay_us(delay_half); |
| 73 | + pin_set(self->sck->phys_port, self->polarity); |
| 74 | + } else { |
| 75 | + pin_set(self->sck->phys_port, self->polarity); |
| 76 | + ets_delay_us(delay_half); |
| 77 | + } |
| 78 | + } |
| 79 | + if (dest_len != 0) { |
| 80 | + dest_buf[i] = data_in; |
| 81 | + } |
| 82 | + // make sure pending tasks have a chance to run |
| 83 | + ets_loop_iter(); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/******************************************************************************/ |
| 88 | +// MicroPython bindings for SPI |
| 89 | + |
| 90 | +STATIC void pyb_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { |
| 91 | + pyb_spi_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 92 | + mp_printf(print, "SPI(baudrate=%u, polarity=%u, phase=%u, sck=%u, mosi=%u, miso=%u)", |
| 93 | + self->baudrate, self->polarity, self->phase, self->sck->phys_port, self->mosi->phys_port, self->miso->phys_port); |
| 94 | +} |
| 95 | + |
| 96 | +STATIC void pyb_spi_init_helper(pyb_spi_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 97 | + enum { ARG_baudrate, ARG_polarity, ARG_phase, ARG_sck, ARG_mosi, ARG_miso }; |
| 98 | + static const mp_arg_t allowed_args[] = { |
| 99 | + { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = -1} }, |
| 100 | + { MP_QSTR_polarity, MP_ARG_INT, {.u_int = -1} }, |
| 101 | + { MP_QSTR_phase, MP_ARG_INT, {.u_int = -1} }, |
| 102 | + { MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, |
| 103 | + { MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, |
| 104 | + { MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, |
| 105 | + }; |
| 106 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 107 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 108 | + |
| 109 | + if (args[ARG_baudrate].u_int != -1) { |
| 110 | + self->baudrate = args[ARG_baudrate].u_int; |
| 111 | + } |
| 112 | + if (args[ARG_polarity].u_int != -1) { |
| 113 | + self->polarity = args[ARG_polarity].u_int; |
| 114 | + } |
| 115 | + if (args[ARG_phase].u_int != -1) { |
| 116 | + self->phase = args[ARG_phase].u_int; |
| 117 | + } |
| 118 | + if (args[ARG_sck].u_obj != MP_OBJ_NULL) { |
| 119 | + self->sck = mp_obj_get_pin_obj(args[ARG_sck].u_obj); |
| 120 | + } |
| 121 | + if (args[ARG_mosi].u_obj != MP_OBJ_NULL) { |
| 122 | + self->mosi = mp_obj_get_pin_obj(args[ARG_mosi].u_obj); |
| 123 | + } |
| 124 | + if (args[ARG_miso].u_obj != MP_OBJ_NULL) { |
| 125 | + self->miso = mp_obj_get_pin_obj(args[ARG_miso].u_obj); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +STATIC mp_obj_t pyb_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { |
| 130 | + mp_arg_check_num(n_args, n_kw, 0, MP_OBJ_FUN_ARGS_MAX, true); |
| 131 | + pyb_spi_obj_t *self = m_new_obj(pyb_spi_obj_t); |
| 132 | + self->base.type = &pyb_spi_type; |
| 133 | + // set defaults |
| 134 | + self->baudrate = 500000; |
| 135 | + self->polarity = 0; |
| 136 | + self->phase = 0; |
| 137 | + self->sck = NULL; |
| 138 | + self->mosi = NULL; |
| 139 | + self->miso = NULL; |
| 140 | + mp_map_t kw_args; |
| 141 | + mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); |
| 142 | + pyb_spi_init_helper(self, n_args, args, &kw_args); |
| 143 | + return MP_OBJ_FROM_PTR(self); |
| 144 | +} |
| 145 | + |
| 146 | +STATIC mp_obj_t pyb_spi_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) { |
| 147 | + pyb_spi_init_helper(args[0], n_args - 1, args + 1, kw_args); |
| 148 | + return mp_const_none; |
| 149 | +} |
| 150 | +MP_DEFINE_CONST_FUN_OBJ_KW(pyb_spi_init_obj, 1, pyb_spi_init); |
| 151 | + |
| 152 | +STATIC mp_obj_t pyb_spi_read(size_t n_args, const mp_obj_t *args) { |
| 153 | + pyb_spi_obj_t *self = MP_OBJ_TO_PTR(args[0]); |
| 154 | + uint8_t write_byte = 0; |
| 155 | + if (n_args == 3) { |
| 156 | + write_byte = mp_obj_get_int(args[2]); |
| 157 | + } |
| 158 | + vstr_t vstr; |
| 159 | + vstr_init_len(&vstr, mp_obj_get_int(args[1])); |
| 160 | + mp_hal_spi_transfer(self, 1, &write_byte, vstr.len, (uint8_t*)vstr.buf); |
| 161 | + return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); |
| 162 | +} |
| 163 | +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_spi_read_obj, 2, 3, pyb_spi_read); |
| 164 | + |
| 165 | +STATIC mp_obj_t pyb_spi_readinto(size_t n_args, const mp_obj_t *args) { |
| 166 | + pyb_spi_obj_t *self = MP_OBJ_TO_PTR(args[0]); |
| 167 | + mp_buffer_info_t bufinfo; |
| 168 | + mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE); |
| 169 | + uint8_t write_byte = 0; |
| 170 | + if (n_args == 3) { |
| 171 | + write_byte = mp_obj_get_int(args[2]); |
| 172 | + } |
| 173 | + mp_hal_spi_transfer(self, 1, &write_byte, bufinfo.len, (uint8_t*)bufinfo.buf); |
| 174 | + return mp_const_none; |
| 175 | +} |
| 176 | +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_spi_readinto_obj, 2, 3, pyb_spi_readinto); |
| 177 | + |
| 178 | +STATIC mp_obj_t pyb_spi_write(mp_obj_t self_in, mp_obj_t wr_buf_in) { |
| 179 | + pyb_spi_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 180 | + mp_buffer_info_t src_buf; |
| 181 | + mp_get_buffer_raise(wr_buf_in, &src_buf, MP_BUFFER_READ); |
| 182 | + mp_hal_spi_transfer(self, src_buf.len, (const uint8_t*)src_buf.buf, 0, NULL); |
| 183 | + return mp_const_none; |
| 184 | +} |
| 185 | +MP_DEFINE_CONST_FUN_OBJ_2(pyb_spi_write_obj, pyb_spi_write); |
| 186 | + |
| 187 | +STATIC mp_obj_t pyb_spi_write_readinto(mp_obj_t self_in, mp_obj_t wr_buf_in, mp_obj_t rd_buf_in) { |
| 188 | + pyb_spi_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 189 | + mp_buffer_info_t src_buf; |
| 190 | + mp_get_buffer_raise(wr_buf_in, &src_buf, MP_BUFFER_READ); |
| 191 | + mp_buffer_info_t dest_buf; |
| 192 | + mp_get_buffer_raise(rd_buf_in, &dest_buf, MP_BUFFER_WRITE); |
| 193 | + if (src_buf.len != dest_buf.len) { |
| 194 | + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "buffers must be the same length")); |
| 195 | + } |
| 196 | + mp_hal_spi_transfer(self, src_buf.len, (const uint8_t*)src_buf.buf, dest_buf.len, (uint8_t*)dest_buf.buf); |
| 197 | + return mp_const_none; |
| 198 | +} |
| 199 | +MP_DEFINE_CONST_FUN_OBJ_3(pyb_spi_write_readinto_obj, pyb_spi_write_readinto); |
| 200 | + |
| 201 | +STATIC const mp_rom_map_elem_t pyb_spi_locals_dict_table[] = { |
| 202 | + { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&pyb_spi_init_obj) }, |
| 203 | + { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&pyb_spi_read_obj) }, |
| 204 | + { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&pyb_spi_readinto_obj) }, |
| 205 | + { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&pyb_spi_write_obj) }, |
| 206 | + { MP_ROM_QSTR(MP_QSTR_write_readinto), MP_ROM_PTR(&pyb_spi_write_readinto_obj) }, |
| 207 | +}; |
| 208 | + |
| 209 | +STATIC MP_DEFINE_CONST_DICT(pyb_spi_locals_dict, pyb_spi_locals_dict_table); |
| 210 | + |
| 211 | +const mp_obj_type_t pyb_spi_type = { |
| 212 | + { &mp_type_type }, |
| 213 | + .name = MP_QSTR_SPI, |
| 214 | + .print = pyb_spi_print, |
| 215 | + .make_new = pyb_spi_make_new, |
| 216 | + .locals_dict = (mp_obj_dict_t*)&pyb_spi_locals_dict, |
| 217 | +}; |
0 commit comments