10000 stmhal/spi: Support new machine SPI methods in legacy SPI object. · danni/micropython@49406b0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 49406b0

Browse files
committed
stmhal/spi: Support new machine SPI methods in legacy SPI object.
1 parent 9b64d19 commit 49406b0

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

stmhal/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
#define MICROPY_PY_UHASHLIB (1)
9494
#define MICROPY_PY_MACHINE (1)
9595
#define MICROPY_PY_MACHINE_I2C (1)
96+
#define MICROPY_PY_MACHINE_SPI (1)
9697
#define MICROPY_PY_FRAMEBUF (1)
9798

9899
#define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1)

stmhal/spi.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "py/nlr.h"
3131
#include "py/runtime.h"
3232
#include "py/mphal.h"
33+
#include "extmod/machine_spi.h"
3334
#include "irq.h"
3435
#include "pin.h"
3536
#include "genhdr/pins.h"
@@ -400,6 +401,17 @@ STATIC void spi_transfer(mp_obj_base_t *self_in, size_t src_len, const uint8_t *
400401
}
401402
}
402403

404+
STATIC void spi_transfer_machine(mp_obj_base_t *self_in, size_t src_len, const uint8_t *src_buf, size_t dest_len, uint8_t *dest_buf) {
405+
if (src_len == 1 && dest_len > 1) {
406+
// this catches read and readinto
407+
// copy the single output byte to the dest buffer and use that as source
408+
memset(dest_buf, src_buf[0], dest_len);
409+
src_len = dest_len;
410+
src_buf = dest_buf;
411+
}
412+
spi_transfer(self_in, src_len, src_buf, dest_len, dest_buf, 100);
413+
}
414+
403415
/******************************************************************************/
404416
/* Micro Python bindings */
405417

@@ -748,6 +760,13 @@ STATIC const mp_map_elem_t pyb_spi_locals_dict_table[] = {
748760
// instance methods
749761
{ MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&pyb_spi_init_obj },
750762
{ MP_OBJ_NEW_QSTR(MP_QSTR_deinit), (mp_obj_t)&pyb_spi_deinit_obj },
763+
764+
{ MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&mp_machine_spi_read_obj },
765+
{ MP_OBJ_NEW_QSTR(MP_QSTR_readinto), (mp_obj_t)&mp_machine_spi_readinto_obj },
766+
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_machine_spi_write_obj },
767+
{ MP_OBJ_NEW_QSTR(MP_QSTR_write_readinto), (mp_obj_t)&mp_machine_spi_write_readinto_obj },
768+
769+
// legacy methods
751770
{ MP_OBJ_NEW_QSTR(MP_QSTR_send), (mp_obj_t)&pyb_spi_send_obj },
752771
{ MP_OBJ_NEW_QSTR(MP_QSTR_recv), (mp_obj_t)&pyb_spi_recv_obj },
753772
{ MP_OBJ_NEW_QSTR(MP_QSTR_send_recv), (mp_obj_t)&pyb_spi_send_recv_obj },
@@ -773,10 +792,15 @@ STATIC const mp_map_elem_t pyb_spi_locals_dict_table[] = {
773792

774793
STATIC MP_DEFINE_CONST_DICT(pyb_spi_locals_dict, pyb_spi_locals_dict_table);
775794

795+
STATIC const mp_machine_spi_p_t pyb_spi_p = {
796+
.transfer = spi_transfer_machine,
797+
};
798+
776799
const mp_obj_type_t pyb_spi_type = {
777800
{ &mp_type_type },
778801
.name = MP_QSTR_SPI,
779802
.print = pyb_spi_print,
780803
.make_new = pyb_spi_make_new,
804+
.protocol = &pyb_spi_p,
781805
.locals_dict = (mp_obj_t)&pyb_spi_locals_dict,
782806
};

0 commit comments

Comments
 (0)
0