|
36 | 36 | #include "py/stream.h"
|
37 | 37 | #include "py/mphal.h"
|
38 | 38 | #include "extmod/machine_spi.h"
|
39 |
| - |
| 39 | +#include "modmachine.h" |
40 | 40 | #include "hspi.h"
|
41 | 41 |
|
42 |
| -mp_obj_t pyb_spi_make_new(const mp_obj_type_t *type, size_t n_args, |
43 |
| - size_t n_kw, const mp_obj_t *args); |
44 |
| - |
45 |
| -typedef struct _pyb_hspi_obj_t { |
| 42 | +typedef struct _machine_hspi_obj_t { |
46 | 43 | mp_obj_base_t base;
|
47 | 44 | uint32_t baudrate;
|
48 | 45 | uint8_t polarity;
|
49 | 46 | uint8_t phase;
|
50 |
| -} pyb_hspi_obj_t; |
| 47 | +} machine_hspi_obj_t; |
51 | 48 |
|
52 |
| - |
53 |
| -STATIC void hspi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) { |
| 49 | +STATIC void machine_hspi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) { |
54 | 50 | (void)self_in;
|
55 | 51 |
|
56 | 52 | if (dest == NULL) {
|
@@ -93,16 +89,17 @@ STATIC void hspi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src
|
93 | 89 | /******************************************************************************/
|
94 | 90 | // MicroPython bindings for HSPI
|
95 | 91 |
|
96 |
| -STATIC void pyb_hspi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { |
97 |
| - pyb_hspi_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 92 | +STATIC void machine_hspi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { |
| 93 | + machine_hspi_obj_t *self = MP_OBJ_TO_PTR(self_in); |
98 | 94 | mp_printf(print, "HSPI(id=1, baudrate=%u, polarity=%u, phase=%u)",
|
99 | 95 | self->baudrate, self->polarity, self->phase);
|
100 | 96 | }
|
101 | 97 |
|
102 |
| -STATIC void pyb_hspi_init_helper(pyb_hspi_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
103 |
| - enum { ARG_id, ARG_baudrate, ARG_polarity, ARG_phase }; |
| 98 | +STATIC void machine_hspi_init(mp_obj_base_t *self_in, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 99 | + machine_hspi_obj_t *self = (machine_hspi_obj_t*)self_in; |
| 100 | + |
| 101 | + enum { ARG_baudrate, ARG_polarity, ARG_phase }; |
104 | 102 | static const mp_arg_t allowed_args[] = {
|
105 |
| - { MP_QSTR_id, MP_ARG_INT, {.u_int = -1} }, |
106 | 103 | { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = -1} },
|
107 | 104 | { MP_QSTR_polarity, MP_ARG_INT, {.u_int = -1} },
|
108 | 105 | { MP_QSTR_phase, MP_ARG_INT, {.u_int = -1} },
|
@@ -150,63 +147,35 @@ STATIC void pyb_hspi_init_helper(pyb_hspi_obj_t *self, size_t n_args, const mp_o
|
150 | 147 | spi_mode(HSPI, self->phase, self->polarity);
|
151 | 148 | }
|
152 | 149 |
|
153 |
| -mp_obj_t pyb_hspi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { |
154 |
| - mp_arg_check_num(n_args, n_kw, 0, 1, true); |
155 |
| - mp_int_t id = -1; |
156 |
| - if (n_args > 0) { |
157 |
| - id = mp_obj_get_int(args[0]); |
158 |
| - } |
159 |
| - |
160 |
| - if (id == -1) { |
161 |
| - // Multiplex to bitbanging SPI |
162 |
| - if (n_args > 0) { |
163 |
| - args++; |
164 |
| - } |
165 |
| - return pyb_spi_make_new(type, 0, n_kw, args); |
166 |
| - } |
167 |
| - |
168 |
| - if (id != 1) { |
| 150 | +mp_obj_t machine_hspi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { |
| 151 | + // args[0] holds the id of the peripheral |
| 152 | + if (args[0] != MP_OBJ_NEW_SMALL_INT(1)) { |
169 | 153 | // FlashROM is on SPI0, so far we don't support its usage
|
170 | 154 | mp_raise_ValueError("");
|
171 | 155 | }
|
172 | 156 |
|
173 |
| - pyb_hspi_obj_t *self = m_new_obj(pyb_hspi_obj_t); |
174 |
| - self->base.type = &pyb_hspi_type; |
| 157 | + machine_hspi_obj_t *self = m_new_obj(machine_hspi_obj_t); |
| 158 | + self->base.type = &machine_hspi_type; |
175 | 159 | // set defaults
|
176 | 160 | self->baudrate = 80000000L;
|
177 | 161 | self->polarity = 0;
|
178 | 162 | self->phase = 0;
|
179 | 163 | mp_map_t kw_args;
|
180 | 164 | mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
|
181 |
| - pyb_hspi_init_helper(self, n_args, args, &kw_args); |
| 165 | + machine_hspi_init((mp_obj_base_t*)self, n_args - 1, args + 1, &kw_args); |
182 | 166 | return MP_OBJ_FROM_PTR(self);
|
183 | 167 | }
|
184 | 168 |
|
185 |
| -STATIC mp_obj_t pyb_hspi_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) { |
186 |
| - pyb_hspi_init_helper(args[0], n_args - 1, args + 1, kw_args); |
187 |
| - return mp_const_none; |
188 |
| -} |
189 |
| -MP_DEFINE_CONST_FUN_OBJ_KW(pyb_hspi_init_obj, 1, pyb_hspi_init); |
190 |
| - |
191 |
| -STATIC const mp_rom_map_elem_t pyb_hspi_locals_dict_table[] = { |
192 |
| - { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&pyb_hspi_init_obj) }, |
193 |
| - { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_machine_spi_read_obj) }, |
194 |
| - { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_machine_spi_readinto_obj) }, |
195 |
| - { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_machine_spi_write_obj) }, |
196 |
| - { MP_ROM_QSTR(MP_QSTR_write_readinto), MP_ROM_PTR(&mp_machine_spi_write_readinto_obj) }, |
197 |
| -}; |
198 |
| - |
199 |
| -STATIC MP_DEFINE_CONST_DICT(pyb_hspi_locals_dict, pyb_hspi_locals_dict_table); |
200 |
| - |
201 |
| -STATIC const mp_machine_spi_p_t pyb_hspi_p = { |
202 |
| - .transfer = hspi_transfer, |
| 169 | +STATIC const mp_machine_spi_p_t machine_hspi_p = { |
| 170 | + .init = machine_hspi_init, |
| 171 | + .transfer = machine_hspi_transfer, |
203 | 172 | };
|
204 | 173 |
|
205 |
| -const mp_obj_type_t pyb_hspi_type = { |
| 174 | +const mp_obj_type_t machine_hspi_type = { |
206 | 175 | { &mp_type_type },
|
207 | 176 | .name = MP_QSTR_HSPI,
|
208 |
| - .print = pyb_hspi_print, |
209 |
| - .make_new = pyb_hspi_make_new, |
210 |
| - .protocol = &pyb_hspi_p, |
211 |
| - .locals_dict = (mp_obj_dict_t*)&pyb_hspi_locals_dict, |
| 177 | + .print = machine_hspi_print, |
| 178 | + .make_new = mp_machine_spi_make_new, // delegate to master constructor |
| 179 | + .protocol = &machine_hspi_p, |
| 180 | + .locals_dict = (mp_obj_dict_t*)&mp_machine_spi_locals_dict, |
212 | 181 | };
|
0 commit comments