Skip to content

Commit 9874593

Browse files
committed
ports/tinyQV: Reinstate SPI.
1 parent 62a5067 commit 9874593

File tree

5 files changed

+41
-37
lines changed

5 files changed

+41
-37
lines changed

ports/tinyQV/modmachine.c

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@ static mp_obj_t machine_pin_high(mp_obj_t self_in) {
192192
}
193193
static MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_high_obj, machine_pin_high);
194194

195-
#if 0
196195
///// SPI /////
197196

198197
typedef struct machine_spi_obj {
@@ -202,11 +201,11 @@ typedef struct machine_spi_obj {
202201
static machine_spi_obj_t machine_spi_obj = {{&machine_spi_type}, .use_dc=false};
203202

204203
mp_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

246249
static 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

278285
static 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

320326
static const mp_rom_map_elem_t machine_pin_locals_dict_table[] = {

ports/tinyQV/modules/_boot.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,3 @@
1414
fs = vfs.VfsLfs2(psram, progsize=64)
1515

1616
vfs.mount(fs, '/')
17-
18-
# Hack for ECP5 testing at 56MHz - set the UART divider for 115200
19-
import machine
20-
machine.mem32[0x800_0088] = 486 # 555 // 2

ports/tinyQV/modules/sd.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
class SDCard:
2222
def __init__(self, cs, divider=6):
23-
self.spi = SPI(divisor=divider, use_cs=False)
23+
self.spi = SPI(divisor=divider)
2424
self.cs = cs
2525

2626
self.cmdbuf = bytearray(6)
@@ -34,14 +34,14 @@ def __init__(self, cs, divider=6):
3434
self.init_card(divider)
3535

3636
def init_spi(self, divider):
37-
self.spi.init(divisor=divider, use_cs=False)
37+
self.spi.init(divisor=divider)
3838

3939
def init_card(self, divider):
4040
# init CS pin
4141
self.cs.init(self.cs.OUT, value=1)
4242

4343
# init SPI bus; use low data rate for initialisation
44-
self.init_spi(8)
44+
self.init_spi(32)
4545

4646
# clock card at least 100 cycles with cs high
4747
for i in range(16):

ports/tinyQV/mpconfigport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ typedef long mp_off_t;
5353
#define MICROPY_PY_MICROPYTHON_MEM_INFO (1)
5454
#define MICROPY_PY_SYS_MAXSIZE (1)
5555
#define MICROPY_PY_MACHINE (1)
56-
#define MICROPY_PY_MACHINE_SPI (0)
56+
#define MICROPY_PY_MACHINE_SPI (1)
5757
#define MICROPY_PY_MACHINE_INCLUDEFILE "ports/tinyQV/modmachine.c"
5858

5959
#define MICROPY_KBD_EXCEPTION (1)

ports/tinyQV/mphalport.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <csr.h>
2+
#include <timer.h>
23
#include <uart.h>
34
#include <py/runtime.h>
45

@@ -8,24 +9,25 @@ static inline void mp_hal_delay_us(mp_uint_t us) {
89
}
910

1011
static inline void mp_hal_delay_ms(mp_uint_t ms) {
11-
uint32_t endtime = read_time() + ms * 1000;
12+
// Use mtime rather than read_time as mtime might be adjusted to the actual clock speed.
13+
uint32_t endtime = get_mtime() + ms * 1000;
1214
int32_t timediff;
1315
do {
1416
mp_event_handle_nowait();
1517
delay_us(100);
16-
timediff = endtime - read_time();
18+
timediff = endtime - get_mtime();
1719
} while (timediff > 0);
1820
}
1921

2022
static inline mp_uint_t mp_hal_ticks_us(void) {
21-
return read_time();
23+
return get_mtime();
2224
}
2325

2426
static inline mp_uint_t mp_hal_ticks_ms(void) {
25-
//return read_time() / 1000;
27+
//return get_mtime() / 1000;
2628

2729
// Faster divide by constant
28-
uint64_t t = read_time();
30+
uint64_t t = get_mtime();
2931
t *= 274877907;
3032
return (mp_uint_t)(t >> 38);
3133
}

0 commit comments

Comments
 (0)