circuitpython/shared-module/sharpdisplay/SharpMemoryFramebuffer.c

278 lines
9.6 KiB
C
Raw Normal View History

/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Jeff Epler 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 <string.h>
#include "py/gc.h"
#include "shared-bindings/board/__init__.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/sharpdisplay/SharpMemoryFramebuffer.h"
#include "shared-module/sharpdisplay/SharpMemoryFramebuffer.h"
#include "supervisor/memory.h"
#include "supervisor/shared/safe_mode.h"
#define SHARPMEM_BIT_WRITECMD_LSB (0x80)
#define SHARPMEM_BIT_VCOM_LSB (0x40)
static void *hybrid_alloc(size_t sz) {
supervisor_allocation *allocation = allocate_memory(align32_size(sz), false);
if (allocation) {
memset(allocation->ptr, 0, sz);
return allocation->ptr;
}
if (gc_alloc_possible()) {
return m_malloc(sz, true);
}
reset_into_safe_mode(MEM_MANAGE);
return NULL; // unreached
}
static inline void hybrid_free(void *ptr_in) {
supervisor_allocation *allocation = allocation_from_ptr(ptr_in);
if (allocation) {
free_memory(allocation);
}
}
STATIC uint8_t bitrev(uint8_t n) {
uint8_t r = 0;
for(int i=0;i<8;i++) r |= ((n>>i) & 1)<<(7-i);
return r;
}
int common_hal_sharpdisplay_framebuffer_get_width(sharpdisplay_framebuffer_obj_t *self) {
return self->width;
}
int common_hal_sharpdisplay_framebuffer_get_height(sharpdisplay_framebuffer_obj_t *self) {
return self->height;
}
int common_hal_sharpdisplay_framebuffer_get_row_stride(sharpdisplay_framebuffer_obj_t *self) {
return (self->width + 7) / 8 + 2;
}
int common_hal_sharpdisplay_framebuffer_get_first_pixel_offset(sharpdisplay_framebuffer_obj_t *self) {
return 2;
}
bool common_hal_sharpdisplay_framebuffer_get_reverse_pixels_in_byte(sharpdisplay_framebuffer_obj_t *self) {
return true;
}
bool common_hal_sharpdisplay_framebuffer_get_pixels_in_byte_share_row(sharpdisplay_framebuffer_obj_t *self) {
return true;
}
void common_hal_sharpdisplay_framebuffer_reset(sharpdisplay_framebuffer_obj_t *self) {
if (self->bus != &self->inline_bus
#if BOARD_SPI
&& self->bus != common_hal_board_get_spi()
#endif
) {
memcpy(&self->inline_bus, self->bus, sizeof(busio_spi_obj_t));
self->bus = &self->inline_bus;
}
}
void common_hal_sharpdisplay_framebuffer_reconstruct(sharpdisplay_framebuffer_obj_t *self) {
sharpdisplay: Fix memory corruption across soft-reset It was incorrect to NULL out the pointer to our heap allocated buffer in `reset`, because subsequent to framebuffer_reset, but while the heap was still active, we could call `get_bufinfo` again, leading to a fresh allocation on the heap that is about to be destroyed. Typical stack trace: ``` #1 0x0006c368 in sharpdisplay_framebuffer_get_bufinfo #2 0x0006ad6e in _refresh_display #3 0x0006b168 in framebufferio_framebufferdisplay_background #4 0x00069d22 in displayio_background #5 0x00045496 in supervisor_background_tasks #6 0x000446e8 in background_callback_run_all #7 0x00045546 in supervisor_run_background_tasks_if_tick #8 0x0005b042 in common_hal_neopixel_write #9 0x00044c4c in clear_temp_status #10 0x000497de in spi_flash_flush_keep_cache #11 0x00049a66 in supervisor_external_flash_flush #12 0x00044b22 in supervisor_flash_flush #13 0x0004490e in filesystem_flush #14 0x00043e18 in cleanup_after_vm #15 0x0004414c in run_repl #16 0x000441ce in main ``` When this happened -- which was inconsistent -- the display would keep some heap allocation across reset which is exactly what we need to avoid. NULLing the pointer in reconstruct follows what RGBMatrix does, and that code is a bit more battle-tested anyway. If I had a motivation for structuring the SharpMemory code differently, I can no longer recall it. Testing performed: Ran my complicated calculator program over multiple iterations without observing signs of heap corruption. Closes: #3473
2020-10-01 10:21:39 -04:00
if (!allocation_from_ptr(self->bufinfo.buf)) {
self->bufinfo.buf = NULL;
}
}
void common_hal_sharpdisplay_framebuffer_get_bufinfo(sharpdisplay_framebuffer_obj_t *self, mp_buffer_info_t *bufinfo) {
if (!self->bufinfo.buf) {
int row_stride = common_hal_sharpdisplay_framebuffer_get_row_stride(self);
int height = common_hal_sharpdisplay_framebuffer_get_height(self);
self->bufinfo.len = row_stride * height + 2;
self->bufinfo.buf = hybrid_alloc(self->bufinfo.len);
uint8_t *data = self->bufinfo.buf;
*data++ = SHARPMEM_BIT_WRITECMD_LSB;
for(int y=0; y<height; y++) {
*data = bitrev(y+1);
data += row_stride;
}
self->full_refresh = true;
}
*bufinfo = self->bufinfo;
}
void common_hal_sharpdisplay_framebuffer_deinit(sharpdisplay_framebuffer_obj_t *self) {
if (self->base.type != &sharpdisplay_framebuffer_type) {
return;
}
if (self->bus == &self->inline_bus) {
common_hal_busio_spi_deinit(self->bus);
}
common_hal_reset_pin(self->chip_select.pin);
hybrid_free(self->bufinfo.buf);
memset(self, 0, sizeof(*self));
}
void common_hal_sharpdisplay_framebuffer_construct(sharpdisplay_framebuffer_obj_t *self, busio_spi_obj_t *spi, mcu_pin_obj_t *chip_select, int baudrate, int width, int height) {
common_hal_digitalio_digitalinout_construct(&self->chip_select, chip_select);
common_hal_digitalio_digitalinout_switch_to_output(&self->chip_select, true, DRIVE_MODE_PUSH_PULL);
common_hal_never_reset_pin(chip_select);
self->bus = spi;
common_hal_busio_spi_never_reset(self->bus);
self->width = width;
self->height = height;
self->baudrate = baudrate;
int row_stride = common_hal_sharpdisplay_framebuffer_get_row_stride(self);
self->bufinfo.len = row_stride * height + 2;
// re-use a supervisor allocation if possible
self->bufinfo.buf = hybrid_alloc(self->bufinfo.len);
uint8_t *data = self->bufinfo.buf;
*data++ = SHARPMEM_BIT_WRITECMD_LSB;
for(int y=0; y<self->height; y++) {
*data = bitrev(y+1);
data += row_stride;
}
self->full_refresh = true;
}
void common_hal_sharpdisplay_framebuffer_swapbuffers(sharpdisplay_framebuffer_obj_t *self, uint8_t *dirty_row_bitmask) {
// claim SPI bus
if (!common_hal_busio_spi_try_lock(self->bus)) {
return;
}
common_hal_busio_spi_configure(self->bus, self->baudrate, 0, 0, 8);
// set chip select high
common_hal_digitalio_digitalinout_set_value(&self->chip_select, true);
// output the toggling signal
uint8_t *data = self->bufinfo.buf;
data[0] ^= SHARPMEM_BIT_VCOM_LSB;
common_hal_busio_spi_write(self->bus, data++, 1);
// output each changed row
size_t row_stride = common_hal_sharpdisplay_framebuffer_get_row_stride(self);
for(int y=0; y<self->height; y++) {
if(self->full_refresh || (dirty_row_bitmask[y/8] & (1 << (y & 7)))) {
common_hal_busio_spi_write(self->bus, data, row_stride);
}
data += row_stride;
}
// output a trailing zero
common_hal_busio_spi_write(self->bus, data, 1);
// set chip select low
common_hal_digitalio_digitalinout_set_value(&self->chip_select, false);
// release SPI bus
common_hal_busio_spi_unlock(self->bus);
self->full_refresh = false;
}
STATIC void sharpdisplay_framebuffer_deinit(mp_obj_t self_in) {
sharpdisplay_framebuffer_obj_t *self = self_in;
common_hal_sharpdisplay_framebuffer_deinit(self);
}
STATIC void sharpdisplay_framebuffer_get_bufinfo(mp_obj_t self_in, mp_buffer_info_t *bufinfo) {
sharpdisplay_framebuffer_obj_t *self = self_in;
common_hal_sharpdisplay_framebuffer_get_bufinfo(self, bufinfo);
}
STATIC int sharpdisplay_framebuffer_get_color_depth(mp_obj_t self_in) {
return 1;
}
STATIC int sharpdisplay_framebuffer_get_height(mp_obj_t self_in) {
sharpdisplay_framebuffer_obj_t *self = self_in;
return common_hal_sharpdisplay_framebuffer_get_height(self);
}
STATIC int sharpdisplay_framebuffer_get_width(mp_obj_t self_in) {
sharpdisplay_framebuffer_obj_t *self = self_in;
return common_hal_sharpdisplay_framebuffer_get_width(self);
}
STATIC int sharpdisplay_framebuffer_get_first_pixel_offset(mp_obj_t self_in) {
sharpdisplay_framebuffer_obj_t *self = self_in;
return common_hal_sharpdisplay_framebuffer_get_first_pixel_offset(self);
}
STATIC bool sharpdisplay_framebuffer_get_pixels_in_byte_share_row(mp_obj_t self_in) {
sharpdisplay_framebuffer_obj_t *self = self_in;
return common_hal_sharpdisplay_framebuffer_get_pixels_in_byte_share_row(self);
}
STATIC bool sharpdisplay_framebuffer_get_reverse_pixels_in_byte(mp_obj_t self_in) {
sharpdisplay_framebuffer_obj_t *self = self_in;
return common_hal_sharpdisplay_framebuffer_get_reverse_pixels_in_byte(self);
}
STATIC int sharpdisplay_framebuffer_get_row_stride(mp_obj_t self_in) {
sharpdisplay_framebuffer_obj_t *self = self_in;
return common_hal_sharpdisplay_framebuffer_get_row_stride(self);
}
STATIC void sharpdisplay_framebuffer_swapbuffers(mp_obj_t self_in, uint8_t *dirty_row_bitmask) {
sharpdisplay_framebuffer_obj_t *self = self_in;
common_hal_sharpdisplay_framebuffer_swapbuffers(self, dirty_row_bitmask);
}
const framebuffer_p_t sharpdisplay_framebuffer_proto = {
MP_PROTO_IMPLEMENT(MP_QSTR_protocol_framebuffer)
.deinit = sharpdisplay_framebuffer_deinit,
.get_bufinfo = sharpdisplay_framebuffer_get_bufinfo,
.get_color_depth = sharpdisplay_framebuffer_get_color_depth,
.get_height = sharpdisplay_framebuffer_get_height,
.get_width = sharpdisplay_framebuffer_get_width,
.swapbuffers = sharpdisplay_framebuffer_swapbuffers,
.get_first_pixel_offset = sharpdisplay_framebuffer_get_first_pixel_offset,
.get_pixels_in_byte_share_row = sharpdisplay_framebuffer_get_pixels_in_byte_share_row,
.get_reverse_pixels_in_byte = sharpdisplay_framebuffer_get_reverse_pixels_in_byte,
.get_row_stride = sharpdisplay_framebuffer_get_row_stride,
};
void common_hal_sharpdisplay_framebuffer_collect_ptrs(sharpdisplay_framebuffer_obj_t *self) {
gc_collect_ptr(self->framebuffer);
gc_collect_ptr(self->bus);
gc_collect_ptr(self->bufinfo.buf);
}