Parcourir la source

Fix ili9341 driver as China sample.

Vladimir N. Shilov il y a 2 ans
Parent
commit
3fce189ac9
7 fichiers modifiés avec 466 ajouts et 3 suppressions
  1. 2 1
      .vscode/settings.json
  2. 1 1
      Makefile
  3. 87 0
      board/ILI9341.h
  4. 1 0
      board/board.mk
  5. 7 1
      board/board_ILI9341.h
  6. 343 0
      board/gdisp_lld_ILI9341.c
  7. 25 0
      board/gdisp_lld_config.h

+ 2 - 1
.vscode/settings.json

@@ -1,4 +1,5 @@
 {
   "makefile.extensionOutputFolder": "./.vscode",
-  "C_Cpp.errorSquiggles": "disabled"
+  "C_Cpp.errorSquiggles": "disabled",
+  "commentTranslate.multiLineMerge": true
 }

+ 1 - 1
Makefile

@@ -115,7 +115,7 @@ include $(CHIBIOS)/os/hal/lib/streams/streams.mk
 # µGFX
 GFXLIB = C:/MCU/uGFX
 include $(GFXLIB)/gfx.mk
-include $(GFXLIB)/drivers/gdisp/ILI9341/driver.mk		# your board
+#include $(GFXLIB)/drivers/gdisp/ILI9341/driver.mk		# your board
 include $(GFXLIB)/drivers/ginput/toggle/Pal/driver.mk		# your button keys
 include $(GFXLIB)/drivers/ginput/touch/ADS7843/driver.mk	# your touch panel
 

+ 87 - 0
board/ILI9341.h

