1 /**
2 * @file lv_draw_nema_gfx_hal.c
3 *
4 * Global functions that implement some HAL functionality
5 * which Nema will call directly.
6 */
7
8 /*********************
9 * INCLUDES
10 *********************/
11
12 #include "../../lv_conf_internal.h"
13 #if LV_USE_NEMA_GFX
14
15 #if LV_USE_NEMA_HAL == LV_NEMA_HAL_STM32
16
17 #include "../../misc/lv_types.h"
18 #include "../../misc/lv_assert.h"
19 #include "../../stdlib/lv_string.h"
20
21 #include <nema_sys_defs.h>
22 #include <nema_core.h>
23
24 #include <assert.h>
25 #include <string.h>
26
27 #include LV_NEMA_STM32_HAL_INCLUDE
28
29 #include <cmsis_os2.h>
30
31 #include "tsi_malloc.h"
32
33 extern GPU2D_HandleTypeDef hgpu2d;
34
35 /*********************
36 * DEFINES
37 *********************/
38
39 #define RING_SIZE 1024 /* Ring Buffer Size in byte */
40
41 /* NemaGFX byte pool size in bytes.
42 * One byte per peixel for masking/stencling plus 10240 for additional allocations.
43 */
44 #if defined(LV_NEMA_GFX_MAX_RESX) && defined(LV_NEMA_GFX_MAX_RESY)
45 #define NEMAGFX_MEM_POOL_SIZE ((LV_NEMA_GFX_MAX_RESX * LV_NEMA_GFX_MAX_RESY) + 10240)
46 #else
47 /* LV_USE_NEMA_VG is 0 so masking/stencling memory is not needed. */
48 #define NEMAGFX_MEM_POOL_SIZE 10240
49 #endif
50
51 /**********************
52 * TYPEDEFS
53 **********************/
54
55 /**********************
56 * STATIC PROTOTYPES
57 **********************/
58
59 #if (USE_HAL_GPU2D_REGISTER_CALLBACKS == 1)
60 static void GPU2D_CommandListCpltCallback(GPU2D_HandleTypeDef * hgpu2d, uint32_t CmdListID);
61 #endif
62
63 /**********************
64 * STATIC VARIABLES
65 **********************/
66
67 static uint8_t nemagfx_pool_mem[NEMAGFX_MEM_POOL_SIZE]; /* NemaGFX memory pool */
68
69 static nema_ringbuffer_t ring_buffer_str;
70 static volatile int last_cl_id = -1;
71
72 //static osSemaphoreId_t nema_irq_sem = NULL; // Declare CL IRQ semaphore
73
74 /**********************
75 * MACROS
76 **********************/
77
78 /**********************
79 * GLOBAL FUNCTIONS
80 **********************/
81
82 #if (USE_HAL_GPU2D_REGISTER_CALLBACKS == 1)
GPU2D_CommandListCpltCallback(GPU2D_HandleTypeDef * hgpu2d,uint32_t CmdListID)83 static void GPU2D_CommandListCpltCallback(GPU2D_HandleTypeDef * hgpu2d, uint32_t CmdListID)
84 #else
85 void HAL_GPU2D_CommandListCpltCallback(GPU2D_HandleTypeDef * hgpu2d, uint32_t CmdListID)
86 #endif
87 {
88 LV_UNUSED(hgpu2d);
89
90 last_cl_id = CmdListID;
91
92 /* Return a token back to a semaphore */
93 // osSemaphoreRelease(nema_irq_sem);
94 }
95
nema_sys_init(void)96 int32_t nema_sys_init(void)
97 {
98 int error_code = 0;
99
100 /* Setup GPU2D Callback */
101 #if (USE_HAL_GPU2D_REGISTER_CALLBACKS == 1)
102 /* Register Command List Complete Callback */
103 HAL_GPU2D_RegisterCommandListCpltCallback(&hgpu2d, GPU2D_CommandListCpltCallback);
104 #endif
105
106 /* Create IRQ semaphore */
107 // nema_irq_sem = osSemaphoreNew(1, 1, NULL);
108 // assert(nema_irq_sem != NULL);
109
110 /* Initialise Mem Space */
111 error_code = tsi_malloc_init_pool_aligned(0, (void *)nemagfx_pool_mem, (uintptr_t)nemagfx_pool_mem,
112 NEMAGFX_MEM_POOL_SIZE, 1, 8);
113 LV_ASSERT(error_code == 0);
114
115 /* Allocate ring_buffer memory */
116 ring_buffer_str.bo = nema_buffer_create(RING_SIZE);
117 LV_ASSERT(ring_buffer_str.bo.base_virt);
118
119 /* Initialize Ring Buffer */
120 error_code = nema_rb_init(&ring_buffer_str, 1);
121 if(error_code < 0) {
122 return error_code;
123 }
124
125 /* Reset last_cl_id counter */
126 last_cl_id = 0;
127
128 return error_code;
129 }
130
nema_reg_read(uint32_t reg)131 uint32_t nema_reg_read(uint32_t reg)
132 {
133 return HAL_GPU2D_ReadRegister(&hgpu2d, reg);
134 }
135
nema_reg_write(uint32_t reg,uint32_t value)136 void nema_reg_write(uint32_t reg, uint32_t value)
137 {
138 HAL_GPU2D_WriteRegister(&hgpu2d, reg, value);
139 }
140
141
142
143
nema_wait_irq(void)144 int nema_wait_irq(void)
145 {
146 return 0;
147 }
148
149
nema_wait_irq_cl(int cl_id)150 int nema_wait_irq_cl(int cl_id)
151 {
152 while(last_cl_id < cl_id) {
153 (void)nema_wait_irq();
154 }
155
156 return 0;
157 }
158
nema_wait_irq_brk(int brk_id)159 int nema_wait_irq_brk(int brk_id)
160 {
161 while(nema_reg_read(GPU2D_BREAKPOINT) == 0U) {
162 (void)nema_wait_irq();
163 }
164
165 return 0;
166 }
167
nema_host_free(void * ptr)168 void nema_host_free(void * ptr)
169 {
170 tsi_free(ptr);
171 }
172
nema_host_malloc(unsigned size)173 void * nema_host_malloc(unsigned size)
174 {
175 return tsi_malloc(size);
176 }
177
nema_buffer_create(int size)178 nema_buffer_t nema_buffer_create(int size)
179 {
180 nema_buffer_t bo;
181 lv_memset(&bo, 0, sizeof(bo));
182 bo.base_virt = tsi_malloc(size);
183 bo.base_phys = (uint32_t)bo.base_virt;
184 bo.size = size;
185 LV_ASSERT_MSG(bo.base_virt != 0, "Unable to allocate memory in nema_buffer_create");
186
187 return bo;
188 }
189
nema_buffer_create_pool(int pool,int size)190 nema_buffer_t nema_buffer_create_pool(int pool, int size)
191 {
192 LV_UNUSED(pool);
193
194 return nema_buffer_create(size);
195 }
196
nema_buffer_map(nema_buffer_t * bo)197 void * nema_buffer_map(nema_buffer_t * bo)
198 {
199 return bo->base_virt;
200 }
201
nema_buffer_unmap(nema_buffer_t * bo)202 void nema_buffer_unmap(nema_buffer_t * bo)
203 {
204 LV_UNUSED(bo);
205 }
206
nema_buffer_destroy(nema_buffer_t * bo)207 void nema_buffer_destroy(nema_buffer_t * bo)
208 {
209 if(bo->fd == -1) {
210 return; /* Buffer weren't allocated! */
211 }
212
213 tsi_free(bo->base_virt);
214
215 bo->base_virt = (void *)0;
216 bo->base_phys = 0;
217 bo->size = 0;
218 bo->fd = -1; /* Buffer not allocated */
219 }
220
nema_buffer_phys(nema_buffer_t * bo)221 uintptr_t nema_buffer_phys(nema_buffer_t * bo)
222 {
223 return bo->base_phys;
224 }
225
nema_buffer_flush(nema_buffer_t * bo)226 void nema_buffer_flush(nema_buffer_t * bo)
227 {
228 LV_UNUSED(bo);
229 }
230
nema_mutex_lock(int mutex_id)231 int nema_mutex_lock(int mutex_id)
232 {
233 int retval = 0;
234
235 LV_UNUSED(mutex_id);
236
237 return retval;
238 }
239
nema_mutex_unlock(int mutex_id)240 int nema_mutex_unlock(int mutex_id)
241 {
242 int retval = 0;
243
244 LV_UNUSED(mutex_id);
245
246 return retval;
247 }
248
platform_disable_cache(void)249 void platform_disable_cache(void)
250 {
251
252 }
253
platform_invalidate_cache(void)254 void platform_invalidate_cache(void)
255 {
256
257 }
258
259 /**********************
260 * STATIC FUNCTIONS
261 **********************/
262
263 #endif /* LV_USE_NEMA_HAL == LV_NEMA_HAL_STM32 */
264
265 #endif /* LV_USE_NEMA_GFX */
266