source: azure_iot_hub_f767zi/trunk/asp_baseplatform/gdic/stlcd_st7789h2/stlcd_st7789h2.c@ 457

Last change on this file since 457 was 457, checked in by coas-nagasima, 4 years ago

ファイルを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 40.8 KB
Line 
1/*
2 * TOPPERS BASE PLATFORM MIDDLEWARE
3 *
4 * Copyright (C) 2017-2018 by TOPPERS PROJECT
5 * Educational Working Group.
6 *
7 * 上記著作権者は,以下の(1)~(4)の条件を満たす場合に限り,本ソフトウェ
8 * ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
9 * 変・再配布(以下,利用と呼ぶ)することを無償で許諾する.
10 * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
11 * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
12 * スコード中に含まれていること.
13 * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
14 * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
15 * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
16 * の無保証規定を掲載すること.
17 * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
18 * 用できない形で再配布する場合には,次のいずれかの条件を満たすこ
19 * と.
20 * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
21 * 作権表示,この利用条件および下記の無保証規定を掲載すること.
22 * (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
23 * 報告すること.
24 * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
25 * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
26 * また,本ソフトウェアのユーザまたはエンドユーザからのいかなる理
27 * 由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
28 * 免責すること.
29 *
30 * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
31 * よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
32 * に対する適合性も含めて,いかなる保証も行わない.また,本ソフトウェ
33 * アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
34 * の責任を負わない.
35 *
36 * @(#) $Id$
37 */
38/**
39 ******************************************************************************
40 * @attention
41 *
42 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
43 *
44 * Redistribution and use in source and binary forms, with or without modification,
45 * are permitted provided that the following conditions are met:
46 * 1. Redistributions of source code must retain the above copyright notice,
47 * this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright notice,
49 * this list of conditions and the following disclaimer in the documentation
50 * and/or other materials provided with the distribution.
51 * 3. Neither the name of STMicroelectronics nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
56 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
58 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
61 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
62 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
63 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
64 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65 *
66 ******************************************************************************
67 */
68
69#include "stlcd_st7789h2.h"
70
71#define sil_orw_mem(a, b) sil_wrw_mem((a), sil_rew_mem(a) | (b))
72#define sil_andw_mem(a, b) sil_wrw_mem((a), sil_rew_mem(a) & ~(b))
73#define sil_modw_mem(a, b, c) sil_wrw_mem((a), (sil_rew_mem(a) & (~b)) | (c))
74
75
76#define POLY_X(Z) ((int32_t)((Points + Z)->X))
77#define POLY_Y(Z) ((int32_t)((Points + Z)->Y))
78#define ABS(X) ((X) > 0 ? (X) : -(X))
79
80#define FMC_BANK2_BASE ((uint32_t)(0x60000000 | 0x04000000))
81#define GPIO_AF12_FMC ((uint8_t)0xCU) /* FMC Alternate Function mapping */
82
83#define GIOD_PINMAP (GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_7 | GPIO_PIN_8 |\
84 GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_14 | GPIO_PIN_15)
85#define GIOE_PINMAP (GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9 |GPIO_PIN_10 |\
86 GPIO_PIN_11 | GPIO_PIN_12 |GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15)
87#define GIOF_PINMAP (GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 |\
88 GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15)
89#define GIOG_PINMAP (GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_9)
90
91
92typedef struct {
93 uint8_t red;
94 uint8_t green;
95 uint8_t blue;
96} ST7789H2_Rgb888;
97
98#define LCD_IO_WriteData(b, r) do { \
99 sil_wrh_mem((uint16_t *)((b)->fmc_base+2), (r)); \
100 asm ("dsb 0xF":::"memory"); \
101 } while(0)
102
103#define LCD_IO_WriteReg(b, r) do { \
104 sil_wrh_mem((uint16_t *)((b)->fmc_base), (r)); \
105 asm ("dsb 0xF":::"memory"); \
106 } while(0)
107
108#define LCD_IO_ReadData(b) (uint16_t)sil_reh_mem((uint16_t *)((b)->fmc_base+2))
109
110
111/** @defgroup STM32F723E_DISCOVERY_LCD_Private_FunctionPrototypes STM32F723E Discovery Lcd Private Prototypes
112 * @{
113 */
114
115static void FillTriangle(LCD_DrawProp_t *pDrawProp, uint16_t x1, uint16_t x2, uint16_t x3, uint16_t y1, uint16_t y2, uint16_t y3);
116
117
118/**
119 * @brief Initializes LCD low level.
120 * @retval None
121 */
122static void LCD_IO_Init(void)
123{
124 GPIO_Init_t GPIO_Init_Data;
125 uint32_t pin, i;
126 uint32_t tmpr = 0;
127
128 /* Enable FMC clock */
129 sil_orw_mem((uint32_t *)(TADR_RCC_BASE+TOFF_RCC_AHB3ENR), RCC_AHB3ENR_FMCEN);
130
131 /* Enable FSMC clock */
132 sil_orw_mem((uint32_t *)(TADR_RCC_BASE+TOFF_RCC_AHB3ENR), RCC_AHB3ENR_FMCEN);
133 sil_orw_mem((uint32_t *)(TADR_RCC_BASE+TOFF_RCC_AHB3RSTR), RCC_AHB3RSTR_FMCRST);
134 sil_andw_mem((uint32_t *)(TADR_RCC_BASE+TOFF_RCC_AHB3RSTR), RCC_AHB3RSTR_FMCRST);
135
136 /* Enable GPIOs clock */
137 sil_orw_mem((uint32_t *)(TADR_RCC_BASE+TOFF_RCC_AHB1ENR), RCC_AHB1ENR_GPIODEN);
138 sil_orw_mem((uint32_t *)(TADR_RCC_BASE+TOFF_RCC_AHB1ENR), RCC_AHB1ENR_GPIOEEN);
139 sil_orw_mem((uint32_t *)(TADR_RCC_BASE+TOFF_RCC_AHB1ENR), RCC_AHB1ENR_GPIOFEN);
140 sil_orw_mem((uint32_t *)(TADR_RCC_BASE+TOFF_RCC_AHB1ENR), RCC_AHB1ENR_GPIOGEN);
141
142 GPIO_Init_Data.mode = GPIO_MODE_AF;
143 GPIO_Init_Data.pull = GPIO_PULLUP;
144 GPIO_Init_Data.otype = GPIO_OTYPE_PP;
145 GPIO_Init_Data.speed = GPIO_SPEED_HIGH;
146 GPIO_Init_Data.alternate = GPIO_AF12_FMC;
147
148 /* GPIOD configuration */
149 /* LCD_PSRAM_D2, LCD_PSRAM_D3, LCD_PSRAM_NOE, LCD_PSRAM_NWE, PSRAM_NE1, LCD_PSRAM_D13,
150 LCD_PSRAM_D14, LCD_PSRAM_D15, PSRAM_A16, PSRAM_A17, LCD_PSRAM_D0, LCD_PSRAM_D1 */
151 pin = GIOD_PINMAP;
152 for(i = 0 ; i < 16 ; i++){
153 if((pin & (1<<i)) != 0)
154 gpio_setup(TADR_GPIOD_BASE, &GPIO_Init_Data, i);
155 }
156
157 /* GPIOE configuration */
158 /* PSRAM_NBL0, PSRAM_NBL1, LCD_PSRAM_D4, LCD_PSRAM_D5, LCD_PSRAM_D6, LCD_PSRAM_D7,
159 LCD_PSRAM_D8, LCD_PSRAM_D9, LCD_PSRAM_D10, LCD_PSRAM_D11, LCD_PSRAM_D12 */
160 pin = GIOE_PINMAP;
161 for(i = 0 ; i < 16 ; i++){
162 if((pin & (1<<i)) != 0)
163 gpio_setup(TADR_GPIOE_BASE, &GPIO_Init_Data, i);
164 }
165
166 /* GPIOF configuration */
167 /* PSRAM_A0, PSRAM_A1, PSRAM_A2, PSRAM_A3, PSRAM_A4, PSRAM_A5,
168 PSRAM_A6, PSRAM_A7, PSRAM_A8, PSRAM_A9 */
169 pin = GIOF_PINMAP;
170 for(i = 0 ; i < 16 ; i++){
171 if((pin & (1<<i)) != 0)
172 gpio_setup(TADR_GPIOF_BASE, &GPIO_Init_Data, i);
173 }
174
175 /* GPIOG configuration */
176 /* PSRAM_A10, PSRAM_A11, PSRAM_A12, PSRAM_A13, PSRAM_A14, PSRAM_A15, LCD_NE */
177 pin = GIOG_PINMAP;
178 for(i = 0 ; i < 16 ; i++){
179 if((pin & (1<<i)) != 0)
180 gpio_setup(TADR_GPIOG_BASE, &GPIO_Init_Data, i);
181 }
182
183 /* Initialize SRAM control Interface */
184 tmpr = sil_rew_mem((uint32_t *)(TADR_FMC_R_BASE+TOFF_FMC_R_BTCR2));
185
186 /* Clear MBKEN, MUXEN, MTYP, MWID, FACCEN, BURSTEN, WAITPOL, WAITCFG, WREN,
187 WAITEN, EXTMOD, ASYNCWAIT, CBURSTRW and CCLKEN bits */
188 tmpr &= ((uint32_t)~(FMC_BCR1_MBKEN | FMC_BCR1_MUXEN | FMC_BCR1_MTYP | \
189 FMC_BCR1_MWID | FMC_BCR1_FACCEN | FMC_BCR1_BURSTEN | \
190 FMC_BCR1_WAITPOL | FMC_BCR1_CPSIZE | FMC_BCR1_WAITCFG | \
191 FMC_BCR1_WREN | FMC_BCR1_WAITEN | FMC_BCR1_EXTMOD | \
192 FMC_BCR1_ASYNCWAIT | FMC_BCR1_CBURSTRW | FMC_BCR1_CCLKEN | FMC_BCR1_WFDIS));
193
194 /* Set NORSRAM device control parameters */
195 tmpr |= (uint32_t)(FMC_BCR1_MWID_0 | FMC_BCR1_WREN | FMC_BCR1_WFDIS);
196
197 sil_wrw_mem((uint32_t *)(TADR_FMC_R_BASE+TOFF_FMC_R_BTCR2), tmpr);
198
199 /* Configure synchronous mode when Continuous clock is enabled for bank2..4 */
200 sil_orw_mem((uint32_t *)(TADR_FMC_R_BASE+TOFF_FMC_R_BTCR0), FMC_BCR1_WFDIS);
201
202 /* Get the BTCR register value */
203 tmpr = sil_rew_mem((uint32_t *)(TADR_FMC_R_BASE+TOFF_FMC_R_BTCR3));
204
205 /* Clear ADDSET, ADDHLD, DATAST, BUSTURN, CLKDIV, DATLAT and ACCMOD bits */
206 tmpr &= ((uint32_t)~(FMC_BTR1_ADDSET | FMC_BTR1_ADDHLD | FMC_BTR1_DATAST | \
207 FMC_BTR1_BUSTURN | FMC_BTR1_CLKDIV | FMC_BTR1_DATLAT | \
208 FMC_BTR1_ACCMOD));
209
210 /* Set FMC_NORSRAM device timing parameters */
211 tmpr |= (uint32_t)(9 /* AddressSetupTime */ |\
212 ((2 /* AddressHoldTime */ ) << 4) |\
213 ((6 /* DataSetupTime */ ) << 8) |\
214 ((1 /* BusTurnAroundDuration */ ) << 16) |\
215 (((2 /* CLKDivision */ )-1) << 20) |\
216 (((2 /* DataLatency */)-2) << 24));
217
218 sil_wrw_mem((uint32_t *)(TADR_FMC_R_BASE+TOFF_FMC_R_BTCR3), tmpr);
219
220 /* Configure Clock division value (in NORSRAM bank 1) when continuous clock is enabled */
221 if((sil_rew_mem((uint32_t *)(TADR_FMC_R_BASE+TOFF_FMC_R_BTCR1)) & FMC_BCR1_CCLKEN) != 0){
222 tmpr = sil_rew_mem((uint32_t *)(TADR_FMC_R_BASE+TOFF_FMC_R_BTCR1));
223 tmpr &= ~(((uint32_t)0x0F) << 20);
224 tmpr |= (uint32_t)(((2 /* CLKDivision */ )-1) << 20);
225 sil_wrw_mem((uint32_t *)(TADR_FMC_R_BASE+TOFF_FMC_R_BTCR1), tmpr);
226 }
227
228 /* Initialize SRAM extended mode timing Interface */
229 sil_wrw_mem((uint32_t *)(TADR_FMC_R_BASE+TOFF_FMC_R_BWTR2), 0xFFFFFFFF);
230
231 /* Enable the NORSRAM device */
232 sil_orw_mem((uint32_t *)(TADR_FMC_R_BASE+TOFF_FMC_R_BTCR2), FMC_BCR1_MBKEN);
233}
234
235
236/**
237 * @brief Writes to the selected LCD register.
238 * @param Command: command value (or register address as named in st7789h2 doc).
239 * @param Parameters: pointer on parameters value (if command uses one or several parameters).
240 * @param NbParameters: number of command parameters (0 if no parameter)
241 * @retval None
242 */
243static void
244ST7789H2_WriteReg(LCD_Handler_t *hlcd, uint8_t Command, uint8_t *Parameters, uint8_t NbParameters)
245{
246 uint8_t i;
247
248 /* Send command */
249 LCD_IO_WriteReg(hlcd, Command);
250
251 /* Send command's parameters if any */
252 for(i = 0 ; i<NbParameters; i++){
253 LCD_IO_WriteData(hlcd, Parameters[i]);
254 }
255}
256
257/**
258 * @brief Reads the selected LCD Register.
259 * @param Command: command value (or register address as named in st7789h2 doc).
260 * @retval Register Value.
261 */
262static uint8_t
263ST7789H2_ReadReg(LCD_Handler_t *hlcd, uint8_t Command)
264{
265 /* Send command */
266 LCD_IO_WriteReg(hlcd, Command);
267
268 /* Read dummy data */
269 LCD_IO_ReadData(hlcd);
270
271 /* Read register value */
272 return (LCD_IO_ReadData(hlcd));
273}
274
275/**
276 * @brief Initialize the st7789h2 LCD Component.
277 * @param None
278 * @retval None
279 */
280static void
281ST7789H2_Init(LCD_Handler_t *hlcd)
282{
283 uint8_t parameter[14];
284
285 /* Initialize st7789h2 low level bus layer ----------------------------------*/
286 /* Sleep In Command */
287 ST7789H2_WriteReg(hlcd, ST7789H2_SLEEP_IN, (uint8_t*)NULL, 0);
288 /* Wait for 10ms */
289 dly_tsk(10);
290
291 /* SW Reset Command */
292 ST7789H2_WriteReg(hlcd, 0x01, (uint8_t*)NULL, 0);
293 /* Wait for 200ms */
294 dly_tsk(200);
295
296 /* Sleep Out Command */
297 ST7789H2_WriteReg(hlcd, ST7789H2_SLEEP_OUT, (uint8_t*)NULL, 0);
298 /* Wait for 120ms */
299 dly_tsk(120);
300
301 /* Normal display for Driver Down side */
302 parameter[0] = 0x00;
303 ST7789H2_WriteReg(hlcd, ST7789H2_NORMAL_DISPLAY, parameter, 1);
304
305 /* Color mode 16bits/pixel */
306 parameter[0] = 0x05;
307 ST7789H2_WriteReg(hlcd, ST7789H2_COLOR_MODE, parameter, 1);
308
309 /* Display inversion On */
310 ST7789H2_WriteReg(hlcd, ST7789H2_DISPLAY_INVERSION, (uint8_t*)NULL, 0);
311
312 /* Set Column address CASET */
313 parameter[0] = 0x00;
314 parameter[1] = 0x00;
315 parameter[2] = 0x00;
316 parameter[3] = 0xEF;
317 ST7789H2_WriteReg(hlcd, ST7789H2_CASET, parameter, 4);
318 /* Set Row address RASET */
319 parameter[0] = 0x00;
320 parameter[1] = 0x00;
321 parameter[2] = 0x00;
322 parameter[3] = 0xEF;
323 ST7789H2_WriteReg(hlcd, ST7789H2_RASET, parameter, 4);
324
325 /*--------------- ST7789H2 Frame rate setting -------------------------------*/
326 /* PORCH control setting */
327 parameter[0] = 0x0C;
328 parameter[1] = 0x0C;
329 parameter[2] = 0x00;
330 parameter[3] = 0x33;
331 parameter[4] = 0x33;
332 ST7789H2_WriteReg(hlcd, ST7789H2_PORCH_CTRL, parameter, 5);
333
334 /* GATE control setting */
335 parameter[0] = 0x35;
336 ST7789H2_WriteReg(hlcd, ST7789H2_GATE_CTRL, parameter, 1);
337
338 /*--------------- ST7789H2 Power setting ------------------------------------*/
339 /* VCOM setting */
340 parameter[0] = 0x1F;
341 ST7789H2_WriteReg(hlcd, ST7789H2_VCOM_SET, parameter, 1);
342
343 /* LCM Control setting */
344 parameter[0] = 0x2C;
345 ST7789H2_WriteReg(hlcd, ST7789H2_LCM_CTRL, parameter, 1);
346
347 /* VDV and VRH Command Enable */
348 parameter[0] = 0x01;
349 parameter[1] = 0xC3;
350 ST7789H2_WriteReg(hlcd, ST7789H2_VDV_VRH_EN, parameter, 2);
351
352 /* VDV Set */
353 parameter[0] = 0x20;
354 ST7789H2_WriteReg(hlcd, ST7789H2_VDV_SET, parameter, 1);
355
356 /* Frame Rate Control in normal mode */
357 parameter[0] = 0x0F;
358 ST7789H2_WriteReg(hlcd, ST7789H2_FR_CTRL, parameter, 1);
359
360 /* Power Control */
361 parameter[0] = 0xA4;
362 parameter[1] = 0xA1;
363 ST7789H2_WriteReg(hlcd, ST7789H2_POWER_CTRL, parameter, 1);
364
365 /*--------------- ST7789H2 Gamma setting ------------------------------------*/
366 /* Positive Voltage Gamma Control */
367 parameter[0] = 0xD0;
368 parameter[1] = 0x08;
369 parameter[2] = 0x11;
370 parameter[3] = 0x08;
371 parameter[4] = 0x0C;
372 parameter[5] = 0x15;
373 parameter[6] = 0x39;
374 parameter[7] = 0x33;
375 parameter[8] = 0x50;
376 parameter[9] = 0x36;
377 parameter[10] = 0x13;
378 parameter[11] = 0x14;
379 parameter[12] = 0x29;
380 parameter[13] = 0x2D;
381 ST7789H2_WriteReg(hlcd, ST7789H2_PV_GAMMA_CTRL, parameter, 14);
382
383 /* Negative Voltage Gamma Control */
384 parameter[0] = 0xD0;
385 parameter[1] = 0x08;
386 parameter[2] = 0x10;
387 parameter[3] = 0x08;
388 parameter[4] = 0x06;
389 parameter[5] = 0x06;
390 parameter[6] = 0x39;
391 parameter[7] = 0x44;
392 parameter[8] = 0x51;
393 parameter[9] = 0x0B;
394 parameter[10] = 0x16;
395 parameter[11] = 0x14;
396 parameter[12] = 0x2F;
397 parameter[13] = 0x31;
398 ST7789H2_WriteReg(hlcd, ST7789H2_NV_GAMMA_CTRL, parameter, 14);
399
400 /* Display ON command */
401 lcd_displayOn(hlcd);
402
403 /* Tearing Effect Line On: Option (00h:VSYNC Interface OFF, 01h:VSYNC Interface ON) */
404 parameter[0] = 0x00;
405 ST7789H2_WriteReg(hlcd, ST7789H2_TEARING_EFFECT, parameter, 1);
406}
407
408/**
409 * @brief Set the Display Orientation.
410 * @param orientation: ST7789H2_ORIENTATION_PORTRAIT, ST7789H2_ORIENTATION_LANDSCAPE
411 * or ST7789H2_ORIENTATION_LANDSCAPE_ROT180
412 * @retval None
413 */
414static void
415ST7789H2_SetOrientation(LCD_Handler_t *hlcd, uint32_t orientation)
416{
417 uint8_t parameter[6];
418
419 if(orientation == ST7789H2_ORIENTATION_LANDSCAPE){
420 parameter[0] = 0x00;
421 }
422 else if(orientation == ST7789H2_ORIENTATION_LANDSCAPE_ROT180){
423 /* Vertical Scrolling Definition */
424 /* TFA describes the Top Fixed Area */
425 parameter[0] = 0x00;
426 parameter[1] = 0x00;
427 /* VSA describes the height of the Vertical Scrolling Area */
428 parameter[2] = 0x01;
429 parameter[3] = 0xF0;
430 /* BFA describes the Bottom Fixed Area */
431 parameter[4] = 0x00;
432 parameter[5] = 0x00;
433 ST7789H2_WriteReg(hlcd, ST7789H2_VSCRDEF, parameter, 6);
434
435 /* Vertical Scroll Start Address of RAM */
436 /* GRAM row nbr (320) - Display row nbr (240) = 80 = 0x50 */
437 parameter[0] = 0x00;
438 parameter[1] = 0x50;
439 ST7789H2_WriteReg(hlcd, ST7789H2_VSCSAD, parameter, 2);
440
441 parameter[0] = 0xC0;
442 }
443 else{
444 parameter[0] = 0x60;
445 }
446 ST7789H2_WriteReg(hlcd, ST7789H2_NORMAL_DISPLAY, parameter, 1);
447}
448
449/**
450 * @brief Set Cursor position.
451 * @param Xpos: specifies the X position.
452 * @param Ypos: specifies the Y position.
453 * @retval None
454 */
455static void
456ST7789H2_SetCursor(LCD_Handler_t *hlcd, uint16_t Xpos, uint16_t Ypos)
457{
458 uint8_t parameter[4];
459 /* CASET: Comumn Addrses Set */
460 parameter[0] = 0x00;
461 parameter[1] = 0x00 + Xpos;
462 parameter[2] = 0x00;
463 parameter[3] = 0xEF + Xpos;
464 ST7789H2_WriteReg(hlcd, ST7789H2_CASET, parameter, 4);
465 /* RASET: Row Addrses Set */
466 parameter[0] = 0x00;
467 parameter[1] = 0x00 + Ypos;
468 parameter[2] = 0x00;
469 parameter[3] = 0xEF + Ypos;
470 ST7789H2_WriteReg(hlcd, ST7789H2_RASET, parameter, 4);
471}
472
473/**
474 * @brief Read pixel from LCD RAM in RGB888 format
475 * @param Xpos: specifies the X position.
476 * @param Ypos: specifies the Y position.
477 * @retval Each RGB pixel color components in a structure
478 */
479static ST7789H2_Rgb888
480ST7789H2_ReadPixel_rgb888(LCD_Handler_t *hlcd, uint16_t Xpos, uint16_t Ypos)
481{
482 ST7789H2_Rgb888 rgb888;
483 uint16_t rgb888_part1, rgb888_part2;
484
485 /* In LCD RAM, pixels are 24 bits packed and read with 16 bits access
486 * Here is the pixels components arrangement in memory :
487 * bits: 15 14 13 12 11 10 09 08 | 07 06 05 04 03 02 01 00
488 * address 0 : red pixel 0 X X | green pixel 0 X X
489 * address 1 : blue pixel 0 X X | red pixel 1 X X
490 * address 2 : green pixel 1 X X | blue pixel 1 X X
491 */
492
493 /* Set Cursor */
494 ST7789H2_SetCursor(hlcd, Xpos, Ypos);
495 /* Prepare to read LCD RAM */
496 ST7789H2_WriteReg(hlcd, ST7789H2_READ_RAM, (uint8_t*)NULL, 0); /* RAM read data command */
497 /* Dummy read */
498 LCD_IO_ReadData(hlcd);
499 /* Read first part of the RGB888 data */
500 rgb888_part1 = LCD_IO_ReadData(hlcd);
501 /* Read first part of the RGB888 data */
502 rgb888_part2 = LCD_IO_ReadData(hlcd);
503
504 /* red component */
505 rgb888.red = (rgb888_part1 & 0xFC00) >> 8;
506 /* green component */
507 rgb888.green = (rgb888_part1 & 0x00FC) >> 0;
508 /* blue component */
509 rgb888.blue = (rgb888_part2 & 0xFC00) >> 8;
510 return rgb888;
511}
512
513/**
514 * @brief Displays a single picture line.
515 * @param pdata: picture address.
516 * @param Xpos: Image X position in the LCD
517 * @param Ypos: Image Y position in the LCD
518 * @param Xsize: Image X size in the LCD
519 * @retval None
520 */
521static void
522ST7789H2_DrawRGBHLine(LCD_Handler_t *hlcd, uint16_t Xpos, uint16_t Ypos, uint16_t Xsize, uint8_t *pdata)
523{
524 uint32_t i = 0;
525 uint32_t posX;
526 uint16_t *rgb565 = (uint16_t*)pdata;
527
528 /* Prepare to write to LCD RAM */
529 ST7789H2_WriteReg(hlcd, ST7789H2_WRITE_RAM, (uint8_t*)NULL, 0); /* RAM write data command */
530
531 for(posX = Xpos ; posX < (Xsize + Xpos) ; posX++){
532 if((posX >= hlcd->winXstart) && (Ypos >= hlcd->winYstart) && /* Check we are in the defined window */
533 (posX <= hlcd->winXend) && (Ypos <= hlcd->winXend)){
534 if(posX != (Xsize + Xpos)){ /* When writing last pixel when size is odd, the third part is not written */
535 LCD_IO_WriteData(hlcd, rgb565[i]);
536 }
537 i++;
538 }
539 }
540}
541
542/*
543 * ST7789H2スクリーンの初期化関数
544 */
545ER
546lcd_init(LCD_Handler_t *hlcd, uint32_t orientation)
547{
548 GPIO_Init_t GPIO_Init_Data;
549 uint16_t id;
550
551 hlcd->_width = ST7789H2_LCD_PIXEL_WIDTH;
552 hlcd->_height = ST7789H2_LCD_PIXEL_HEIGHT;
553 hlcd->winXstart = 0;
554 hlcd->winYstart = 0;
555 hlcd->winXend = hlcd->_width - 1;
556 hlcd->winXend = hlcd->_height - 1;
557
558 /* Initialize LCD special pins GPIOs */
559 sil_orw_mem((uint32_t *)(TADR_RCC_BASE+TOFF_RCC_AHB1ENR), hlcd->rst_clk);
560 sil_orw_mem((uint32_t *)(TADR_RCC_BASE+TOFF_RCC_AHB1ENR), hlcd->te_clk);
561 sil_orw_mem((uint32_t *)(TADR_RCC_BASE+TOFF_RCC_AHB1ENR), hlcd->bl_clk);
562
563 GPIO_Init_Data.mode = GPIO_MODE_OUTPUT; /* LCD_RESET pin has to be manually controlled */
564 GPIO_Init_Data.speed = hlcd->gpio_speed;
565 GPIO_Init_Data.pull = hlcd->rst_pull;
566 GPIO_Init_Data.otype = GPIO_OTYPE_PP;
567 gpio_setup(hlcd->rst_base, &GPIO_Init_Data, hlcd->rst_pin);
568 sil_wrw_mem((uint32_t *)(hlcd->rst_base+TOFF_GPIO_BSRR), (1<<hlcd->rst_pin) << 16);
569
570 /* LCD_TE GPIO configuration */
571 GPIO_Init_Data.mode = GPIO_MODE_INPUT; /* LCD_TE pin has to be manually managed */
572 gpio_setup(hlcd->te_base, &GPIO_Init_Data, hlcd->te_pin);
573
574 /* LCD_BL_CTRL GPIO configuration */
575 GPIO_Init_Data.mode = GPIO_MODE_OUTPUT; /* LCD_BL_CTRL pin has to be manually controlled */
576 GPIO_Init_Data.pull = hlcd->bl_pull;
577 gpio_setup(hlcd->bl_base, &GPIO_Init_Data, hlcd->bl_pin);
578
579 /* Backlight control signal assertion */
580 if(hlcd->bl_active == 0)
581 sil_wrw_mem((uint32_t *)(hlcd->bl_base+TOFF_GPIO_BSRR), (1<<hlcd->bl_pin) << 16);
582 else
583 sil_wrw_mem((uint32_t *)(hlcd->bl_base+TOFF_GPIO_BSRR), (1<<hlcd->bl_pin));
584
585 /* Apply hardware reset according to procedure indicated in FRD154BP2901 documentation */
586 dly_tsk(5); /* Reset signal asserted during 5ms */
587 sil_wrw_mem((uint32_t *)(hlcd->rst_base+TOFF_GPIO_BSRR), (1<<hlcd->rst_pin));
588 dly_tsk(10); /* Reset signal released during 10ms */
589 sil_wrw_mem((uint32_t *)(hlcd->rst_base+TOFF_GPIO_BSRR), (1<<hlcd->rst_pin) << 16);
590 dly_tsk(20); /* Reset signal asserted during 20ms */
591 sil_wrw_mem((uint32_t *)(hlcd->rst_base+TOFF_GPIO_BSRR), (1<<hlcd->rst_pin));
592 dly_tsk(10); /* Reset signal released during 10ms */
593
594 LCD_IO_Init();
595 id = ST7789H2_ReadReg(hlcd, ST7789H2_LCD_ID);
596 if(id == ST7789H2_ID){
597 /* LCD Init */
598 ST7789H2_Init(hlcd);
599
600 if(orientation == LCD_ORIENTATION_PORTRAIT){
601 ST7789H2_SetOrientation(hlcd, LCD_ORIENTATION_PORTRAIT);
602 }
603 else if(orientation == LCD_ORIENTATION_LANDSCAPE_ROT180){
604 ST7789H2_SetOrientation(hlcd, LCD_ORIENTATION_LANDSCAPE_ROT180);
605 }
606 else{
607 /* Default landscape orientation is selected */
608 }
609 return E_OK;
610 }
611 else
612 return E_SYS;
613}
614
615/*
616 * ST7789H2スクリーンの終了関数
617 */
618ER
619lcd_deinit(LCD_Handler_t *hlcd)
620{
621 /* Actually LcdDrv does not provide a DeInit function */
622 return E_OK;
623}
624
625/*
626 * ST7789H2スクリーンレイヤ初期化関数
627 */
628ER
629lcd_layerdefaultinit(LCD_Handler_t *hlcd, LCD_DrawProp_t *pDrawProp, uint16_t LayerIndex, uint32_t FB_Address)
630{
631 pDrawProp->BackColor = 0xFFFF;
632 pDrawProp->TextColor = 0x0000;
633 pDrawProp->hlcd = hlcd;
634 return E_OK;
635}
636
637/*
638 * ST7789H2表示ウィンドウ設定
639 */
640void
641lcd_setAddrWindow(LCD_Handler_t *hlcd, uint16_t Xpos, uint16_t Ypos, uint16_t Width, uint16_t Height)
642{
643 if(Xpos < hlcd->_width){
644 hlcd->winXstart = Xpos;
645 }
646 else{
647 hlcd->winXstart = 0;
648 }
649
650 if(Ypos < hlcd->_height){
651 hlcd->winYstart = Ypos;
652 }
653 else{
654 hlcd->winYstart = 0;
655 }
656
657 if(Width + Xpos <= hlcd->_width){
658 hlcd->winXend = Width + Xpos - 1;
659 }
660 else{
661 hlcd->winXend = hlcd->_width - 1;
662 }
663
664 if(Height + Ypos <= hlcd->_height){
665 hlcd->winXend = Height + Ypos - 1;
666 }
667 else{
668 hlcd->winXend = hlcd->_height-1;
669 }
670}
671
672/*
673 * ST7789H2スクリーンクリア
674 */
675void
676lcd_clear(LCD_Handler_t *hlcd, uint16_t Color)
677{
678 uint32_t counter = 0;
679 uint32_t y_size = 0;
680
681 y_size = hlcd->_height;
682
683 for(counter = 0; counter < y_size; counter++){
684 lcd_drawFastHLine(hlcd, 0, counter, hlcd->_width, Color);
685 }
686}
687
688/*
689 * PIXEL読み出し
690 * param1 hlcd: Pointer to LCD Handler
691 * param2 x: X position
692 * param3 y: Y position
693 */
694uint16_t
695lcd_readPixel(LCD_Handler_t *hlcd, uint16_t Xpos, uint16_t Ypos)
696{
697 ST7789H2_Rgb888 rgb888;
698 uint8_t r, g, b;
699 uint16_t rgb565;
700
701 /* Set Cursor */
702 ST7789H2_SetCursor(hlcd, Xpos, Ypos);
703
704 /* Read RGB888 data from LCD RAM */
705 rgb888 = ST7789H2_ReadPixel_rgb888(hlcd, Xpos, Ypos);
706
707 /* Convert RGB888 to RGB565 */
708 r = ((rgb888.red & 0xF8) >> 3); /* Extract the red component 5 most significant bits */
709 g = ((rgb888.green & 0xFC) >> 2); /* Extract the green component 6 most significant bits */
710 b = ((rgb888.blue & 0xF8) >> 3); /* Extract the blue component 5 most significant bits */
711
712 rgb565 = ((uint16_t)(r) << 11) + ((uint16_t)(g) << 5) + ((uint16_t)(b) << 0);
713 return (rgb565);
714}
715
716/*
717 * PIXEL描画
718 * param1 hlcd: Pointer to LCD Handler
719 * param2 x: X position
720 * param3 y: Y position
721 * param4 color: color value
722 */
723void
724lcd_drawPixel(LCD_Handler_t *hlcd, int16_t Xpos, int16_t Ypos, uint16_t RGB_Code)
725{
726 ST7789H2_SetCursor(hlcd, Xpos, Ypos);
727
728 /* Prepare to write to LCD RAM */
729 ST7789H2_WriteReg(hlcd, ST7789H2_WRITE_RAM, (uint8_t*)NULL, 0); /* RAM write data command */
730
731 /* Write RAM data */
732 LCD_IO_WriteData(hlcd, RGB_Code);
733}
734
735/*
736 * 垂直方向LINEの高速描画
737 * param1 hlcd: Pointer to LCD Handler
738 * param2 x: start X position
739 * param3 y: start Y position
740 * param4 h: height
741 * param5 color: color value
742 */
743void
744lcd_drawFastHLine(LCD_Handler_t *hlcd, uint16_t Xpos, uint16_t Ypos, uint16_t Length, uint32_t color)
745{
746 uint16_t counter = 0;
747
748 /* Set Cursor */
749 ST7789H2_SetCursor(hlcd, Xpos, Ypos);
750
751 /* Prepare to write to LCD RAM */
752 ST7789H2_WriteReg(hlcd, ST7789H2_WRITE_RAM, (uint8_t*)NULL, 0); /* RAM write data command */
753
754 /* Sent a complete line */
755 for(counter = 0; counter < Length; counter++){
756 LCD_IO_WriteData(hlcd, color);
757 }
758}
759
760/*
761 * 水平方向LINEの高速描画
762 * param1 hlcd: Pointer to LCD Handler
763 * param2 x: start X position
764 * param3 y: start Y position
765 * param4 w: width
766 * param5 color: color value
767 */
768void
769lcd_drawFastVLine(LCD_Handler_t *hlcd, uint16_t Xpos, uint16_t Ypos, uint16_t Length, uint32_t color)
770{
771 uint16_t counter = 0;
772
773 /* Set Cursor */
774 ST7789H2_SetCursor(hlcd, Xpos, Ypos);
775
776 /* Prepare to write to LCD RAM */
777 ST7789H2_WriteReg(hlcd, ST7789H2_WRITE_RAM, (uint8_t*)NULL, 0); /* RAM write data command */
778
779 /* Fill a complete vertical line */
780 for(counter = 0; counter < Length; counter++){
781 lcd_drawPixel(hlcd, Xpos, Ypos + counter, color);
782 }
783}
784
785/*
786 * BITMAP描画
787 * param1 hlcd: Pointer to LCD Handler
788 * param2 x0: Bmp X position in the LCD
789 * param3 y0: Bmp Y position in the LCD
790 * param4 pbmp: Pointer to Bmp picture address in the internal Flash
791 */
792void
793lcd_drawBitmap(LCD_Handler_t *hlcd, uint16_t Xpos, uint16_t Ypos, uint8_t *pbmp)
794{
795 uint32_t height = 0;
796 uint32_t width = 0;
797 uint32_t index = 0, size = 0;
798 uint32_t posY;
799 uint32_t nb_line = 0;
800 uint16_t Xsize = hlcd->winXend - hlcd->winXstart + 1;
801 uint16_t Ysize = hlcd->winXend - hlcd->winYstart + 1;
802
803 /* Read bitmap width */
804 width = pbmp[18] + (pbmp[19] << 8) + (pbmp[20] << 16) + (pbmp[21] << 24);
805
806 /* Read bitmap height */
807 height = pbmp[22] + (pbmp[23] << 8) + (pbmp[24] << 16) + (pbmp[25] << 24);
808
809 lcd_setAddrWindow(hlcd, Xpos, Ypos, width, height);
810
811 /* Read bitmap size */
812 size = *(volatile uint16_t *) (pbmp + 2);
813 size |= (*(volatile uint16_t *) (pbmp + 4)) << 16;
814 /* Get bitmap data address offset */
815 index = *(volatile uint16_t *) (pbmp + 10);
816 index |= (*(volatile uint16_t *) (pbmp + 12)) << 16;
817 size = (size - index)/2;
818 pbmp += index;
819
820 for (posY = (Ypos + Ysize); posY > Ypos; posY--){ /* In BMP files the line order is inverted */
821 /* Set Cursor */
822 ST7789H2_SetCursor(hlcd, Xpos, posY - 1);
823
824 /* Draw one line of the picture */
825 ST7789H2_DrawRGBHLine(hlcd, Xpos, posY - 1, Xsize, (pbmp + (nb_line * Xsize * 2)));
826 nb_line++;
827 }
828 lcd_setAddrWindow(hlcd, 0, 0, hlcd->_width, hlcd->_height);
829}
830
831/*
832 * バッファコピー
833 * param1 hlcd: Pointer to LCD Handler
834 * param2 Xpos: コピー先X座標
835 * param3 Ypos: コピー先Y座標
836 * param4 Xsize: バッファ幅
837 * param5 Ysize: バッファ高さ
838 * param6 pdata: コピー元バッファポインタ
839 */
840void
841lcd_drawRGBImage(LCD_Handler_t *hlcd, uint16_t Xpos, uint16_t Ypos, uint16_t Xsize, uint16_t Ysize, uint8_t *pdata)
842{
843 uint32_t posY;
844 uint32_t nb_line = 0;
845
846 lcd_setAddrWindow(hlcd, Xpos, Ypos, Xsize, Ysize);
847
848 for(posY = Ypos ; posY < (Ypos + Ysize) ; posY++){
849 /* Set Cursor */
850 ST7789H2_SetCursor(hlcd, Xpos, posY);
851
852 /* Draw one line of the picture */
853 ST7789H2_DrawRGBHLine(hlcd, Xpos, posY, Xsize, (pdata + (nb_line * Xsize * 2)));
854 nb_line++;
855 }
856 lcd_setAddrWindow(hlcd, 0, 0, hlcd->_width, hlcd->_height);
857}
858
859/*
860 * RECTANGLE塗りつぶし描画
861 * param1 hlcd: Pointer to LCD Handler
862 * param2 x: left X position
863 * param3 y: top Y position
864 * param4 w: width
865 * param5 h: height
866 * param6 color: color value
867 */
868void
869lcd_fillRect(LCD_Handler_t *hlcd, uint16_t Xpos, uint16_t Ypos, uint16_t Width, uint16_t Height, uint32_t color)
870{
871 do{
872 lcd_drawFastHLine(hlcd, Xpos, Ypos++, Width, color);
873 }
874 while(Height--);
875}
876
877
878/*
879 * 線描画
880 * param1 pDrawProp: Pointer to Draw Prop
881 * param2 x1: Point 1 X position
882 * param3 y1: Point 1 Y position
883 * param4 x2: Point 2 X position
884 * param5 y2: Point 2 Y position
885 */
886void
887lcd_drawLine(LCD_DrawProp_t *pDrawProp, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
888{
889 int16_t deltax = 0, deltay = 0, x = 0, y = 0, xinc1 = 0, xinc2 = 0,
890 yinc1 = 0, yinc2 = 0, den = 0, num = 0, numadd = 0, numpixels = 0,
891 curpixel = 0;
892
893 deltax = ABS(x2 - x1); /* The difference between the x's */
894 deltay = ABS(y2 - y1); /* The difference between the y's */
895 x = x1; /* Start x off at the first pixel */
896 y = y1; /* Start y off at the first pixel */
897
898 if(x2 >= x1){ /* The x-values are increasing */
899 xinc1 = 1;
900 xinc2 = 1;
901 }
902 else{ /* The x-values are decreasing */
903 xinc1 = -1;
904 xinc2 = -1;
905 }
906
907 if(y2 >= y1){ /* The y-values are increasing */
908 yinc1 = 1;
909 yinc2 = 1;
910 }
911 else{ /* The y-values are decreasing */
912 yinc1 = -1;
913 yinc2 = -1;
914 }
915
916 if(deltax >= deltay){ /* There is at least one x-value for every y-value */
917 xinc1 = 0; /* Don't change the x when numerator >= denominator */
918 yinc2 = 0; /* Don't change the y for every iteration */
919 den = deltax;
920 num = deltax / 2;
921 numadd = deltay;
922 numpixels = deltax; /* There are more x-values than y-values */
923 }
924 else{ /* There is at least one y-value for every x-value */
925 xinc2 = 0; /* Don't change the x for every iteration */
926 yinc1 = 0; /* Don't change the y when numerator >= denominator */
927 den = deltay;
928 num = deltay / 2;
929 numadd = deltax;
930 numpixels = deltay; /* There are more y-values than x-values */
931 }
932
933 for(curpixel = 0; curpixel <= numpixels; curpixel++){
934 lcd_drawPixel(pDrawProp->hlcd, x, y, pDrawProp->TextColor); /* Draw the current pixel */
935 num += numadd; /* Increase the numerator by the top of the fraction */
936 if(num >= den){ /* Check if numerator >= denominator */
937 num -= den; /* Calculate the new numerator value */
938 x += xinc1; /* Change the x as appropriate */
939 y += yinc1; /* Change the y as appropriate */
940 }
941 x += xinc2; /* Change the x as appropriate */
942 y += yinc2; /* Change the y as appropriate */
943 }
944}
945
946/*
947 * RECTANGLE描画
948 * param1 pDrawProp: Pointer to Draw Prop
949 * param2 x: left X position
950 * param3 y: top Y position
951 * param4 w: width
952 * param5 h: height
953 */
954void
955lcd_drawRect(LCD_DrawProp_t *pDrawProp, uint16_t Xpos, uint16_t Ypos, uint16_t Width, uint16_t Height)
956{
957 /* Draw horizontal lines */
958 lcd_drawFastHLine(pDrawProp->hlcd, Xpos, Ypos, Width, pDrawProp->TextColor);
959 lcd_drawFastHLine(pDrawProp->hlcd, Xpos, (Ypos+ Height), Width, pDrawProp->TextColor);
960
961 /* Draw vertical lines */
962 lcd_drawFastVLine(pDrawProp->hlcd, Xpos, Ypos, Height, pDrawProp->TextColor);
963 lcd_drawFastVLine(pDrawProp->hlcd, (Xpos + Width), Ypos, Height, pDrawProp->TextColor);
964}
965
966/*
967 * 円描画
968 * param1 pDrawProp: Pointer to Draw Prop
969 * param2 x0: X position
970 * param3 y0: Y position
971 * param4 Radius: Circle radius
972 */
973void
974lcd_DrawCircle(LCD_DrawProp_t *pDrawProp, uint16_t Xpos, uint16_t Ypos, uint16_t Radius)
975{
976 int32_t decision; /* Decision Variable */
977 uint32_t current_x; /* Current X Value */
978 uint32_t current_y; /* Current Y Value */
979
980 decision = 3 - (Radius << 1);
981 current_x = 0;
982 current_y = Radius;
983
984 while (current_x <= current_y){
985 lcd_drawPixel(pDrawProp->hlcd, (Xpos + current_x), (Ypos - current_y), pDrawProp->TextColor);
986 lcd_drawPixel(pDrawProp->hlcd, (Xpos - current_x), (Ypos - current_y), pDrawProp->TextColor);
987 lcd_drawPixel(pDrawProp->hlcd, (Xpos + current_y), (Ypos - current_x), pDrawProp->TextColor);
988 lcd_drawPixel(pDrawProp->hlcd, (Xpos - current_y), (Ypos - current_x), pDrawProp->TextColor);
989 lcd_drawPixel(pDrawProp->hlcd, (Xpos + current_x), (Ypos + current_y), pDrawProp->TextColor);
990 lcd_drawPixel(pDrawProp->hlcd, (Xpos - current_x), (Ypos + current_y), pDrawProp->TextColor);
991 lcd_drawPixel(pDrawProp->hlcd, (Xpos + current_y), (Ypos + current_x), pDrawProp->TextColor);
992 lcd_drawPixel(pDrawProp->hlcd, (Xpos - current_y), (Ypos + current_x), pDrawProp->TextColor);
993
994 if(decision < 0){
995 decision += (current_x << 2) + 6;
996 }
997 else{
998 decision += ((current_x - current_y) << 2) + 10;
999 current_y--;
1000 }
1001 current_x++;
1002 }
1003}
1004
1005/*
1006 * PLOY-LINE描画
1007 * param1 pDrawProp: Pointer to Draw Prop
1008 * param2 Points: Pointer to the points array
1009 * param3 PointCount: Number of points
1010 */
1011void
1012lcd_drawPolygon(LCD_DrawProp_t *pDrawProp, pPoint Points, uint16_t PointCount)
1013{
1014 int16_t x = 0, y = 0;
1015
1016 if(PointCount < 2)
1017 return;
1018
1019 lcd_drawLine(pDrawProp, Points->X, Points->Y, (Points+PointCount-1)->X, (Points+PointCount-1)->Y);
1020
1021 while(--PointCount){
1022 x = Points->X;
1023 y = Points->Y;
1024 Points++;
1025 lcd_drawLine(pDrawProp, x, y, Points->X, Points->Y);
1026 }
1027}
1028
1029/*
1030 * 楕円描画
1031 * param1 pDrawProp: Pointer to Draw Prop
1032 * param2 x0: X position
1033 * param3 y0: Y position
1034 * param4 XRadius: Ellipse X radius
1035 * param5 YRadius: Ellipse Y radius
1036 */
1037void
1038lcd_DrawEllipse(LCD_DrawProp_t *pDrawProp, int Xpos, int Ypos, int XRadius, int YRadius)
1039{
1040 LCD_Handler_t *hlcd = pDrawProp->hlcd;
1041 int x = 0, y = -YRadius, err = 2-2*XRadius, e2;
1042 float k = 0, rad1 = 0, rad2 = 0;
1043
1044 rad1 = XRadius;
1045 rad2 = YRadius;
1046
1047 k = (float)(rad2/rad1);
1048
1049 do{
1050 lcd_drawPixel(hlcd, (Xpos-(uint16_t)(x/k)), (Ypos+y), pDrawProp->TextColor);
1051 lcd_drawPixel(hlcd, (Xpos+(uint16_t)(x/k)), (Ypos+y), pDrawProp->TextColor);
1052 lcd_drawPixel(hlcd, (Xpos+(uint16_t)(x/k)), (Ypos-y), pDrawProp->TextColor);
1053 lcd_drawPixel(hlcd, (Xpos-(uint16_t)(x/k)), (Ypos-y), pDrawProp->TextColor);
1054
1055 e2 = err;
1056 if(e2 <= x){
1057 err += ++x*2+1;
1058 if(-y == x && e2 <= y)
1059 e2 = 0;
1060 }
1061 if(e2 > y)
1062 err += ++y*2+1;
1063 }while (y <= 0);
1064}
1065
1066/*
1067 * 円塗りつぶし描画
1068 * param1 pDrawProp: Pointer to Draw Prop
1069 * param2 x0: X position
1070 * param3 y0: Y position
1071 * param4 Radius: Circle radius
1072 */
1073void
1074lcd_fillCircle(LCD_DrawProp_t *pDrawProp, uint16_t Xpos, uint16_t Ypos, uint16_t Radius)
1075{
1076 LCD_Handler_t *hlcd = pDrawProp->hlcd;
1077 int32_t decision; /* Decision Variable */
1078 uint32_t current_x; /* Current X Value */
1079 uint32_t current_y; /* Current Y Value */
1080
1081 decision = 3 - (Radius << 1);
1082 current_x = 0;
1083 current_y = Radius;
1084
1085 while(current_x <= current_y){
1086 if(current_y > 0){
1087 lcd_drawFastHLine(hlcd, Xpos - current_y, Ypos + current_x, 2*current_y, pDrawProp->TextColor);
1088 lcd_drawFastHLine(hlcd, Xpos - current_y, Ypos - current_x, 2*current_y, pDrawProp->TextColor);
1089 }
1090
1091 if(current_x > 0){
1092 lcd_drawFastHLine(hlcd, Xpos - current_x, Ypos - current_y, 2*current_x, pDrawProp->TextColor);
1093 lcd_drawFastHLine(hlcd, Xpos - current_x, Ypos + current_y, 2*current_x, pDrawProp->TextColor);
1094 }
1095 if(decision < 0){
1096 decision += (current_x << 2) + 6;
1097 }
1098 else{
1099 decision += ((current_x - current_y) << 2) + 10;
1100 current_y--;
1101 }
1102 current_x++;
1103 }
1104 lcd_DrawCircle(pDrawProp, Xpos, Ypos, Radius);
1105}
1106
1107/*
1108 * 楕円塗りつぶし描画
1109 * param1 pDrawProp: Pointer to Draw Prop
1110 * param2 x0: X position
1111 * param3 y0: Y position
1112 * param4 XRadius: Ellipse X radius
1113 * param5 YRadius: Ellipse Y radius
1114 */
1115void
1116lcd_fillEllipse(LCD_DrawProp_t *pDrawProp, int Xpos, int Ypos, int XRadius, int YRadius)
1117{
1118 LCD_Handler_t *hlcd = pDrawProp->hlcd;
1119 int x = 0, y = -YRadius, err = 2-2*XRadius, e2;
1120 float k = 0, rad1 = 0, rad2 = 0;
1121
1122 rad1 = XRadius;
1123 rad2 = YRadius;
1124 k = (float)(rad2/rad1);
1125
1126 do{
1127 lcd_drawFastHLine(hlcd, (Xpos-(uint16_t)(x/k)), (Ypos+y), (2*(uint16_t)(x/k) + 1), pDrawProp->TextColor);
1128 lcd_drawFastHLine(hlcd, (Xpos-(uint16_t)(x/k)), (Ypos-y), (2*(uint16_t)(x/k) + 1), pDrawProp->TextColor);
1129
1130 e2 = err;
1131 if(e2 <= x){
1132 err += ++x*2+1;
1133 if(-y == x && e2 <= y)
1134 e2 = 0;
1135 }
1136 if(e2 > y)
1137 err += ++y*2+1;
1138 }
1139 while(y <= 0);
1140}
1141
1142/*
1143 * PLOY-LINE塗りつぶし描画
1144 * param1 pDrawProp: Pointer to Draw Prop
1145 * param2 Points: Pointer to the points array
1146 * param3 PointCount: Number of points
1147 */
1148void
1149lcd_fillPolygon(LCD_DrawProp_t *pDrawProp, pPoint Points, uint16_t PointCount)
1150{
1151 int16_t X = 0, Y = 0, X2 = 0, Y2 = 0, X_center = 0, Y_center = 0, X_first = 0, Y_first = 0, pixelX = 0, pixelY = 0, counter = 0;
1152 uint16_t IMAGE_LEFT = 0, IMAGE_RIGHT = 0, IMAGE_TOP = 0, IMAGE_BOTTOM = 0;
1153
1154 IMAGE_LEFT = IMAGE_RIGHT = Points->X;
1155 IMAGE_TOP= IMAGE_BOTTOM = Points->Y;
1156
1157 for(counter = 1; counter < PointCount; counter++){
1158 pixelX = POLY_X(counter);
1159 if(pixelX < IMAGE_LEFT){
1160 IMAGE_LEFT = pixelX;
1161 }
1162 if(pixelX > IMAGE_RIGHT){
1163 IMAGE_RIGHT = pixelX;
1164 }
1165
1166 pixelY = POLY_Y(counter);
1167 if(pixelY < IMAGE_TOP){
1168 IMAGE_TOP = pixelY;
1169 }
1170 if(pixelY > IMAGE_BOTTOM){
1171 IMAGE_BOTTOM = pixelY;
1172 }
1173 }
1174
1175 if(PointCount < 2){
1176 return;
1177 }
1178
1179 X_center = (IMAGE_LEFT + IMAGE_RIGHT)/2;
1180 Y_center = (IMAGE_BOTTOM + IMAGE_TOP)/2;
1181
1182 X_first = Points->X;
1183 Y_first = Points->Y;
1184
1185 while(--PointCount){
1186 X = Points->X;
1187 Y = Points->Y;
1188 Points++;
1189 X2 = Points->X;
1190 Y2 = Points->Y;
1191
1192 FillTriangle(pDrawProp, X, X2, X_center, Y, Y2, Y_center);
1193 FillTriangle(pDrawProp, X, X_center, X2, Y, Y_center, Y2);
1194 FillTriangle(pDrawProp, X_center, X2, X, Y_center, Y2, Y);
1195 }
1196 FillTriangle(pDrawProp, X_first, X2, X_center, Y_first, Y2, Y_center);
1197 FillTriangle(pDrawProp, X_first, X_center, X2, Y_first, Y_center, Y2);
1198 FillTriangle(pDrawProp, X_center, X2, X_first, Y_center, Y2, Y_first);
1199}
1200
1201/*
1202 * 表示オン
1203 * param1 hlcd: Pointer to LCD Handler
1204 */
1205void
1206lcd_displayOn(LCD_Handler_t *hlcd)
1207{
1208 /* Display ON command */
1209 ST7789H2_WriteReg(hlcd, ST7789H2_DISPLAY_ON, (uint8_t*)NULL, 0);
1210
1211 /* Sleep Out command */
1212 ST7789H2_WriteReg(hlcd, ST7789H2_SLEEP_OUT, (uint8_t*)NULL, 0);
1213}
1214
1215/*
1216 * 表示オフ
1217 * param1 hlcd: Pointer to LCD Handler
1218 */
1219void
1220lcd_displayOff(LCD_Handler_t *hlcd)
1221{
1222 uint8_t parameter[1];
1223 parameter[0] = 0xFE;
1224 /* Display OFF command */
1225 ST7789H2_WriteReg(hlcd, ST7789H2_DISPLAY_OFF, parameter, 1);
1226 /* Sleep In Command */
1227 ST7789H2_WriteReg(hlcd, ST7789H2_SLEEP_IN, (uint8_t*)NULL, 0);
1228 /* Wait for 10ms */
1229 dly_tsk(10);
1230}
1231
1232
1233/*
1234 * トライアングルフィル
1235 * param1 pDrawProp: Pointer to Draw Prop
1236 * param2 x1: 点1 X座標始点
1237 * param3 y1: 点1 Y座標始点
1238 * param4 x2: 点2 X座標始点
1239 * param5 y2: 点2 Y座標始点
1240 * param6 x3: 点3 X座標始点
1241 * param7 y3: 点3 Y座標始点
1242 */
1243static void
1244FillTriangle(LCD_DrawProp_t *pDrawProp, uint16_t x1, uint16_t x2, uint16_t x3, uint16_t y1, uint16_t y2, uint16_t y3)
1245{
1246 int16_t deltax = 0, deltay = 0, x = 0, y = 0, xinc1 = 0, xinc2 = 0,
1247 yinc1 = 0, yinc2 = 0, den = 0, num = 0, numadd = 0, numpixels = 0,
1248 curpixel = 0;
1249
1250 deltax = ABS(x2 - x1); /* The difference between the x's */
1251 deltay = ABS(y2 - y1); /* The difference between the y's */
1252 x = x1; /* Start x off at the first pixel */
1253 y = y1; /* Start y off at the first pixel */
1254
1255 if(x2 >= x1){ /* The x-values are increasing */
1256 xinc1 = 1;
1257 xinc2 = 1;
1258 }
1259 else{ /* The x-values are decreasing */
1260 xinc1 = -1;
1261 xinc2 = -1;
1262 }
1263
1264 if(y2 >= y1){ /* The y-values are increasing */
1265 yinc1 = 1;
1266 yinc2 = 1;
1267 }
1268 else{ /* The y-values are decreasing */
1269 yinc1 = -1;
1270 yinc2 = -1;
1271 }
1272
1273 if(deltax >= deltay){ /* There is at least one x-value for every y-value */
1274 xinc1 = 0; /* Don't change the x when numerator >= denominator */
1275 yinc2 = 0; /* Don't change the y for every iteration */
1276 den = deltax;
1277 num = deltax / 2;
1278 numadd = deltay;
1279 numpixels = deltax; /* There are more x-values than y-values */
1280 }
1281 else{ /* There is at least one y-value for every x-value */
1282 xinc2 = 0; /* Don't change the x for every iteration */
1283 yinc1 = 0; /* Don't change the y when numerator >= denominator */
1284 den = deltay;
1285 num = deltay / 2;
1286 numadd = deltax;
1287 numpixels = deltay; /* There are more y-values than x-values */
1288 }
1289
1290 for(curpixel = 0 ; curpixel <= numpixels ; curpixel++){
1291 lcd_drawLine(pDrawProp, x, y, x3, y3);
1292 num += numadd; /* Increase the numerator by the top of the fraction */
1293 if(num >= den){ /* Check if numerator >= denominator */
1294 num -= den; /* Calculate the new numerator value */
1295 x += xinc1; /* Change the x as appropriate */
1296 y += yinc1; /* Change the y as appropriate */
1297 }
1298 x += xinc2; /* Change the x as appropriate */
1299 y += yinc2; /* Change the y as appropriate */
1300 }
1301}
1302
Note: See TracBrowser for help on using the repository browser.