@@ -192,7 +192,6 @@ static mp_obj_t machine_pin_high(mp_obj_t self_in) {
192192}
193193static MP_DEFINE_CONST_FUN_OBJ_1 (machine_pin_high_obj , machine_pin_high ) ;
194194
195- #if 0
196195///// SPI /////
197196
198197typedef struct machine_spi_obj {
@@ -202,11 +201,11 @@ typedef struct machine_spi_obj {
202201static machine_spi_obj_t machine_spi_obj = {{& machine_spi_type }, .use_dc = false};
203202
204203mp_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 ) {
205- enum { ARG_divisor , ARG_read_latency , ARG_use_cs , ARG_use_dc };
204+ enum { ARG_divisor , ARG_read_latency , ARG_cs_pin , ARG_use_dc };
206205 static const mp_arg_t allowed_args [] = {
207206 { MP_QSTR_divisor , MP_ARG_INT , {.u_int = 4 } },
208207 { MP_QSTR_read_latency , MP_ARG_INT , {.u_int = 0 } },
209- { MP_QSTR_use_cs , MP_ARG_BOOL , {.u_bool = true } },
208+ { MP_QSTR_cs_pin , MP_ARG_OBJ , {.u_rom_obj = MP_ROM_NONE } },
210209 { MP_QSTR_use_dc , MP_ARG_BOOL , {.u_bool = false} },
211210 };
212211
@@ -218,21 +217,25 @@ mp_obj_t machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n
218217 machine_spi_obj_t * self = (machine_spi_obj_t * )& machine_spi_obj ;
219218
220219 // Initialise the SPI peripheral
221- int spi_config = (args [ARG_divisor ].u_int >> 2 ) - 1 ;
220+ int spi_config = (args [ARG_divisor ].u_int >> 1 ) - 1 ;
222221 if (spi_config < 0 ) spi_config = 0 ;
223- if (spi_config > 3 ) spi_config = 3 ;
224- if (args [ARG_read_latency ].u_int != 0 ) spi_config |= 4 ;
222+ if (spi_config > 127 ) spi_config = 127 ;
223+ if (args [ARG_read_latency ].u_int != 0 ) spi_config |= 0x80 ;
225224 spi_set_config (spi_config );
226225
227226 // Determine which pins must be selected away from GPIO use
228- int spi_pins = 0x28 ;
229- if (args [ARG_use_cs ].u_bool ) spi_pins |= 0x10 ;
230- if (args [ARG_use_dc ].u_bool ) spi_pins |= 0x04 ;
227+ set_gpio_func (3 , 30 ); // SPI MOSI
228+ set_gpio_func (5 , 30 ); // SPI SCK
231229 self -> use_dc = args [ARG_use_dc ].u_bool ;
230+ if (self -> use_dc ) {
231+ set_gpio_func (2 , 30 );
232+ }
232233
233- int sel = get_gpio_sel ();
234- sel &= ~spi_pins ;
235- set_gpio_sel (sel );
234+ if (args [ARG_cs_pin ].u_obj != mp_const_none )
235+ {
236+ machine_pin_obj_t * cs_pin = machine_pin_find (args [ARG_cs_pin ].u_obj );
237+ set_gpio_func (cs_pin -> id , 30 );
238+ }
236239
237240 return MP_OBJ_FROM_PTR (self );
238241}
@@ -244,11 +247,11 @@ static void machine_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_prin
244247}
245248
246249static 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 ) {
247- enum { ARG_divisor , ARG_read_latency , ARG_use_cs , ARG_use_dc };
250+ enum { ARG_divisor , ARG_read_latency , ARG_cs_pin , ARG_use_dc };
248251 static const mp_arg_t allowed_args [] = {
249252 { MP_QSTR_divisor , MP_ARG_INT , {.u_int = 4 } },
250253 { MP_QSTR_read_latency , MP_ARG_INT , {.u_int = 0 } },
251- { MP_QSTR_use_cs , MP_ARG_BOOL , {.u_bool = true } },
254+ { MP_QSTR_cs_pin , MP_ARG_OBJ , {.u_rom_obj = MP_ROM_NONE } },
252255 { MP_QSTR_use_dc , MP_ARG_BOOL , {.u_bool = false} },
253256 };
254257
@@ -258,21 +261,25 @@ static void machine_spi_init(mp_obj_base_t *self_in, size_t n_args, const mp_obj
258261 mp_arg_parse_all (n_args , pos_args , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
259262
260263 // Initialise the SPI peripheral
261- int spi_config = (args [ARG_divisor ].u_int >> 2 ) - 1 ;
264+ int spi_config = (args [ARG_divisor ].u_int >> 1 ) - 1 ;
262265 if (spi_config < 0 ) spi_config = 0 ;
263- if (spi_config > 3 ) spi_config = 3 ;
264- if (args [ARG_read_latency ].u_int != 0 ) spi_config |= 4 ;
266+ if (spi_config > 127 ) spi_config = 127 ;
267+ if (args [ARG_read_latency ].u_int != 0 ) spi_config |= 0x80 ;
265268 spi_set_config (spi_config );
266269
267270 // Determine which pins must be selected away from GPIO use
268- int spi_pins = 0x28 ;
269- if (args [ARG_use_cs ].u_bool ) spi_pins |= 0x10 ;
270- if (args [ARG_use_dc ].u_bool ) spi_pins |= 0x04 ;
271+ set_gpio_func (3 , 30 ); // SPI MOSI
272+ set_gpio_func (5 , 30 ); // SPI SCK
271273 self -> use_dc = args [ARG_use_dc ].u_bool ;
274+ if (self -> use_dc ) {
275+ set_gpio_func (2 , 30 );
276+ }
272277
273- int sel = get_gpio_sel ();
274- sel &= ~spi_pins ;
275- set_gpio_sel (sel );
278+ if (args [ARG_cs_pin ].u_obj != mp_const_none )
279+ {
280+ machine_pin_obj_t * cs_pin = machine_pin_find (args [ARG_cs_pin ].u_obj );
281+ set_gpio_func (cs_pin -> id , 30 );
282+ }
276283}
277284
278285static void machine_spi_transfer (mp_obj_base_t * self_in , size_t len , const uint8_t * src , uint8_t * dest ) {
@@ -314,7 +321,6 @@ mp_obj_base_t *mp_hal_get_spi_obj(mp_obj_t o) {
314321 mp_raise_TypeError (MP_ERROR_TEXT ("expecting an SPI object" ));
315322 }
316323}
317- #endif
318324
319325
320326static const mp_rom_map_elem_t machine_pin_locals_dict_table [] = {
0 commit comments