3434#include "shared-module/sharpdisplay/SharpMemoryFramebuffer.h"
3535
3636#include "supervisor/memory.h"
37- #include "supervisor/shared/safe_mode.h"
3837
3938#define SHARPMEM_BIT_WRITECMD_LSB (0x80)
4039#define SHARPMEM_BIT_VCOM_LSB (0x40)
4140
42- static void * hybrid_alloc (size_t sz ) {
43- supervisor_allocation * allocation = allocate_memory (align32_size (sz ), false, false);
44- if (allocation ) {
45- memset (allocation -> ptr , 0 , sz );
46- return allocation -> ptr ;
47- }
48- if (gc_alloc_possible ()) {
49- return m_malloc (sz , true);
50- }
51- reset_into_safe_mode (MEM_MANAGE );
52- return NULL ; // unreached
53- }
54-
55- static inline void hybrid_free (void * ptr_in ) {
56- supervisor_allocation * allocation = allocation_from_ptr (ptr_in );
57-
58- if (allocation ) {
59- free_memory (allocation );
60- }
61- }
62-
6341STATIC uint8_t bitrev (uint8_t n ) {
6442 uint8_t r = 0 ;
6543 for (int i = 0 ;i < 8 ;i ++ ) r |= ((n >>i ) & 1 )<<(7 - i );
@@ -102,17 +80,22 @@ void common_hal_sharpdisplay_framebuffer_reset(sharpdisplay_framebuffer_obj_t *s
10280}
10381
10482void common_hal_sharpdisplay_framebuffer_reconstruct (sharpdisplay_framebuffer_obj_t * self ) {
105- if (! allocation_from_ptr ( self -> bufinfo . buf )) {
106- self -> bufinfo .buf = NULL ;
107- }
83+ // Look up the allocation by the old pointer and get the new pointer from it.
84+ supervisor_allocation * alloc = allocation_from_ptr ( self -> bufinfo .buf ) ;
85+ self -> bufinfo . buf = alloc ? alloc -> ptr : NULL ;
10886}
10987
11088void common_hal_sharpdisplay_framebuffer_get_bufinfo (sharpdisplay_framebuffer_obj_t * self , mp_buffer_info_t * bufinfo ) {
11189 if (!self -> bufinfo .buf ) {
11290 int row_stride = common_hal_sharpdisplay_framebuffer_get_row_stride (self );
11391 int height = common_hal_sharpdisplay_framebuffer_get_height (self );
11492 self -> bufinfo .len = row_stride * height + 2 ;
115- self -> bufinfo .buf = hybrid_alloc (self -> bufinfo .len );
93+ supervisor_allocation * alloc = allocate_memory (align32_size (self -> bufinfo .len ), false, true);
94+ if (alloc == NULL ) {
95+ m_malloc_fail (self -> bufinfo .len );
96+ }
97+ self -> bufinfo .buf = alloc -> ptr ;
98+ memset (alloc -> ptr , 0 , self -> bufinfo .len );
11699
117100 uint8_t * data = self -> bufinfo .buf ;
118101 * data ++ = SHARPMEM_BIT_WRITECMD_LSB ;
@@ -123,7 +106,9 @@ void common_hal_sharpdisplay_framebuffer_get_bufinfo(sharpdisplay_framebuffer_ob
123106 }
124107 self -> full_refresh = true;
125108 }
126- * bufinfo = self -> bufinfo ;
109+ if (bufinfo ) {
110+ * bufinfo = self -> bufinfo ;
111+ }
127112}
128113
129114void common_hal_sharpdisplay_framebuffer_deinit (sharpdisplay_framebuffer_obj_t * self ) {
@@ -137,7 +122,7 @@ void common_hal_sharpdisplay_framebuffer_deinit(sharpdisplay_framebuffer_obj_t *
137122
138123 common_hal_reset_pin (self -> chip_select .pin );
139124
140- hybrid_free ( self -> bufinfo .buf );
125+ free_memory ( allocation_from_ptr ( self -> bufinfo .buf ) );
141126
142127 memset (self , 0 , sizeof (* self ));
143128}
@@ -154,19 +139,7 @@ void common_hal_sharpdisplay_framebuffer_construct(sharpdisplay_framebuffer_obj_
154139 self -> height = height ;
155140 self -> baudrate = baudrate ;
156141
157- int row_stride = common_hal_sharpdisplay_framebuffer_get_row_stride (self );
158- self -> bufinfo .len = row_stride * height + 2 ;
159- // re-use a supervisor allocation if possible
160- self -> bufinfo .buf = hybrid_alloc (self -> bufinfo .len );
161-
162- uint8_t * data = self -> bufinfo .buf ;
163- * data ++ = SHARPMEM_BIT_WRITECMD_LSB ;
164-
165- for (int y = 0 ; y < self -> height ; y ++ ) {
166- * data = bitrev (y + 1 );
167- data += row_stride ;
168- }
169- self -> full_refresh = true;
142+ common_hal_sharpdisplay_framebuffer_get_bufinfo (self , NULL );
170143}
171144
172145void common_hal_sharpdisplay_framebuffer_swapbuffers (sharpdisplay_framebuffer_obj_t * self , uint8_t * dirty_row_bitmask ) {
@@ -271,7 +244,5 @@ const framebuffer_p_t sharpdisplay_framebuffer_proto = {
271244};
272245
273246void common_hal_sharpdisplay_framebuffer_collect_ptrs (sharpdisplay_framebuffer_obj_t * self ) {
274- gc_collect_ptr (self -> framebuffer );
275247 gc_collect_ptr (self -> bus );
276- gc_collect_ptr (self -> bufinfo .buf );
277248}
0 commit comments