circuitpython/supervisor/shared/port.c

82 lines
2.4 KiB
C

/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2022 Scott Shawcroft for Adafruit Industries
*
* 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 "supervisor/port.h"
#include <string.h>
#include "py/runtime.h"
#include "lib/tlsf/tlsf.h"
static tlsf_t heap;
MP_WEAK void port_wake_main_task(void) {
}
MP_WEAK void port_wake_main_task_from_isr(void) {
}
MP_WEAK void port_yield(void) {
}
MP_WEAK void port_boot_info(void) {
}
MP_WEAK void port_heap_init(void) {
uint32_t *heap_bottom = port_heap_get_bottom();
uint32_t *heap_top = port_heap_get_top();
size_t size = (heap_top - heap_bottom) * sizeof(uint32_t);
heap = tlsf_create_with_pool(heap_bottom, size, size);
}
MP_WEAK void *port_malloc(size_t size, bool dma_capable) {
void *block = tlsf_malloc(heap, size);
return block;
}
MP_WEAK void port_free(void *ptr) {
tlsf_free(heap, ptr);
}
MP_WEAK void port_realloc(void *ptr, size_t size) {
tlsf_realloc(heap, ptr, size);
}
static void max_size_walker(void *ptr, size_t size, int used, void *user) {
size_t *max_size = (size_t *)user;
if (!used && *max_size < size) {
*max_size = size;
}
}
MP_WEAK size_t port_heap_get_largest_free_size(void) {
size_t max_size = 0;
tlsf_walk_pool(tlsf_get_pool(heap), max_size_walker, &max_size);
// IDF does this. Not sure why.
return tlsf_fit_size(heap, max_size);
}