3
3
#include "softtimer.h"
4
4
#include <stdint.h>
5
5
#include <stdbool.h>
6
+ #include <stdlib.h>
6
7
#include "../../../lvgl/lvgl.h"
7
8
#include "../../../lv_conf.h"
8
9
#include "../../include/common.h"
@@ -25,20 +26,27 @@ LTDC_HandleTypeDef *hltdc = NULL; // handle to LTDC, referenced in stm
25
26
DMA2D_HandleTypeDef * hdma2d = NULL ; // handle to DMA2D, referenced in stm32_it.c
26
27
i2c_t * i2c_ts = NULL ; // I2C handle for touchscreen
27
28
struct _disp_drv_t * dma2d_disp_drv = NULL ; // handle to display driver
28
- lv_color_t * fb = NULL ; // framebuffer pointer
29
+ lv_color_t * fb [ 2 ] = { NULL , NULL } ; // framebuffer pointers
29
30
uint32_t w = 0 ; // display width
30
31
uint32_t h = 0 ; // display height
31
32
volatile bool dma2d_pend = false; // flag of DMA2D pending operation
32
33
33
34
static bool config_ltdc (void );
34
35
static bool config_dma2d (void );
35
36
36
- STATIC mp_obj_t mp_rk043fn48h_bytearray (mp_obj_t n_obj ) {
37
- size_t n = (size_t )mp_obj_get_int (n_obj );
38
- // allocation on extRAM with 1KB alignment
39
- void * p = m_malloc (n + 1024 );
40
- p = (void * )((uint32_t )p + 1024 - (uint32_t )p % 1024 );
41
- <
10000
span class=pl-k>return mp_obj_new_bytearray_by_ref (n , (void * )p );
37
+ STATIC mp_obj_t mp_rk043fn48h_framebuffer (mp_obj_t n_obj ) {
38
+ int n = mp_obj_get_int (n_obj ) - 1 ;
39
+
40
+ if (n < 0 || n > 1 ){
41
+ return mp_const_none ;
42
+ }
43
+
44
+ if (fb [n ]== NULL ){
45
+ // allocation on extRAM with 1KB alignment to speed up LTDC burst access on AHB
46
+ fb [n ] = MP_STATE_PORT (rk043fn48h_fb [n ]) = m_malloc (sizeof (lv_color_t ) * w * h + 1024 );
47
+ fb [n ] = (lv_color_t * )((uint32_t )fb [n ] + 1024 - (uint32_t )fb [n ] % 1024 );
48
+ }
49
+ return mp_obj_new_bytearray_by_ref (sizeof (lv_color_t ) * w * h , (void * )fb [n ]);
42
50
}
43
51
44
52
STATIC mp_obj_t mp_rk043fn48h_init (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
@@ -55,10 +63,9 @@ STATIC mp_obj_t mp_rk043fn48h_init(size_t n_args, const mp_obj_t *pos_args, mp_m
55
63
w = args [ARG_w ].u_int ;
56
64
h = args [ARG_h ].u_int ;
57
65
58
- uint32_t p = (uint32_t )m_malloc (sizeof (lv_color_t ) * w * h + 1024 );
59
- fb = (lv_color_t * )(p + 1024 - p % 1024 ); //speeds up LTDC burst access on AHB
66
+ mp_rk043fn48h_framebuffer (mp_obj_new_int (1 ));
60
67
61
- if (fb == NULL ) {
68
+ if (fb [ 0 ] == NULL ) {
62
69
nlr_raise (mp_obj_new_exception_msg (& mp_type_RuntimeError , "Failed allocating frame buffer" ));
63
70
}
64
71
@@ -91,8 +98,16 @@ STATIC mp_obj_t mp_rk043fn48h_deinit() {
91
98
HAL_GPIO_WritePin (LCD_DISP_GPIO_PORT , LCD_DISP_PIN , GPIO_PIN_RESET );
92
99
HAL_GPIO_WritePin (LCD_BL_CTRL_GPIO_PORT , LCD_BL_CTRL_PIN , GPIO_PIN_RESET );
93
100
hltdc = NULL ;
94
- m_free (fb );
95
- fb = NULL ;
101
+ }
102
+
103
+ if (fb [0 ]!= NULL ){
104
+ m_free (MP_STATE_PORT (rk043fn48h_fb [0 ]));
105
+ fb [0 ]= NULL ;
106
+ }
107
+
108
+ if (fb [1 ]!= NULL ){
109
+ m_free (MP_STATE_PORT (rk043fn48h_fb [1 ]));
110
+ fb [1 ]= NULL ;
96
111
}
97
112
98
113
BSP_TS_DeInit ();
@@ -115,7 +130,7 @@ STATIC void mp_rk043fn48h_flush(struct _disp_drv_t *disp_drv, const lv_area_t *a
115
130
HAL_DMA2D_Init (hdma2d );
116
131
HAL_DMA2D_Start_IT (hdma2d ,
117
132
(uint32_t )color_p ,
118
- (uint32_t )(fb + area -> x1 + area -> y1 * w ),
133
+ (uint32_t )(fb [ 0 ] + area -> x1 + area -> y1 * w ),
119
134
lv_area_get_width (area ),
120
135
lv_area_get_height (area ));
121
136
}
@@ -188,7 +203,7 @@ STATIC bool mp_rk043fn48h_ts_read(struct _lv_indev_drv_t *indev_drv, lv_indev_da
188
203
189
204
STATIC MP_DEFINE_CONST_FUN_OBJ_KW (mp_rk043fn48h_init_obj , 0 , mp_rk043fn48h_init );
190
205
STATIC MP_DEFINE_CONST_FUN_OBJ_0 (mp_rk043fn48h_deinit_obj , mp_rk043fn48h_deinit );
191
- STATIC MP_DEFINE_CONST_FUN_OBJ_1 (mp_rk043fn48h_bytearray_obj , mp_rk043fn48h_bytearray );
206
+ STATIC MP_DEFINE_CONST_FUN_OBJ_1 (mp_rk043fn48h_framebuffer_obj , mp_rk043fn48h_framebuffer );
192
207
DEFINE_PTR_OBJ (mp_rk043fn48h_flush );
193
208
DEFINE_PTR_OBJ (mp_rk043fn48h_gpu_blend );
194
209
DEFINE_PTR_OBJ (mp_rk043fn48h_gpu_fill );
@@ -202,7 +217,7 @@ STATIC const mp_rom_map_elem_t rk043fn48h_globals_table[] = {
202
217
{ MP_ROM_QSTR (MP_QSTR_gpu_blend ), MP_ROM_PTR (& PTR_OBJ (mp_rk043fn48h_gpu_blend ))},
203
218
{ MP_ROM_QSTR (MP_QSTR_gpu_fill ), MP_ROM_PTR (& PTR_OBJ (mp_rk043fn48h_gpu_fill ))},
204
219
{ MP_ROM_QSTR (MP_QSTR_ts_read ), MP_ROM_PTR (& PTR_OBJ (mp_rk043fn48h_ts_read ))},
205
- { MP_ROM_QSTR (MP_QSTR_bytearray ), MP_ROM_PTR (& PTR_OBJ (mp_rk043fn48h_bytearray ))},
220
+ { MP_ROM_QSTR (MP_QSTR_framebuffer ), MP_ROM_PTR (& PTR_OBJ (mp_rk043fn48h_framebuffer ))},
206
221
};
207
222
208
223
STATIC MP_DEFINE_CONST_DICT (
@@ -260,7 +275,7 @@ static bool config_ltdc(void) {
260
275
#error "modrk043fn48h: LV_COLOR_DEPTH not supported"
261
276
#endif
262
277
263
- pLayerCfg .FBStartAdress = (uint32_t )fb ;
278
+ pLayerCfg .FBStartAdress = (uint32_t )fb [ 0 ] ;
264
279
pLayerCfg .Alpha = 255 ;
265
280
pLayerCfg .Alpha0 = 0 ;
266
281
pLayerCfg .Backcolor .Blue = 0 ;
0 commit comments