|
28 | 28 | #include <stdint.h> |
29 | 29 | #include <string.h> |
30 | 30 |
|
31 | | -#include "ets_sys.h" |
32 | | -#include "etshal.h" |
33 | | -#include "ets_alt_task.h" |
34 | | - |
35 | 31 | #include "py/runtime.h" |
36 | 32 | #include "py/stream.h" |
37 | 33 | #include "py/mphal.h" |
38 | 34 | #include "extmod/machine_spi.h" |
39 | 35 |
|
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 | | - mp_hal_pin_obj_t sck; |
46 | | - mp_hal_pin_obj_t mosi; |
47 | | - mp_hal_pin_obj_t miso; |
48 | | -} pyb_spi_obj_t; |
49 | | - |
50 | | -STATIC void mp_hal_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) { |
51 | | - pyb_spi_obj_t *self = (pyb_spi_obj_t*)self_in; |
52 | | - // only MSB transfer is implemented |
53 | | - uint32_t delay_half = 500000 / self->baudrate + 1; |
54 | | - for (size_t i = 0; i < len; ++i) { |
55 | | - uint8_t data_out = src[i]; |
56 | | - uint8_t data_in = 0; |
57 | | - for (int j = 0; j < 8; ++j, data_out <<= 1) { |
58 | | - mp_hal_pin_write(self->mosi, (data_out >> 7) & 1); |
59 | | - if (self->phase == 0) { |
60 | | - ets_delay_us(delay_half); |
61 | | - mp_hal_pin_write(self->sck, 1 - self->polarity); |
62 | | - } else { |
63 | | - mp_hal_pin_write(self->sck, 1 - self->polarity); |
64 | | - ets_delay_us(delay_half); |
65 | | - } |
66 | | - data_in = (data_in << 1) | mp_hal_pin_read(self->miso); |
67 | | - if (self->phase == 0) { |
68 | | - ets_delay_us(delay_half); |
69 | | - mp_hal_pin_write(self->sck, self->polarity); |
70 | | - } else { |
71 | | - mp_hal_pin_write(self->sck, self->polarity); |
72 | | - ets_delay_us(delay_half); |
73 | | - } |
74 | | - } |
75 | | - if (dest != NULL) { |
76 | | - dest[i] = data_in; |
77 | | - } |
78 | | - // make sure pending tasks have a chance to run |
79 | | - ets_loop_iter(); |
80 | | - } |
81 | | -} |
82 | | - |
83 | 36 | /******************************************************************************/ |
84 | 37 | // MicroPython bindings for SPI |
85 | 38 |
|
86 | 39 | STATIC void pyb_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { |
87 | | - pyb_spi_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 40 | + mp_machine_soft_spi_obj_t *self = MP_OBJ_TO_PTR(self_in); |
88 | 41 | mp_printf(print, "SPI(baudrate=%u, polarity=%u, phase=%u, sck=%u, mosi=%u, miso=%u)", |
89 | 42 | self->baudrate, self->polarity, self->phase, self->sck, self->mosi, self->miso); |
90 | 43 | } |
91 | 44 |
|
92 | | -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) { |
| 45 | +STATIC void pyb_spi_init_helper(mp_machine_soft_spi_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
93 | 46 | enum { ARG_baudrate, ARG_polarity, ARG_phase, ARG_sck, ARG_mosi, ARG_miso }; |
94 | 47 | static const mp_arg_t allowed_args[] = { |
95 | 48 | { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = -1} }, |
@@ -130,7 +83,7 @@ STATIC void pyb_spi_init_helper(pyb_spi_obj_t *self, size_t n_args, const mp_obj |
130 | 83 |
|
131 | 84 | 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) { |
132 | 85 | mp_arg_check_num(n_args, n_kw, 0, MP_OBJ_FUN_ARGS_MAX, true); |
133 | | - pyb_spi_obj_t *self = m_new_obj(pyb_spi_obj_t); |
| 86 | + mp_machine_soft_spi_obj_t *self = m_new_obj(mp_machine_soft_spi_obj_t); |
134 | 87 | self->base.type = &pyb_spi_type; |
135 | 88 | // set defaults |
136 | 89 | self->baudrate = 500000; |
@@ -162,7 +115,7 @@ STATIC const mp_rom_map_elem_t pyb_spi_locals_dict_table[] = { |
162 | 115 | STATIC MP_DEFINE_CONST_DICT(pyb_spi_locals_dict, pyb_spi_locals_dict_table); |
163 | 116 |
|
164 | 117 | STATIC const mp_machine_spi_p_t pyb_spi_p = { |
165 | | - .transfer = mp_hal_spi_transfer, |
| 118 | + .transfer = mp_machine_soft_spi_transfer, |
166 | 119 | }; |
167 | 120 |
|
168 | 121 | const mp_obj_type_t pyb_spi_type = { |
|
0 commit comments