py: Allow to pass in read-only buffers to viper and inline-asm funcs.
Fixes #4936.
This commit is contained in:
parent
2d3d4f7483
commit
cd35dd9d9a
@ -65,7 +65,7 @@ mp_uint_t mp_native_from_obj(mp_obj_t obj, mp_uint_t type) {
|
|||||||
case MP_NATIVE_TYPE_UINT: return mp_obj_get_int_truncated(obj);
|
case MP_NATIVE_TYPE_UINT: return mp_obj_get_int_truncated(obj);
|
||||||
default: { // cast obj to a pointer
|
default: { // cast obj to a pointer
|
||||||
mp_buffer_info_t bufinfo;
|
mp_buffer_info_t bufinfo;
|
||||||
if (mp_get_buffer(obj, &bufinfo, MP_BUFFER_RW)) {
|
if (mp_get_buffer(obj, &bufinfo, MP_BUFFER_READ)) {
|
||||||
return (mp_uint_t)bufinfo.buf;
|
return (mp_uint_t)bufinfo.buf;
|
||||||
} else {
|
} else {
|
||||||
// assume obj is an integer that represents an address
|
// assume obj is an integer that represents an address
|
||||||
|
@ -472,7 +472,7 @@ STATIC mp_uint_t convert_obj_for_inline_asm(mp_obj_t obj) {
|
|||||||
return (mp_uint_t)items;
|
return (mp_uint_t)items;
|
||||||
} else {
|
} else {
|
||||||
mp_buffer_info_t bufinfo;
|
mp_buffer_info_t bufinfo;
|
||||||
if (mp_get_buffer(obj, &bufinfo, MP_BUFFER_WRITE)) {
|
if (mp_get_buffer(obj, &bufinfo, MP_BUFFER_READ)) {
|
||||||
// supports the buffer protocol, return a pointer to the data
|
// supports the buffer protocol, return a pointer to the data
|
||||||
return (mp_uint_t)bufinfo.buf;
|
return (mp_uint_t)bufinfo.buf;
|
||||||
} else {
|
} else {
|
||||||
|
@ -55,3 +55,7 @@ print(b, n)
|
|||||||
b = array.array('b', (10, 20, 30, 40, 50, 60, 70, 80))
|
b = array.array('b', (10, 20, 30, 40, 50, 60, 70, 80))
|
||||||
n = asm_sum_bytes(len(b), b)
|
n = asm_sum_bytes(len(b), b)
|
||||||
print(b, n)
|
print(b, n)
|
||||||
|
|
||||||
|
b = b'\x01\x02\x03\x04'
|
||||||
|
n = asm_sum_bytes(len(b), b)
|
||||||
|
print(b, n)
|
||||||
|
@ -1,2 +1,3 @@
|
|||||||
array('l', [100, 200, 300, 400]) 1000
|
array('l', [100, 200, 300, 400]) 1000
|
||||||
array('b', [10, 20, 30, 40, 50, 60, 70, 80]) 360
|
array('b', [10, 20, 30, 40, 50, 60, 70, 80]) 360
|
||||||
|
b'\x01\x02\x03\x04' 10
|
||||||
|
@ -9,6 +9,13 @@ def memset(dest:ptr8, c:int, n:int):
|
|||||||
for i in range(n):
|
for i in range(n):
|
||||||
dest[i] = c
|
dest[i] = c
|
||||||
|
|
||||||
|
@micropython.viper
|
||||||
|
def memsum(src:ptr8, n:int) -> int:
|
||||||
|
s = 0
|
||||||
|
for i in range(n):
|
||||||
|
s += src[i]
|
||||||
|
return s
|
||||||
|
|
||||||
# create array and get its address
|
# create array and get its address
|
||||||
ar = bytearray('0000')
|
ar = bytearray('0000')
|
||||||
addr = get_addr(ar)
|
addr = get_addr(ar)
|
||||||
@ -27,3 +34,6 @@ print(ar)
|
|||||||
# pass direct pointer to array buffer, with offset
|
# pass direct pointer to array buffer, with offset
|
||||||
memset(addr + 2, ord('3'), len(ar) - 2)
|
memset(addr + 2, ord('3'), len(ar) - 2)
|
||||||
print(ar)
|
print(ar)
|
||||||
|
|
||||||
|
# pass a read-only bytes object in
|
||||||
|
print(memsum(b'\x01\x02\x03\x04', 4))
|
||||||
|
@ -4,3 +4,4 @@ bytearray(b'0000')
|
|||||||
bytearray(b'1111')
|
bytearray(b'1111')
|
||||||
bytearray(b'2222')
|
bytearray(b'2222')
|
||||||
bytearray(b'2233')
|
bytearray(b'2233')
|
||||||
|
10
|
||||||
|
Loading…
x
Reference in New Issue
Block a user