developed_by_feng-arch_add_dma_support_in_machine_spi #1
@ -2,4 +2,4 @@
|
||||
#define MICROPY_HW_BOARD_NAME "Raspberry Pi Pico"
|
||||
// Modified from MPY origin to reduce flash storage to accommodate larger program flash requirement
|
||||
// of lvgl and its bindings. Developers should review this setting when adding additional features
|
||||
#define MICROPY_HW_FLASH_STORAGE_BYTES (1024 * 1024)
|
||||
#define MICROPY_HW_FLASH_STORAGE_BYTES (512 * 1024)
|
||||
|
||||
341
user_modules/eigenmath/eheap.c
Normal file
341
user_modules/eigenmath/eheap.c
Normal file
@ -0,0 +1,341 @@
|
||||
/*========================================================================
|
||||
* eheap.c – Improved standalone heap manager
|
||||
* 32-bit, aligned, safe operations
|
||||
*======================================================================*/
|
||||
|
||||
#include "eheap.h"
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include "py/runtime.h"
|
||||
|
||||
#ifndef EHEAP_ALIGN
|
||||
#define EHEAP_ALIGN 4 /* default byte alignment (power of two) */
|
||||
#endif
|
||||
|
||||
#define ALIGN_MASK (EHEAP_ALIGN - 1)
|
||||
#define ALIGN_UP(x) (((x) + ALIGN_MASK) & ~ALIGN_MASK)
|
||||
|
||||
/*-------------------------------------------------- block header layout */
|
||||
typedef struct block_link {
|
||||
struct block_link *next;
|
||||
size_t size; /* MSB=1 => allocated, lower bits => block size */
|
||||
} block_t;
|
||||
|
||||
#define USED_MASK ((size_t)1 << (sizeof(size_t)*8 - 1))
|
||||
#define IS_USED(b) (((b)->size) & USED_MASK)
|
||||
#define MARK_USED(b) ((b)->size |= USED_MASK)
|
||||
#define MARK_FREE(b) ((b)->size &= ~USED_MASK)
|
||||
#define BLOCK_SIZE(b) ((b)->size & ~USED_MASK)
|
||||
|
||||
#define HDR_SIZE ALIGN_UP(sizeof(block_t))
|
||||
#define MIN_SPLIT (HDR_SIZE * 2)
|
||||
|
||||
/*-------------------------------------------------- heap globals */
|
||||
static uint8_t *heap_base = NULL;
|
||||
static uint8_t *heap_end = NULL;
|
||||
static size_t heap_total = 0;
|
||||
|
||||
static block_t start_node; /* dummy head */
|
||||
static block_t end_marker; /* tail sentinel storage */
|
||||
static block_t *end_node = &end_marker;
|
||||
|
||||
static size_t free_bytes = 0;
|
||||
static size_t min_free = 0;
|
||||
static bool initialized = false;
|
||||
|
||||
/*-------------------------------------------------------------------*/
|
||||
static bool is_valid_block(block_t *blk) {
|
||||
uint8_t *ptr = (uint8_t*)blk;
|
||||
if (ptr < heap_base || ptr >= heap_end) return false;
|
||||
return (((uintptr_t)ptr - (uintptr_t)heap_base) & ALIGN_MASK) == 0;
|
||||
}
|
||||
|
||||
/* Insert and coalesce a free block (address-ordered), with overflow guards */
|
||||
static void insert_free(block_t *blk) {
|
||||
if (!is_valid_block(blk) || IS_USED(blk)) {
|
||||
mp_raise_msg(&mp_type_MemoryError, MP_ERROR_TEXT("insert_free: invalid or used block"));
|
||||
return;
|
||||
}
|
||||
size_t blk_sz = BLOCK_SIZE(blk);
|
||||
/* guard pointer addition overflow */
|
||||
if (blk_sz > (size_t)(heap_end - (uint8_t*)blk)) {
|
||||
mp_raise_msg(&mp_type_MemoryError, MP_ERROR_TEXT("insert_free: block size overflow"));
|
||||
return;
|
||||
}
|
||||
uint8_t *blk_end = (uint8_t*)blk + blk_sz;
|
||||
|
||||
block_t *prev = &start_node;
|
||||
/* find insertion point */
|
||||
while (prev->next < blk && prev->next != end_node) {
|
||||
prev = prev->next;
|
||||
if (!is_valid_block(prev)) {
|
||||
mp_raise_msg(&mp_type_MemoryError, MP_ERROR_TEXT("insert_free: corrupted free list"));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* forward merge */
|
||||
if (prev->next != end_node) {
|
||||
block_t *fwd = prev->next;
|
||||
if (!IS_USED(fwd) &&
|
||||
(uint8_t*)fwd == blk_end) {
|
||||
/* fuse sizes */
|
||||
size_t total = blk_sz + BLOCK_SIZE(fwd);
|
||||
if (total < blk_sz) { /* overflow? */
|
||||
mp_raise_msg(&mp_type_MemoryError, MP_ERROR_TEXT("insert_free: combine overflow"));
|
||||
return;
|
||||
}
|
||||
blk->size = total;
|
||||
blk->next = fwd->next;
|
||||
blk_sz = total; /* update for potential backward merge */
|
||||
} else {
|
||||
blk->next = fwd;
|
||||
}
|
||||
} else {
|
||||
blk->next = end_node;
|
||||
}
|
||||
|
||||
/* backward merge */
|
||||
if (prev != &start_node && !IS_USED(prev)) {
|
||||
uint8_t *prev_end = (uint8_t*)prev + BLOCK_SIZE(prev);
|
||||
if (prev_end == (uint8_t*)blk) {
|
||||
size_t total = BLOCK_SIZE(prev) + blk_sz;
|
||||
if (total < BLOCK_SIZE(prev)) { /* overflow? */
|
||||
mp_raise_msg(&mp_type_MemoryError, MP_ERROR_TEXT("insert_free: combine overflow"));
|
||||
return;
|
||||
}
|
||||
prev->size = total;
|
||||
prev->next = blk->next;
|
||||
return;
|
||||
}
|
||||
}
|
||||
prev->next = blk;
|
||||
}
|
||||
|
||||
static void heap_init_once(void) {
|
||||
if (initialized) return;
|
||||
|
||||
/* single free block covers [heap_base .. heap_end) excluding end_node */
|
||||
block_t *first = (block_t*)heap_base;
|
||||
first->size = (heap_total - HDR_SIZE);
|
||||
MARK_FREE(first);
|
||||
first->next = end_node;
|
||||
|
||||
start_node.next = first;
|
||||
start_node.size = 0;
|
||||
|
||||
/* initialize end marker */
|
||||
end_node->next = NULL;
|
||||
end_node->size = 0;
|
||||
MARK_USED(end_node);
|
||||
|
||||
free_bytes = BLOCK_SIZE(first);
|
||||
min_free = free_bytes;
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
void eheap_init(void *buf, size_t bytes) {
|
||||
if (!buf || bytes <= HDR_SIZE*2 + ALIGN_MASK) {
|
||||
mp_raise_msg(&mp_type_MemoryError, MP_ERROR_TEXT("eheap_init: invalid region"));
|
||||
return;
|
||||
}
|
||||
|
||||
/* align base upward */
|
||||
uintptr_t start = ALIGN_UP((uintptr_t)buf);
|
||||
size_t loss = start - (uintptr_t)buf;
|
||||
bytes = (bytes > loss) ? bytes - loss : 0;
|
||||
bytes = (bytes / EHEAP_ALIGN) * EHEAP_ALIGN;
|
||||
if (bytes <= HDR_SIZE*2) {
|
||||
mp_raise_msg(&mp_type_MemoryError, MP_ERROR_TEXT("eheap_init: too small after align"));
|
||||
return;
|
||||
}
|
||||
|
||||
heap_base = (uint8_t*)start;
|
||||
heap_total = bytes;
|
||||
|
||||
/* reserve tail for end_node */
|
||||
size_t res = ALIGN_UP(sizeof(block_t));
|
||||
heap_total -= res;
|
||||
end_node = (block_t*)(heap_base + heap_total);
|
||||
|
||||
heap_end = heap_base + heap_total;
|
||||
|
||||
initialized = false;
|
||||
heap_init_once();
|
||||
}
|
||||
|
||||
void* e_malloc(size_t size) {
|
||||
if (size == 0 || !initialized) return NULL;
|
||||
|
||||
/* check overflow */
|
||||
if (size > SIZE_MAX - HDR_SIZE) return NULL;
|
||||
size_t needed = ALIGN_UP(size + HDR_SIZE);
|
||||
|
||||
block_t *prev = &start_node;
|
||||
block_t *cur = start_node.next;
|
||||
|
||||
while (cur != end_node) {
|
||||
if (!is_valid_block(cur)) {
|
||||
mp_raise_msg(&mp_type_MemoryError, MP_ERROR_TEXT("e_malloc: corrupted heap"));
|
||||
return NULL;
|
||||
}
|
||||
if (!IS_USED(cur) && BLOCK_SIZE(cur) >= needed) {
|
||||
size_t remain = BLOCK_SIZE(cur) - needed;
|
||||
if (remain >= MIN_SPLIT) {
|
||||
/* split */
|
||||
block_t *split = (block_t*)((uint8_t*)cur + needed);
|
||||
split->size = remain;
|
||||
MARK_FREE(split);
|
||||
split->next = cur->next;
|
||||
|
||||
cur->size = needed;
|
||||
prev->next = split;
|
||||
} else {
|
||||
/* use entire */
|
||||
prev->next = cur->next;
|
||||
needed = BLOCK_SIZE(cur);
|
||||
}
|
||||
MARK_USED(cur);
|
||||
|
||||
free_bytes -= needed;
|
||||
if (free_bytes < min_free) min_free = free_bytes;
|
||||
return (uint8_t*)cur + HDR_SIZE;
|
||||
}
|
||||
prev = cur;
|
||||
cur = cur->next;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void e_free(void *ptr) {
|
||||
if (!ptr || !initialized) return;
|
||||
|
||||
uint8_t *p = (uint8_t*)ptr;
|
||||
if (p < heap_base + HDR_SIZE || p >= heap_end) {
|
||||
mp_raise_msg(&mp_type_MemoryError, MP_ERROR_TEXT("e_free: invalid ptr"));
|
||||
return;
|
||||
}
|
||||
if (((uintptr_t)p - HDR_SIZE) & ALIGN_MASK) {
|
||||
mp_raise_msg(&mp_type_MemoryError, MP_ERROR_TEXT("e_free: unaligned ptr"));
|
||||
return;
|
||||
}
|
||||
|
||||
block_t *blk = (block_t*)(p - HDR_SIZE);
|
||||
if (!IS_USED(blk)) {
|
||||
//mp_raise_msg(&mp_type_MemoryError, MP_ERROR_TEXT("e_free: double free"));
|
||||
return;
|
||||
}
|
||||
|
||||
size_t sz = BLOCK_SIZE(blk);
|
||||
if (sz == 0 || (uint8_t*)blk + sz > heap_end) {
|
||||
mp_raise_msg(&mp_type_MemoryError, MP_ERROR_TEXT("e_free: bad size"));
|
||||
return;
|
||||
}
|
||||
|
||||
MARK_FREE(blk);
|
||||
free_bytes += sz;
|
||||
insert_free(blk);
|
||||
}
|
||||
|
||||
void* e_realloc(void *ptr, size_t new_size) {
|
||||
if (!ptr) return e_malloc(new_size);
|
||||
if (new_size == 0) { e_free(ptr); return NULL; }
|
||||
if (!initialized) return NULL;
|
||||
|
||||
uint8_t *p = (uint8_t*)ptr;
|
||||
if (p < heap_base + HDR_SIZE || p >= heap_end) {
|
||||
mp_raise_msg(&mp_type_MemoryError, MP_ERROR_TEXT("e_realloc: invalid ptr"));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
block_t *blk = (block_t*)(p - HDR_SIZE);
|
||||
if (!IS_USED(blk)) {
|
||||
mp_raise_msg(&mp_type_MemoryError, MP_ERROR_TEXT("e_realloc: block not used"));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t curr = BLOCK_SIZE(blk) - HDR_SIZE;
|
||||
if (new_size <= curr) return ptr;
|
||||
|
||||
/* try expand into next free block */
|
||||
uint8_t *next_addr = (uint8_t*)blk + BLOCK_SIZE(blk);
|
||||
if (next_addr + HDR_SIZE <= heap_end) {
|
||||
block_t *next = (block_t*)next_addr;
|
||||
if (is_valid_block(next) && !IS_USED(next)) {
|
||||
size_t combined = BLOCK_SIZE(blk) + BLOCK_SIZE(next);
|
||||
size_t need = ALIGN_UP(new_size + HDR_SIZE);
|
||||
if (combined >= need) {
|
||||
/* remove next from free list */
|
||||
block_t *prev = &start_node;
|
||||
while (prev->next != next && prev->next != end_node) {
|
||||
prev = prev->next;
|
||||
}
|
||||
if (prev->next == next) {
|
||||
prev->next = next->next;
|
||||
/* compute new free_bytes: remove next size */
|
||||
free_bytes -= BLOCK_SIZE(next);
|
||||
|
||||
/* update blk size */
|
||||
blk->size = (blk->size & USED_MASK) | need;
|
||||
size_t leftover = combined - need;
|
||||
if (leftover >= MIN_SPLIT) {
|
||||
block_t *split = (block_t*)((uint8_t*)blk + need);
|
||||
split->size = leftover;
|
||||
MARK_FREE(split);
|
||||
insert_free(split);
|
||||
} else {
|
||||
/* absorb all */
|
||||
blk->size = (blk->size & USED_MASK) | combined;
|
||||
}
|
||||
if (free_bytes < min_free) min_free = free_bytes;
|
||||
return ptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* fallback: alloc-copy-free */
|
||||
void *nptr = e_malloc(new_size);
|
||||
if (nptr) {
|
||||
memcpy(nptr, ptr, curr);
|
||||
e_free(ptr);
|
||||
}
|
||||
return nptr;
|
||||
}
|
||||
|
||||
size_t e_heap_free(void) { return free_bytes; }
|
||||
size_t e_heap_min_free(void) { return min_free; }
|
||||
|
||||
|
||||
int e_heap_fragmentation(void) {
|
||||
if (!initialized || free_bytes == 0) return 0;
|
||||
size_t largest = 0;
|
||||
for (block_t *b = start_node.next; b != end_node; b = b->next) {
|
||||
if (!IS_USED(b) && BLOCK_SIZE(b) > largest) {
|
||||
largest = BLOCK_SIZE(b);
|
||||
}
|
||||
}
|
||||
if (largest == 0) return 100;
|
||||
return (int)(100 - (largest * 100) / free_bytes);
|
||||
}
|
||||
|
||||
bool e_heap_validate(void) {
|
||||
if (!initialized) return false;
|
||||
size_t counted = 0;
|
||||
for (block_t *b = start_node.next; b != end_node; b = b->next) {
|
||||
if (!is_valid_block(b)) return false;
|
||||
if ((uint8_t*)b + BLOCK_SIZE(b) > heap_end) return false;
|
||||
if (!IS_USED(b)) {
|
||||
counted += BLOCK_SIZE(b);
|
||||
/* ensure no adjacent free blocks */
|
||||
block_t *n = b->next;
|
||||
if (n != end_node && !IS_USED(n) &&
|
||||
(uint8_t*)b + BLOCK_SIZE(b) == (uint8_t*)n) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return (counted == free_bytes);
|
||||
}
|
||||
|
||||
33
user_modules/eigenmath/eheap.h
Normal file
33
user_modules/eigenmath/eheap.h
Normal file
@ -0,0 +1,33 @@
|
||||
/*========================================================================
|
||||
* eheap.h – Minimal standalone heap manager (Freertos heap_4 style)
|
||||
* ---------------------------------------------------------------------
|
||||
* API:
|
||||
* void eheap_init(void *buffer, size_t size);
|
||||
* void* e_malloc(size_t bytes);
|
||||
* void e_free(void *ptr);
|
||||
* void* e_realloc(void *ptr, size_t new_size);
|
||||
* size_t e_heap_free(void);
|
||||
* size_t e_heap_min_free(void);
|
||||
*======================================================================*/
|
||||
|
||||
#ifndef EHEAP_H
|
||||
#define EHEAP_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void eheap_init(void *buffer, size_t size);
|
||||
void* e_malloc(size_t size);
|
||||
void e_free(void *ptr);
|
||||
void* e_realloc(void *ptr, size_t new_size);
|
||||
size_t e_heap_free(void);
|
||||
size_t e_heap_min_free(void);
|
||||
int e_heap_fragmentation(void);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* EHEAP_H */
|
||||
File diff suppressed because it is too large
Load Diff
35
user_modules/eigenmath/eigenmath.h
Normal file
35
user_modules/eigenmath/eigenmath.h
Normal file
@ -0,0 +1,35 @@
|
||||
|
||||
#ifndef EIGENMATH_H
|
||||
#define EIGENMATH_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
#define STRBUFLEN 1000
|
||||
#define BUCKETSIZE 100
|
||||
#define MAXDIM 24
|
||||
|
||||
//extern struct atom *mem ;
|
||||
//extern struct atom **stack ; //
|
||||
//extern struct atom **symtab ; // symbol table
|
||||
//extern struct atom **binding ;
|
||||
//extern struct atom **usrfunc ;
|
||||
//extern char *strbuf ;
|
||||
|
||||
//extern uint32_t STACKSIZE ; // evaluation stack
|
||||
//extern uint32_t MAXATOMS ; // 10,240 atoms
|
||||
|
||||
extern bool noprint;
|
||||
extern char *outbuf;
|
||||
extern int outbuf_index;
|
||||
extern void eigenmath_init(uint8_t *pHeap,size_t heapSize);
|
||||
extern void run(char *buf);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* EHEAP_H */
|
||||
281
user_modules/eigenmath/eigenmath_mpy.c
Normal file
281
user_modules/eigenmath/eigenmath_mpy.c
Normal file
@ -0,0 +1,281 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <ctype.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <setjmp.h>
|
||||
#include <math.h>
|
||||
#include <errno.h>
|
||||
#include "py/obj.h"
|
||||
//#include "py/mpconfig.h"
|
||||
#include "py/misc.h"
|
||||
#include "py/runtime.h"
|
||||
#include "py/objstr.h"
|
||||
#include "shared/readline/readline.h"
|
||||
#include "py/binary.h"
|
||||
#include "py/gc.h"
|
||||
#include "py/stream.h"
|
||||
#include "eigenmath.h"
|
||||
#include "eheap.h"
|
||||
//-DPICO_STACK_SIZE=0x4000 ??
|
||||
|
||||
typedef struct _mp_obj_eigenmath_t {
|
||||
mp_obj_base_t base;
|
||||
size_t heapSize;
|
||||
uint8_t *pHeap;
|
||||
} mp_obj_eigenmath_t;
|
||||
|
||||
static void eigenmath_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||
mp_printf(print, "<EigenMath instance>");
|
||||
}
|
||||
|
||||
static mp_obj_t eigenmath_make_new(const mp_obj_type_t *type,
|
||||
size_t n_args, size_t n_kw,
|
||||
const mp_obj_t *args) {
|
||||
|
||||
mp_arg_check_num(n_args, n_kw, 1, 1, false);
|
||||
mp_obj_eigenmath_t *self = mp_obj_malloc(mp_obj_eigenmath_t, type);
|
||||
self->base.type = type;
|
||||
self->heapSize = mp_obj_get_int(args[0]); // 350 * 1024; // 350KB
|
||||
//mp_printf(&mp_plat_print,"heapSize = %d\n", self->heapSize);
|
||||
|
||||
self->pHeap = (uint8_t *)m_malloc(self->heapSize);
|
||||
|
||||
//mp_printf(&mp_plat_print,"ptemp = %x\n", (uint32_t)(ptemp));
|
||||
//mp_printf(&mp_plat_print,"self->pHeap = %x\n", (uint32_t)(self->pHeap));
|
||||
if (self->pHeap == NULL){
|
||||
mp_raise_msg(&mp_type_MemoryError, MP_ERROR_TEXT("Failed to initialize heap"));
|
||||
return MP_OBJ_NULL;
|
||||
}
|
||||
|
||||
|
||||
eigenmath_init(self->pHeap,self->heapSize);
|
||||
|
||||
return MP_OBJ_FROM_PTR(self);
|
||||
}
|
||||
|
||||
static mp_obj_t eigenmath_run(size_t n_args, const mp_obj_t *args) {
|
||||
//mp_obj_eigenmath_t *self = MP_OBJ_TO_PTR(self_in);mp_obj_t input_str_obj
|
||||
size_t len;
|
||||
if (n_args >= 3){
|
||||
mp_obj_t arg = args[2];
|
||||
if (mp_obj_is_bool(arg) || mp_obj_is_int(arg)) {
|
||||
noprint = mp_obj_is_true(arg);
|
||||
} else {
|
||||
mp_raise_TypeError(MP_ERROR_TEXT("expected a bool"));
|
||||
}
|
||||
} else {
|
||||
noprint = false;
|
||||
}
|
||||
|
||||
if (!mp_obj_is_str(args[1])) {
|
||||
mp_raise_TypeError(MP_ERROR_TEXT("expected a string as input"));
|
||||
}
|
||||
const char *buf = mp_obj_str_get_data(args[1], &len);
|
||||
|
||||
//GET_STR_DATA_LEN(input_str_obj, str, str_len);
|
||||
run((char *)buf);
|
||||
|
||||
if (noprint == true){
|
||||
return mp_obj_new_bytearray_by_ref(outbuf_index-1, outbuf);
|
||||
// return memoryview
|
||||
//return mp_obj_new_memoryview(BYTEARRAY_TYPECODE, outbuf);
|
||||
|
||||
}else{
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(eigenmath_run_obj,2,3, eigenmath_run);
|
||||
|
||||
|
||||
|
||||
|
||||
static mp_obj_t eigenmath_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
||||
//mp_obj_eigenmath_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
noprint = false;
|
||||
if (n_args != 1 || n_kw != 0) {
|
||||
mp_raise_TypeError(MP_ERROR_TEXT("Expected 1 positional argument"));
|
||||
}
|
||||
|
||||
const char *cmd = mp_obj_str_get_str(args[0]);
|
||||
run((char *)cmd); //
|
||||
return mp_const_none;
|
||||
}
|
||||
|
||||
static mp_obj_t eigenmath_cmd(mp_obj_t self_in) {
|
||||
//mp_obj_eigenmath_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
vstr_t* vstr_inbuf = vstr_new(1);
|
||||
for (;;) {
|
||||
vstr_clear(vstr_inbuf);
|
||||
int res = readline(vstr_inbuf,"eigenmath> ");
|
||||
mp_printf(&mp_plat_print, "Eigenmath run:\n");
|
||||
mp_printf(&mp_plat_print, "res=%d\n", res);
|
||||
mp_printf(&mp_plat_print, "%s\n", vstr_inbuf->buf);
|
||||
run(vstr_inbuf->buf);
|
||||
}
|
||||
return mp_const_none;
|
||||
|
||||
}
|
||||
static MP_DEFINE_CONST_FUN_OBJ_1(eigenmath_cmd_obj, eigenmath_cmd);
|
||||
|
||||
|
||||
|
||||
static mp_obj_t eigenmath_runfile(size_t n_args, const mp_obj_t *args ) {//mp_obj_t input_file_obj
|
||||
|
||||
if (n_args >= 3){
|
||||
mp_obj_t arg = args[2];
|
||||
if (mp_obj_is_bool(arg) || mp_obj_is_int(arg)) {
|
||||
noprint = mp_obj_is_true(arg);
|
||||
} else {
|
||||
mp_raise_TypeError(MP_ERROR_TEXT("expected a bool"));
|
||||
}
|
||||
} else {
|
||||
noprint = false;
|
||||
}
|
||||
|
||||
|
||||
const mp_stream_p_t *stream_p = mp_get_stream_raise(args[1], MP_STREAM_OP_READ | MP_STREAM_OP_IOCTL);
|
||||
if (stream_p == NULL) {
|
||||
mp_raise_TypeError(MP_ERROR_TEXT("expected a file-like object"));
|
||||
}
|
||||
int error = 0;
|
||||
|
||||
// get file size
|
||||
struct mp_stream_seek_t seek = {
|
||||
.offset = 0,
|
||||
.whence = MP_SEEK_END,
|
||||
};
|
||||
mp_obj_t input_file_obj = args[1];
|
||||
if (stream_p->ioctl(input_file_obj, MP_STREAM_SEEK, (uintptr_t)&seek, &error) == MP_STREAM_ERROR) {
|
||||
mp_raise_OSError(error);
|
||||
}
|
||||
mp_off_t size = seek.offset;
|
||||
|
||||
// move to front
|
||||
seek.offset = 0;
|
||||
seek.whence = MP_SEEK_SET;
|
||||
if (stream_p->ioctl(input_file_obj, MP_STREAM_SEEK, (uintptr_t)&seek, &error) == MP_STREAM_ERROR) {
|
||||
mp_raise_OSError(error);
|
||||
}
|
||||
|
||||
// get buffer
|
||||
char *buf = m_new(char, size + 1);
|
||||
|
||||
// read file
|
||||
mp_uint_t out_sz = stream_p->read(input_file_obj, buf, size, &error);
|
||||
if (error != 0 || out_sz != size) {
|
||||
m_del(char, buf, size + 1);
|
||||
mp_raise_OSError(error);
|
||||
}
|
||||
|
||||
// add end
|
||||
buf[out_sz] = '\0';
|
||||
|
||||
// run
|
||||
run(buf);
|
||||
|
||||
// release buffer
|
||||
m_del(char, buf, size + 1);
|
||||
|
||||
if (noprint == true){
|
||||
return mp_obj_new_bytearray_by_ref(outbuf_index-1, outbuf);
|
||||
// return memoryview
|
||||
//return mp_obj_new_memoryview(BYTEARRAY_TYPECODE, bytearray);
|
||||
|
||||
}else{
|
||||
return mp_const_none;
|
||||
}
|
||||
}
|
||||
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(eigenmath_runfile_obj, 2,3,eigenmath_runfile);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
extern int free_count;
|
||||
extern int MAXATOMS;
|
||||
static mp_obj_t eigenmath_status(mp_obj_t self_in) {
|
||||
//mp_obj_eigenmath_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
int fragmentation = e_heap_fragmentation();
|
||||
size_t free_bytes = e_heap_free();
|
||||
size_t min_free = e_heap_min_free();
|
||||
int num_atoms = free_count;
|
||||
mp_printf(&mp_plat_print,"Heap fragmentation: %d%%\n", fragmentation);
|
||||
mp_printf(&mp_plat_print,"Free bytes in Heap: %d\n", (int)free_bytes);
|
||||
mp_printf(&mp_plat_print,"Minimum free bytes in Heap: %d\n", (int)min_free);
|
||||
mp_printf(&mp_plat_print,"Number of free atoms: %d of %d\n", num_atoms,MAXATOMS);
|
||||
return mp_const_none;
|
||||
|
||||
}
|
||||
static MP_DEFINE_CONST_FUN_OBJ_1(eigenmath_status_obj, eigenmath_status);
|
||||
|
||||
|
||||
|
||||
extern struct atom *zero;
|
||||
static mp_obj_t eigenmath_del(mp_obj_t self_in) {
|
||||
mp_obj_eigenmath_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
m_free(&self->pHeap); // deinitialize the hea
|
||||
zero = NULL;
|
||||
return mp_const_none;
|
||||
}
|
||||
static MP_DEFINE_CONST_FUN_OBJ_1(eigenmath_del_obj, eigenmath_del);
|
||||
|
||||
extern struct atom *zero;
|
||||
static mp_obj_t eigenmath_reset(mp_obj_t self_in) {
|
||||
mp_obj_eigenmath_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
eigenmath_init(self->pHeap,self->heapSize);
|
||||
zero = NULL;//triger the symbol table initialization
|
||||
return mp_const_none;
|
||||
}
|
||||
static MP_DEFINE_CONST_FUN_OBJ_1(eigenmath_reset_obj, eigenmath_reset);
|
||||
|
||||
mp_obj_t eigenmath_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
|
||||
if (dest[0] == MP_OBJ_NULL && attr == MP_QSTR___del__) {
|
||||
dest[0] = MP_OBJ_FROM_PTR(&eigenmath_del_obj);
|
||||
dest[1] = self_in;
|
||||
}else{
|
||||
// For any other attribute, indicate that lookup should continue in the locals dict
|
||||
dest[1] = MP_OBJ_SENTINEL;
|
||||
return MP_OBJ_NULL;
|
||||
}
|
||||
|
||||
return MP_OBJ_NULL;
|
||||
}
|
||||
static const mp_rom_map_elem_t eigenmath_locals_dict_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR_run), MP_ROM_PTR(&eigenmath_run_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_runfile), MP_ROM_PTR(&eigenmath_runfile_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_cmd), MP_ROM_PTR(&eigenmath_cmd_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&eigenmath_reset_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_status), MP_ROM_PTR(&eigenmath_status_obj) },
|
||||
};
|
||||
static MP_DEFINE_CONST_DICT(eigenmath_locals_dict, eigenmath_locals_dict_table);
|
||||
|
||||
|
||||
MP_DEFINE_CONST_OBJ_TYPE(
|
||||
eigenmath_type,
|
||||
MP_QSTR_EigenMath,
|
||||
MP_TYPE_FLAG_NONE,
|
||||
make_new, eigenmath_make_new,
|
||||
call,eigenmath_call, // call handler for the run method
|
||||
attr, eigenmath_attr, // attr handler before locals_dict
|
||||
locals_dict, &eigenmath_locals_dict,
|
||||
print, eigenmath_print
|
||||
);
|
||||
|
||||
static const mp_rom_map_elem_t eigenmath_module_globals_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_eigenmath) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_EigenMath), MP_ROM_PTR(&eigenmath_type) },
|
||||
};
|
||||
static MP_DEFINE_CONST_DICT(mp_module_eigenmath_globals, eigenmath_module_globals_table);
|
||||
|
||||
const mp_obj_module_t eigenmath_user_cmodule = {
|
||||
.base = { &mp_type_module },
|
||||
.globals = (mp_obj_dict_t*)&mp_module_eigenmath_globals,
|
||||
};
|
||||
|
||||
MP_REGISTER_MODULE(MP_QSTR_eigenmath, eigenmath_user_cmodule);
|
||||
@ -4,12 +4,16 @@ add_library(usermod_eigenmath INTERFACE)
|
||||
# Add our source files to the lib
|
||||
target_sources(usermod_eigenmath INTERFACE
|
||||
${CMAKE_CURRENT_LIST_DIR}/eigenmath.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/eheap.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/eigenmath_mpy.c
|
||||
)
|
||||
|
||||
# Add the current directory as an include directory.
|
||||
target_include_directories(usermod_eigenmath INTERFACE
|
||||
${CMAKE_CURRENT_LIST_DIR}
|
||||
)
|
||||
set(PICO_STACK_SIZE 0x4000 CACHE STRING "App stack size" FORCE)
|
||||
|
||||
# Link our INTERFACE library to the usermod target.
|
||||
target_link_libraries(usermod INTERFACE usermod_eigenmath)
|
||||
|
||||
|
||||
@ -1,8 +1,10 @@
|
||||
CEXAMPLE_MOD_DIR := $(USERMOD_DIR)
|
||||
PICOCALCDISPLAY_MOD_DIR := $(USERMOD_DIR)
|
||||
|
||||
# Add all C files to SRC_USERMOD.
|
||||
SRC_USERMOD += $(CEXAMPLE_MOD_DIR)/eigenmath.c
|
||||
SRC_USERMOD += $(PICOCALCDISPLAY_MOD_DIR)/eigenmath.c\
|
||||
$(PICOCALCDISPLAY_MOD_DIR)/eigenmath_mpy.h\
|
||||
$(PICOCALCDISPLAY_MOD_DIR)/eheap.c\
|
||||
|
||||
# We can add our module folder to include paths if needed
|
||||
# This is not actually needed in this example.
|
||||
CFLAGS_USERMOD += -I$(CEXAMPLE_MOD_DIR)
|
||||
CFLAGS_USERMOD += -I$(PICOCALCDISPLAY_MOD_DIR)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user