10000 extmod: Add machine_spi with generic SPI C-protocol and helper methods. · danni/micropython@0823c1b · GitHub
[go: up one dir, main page]

Skip to content

Commit 0823c1b

Browse files
committed
extmod: Add machine_spi with generic SPI C-protocol and helper methods.
The idea is that all ports can use these helper methods and only need to provide initialisation of the SPI bus, as well as a single transfer function. The coding pattern follows the stream protocol and helper methods.
1 parent 9c04ef2 commit 0823c1b

File tree

4 files changed

+132
-0
lines changed

4 files changed

+132
-0
lines changed

extmod/machine_spi.c

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <stdio.h>
28+
29+
#include "py/runtime.h"
30+
#include "extmod/machine_spi.h"
31+
32+
#if MICROPY_PY_MACHINE_SPI
33+
34+
STATIC void mp_machine_spi_transfer(mp_obj_t self, size_t slen, const uint8_t *src, size_t dlen, uint8_t *dest) {
35+
mp_obj_base_t *s = (mp_obj_base_t*)MP_OBJ_TO_PTR(self);
36+
mp_machine_spi_p_t *spi_p = (mp_machine_spi_p_t*)s->type->protocol;
37+
spi_p->transfer(s, slen, src, dlen, dest);
38+
}
39+
40+
STATIC mp_obj_t mp_machine_spi_read(size_t n_args, const mp_obj_t *args) {
41+
uint8_t write_byte = 0;
42+
if (n_args == 3) {
43+
write_byte = mp_obj_get_int(args[2]);
44+
}
45+
vstr_t vstr;
46+
vstr_init_len(&vstr, mp_obj_get_int(args[1]));
47+
mp_machine_spi_transfer(args[0], 1, &write_byte, vstr.len, (uint8_t*)vstr.buf);
48+
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
49+
}
50+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_machine_spi_read_obj, 2, 3, mp_machine_spi_read);
51+
52+
STATIC mp_obj_t mp_machine_spi_readinto(size_t n_args, const mp_obj_t *args) {
53+
mp_buffer_info_t bufinfo;
54+
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);
55+
uint8_t write_byte = 0;
56+
if (n_args == 3) {
57+
write_byte = mp_obj_get_int(args[2]);
58+
}
59+
mp_machine_spi_transfer(args[0], 1, &write_byte, bufinfo.len, (uint8_t*)bufinfo.buf);
60+
return mp_const_none;
61+
}
62+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_machine_spi_readinto_obj, 2, 3, mp_machine_spi_readinto);
63+
64+
STATIC mp_obj_t mp_machine_spi_write(mp_obj_t self, mp_obj_t wr_buf) {
65+
mp_buffer_info_t src;
66+
mp_get_buffer_raise(wr_buf, &src, MP_BUFFER_READ);
67+
mp_machine_spi_transfer(self, src.len, (const uint8_t*)src.buf, 0, NULL);
68+
return mp_const_none;
69+
}
70+
MP_DEFINE_CONST_FUN_OBJ_2(mp_machine_spi_write_obj, mp_machine_spi_write);
71+
72+
STATIC mp_obj_t mp_machine_spi_write_readinto(mp_obj_t self, mp_obj_t wr_buf, mp_obj_t rd_buf) {
73+
mp_buffer_info_t src;
74+
mp_get_buffer_raise(wr_buf, &src, MP_BUFFER_READ);
75+
mp_buffer_info_t dest;
76+
mp_get_buffer_raise(rd_buf, &dest, MP_BUFFER_WRITE);
77+
if (src.len != dest.len) {
78+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "buffers must be the same length"));
79+
}
80+
mp_machine_spi_transfer(self, src.len, (const uint8_t*)src.buf, dest.len, (uint8_t*)dest.buf);
81+
return mp_const_none;
82+
}
83+
MP_DEFINE_CONST_FUN_OBJ_3(mp_machine_spi_write_readinto_obj, mp_machine_spi_write_readinto);
84+
85+
#endif // MICROPY_PY_MACHINE_SPI

extmod/machine_spi.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_EXTMOD_MACHINE_SPI_H
28+
#define MICROPY_INCLUDED_EXTMOD_MACHINE_SPI_H
29+
30+
#include "py/obj.h"
31+
32+
// SPI protocol
33+
typedef struct _mp_machine_spi_p_t {
34+
void (*transfer)(mp_obj_base_t *obj, size_t slen, const uint8_t *src, size_t dlen, uint8_t *dest);
35+
} mp_machine_spi_p_t;
36+
37+
MP_DECLARE_CONST_FUN_OBJ(mp_machine_spi_read_obj);
38+
MP_DECLARE_CONST_FUN_OBJ(mp_machine_spi_readinto_obj);
39+
MP_DECLARE_CONST_FUN_OBJ(mp_machine_spi_write_obj);
40+
MP_DECLARE_CONST_FUN_OBJ(mp_machine_spi_write_readinto_obj);
41+
42+
#endif // MICROPY_INCLUDED_EXTMOD_MACHINE_SPI_H

py/mpconfig.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,10 @@ typedef double mp_float_t;
923923
#define MICROPY_PY_MACHINE_I2C (0)
924924
#endif
925925

926+
#ifndef MICROPY_PY_MACHINE_SPI
927+
#define MICROPY_PY_MACHINE_SPI (0)
928+
#endif
929+
926930
#ifndef MICROPY_PY_USSL
927931
#define MICROPY_PY_USSL (0)
928932
#endif

py/py.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ PY_O_BASENAME = \
208208
../extmod/machine_pinbase.o \
209209
../extmod/machine_pulse.o \
210210
../extmod/machine_i2c.o \
211+
../extmod/machine_spi.o \
211212
../extmod/modussl_axtls.o \
212213
../extmod/modurandom.o \
213214
../extmod/modwebsocket.o \

0 commit comments

Comments
 (0)
0