extmod: Add machine.bitstream.
This is a generic API for synchronously bit-banging data on a pin. Initially this adds a single supported encoding, which supports controlling WS2812 LEDs. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
This commit is contained in:
parent
226c0341ca
commit
870000f35b
@ -6,6 +6,7 @@ set(MICROPY_OOFATFS_DIR "${MICROPY_DIR}/lib/oofatfs")
|
|||||||
set(MICROPY_SOURCE_EXTMOD
|
set(MICROPY_SOURCE_EXTMOD
|
||||||
${MICROPY_DIR}/shared/libc/abort_.c
|
${MICROPY_DIR}/shared/libc/abort_.c
|
||||||
${MICROPY_DIR}/shared/libc/printf.c
|
${MICROPY_DIR}/shared/libc/printf.c
|
||||||
|
${MICROPY_EXTMOD_DIR}/machine_bitstream.c
|
||||||
${MICROPY_EXTMOD_DIR}/machine_i2c.c
|
${MICROPY_EXTMOD_DIR}/machine_i2c.c
|
||||||
${MICROPY_EXTMOD_DIR}/machine_mem.c
|
${MICROPY_EXTMOD_DIR}/machine_mem.c
|
||||||
${MICROPY_EXTMOD_DIR}/machine_pulse.c
|
${MICROPY_EXTMOD_DIR}/machine_pulse.c
|
||||||
|
65
extmod/machine_bitstream.c
Normal file
65
extmod/machine_bitstream.c
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the MicroPython project, http://micropython.org/
|
||||||
|
*
|
||||||
|
* The MIT License (MIT)
|
||||||
|
*
|
||||||
|
* Copyright (c) 2021 Jim Mussared
|
||||||
|
* Copyright (c) 2021 Damien P. George
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "py/runtime.h"
|
||||||
|
#include "py/mphal.h"
|
||||||
|
#include "extmod/machine_bitstream.h"
|
||||||
|
|
||||||
|
#if MICROPY_PY_MACHINE_BITSTREAM
|
||||||
|
|
||||||
|
// Timing is a 4-tuple of (high_time_0, low_time_0, high_time_1, low_time_1)
|
||||||
|
// suitable for driving WS2812.
|
||||||
|
#define MICROPY_MACHINE_BITSTREAM_TYPE_HIGH_LOW (0)
|
||||||
|
|
||||||
|
// machine.bitstream(pin, encoding, (timing), bytes)
|
||||||
|
STATIC mp_obj_t machine_bitstream_(size_t n_args, const mp_obj_t *args) {
|
||||||
|
mp_hal_pin_obj_t pin = mp_hal_get_pin_obj(args[0]);
|
||||||
|
int encoding = mp_obj_get_int(args[1]);
|
||||||
|
|
||||||
|
mp_buffer_info_t bufinfo;
|
||||||
|
mp_get_buffer_raise(args[3], &bufinfo, MP_BUFFER_READ);
|
||||||
|
|
||||||
|
switch (encoding) {
|
||||||
|
case MICROPY_MACHINE_BITSTREAM_TYPE_HIGH_LOW: {
|
||||||
|
uint32_t timing_ns[4];
|
||||||
|
mp_obj_t *timing;
|
||||||
|
mp_obj_get_array_fixed_n(args[2], 4, &timing);
|
||||||
|
for (size_t i = 0; i < 4; ++i) {
|
||||||
|
timing_ns[i] = mp_obj_get_int(timing[i]);
|
||||||
|
}
|
||||||
|
machine_bitstream_high_low(pin, timing_ns, bufinfo.buf, bufinfo.len);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
mp_raise_ValueError(MP_ERROR_TEXT("encoding"));
|
||||||
|
}
|
||||||
|
|
||||||
|
return mp_const_none;
|
||||||
|
}
|
||||||
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_bitstream_obj, 4, 4, machine_bitstream_);
|
||||||
|
|
||||||
|
#endif // MICROPY_PY_MACHINE_BITSTREAM
|
37
extmod/machine_bitstream.h
Normal file
37
extmod/machine_bitstream.h
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the MicroPython project, http://micropython.org/
|
||||||
|
*
|
||||||
|
* The MIT License (MIT)
|
||||||
|
*
|
||||||
|
* Copyright (c) 2021 Jim Mussared
|
||||||
|
* Copyright (c) 2021 Damien P. George
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
#ifndef MICROPY_INCLUDED_EXTMOD_MACHINE_BITSTREAM_H
|
||||||
|
#define MICROPY_INCLUDED_EXTMOD_MACHINE_BITSTREAM_H
|
||||||
|
|
||||||
|
#include "py/obj.h"
|
||||||
|
#include "py/mphal.h"
|
||||||
|
|
||||||
|
void machine_bitstream_high_low(mp_hal_pin_obj_t pin, uint32_t *timing_ns, const uint8_t *buf, size_t len);
|
||||||
|
|
||||||
|
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(machine_bitstream_obj);
|
||||||
|
|
||||||
|
#endif // MICROPY_INCLUDED_EXTMOD_MACHINE_BITSTREAM_H
|
@ -1470,6 +1470,11 @@ typedef double mp_float_t;
|
|||||||
#define MICROPY_PY_MACHINE (0)
|
#define MICROPY_PY_MACHINE (0)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// Whether to include: bitstream
|
||||||
|
#ifndef MICROPY_PY_MACHINE_BITSTREAM
|
||||||
|
#define MICROPY_PY_MACHINE_BITSTREAM (0)
|
||||||
|
#endif
|
||||||
|
|
||||||
// Whether to include: time_pulse_us
|
// Whether to include: time_pulse_us
|
||||||
#ifndef MICROPY_PY_MACHINE_PULSE
|
#ifndef MICROPY_PY_MACHINE_PULSE
|
||||||
#define MICROPY_PY_MACHINE_PULSE (0)
|
#define MICROPY_PY_MACHINE_PULSE (0)
|
||||||
|
1
py/py.mk
1
py/py.mk
@ -184,6 +184,7 @@ PY_EXTMOD_O_BASENAME = \
|
|||||||
extmod/moducryptolib.o \
|
extmod/moducryptolib.o \
|
||||||
extmod/modubinascii.o \
|
extmod/modubinascii.o \
|
||||||
extmod/virtpin.o \
|
extmod/virtpin.o \
|
||||||
|
extmod/machine_bitstream.o \
|
||||||
extmod/machine_mem.o \
|
extmod/machine_mem.o \
|
||||||
extmod/machine_pinbase.o \
|
extmod/machine_pinbase.o \
|
||||||
extmod/machine_signal.o \
|
extmod/machine_signal.o \
|
||||||
|
Loading…
Reference in New Issue
Block a user