@@ -0,0 +1,87 @@
+
+#define LCD_VERTICAL_MAX        		320
+#define LCD_HORIZONTAL_MAX      		240
+
+#define ILI9341_DEVICE_CODE_READ_REG    0x00
+#define ILI9341_SOFT_RESET_REG			0x01
+#define ILI9341_IDENTINFO_R_REG			0x04
+#define ILI9341_STATUS_R_REG			0x09
+#define ILI9341_POWERMODE_R_REG			0x0A
+#define ILI9341_MADCTL_R_REG			0x0B
+#define ILI9341_PIXFORMAT_R_REG			0x0C
+#define ILI9341_IMGFORMAT_R_REG			0x0D
+#define ILI9341_SIGMODE_R_REG			0x0E
+#define ILI9341_SD_RESULT_R_REG			0x0F
+#define ILI9341_SLEEP_ENTER_REG			0x10
+#define ILI9341_SLEEP_OUT_REG			0x11
+#define ILI9341_PARTIALMODE_REG			0x12
+#define ILI9341_NORDISPMODE_REG			0x13
+#define ILI9341_INVERSIONOFF_REG		0x20
+#define ILI9341_INVERSIONON_REG			0x21
+#define ILI9341_GAMMASET_REG			0x26
+#define ILI9341_DISPLAYOFF_REG			0x28
+#define ILI9341_DISPLAYON_REG			0x29
+#define ILI9341_COLADDRSET_REG			0x2A
+#define ILI9341_PAGEADDRSET_REG			0x2B
+#define ILI9341_MEMORYWRITE_REG			0x2C
+#define ILI9341_COLORSET_REG			0x2D
+#define ILI9341_MEMORYREAD_REG			0x2E
+#define ILI9341_PARTIALAREA_REG			0x30
+#define ILI9341_VERTSCROLL_REG			0x33
+#define ILI9341_TEAREFFECTLINEOFF_REG	0x34
+#define ILI9341_TEAREFFECTLINEON_REG	0x35
+#define ILI9341_MEMACCESS_REG			0x36
+#define ILI9341_VERSCRSRART_REG			0x37
+#define ILI9341_IDLEMODEOFF_REG			0x38
+#define ILI9341_IDLEMODEON_REG			0x39
+#define ILI9341_PIXFORMATSET_REG		0x3A
+#define ILI9341_WRITEMEMCONTINUE_REG	0x3C
+#define ILI9341_READMEMCONTINUE_REG		0x3E
+#define ILI9341_SETTEATSCAN_REG			0x44
+#define ILI9341_GETSCANLINE_REG			0x45
+#define ILI9341_WRITEBRIGHT_REG			0x51
+#define ILI9341_READBRIGHT_REG			0x52
+#define ILI9341_WRITECTRL_REG			0x53
+#define ILI9341_READCTRL_REG			0x54
+#define ILI9341_WRITECABC_REG			0x55
+#define ILI9341_READCABC_REG			0x56
+#define ILI9341_WRITECABCMB_REG			0x5E
+#define ILI9341_READCABCMB_REG			0x5F
+#define ILI9341_RGB_ISCTL_REG			0xB0
+#define ILI9341_FRAMECTL_NOR_REG		0xB1
+#define ILI9341_FRAMECTL_IDLE_REG		0xB2
+#define ILI9341_FRAMECTL_PARTIAL_REG	0xB3
+#define ILI9341_INVERCTL_REG			0xB4
+#define ILI9341_BLANKPORCTL_REG			0xB5
+#define ILI9341_FUNCTONCTL_REG			0xB6
+#define ILI9341_ENTRYMODE_REG			0xB7
+#define ILI9341_BLIGHTCTL1_REG			0xB8
+#define ILI9341_BLIGHTCTL2_REG			0xB9
+#define ILI9341_BLIGHTCTL3_REG			0xBA
+#define ILI9341_BLIGHTCTL4_REG			0xBB
+#define ILI9341_BLIGHTCTL5_REG			0xBC
+#define ILI9341_BLIGHTCTL7_REG			0xBE
+#define ILI9341_BLIGHTCTL8_REG			0xBF
+#define ILI9341_POWERCTL1_REG			0xC0
+#define ILI9341_POWERCTL2_REG			0xC1
+#define ILI9341_VCOMCTL1_REG			0xC5
+#define ILI9341_VCOMCTL2_REG			0xC7
+#define ILI9341_POWERCTLA_REG			0xCB
+#define ILI9341_POWERCTLB_REG			0xCF
+#define ILI9341_NVMEMWRITE_REG			0xD0
+#define ILI9341_NVMEMPROTECTKEY_REG		0xD1
+#define ILI9341_NVMEMSTATUS_REG			0xD2
+#define ILI9341_READID4_REG				0xD3
+#define ILI9341_READID1_REG				0xDA
+#define ILI9341_READID2_REG				0xDB
+#define ILI9341_READID3_REG				0xDC
+#define ILI9341_POSGAMMACORRECTION_REG	0xE0
+#define ILI9341_NEGGAMMACORRECTION_REG	0xE1
+#define ILI9341_DIGGAMCTL1_REG			0xE2
+#define ILI9341_DIGGAMCTL2_REG			0xE3
+#define ILI9341_DIVTIMCTL_A_REG			0xE8
+#define ILI9341_DIVTIMCTL_B_REG			0xEA
+#define ILI9341_POWONSEQCTL_REG			0xED
+#define ILI9341_ENABLE_3G_REG			0xF2
+#define ILI9341_INTERFCTL_REG			0xF6
+#define ILI9341_PUMPRATIOCTL_REG		0xF7

+ 1 - 0
board/board.mk

@@ -1,5 +1,6 @@
 # List of all the board related files.
 BOARDSRC = ./board/board.c
+BOARDSRC += ./board/gdisp_lld_ILI9341.c
 
 # Required include directories
 BOARDINC = ./board

+ 7 - 1
board/board_ILI9341.h

