First draft of eveL, the low-level module of the Gameduino (and BridgeTek EVE) bindings.

[adafruit/circuitpython#2578]
crypto-aes
James Bowman 3 years ago
parent 67440acd3e
commit 7fd30e7d20

@ -10,3 +10,5 @@ QSPI_FLASH_FILESYSTEM = 1
EXTERNAL_FLASH_DEVICE_COUNT = 3
EXTERNAL_FLASH_DEVICES = "S25FL116K, S25FL216K, GD25Q16C"
LONGINT_IMPL = MPZ
CIRCUITPY_EVEL = 1

@ -8,3 +8,5 @@ MCU_CHIP = nrf52840
QSPI_FLASH_FILESYSTEM = 1
EXTERNAL_FLASH_DEVICE_COUNT = 1
EXTERNAL_FLASH_DEVICES = "GD25Q16C"
CIRCUITPY_EVEL = 1

@ -157,6 +157,9 @@ endif
ifeq ($(CIRCUITPY_MATH),1)
SRC_PATTERNS += math/%
endif
ifeq ($(CIRCUITPY_EVEL),1)
SRC_PATTERNS += eveL/%
endif
ifeq ($(CIRCUITPY_MICROCONTROLLER),1)
SRC_PATTERNS += microcontroller/%
endif
@ -298,6 +301,7 @@ $(filter $(SRC_PATTERNS), \
fontio/Glyph.c \
microcontroller/RunMode.c \
math/__init__.c \
eveL/__init__.c \
)
SRC_BINDINGS_ENUMS += \

@ -384,6 +384,13 @@ extern const struct _mp_obj_module_t math_module;
#define MATH_MODULE
#endif
#if CIRCUITPY_EVEL
extern const struct _mp_obj_module_t eveL_module;
#define EVEL_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_eveL), (mp_obj_t)&eveL_module },
#else
#define EVEL_MODULE
#endif
#if CIRCUITPY_MICROCONTROLLER
extern const struct _mp_obj_module_t microcontroller_module;
#define MICROCONTROLLER_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_microcontroller), (mp_obj_t)&microcontroller_module },
@ -617,6 +624,7 @@ extern const struct _mp_obj_module_t ustack_module;
I2CSLAVE_MODULE \
JSON_MODULE \
MATH_MODULE \
EVEL_MODULE \
MICROCONTROLLER_MODULE \
NEOPIXEL_WRITE_MODULE \
NETWORK_MODULE \

@ -174,6 +174,11 @@ CIRCUITPY_MATH = $(CIRCUITPY_ALWAYS_BUILD)
endif
CFLAGS += -DCIRCUITPY_MATH=$(CIRCUITPY_MATH)
ifndef CIRCUITPY_EVEL
CIRCUITPY_EVEL = $(CIRCUITPY_ALWAYS_BUILD)
endif
CFLAGS += -DCIRCUITPY_EVEL=$(CIRCUITPY_EVEL)
ifndef CIRCUITPY_MICROCONTROLLER
CIRCUITPY_MICROCONTROLLER = $(CIRCUITPY_DEFAULT_BUILD)
endif

@ -0,0 +1,225 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2014 Paul Sokolovsky
*
* 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 <stdio.h>
#include <assert.h>
#include <string.h>
#include "py/runtime.h"
#include "py/binary.h"
// #if MICROPY_PY_BUILTINS_EVEL
typedef struct _mp_obj_EVEL_t {
mp_obj_base_t base;
mp_obj_t writer;
mp_obj_t dest[3];
size_t n;
uint8_t buf[512];
} mp_obj_EVEL_t;
STATIC void _write(mp_obj_EVEL_t *EVEL, mp_obj_t b) {
EVEL->dest[2] = b;
mp_call_method_n_kw(1, 0, EVEL->dest);
}
STATIC mp_obj_t _register(mp_obj_t self, mp_obj_t o) {
mp_obj_EVEL_t *EVEL = self;
EVEL->n = 0;
mp_printf(&mp_plat_print, "register %p %d\n", EVEL, EVEL->n);
mp_load_method(o, MP_QSTR_write, EVEL->dest);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(register_obj, _register);
STATIC mp_obj_t _flush(mp_obj_t self) {
mp_obj_EVEL_t *EVEL = self;
// mp_printf(&mp_plat_print, "flush %p %d\n", EVEL, EVEL->n);
if (EVEL->n != 0) {
_write(EVEL, mp_obj_new_bytearray_by_ref(EVEL->n, EVEL->buf));
EVEL->n = 0;
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(flush_obj, _flush);
STATIC void *append(mp_obj_EVEL_t *EVEL, size_t m) {
if ((EVEL->n + m) > sizeof(EVEL->buf))
_flush((mp_obj_t)EVEL);
uint8_t *r = EVEL->buf + EVEL->n;
EVEL->n += m;
return (void*)r;
}
STATIC mp_obj_t _cc(mp_obj_t self, mp_obj_t b) {
mp_obj_EVEL_t *EVEL = self;
mp_buffer_info_t buffer_info;
mp_get_buffer_raise(b, &buffer_info, MP_BUFFER_READ);
// mp_printf(&mp_plat_print, "flush %p %d %p\n", EVEL, buffer_info.len, EVEL->writer);
if (buffer_info.len <= sizeof(EVEL->buf)) {
uint8_t *p = (uint8_t*)append(EVEL, buffer_info.len);
// memcpy(p, buffer_info.buf, buffer_info.len);
uint8_t *s = buffer_info.buf;
for (size_t i = 0; i < buffer_info.len; i++)
*p++ = *s++;
} else {
_flush(self);
_write(EVEL, b);
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(cc_obj, _cc);
#define C4(self, u) (*(uint32_t*)append((self), sizeof(uint32_t)) = (u))
#include "modeveL-gen.h"
// Hand-written functions {
STATIC mp_obj_t _cmd0(mp_obj_t self, mp_obj_t n) {
C4(self, (0xffffff00 | mp_obj_get_int_truncated(n)));
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(cmd0_obj, _cmd0);
STATIC mp_obj_t _vertex2f(mp_obj_t self , mp_obj_t a0, mp_obj_t a1) {
int16_t x = (int16_t)(16 * mp_obj_get_float(a0));
int16_t y = (int16_t)(16 * mp_obj_get_float(a1));
C4(self, (0x40000000 | ((x & 32767) << 15) | (y & 32767)));
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_3(vertex2f_obj, _vertex2f);
STATIC mp_obj_t _cmd(size_t n_args, const mp_obj_t *args) {
mp_obj_t self = args[0];
mp_obj_t num = args[1];
mp_buffer_info_t fmt;
mp_get_buffer_raise(args[2], &fmt, MP_BUFFER_READ);
size_t len;
mp_obj_t *items;
mp_obj_tuple_get(args[3], &len, &items);
// Count how many 32-bit words required
size_t n = 0;
for (size_t i = 0; i < fmt.len; n++) {
switch (((char*)fmt.buf)[i]) {
case 'I':
case 'i':
i += 1;
break;
case 'H':
case 'h':
i += 2;
break;
default:
break;
}
}
mp_printf(&mp_plat_print, "n=%d\n", n);
uint32_t *p = (uint32_t*)append(self, sizeof(uint32_t) * (1 + n));
*p++ = 0xffffff00 | mp_obj_get_int_truncated(num);
mp_obj_t *a = items;
uint32_t lo;
for (size_t i = 0; i < fmt.len; ) {
switch (((char*)fmt.buf)[i]) {
case 'I':
case 'i':
*p++ = mp_obj_get_int_truncated(*a++);
mp_printf(&mp_plat_print, " %d %08x\n", p[-1]);
i += 1;
break;
case 'H':
case 'h':
lo = mp_obj_get_int_truncated(*a++) & 0xffff;
*p++ = lo | (mp_obj_get_int_truncated(*a++) << 16);
i += 2;
break;
default:
break;
}
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(cmd_obj, 4, 4, _cmd);
STATIC const mp_rom_map_elem_t EVEL_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_register), MP_ROM_PTR(&register_obj) },
{ MP_ROM_QSTR(MP_QSTR_cc), MP_ROM_PTR(&cc_obj) },
{ MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&flush_obj) },
{ MP_ROM_QSTR(MP_QSTR_Vertex2f), MP_ROM_PTR(&vertex2f_obj) },
{ MP_ROM_QSTR(MP_QSTR_cmd), MP_ROM_PTR(&cmd_obj) },
{ MP_ROM_QSTR(MP_QSTR_cmd0), MP_ROM_PTR(&cmd0_obj) },
ROM_DECLS
};
STATIC MP_DEFINE_CONST_DICT(EVEL_locals_dict, EVEL_locals_dict_table);
STATIC void EVEL_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
(void)self_in;
(void)kind;
mp_printf(print, "<EVEL>");
}
STATIC mp_obj_t EVEL_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
// mp_arg_check_num(n_args, kw_args, 1, 1, false);
mp_obj_EVEL_t *o = m_new_obj(mp_obj_EVEL_t);
mp_printf(&mp_plat_print, "EVEL init\n");
o->base.type = type;
return o;
}
// STATIC void EVEL_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
// printf("HERE\n");
// mp_type_type.attr(self_in, attr, dest);
// printf("THERE %p %p\n", dest[0], dest[1]);
// }
STATIC const mp_obj_type_t EVEL_type = {
{ &mp_type_type },
// Save on qstr's, reuse same as for module
.name = MP_QSTR_EVEL,
.print = EVEL_print,
.make_new = EVEL_make_new,
// .attr = EVEL_attr,
.locals_dict = (void*)&EVEL_locals_dict,
};
STATIC const mp_rom_map_elem_t mp_module_eveL_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_eveL) },
{ MP_ROM_QSTR(MP_QSTR_EVEL), MP_OBJ_FROM_PTR(&EVEL_type) },
};
STATIC MP_DEFINE_CONST_DICT(mp_module_eveL_globals, mp_module_eveL_globals_table);
const mp_obj_module_t eveL_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&mp_module_eveL_globals,
};
// #endif // MICROPY_PY_BUILTINS_EVEL

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save