Skip to content

Commit cd35dd9

Browse files
committed
py: Allow to pass in read-only buffers to viper and inline-asm funcs.
Fixes adafruit#4936.
1 parent 2d3d4f7 commit cd35dd9

File tree

6 files changed

+18
-2
lines changed

6 files changed

+18
-2
lines changed

py/nativeglue.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ mp_uint_t mp_native_from_obj(mp_obj_t obj, mp_uint_t type) {
6565
case MP_NATIVE_TYPE_UINT: return mp_obj_get_int_truncated(obj);
6666
default: { // cast obj to a pointer
6767
mp_buffer_info_t bufinfo;
68-
if (mp_get_buffer(obj, &bufinfo, MP_BUFFER_RW)) {
68+
if (mp_get_buffer(obj, &bufinfo, MP_BUFFER_READ)) {
6969
return (mp_uint_t)bufinfo.buf;
7070
} else {
7171
// assume obj is an integer that represents an address

py/objfun.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ STATIC mp_uint_t convert_obj_for_inline_asm(mp_obj_t obj) {
472472
return (mp_uint_t)items;
473473
} else {
474474
mp_buffer_info_t bufinfo;
475-
if (mp_get_buffer(obj, &bufinfo, MP_BUFFER_WRITE)) {
475+
if (mp_get_buffer(obj, &bufinfo, MP_BUFFER_READ)) {
476476
// supports the buffer protocol, return a pointer to the data
477477
return (mp_uint_t)bufinfo.buf;
478478
} else {

tests/inlineasm/asmsum.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,7 @@ def asm_sum_bytes(r0, r1):
5555
b = array.array('b', (10, 20, 30, 40, 50, 60, 70, 80))
5656
n = asm_sum_bytes(len(b), b)
5757
print(b, n)
58+
59+
b = b'\x01\x02\x03\x04'
60+
n = asm_sum_bytes(len(b), b)
61+
print(b, n)

tests/inlineasm/asmsum.py.exp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
array('l', [100, 200, 300, 400]) 1000
22
array('b', [10, 20, 30, 40, 50, 60, 70, 80]) 360
3+
b'\x01\x02\x03\x04' 10

tests/micropython/viper_addr.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ def memset(dest:ptr8, c:int, n:int):
99
for i in range(n):
1010
dest[i] = c
1111

12+
@micropython.viper
13+
def memsum(src:ptr8, n:int) -> int:
14+
s = 0
15+
for i in range(n):
16+
s += src[i]
17+
return s
18+
1219
# create array and get its address
1320
ar = bytearray('0000')
1421
addr = get_addr(ar)
@@ -27,3 +34,6 @@ def memset(dest:ptr8, c:int, n:int):
2734
# pass direct pointer to array buffer, with offset
2835
memset(addr + 2, ord('3'), len(ar) - 2)
2936
print(ar)
37+
38+
# pass a read-only bytes object in
39+
print(memsum(b'\x01\x02\x03\x04', 4))

tests/micropython/viper_addr.py.exp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ bytearray(b'0000')
44
bytearray(b'1111')
55
bytearray(b'2222')
66
bytearray(b'2233')
7+
10

0 commit comments

Comments
 (0)