@@ -66,7 +66,13 @@ static GFXINLINE void init_board(GDisplay *g) {
   /* Bank1 NOR/SRAM timing register for write configuration, if extended mode is used */
   // Address setup time (ADDSET) is 8 HCLK = 48ns
   // Data save time is 6ns*9 HCLK = 54ns
-  FSMC_Bank1E->BWTR[FSMC_Bank] = (uint32_t)(0x08 | (0x08 << 8));
+  //FSMC_Bank1E->BWTR[FSMC_Bank] = (uint32_t)(0x08 | (0x08 << 8));
+  /* ILI9341 can WR fastes: */
+  //FSMC_Bank1E->BWTR[FSMC_Bank]&=~(0XF<<0);	//Address setup time (ADDSET) is cleared
+  //FSMC_Bank1E->BWTR[FSMC_Bank]&=~(0XF<<8);	//Data storage time is cleared
+  //FSMC_Bank1E->BWTR[FSMC_Bank] |=3<<0;  //Address setup time (ADDSET) is 3 HCLK = 18ns
+  //FSMC_Bank1E->BWTR[FSMC_Bank] |=2<<8;  //Data storage time is 6ns*3 HCLK=18ns
+  FSMC_Bank1E->BWTR[FSMC_Bank] = (uint32_t)((3 << 0) | (2 <<8 ));
 
   /* Enable the selected NOR/SRAM Bank by setting the PBKEN bit in the BCRx register */
   FSMC_Bank1->BTCR[FSMC_Bank] |= 0x01;

+ 343 - 0
board/gdisp_lld_ILI9341.c

@@ -0,0 +1,343 @@
+/*
+ * This file is subject to the terms of the GFX License. If a copy of
+ * the license was not distributed with this file, you can obtain one at:
+ *
+ *              http://ugfx.io/license.html
+ */
+
+#include "gfx.h"
+
+#if GFX_USE_GDISP
+
+#if defined(GDISP_SCREEN_HEIGHT) || defined(GDISP_SCREEN_HEIGHT)
+	#if GFX_COMPILER_WARNING_TYPE == GFX_COMPILER_WARNING_DIRECT
+		#warning "GDISP: This low level driver does not support setting a screen size. It is being ignored."
+	#elif GFX_COMPILER_WARNING_TYPE == GFX_COMPILER_WARNING_MACRO
+		COMPILER_WARNING("GDISP: This low level driver does not support setting a screen size. It is being ignored.")
+	#endif
+	#undef GDISP_SCREEN_WIDTH
+	#undef GDISP_SCREEN_HEIGHT
+#endif
+
+#define GDISP_DRIVER_VMT			GDISPVMT_ILI9341
+#include "gdisp_lld_config.h"
+#include "src/gdisp/gdisp_driver.h"
+
+#include "board_ILI9341.h"
+
+/*===========================================================================*/
+/* Driver local definitions.                                                 */
+/*===========================================================================*/
+
+#ifndef GDISP_SCREEN_HEIGHT
+	#define GDISP_SCREEN_HEIGHT		320
+#endif
+#ifndef GDISP_SCREEN_WIDTH
+	#define GDISP_SCREEN_WIDTH		240
+#endif
+#ifndef GDISP_INITIAL_CONTRAST
+	#define GDISP_INITIAL_CONTRAST	50
+#endif
+#ifndef GDISP_INITIAL_BACKLIGHT
+	#define GDISP_INITIAL_BACKLIGHT	100
+#endif
+
+#include "ILI9341.h"
+
+/*===========================================================================*/
+/* Driver local functions.                                                   */
+/*===========================================================================*/
+
+// Some common routines and macros
+#define dummy_read(g)				{ volatile gU16 dummy; dummy = read_data(g); (void) dummy; }
+#define write_reg(g, reg, data)		{ write_index(g, reg); write_data(g, data); }
+#define write_data16(g, data)		{ write_data(g, data >> 8); write_data(g, (gU8)data); }
+#define delay(us)					gfxSleepMicroseconds(us)
+#define delayms(ms)					gfxSleepMilliseconds(ms)
+
+static void set_viewport(GDisplay *g) {
+	write_index(g, 0x2A);
+	write_data(g, (g->p.x >> 8));
+	write_data(g, (gU8) g->p.x);
+	write_data(g, (g->p.x + g->p.cx - 1) >> 8);
+	write_data(g, (gU8) (g->p.x + g->p.cx - 1));
+
+	write_index(g, 0x2B);
+	write_data(g, (g->p.y >> 8));
+	write_data(g, (gU8) g->p.y);
+	write_data(g, (g->p.y + g->p.cy - 1) >> 8);
+	write_data(g, (gU8) (g->p.y + g->p.cy - 1));
+}
+
+/*===========================================================================*/
+/* Driver exported functions.                                                */
+/*===========================================================================*/
+
+LLDSPEC gBool gdisp_lld_init(GDisplay *g) {
+	// No private area for this controller
+	g->priv = 0;
+
+	// Initialise the board interface
+	init_board(g);
+
+	// Hardware reset
+	setpin_reset(g, gTrue);
+	gfxSleepMilliseconds(20);
+	setpin_reset(g, gFalse);
+	gfxSleepMilliseconds(20);
+
+	// Get the bus for the following initialisation commands
+	acquire_bus(g);
+
+	write_index(g, 0x01); //software reset
+	gfxSleepMilliseconds(5);
+	write_index(g, 0x28);
+	// display off
+	//---------------------------------------------------------
+	write_index(g, 0xcf); //Power control B
+	write_data(g, 0x00);
+	__NOP();
+	write_data(g, 0xc1); // 0x83 + PCEQ (Power Saving)
+	write_data(g, 0x30);
+
+	write_index(g, 0xed); //Power on sequence control
+	write_data(g, 0x64);
+	write_data(g, 0x03);
+	write_data(g, 0x12);
+	write_data(g, 0x81);
+
+	write_index(g, 0xe8); //Driver timing control A
+	write_data(g, 0x85);
+	write_data(g, 0x10); // 01
+	write_data(g, 0x7a); // 79
+
+	write_index(g, 0xcb); //Power control A, default values:
+	write_data(g, 0x39);
+	write_data(g, 0x2c);
+	write_data(g, 0x00);
+	write_data(g, 0x34);
+	write_data(g, 0x02);
+
+	write_index(g, 0xf7); //Pump ratio control 
+	write_data(g, 0x20);
+
+	write_index(g, 0xea); //Driver timing control B
+	write_data(g, 0x00);
+	write_data(g, 0x00);
+
+	write_index(g, 0xc0); //Power control 1
+	write_data(g, 0x1b);	// 26
+	write_index(g, 0xc1); //Power control 2
+	write_data(g, 0x01);	// 11
+
+	write_index(g, 0xc5); //Vcom control 1
+	write_data(g, 0x30);	// 35
+	write_data(g, 0x30);	// 3E
+	write_index(g, 0xc7); //Vcom control 2
+	write_data(g, 0xb7); 	// 0xbe
+
+	write_index(g, 0x36);	//Memory access control
+	write_data(g, 0x48);	// 0048 my,mx,mv,ml,BGR,mh,0.0
+	write_index(g, 0x3a); //Pixel format set
+	write_data(g, 0x55);	// 16 bit/pixel
+
+	write_index(g, 0xb1);	//Frame Rate Control
+	write_data(g, 0x00);
+	write_data(g, 0x1a); // 1b
+
+	write_index(g, 0xb6); //Display function control
+	write_data(g, 0x0a);
+	write_data(g, 0xa2); //82
+//	write_data(g, 0x27);
+//	write_data(g, 0x00);
+
+	write_index(g, 0xf2); //3Gamma Function Disable
+	write_data(g, 0x00);	// 08
+
+	write_index(g, 0x26); //Gamma Set
+	write_data(g, 0x01);	//default value
+
+	write_index(g, 0xE0); //Positive Gamma Correction
+	write_data(g, 0x0f);	// 1a
+	write_data(g, 0x2a);	// 1f
+	write_data(g, 0x28);	// 18
+	write_data(g, 0x08);	// 0a
+	write_data(g, 0x0e);	// 0f
+	write_data(g, 0x08);	// 06
+	write_data(g, 0x54);	// 45
+	write_data(g, 0xa9);	// 87
+	write_data(g, 0x43);	// 32
+	write_data(g, 0x0a);	// 0a
+	write_data(g, 0x0f);	// 07
+	write_data(g, 0x00);	// 02
+	write_data(g, 0x00);	// 07
+	write_data(g, 0x00);	// 05
+	write_data(g, 0x00);
+
+	write_index(g, 0xE1); //Negative Gamma Correction
+	write_data(g, 0x00);
+	write_data(g, 0x15);	// 25
+	write_data(g, 0x17);	// 27
+	write_data(g, 0x07);	// 05
+	write_data(g, 0x11);	// 10
+	write_data(g, 0x06);	// 09
+	write_data(g, 0x2b);	// 3a
+	write_data(g, 0x56);	// 78
+	write_data(g, 0x3c);	// 4d
+	write_data(g, 0x05);
+	write_data(g, 0x10);	// 18
+	write_data(g, 0x0f);	// 0d
+	write_data(g, 0x3f);	// 38
+	write_data(g, 0x3f);	// 3a
+	write_data(g, 0x0f);	// 1f
+
+	write_index(g, 0x2b);	//Page Address Set, size = 319
+	write_data(g, 0x00);
+	write_data(g, 0x00);
+	write_data(g, 0x01);
+	write_data(g, 0x3F);
+
+	write_index(g, 0x2a);	//Column Address Set, size = 239
+	write_data(g, 0x00);
+	write_data(g, 0x00);
+	write_data(g, 0x00);
+	write_data(g, 0xEF);
+
+	write_index(g, 0x11); //Sleep Out
+	gfxSleepMilliseconds(5); // 120 ?
+
+	write_index(g, 0x29);	//Display ON
+
+  // Finish Init
+  post_init_board(g);
+
+ 	// Release the bus
+	release_bus(g);
+	
+	/* Turn on the back-light */
+	set_backlight(g, GDISP_INITIAL_BACKLIGHT);
+
+	/* Initialise the GDISP structure */
+	g->g.Width = GDISP_SCREEN_WIDTH;
+	g->g.Height = GDISP_SCREEN_HEIGHT;
+	g->g.Orientation = gOrientation0;
+	g->g.Powermode = gPowerOn;
+	g->g.Backlight = GDISP_INITIAL_BACKLIGHT;
+	g->g.Contrast = GDISP_INITIAL_CONTRAST;
+	return gTrue;
+}
+
+#if GDISP_HARDWARE_STREAM_WRITE
+	LLDSPEC	void gdisp_lld_write_start(GDisplay *g) {
+		acquire_bus(g);
+		set_viewport(g);
+		write_index(g, 0x2C);
+	}
+	LLDSPEC	void gdisp_lld_write_color(GDisplay *g) {
+		//write_data16(g, gdispColor2Native(g->p.color));
+		write_data(g, gdispColor2Native(g->p.color));
+	}
+	LLDSPEC	void gdisp_lld_write_stop(GDisplay *g) {
+		release_bus(g);
+	}
+#endif
+
+#if GDISP_HARDWARE_STREAM_READ
+	LLDSPEC	void gdisp_lld_read_start(GDisplay *g) {
+		acquire_bus(g);
+		set_viewport(g);
+		write_index(g, 0x2E);
+		setreadmode(g);
+		dummy_read(g);
+	}
+	LLDSPEC	gColor gdisp_lld_read_color(GDisplay *g) {
+		gU16	data;
+
+		data = read_data(g);
+		return gdispNative2Color(data);
+	}
+	LLDSPEC	void gdisp_lld_read_stop(GDisplay *g) {
+		setwritemode(g);
+		release_bus(g);
+	}
+#endif
+
+#if GDISP_NEED_CONTROL && GDISP_HARDWARE_CONTROL
+	LLDSPEC void gdisp_lld_control(GDisplay *g) {
+		switch(g->p.x) {
+		case GDISP_CONTROL_POWER:
+			if (g->g.Powermode == (gPowermode)g->p.ptr)
+				return;
+			switch((gPowermode)g->p.ptr) {
+			case gPowerOff:
+			case gPowerSleep:
+			case gPowerDeepSleep:
+				acquire_bus(g);
+				write_reg(g, 0x0010, 0x0001);	/* enter sleep mode */
+				release_bus(g);
+				break;
+			case gPowerOn:
+				acquire_bus(g);
+				write_reg(g, 0x0011, 0x0000);	/* leave sleep mode */
+				gfxSleepMilliseconds(5);
+				release_bus(g);
+				break;
+			default:
+				return;
+			}
+			g->g.Powermode = (gPowermode)g->p.ptr;
+			return;
+
+		case GDISP_CONTROL_ORIENTATION:
+			if (g->g.Orientation == (gOrientation)g->p.ptr)
+				return;
+			switch((gOrientation)g->p.ptr) {
+			case gOrientation0:
+				acquire_bus(g);
+				write_reg(g, 0x36, 0x48);	/* X and Y axes non-inverted */
+				release_bus(g);
+				g->g.Height = GDISP_SCREEN_HEIGHT;
+				g->g.Width = GDISP_SCREEN_WIDTH;
+				break;
+			case gOrientation90:
+				acquire_bus(g);
+				write_reg(g, 0x36, 0xE8);	/* Invert X and Y axes */
+				release_bus(g);
+				g->g.Height = GDISP_SCREEN_WIDTH;
+				g->g.Width = GDISP_SCREEN_HEIGHT;
+				break;
+			case gOrientation180:
+				acquire_bus(g);
+				write_reg(g, 0x36, 0x88);		/* X and Y axes non-inverted */
+				release_bus(g);
+				g->g.Height = GDISP_SCREEN_HEIGHT;
+				g->g.Width = GDISP_SCREEN_WIDTH;
+				break;
+			case gOrientation270:
+				acquire_bus(g);
+				write_reg(g, 0x36, 0x28);	/* Invert X and Y axes */
+				release_bus(g);
+				g->g.Height = GDISP_SCREEN_WIDTH;
+				g->g.Width = GDISP_SCREEN_HEIGHT;
+				break;
+			default:
+				return;
+			}
+			g->g.Orientation = (gOrientation)g->p.ptr;
+			return;
+
+        case GDISP_CONTROL_BACKLIGHT:
+            if ((unsigned)g->p.ptr > 100)
+            	g->p.ptr = (void *)100;
+            set_backlight(g, (unsigned)g->p.ptr);
+            g->g.Backlight = (unsigned)g->p.ptr;
+            return;
+
+		//case GDISP_CONTROL_CONTRAST:
+        default:
+            return;
+		}
+	}
+#endif
+
+#endif /* GFX_USE_GDISP */

+ 25 - 0
board/gdisp_lld_config.h

@@ -0,0 +1,25 @@
+/*
+ * This file is subject to the terms of the GFX License. If a copy of
+ * the license was not distributed with this file, you can obtain one at:
+ *
+ *              http://ugfx.io/license.html
+ */
+
+#ifndef _GDISP_LLD_CONFIG_H
+#define _GDISP_LLD_CONFIG_H
+
+#if GFX_USE_GDISP
+
+/*===========================================================================*/
+/* Driver hardware support.                                                  */
+/*===========================================================================*/
+
+#define GDISP_HARDWARE_STREAM_WRITE		GFXON
+#define GDISP_HARDWARE_STREAM_READ		GFXON
+#define GDISP_HARDWARE_CONTROL			  GFXON
+
+#define GDISP_LLD_PIXELFORMAT			    GDISP_PIXELFORMAT_RGB565
+
+#endif	/* GFX_USE_GDISP */
+
+#endif	/* _GDISP_LLD_CONFIG_H */