drv_lcd.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. /*
  2. * File : drv_lcd.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2009 RT-Thread Develop Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2018-09-13 xuzhuoyi first implementation
  13. */
  14. #include "drv_lcd.h"
  15. #include <finsh.h>
  16. //#define DEBUG
  17. #ifdef DEBUG
  18. #define DEBUG_PRINTF(...) rt_kprintf(__VA_ARGS__)
  19. #else
  20. #define DEBUG_PRINTF(...)
  21. #endif
  22. typedef struct
  23. {
  24. rt_uint16_t width; //LCD 宽度
  25. rt_uint16_t height; //LCD 高度
  26. rt_uint16_t id; //LCD ID
  27. rt_uint8_t dir; //横屏还是竖屏控制:0,竖屏;1,横屏。
  28. rt_uint16_t wramcmd; //开始写gram指令
  29. rt_uint16_t setxcmd; //设置x坐标指令
  30. rt_uint16_t setycmd; //设置y坐标指令
  31. } lcd_info_t;
  32. typedef struct
  33. {
  34. volatile rt_uint16_t reg;
  35. volatile rt_uint16_t ram;
  36. } lcd_ili9341_t;
  37. //使用NOR/SRAM的 Bank1.sector1,地址位HADDR[27,26]=00 A18作为数据命令区分线
  38. //注意设置时STM32内部会右移一位对其!
  39. #define LCD_ILI9341_BASE ((rt_uint32_t)(0x60000000 | 0x0007FFFE))
  40. #define ili9341 ((lcd_ili9341_t *) LCD_ILI9341_BASE)
  41. //////////////////////////////////////////////////////////////////////////////////
  42. //扫描方向定义
  43. #define L2R_U2D 0 //从左到右,从上到下
  44. #define L2R_D2U 1 //从左到右,从下到上
  45. #define R2L_U2D 2 //从右到左,从上到下
  46. #define R2L_D2U 3 //从右到左,从下到上
  47. #define U2D_L2R 4 //从上到下,从左到右
  48. #define U2D_R2L 5 //从上到下,从右到左
  49. #define D2U_L2R 6 //从下到上,从左到右
  50. #define D2U_R2L 7 //从下到上,从右到左
  51. #define DFT_SCAN_DIR L2R_U2D //默认的扫描方向
  52. static lcd_info_t lcddev;
  53. LTDC_HandleTypeDef LtdcHandler;
  54. static RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
  55. /* Default LCD configuration with LCD Layer 1 */
  56. static uint32_t ActiveLayer = 0;
  57. SPI_HandleTypeDef hspi5;
  58. static SPI_HandleTypeDef SpiHandle;
  59. void delay_us(rt_uint32_t nus)
  60. {
  61. //rt_thread_delay(1);
  62. while (nus--) {
  63. __NOP();
  64. }
  65. }
  66. void delay_ms(rt_uint32_t nms)
  67. {
  68. //rt_thread_delay((RT_TICK_PER_SECOND * nms + 999) / 1000);
  69. while (nms--)
  70. {
  71. int i;
  72. for (i = 0; i < 10000; i++)
  73. {
  74. __NOP();
  75. }
  76. }
  77. }
  78. /**
  79. * @brief SPI MSP Init.
  80. * @param hspi: SPI handle
  81. */
  82. static void SPIx_MspInit(SPI_HandleTypeDef *hspi)
  83. {
  84. GPIO_InitTypeDef GPIO_InitStructure;
  85. /* Enable SPIx clock */
  86. DISCOVERY_SPIx_CLK_ENABLE();
  87. /* Enable DISCOVERY_SPI GPIO clock */
  88. DISCOVERY_SPIx_GPIO_CLK_ENABLE();
  89. /* configure SPI SCK, MOSI and MISO */
  90. GPIO_InitStructure.Pin = (DISCOVERY_SPIx_SCK_PIN | DISCOVERY_SPIx_MOSI_PIN | DISCOVERY_SPIx_MISO_PIN);
  91. GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
  92. GPIO_InitStructure.Pull = GPIO_PULLDOWN;
  93. GPIO_InitStructure.Speed = GPIO_SPEED_MEDIUM;
  94. GPIO_InitStructure.Alternate = DISCOVERY_SPIx_AF;
  95. HAL_GPIO_Init(DISCOVERY_SPIx_GPIO_PORT, &GPIO_InitStructure);
  96. }
  97. /**
  98. * @brief Selects the LCD Layer.
  99. * @param LayerIndex: the Layer foreground or background.
  100. */
  101. void BSP_LCD_SelectLayer(uint32_t LayerIndex)
  102. {
  103. ActiveLayer = LayerIndex;
  104. }
  105. /**
  106. * @brief Initializes the LTDC MSP.
  107. */
  108. __weak void BSP_LCD_MspInit(void)
  109. {
  110. GPIO_InitTypeDef GPIO_InitStructure;
  111. /* Enable the LTDC and DMA2D Clock */
  112. __HAL_RCC_LTDC_CLK_ENABLE();
  113. __HAL_RCC_DMA2D_CLK_ENABLE();
  114. /* Enable GPIOs clock */
  115. __HAL_RCC_GPIOA_CLK_ENABLE();
  116. __HAL_RCC_GPIOB_CLK_ENABLE();
  117. __HAL_RCC_GPIOC_CLK_ENABLE();
  118. __HAL_RCC_GPIOD_CLK_ENABLE();
  119. __HAL_RCC_GPIOF_CLK_ENABLE();
  120. __HAL_RCC_GPIOG_CLK_ENABLE();
  121. /* GPIOs Configuration */
  122. /*
  123. +------------------------+-----------------------+----------------------------+
  124. + LCD pins assignment +
  125. +------------------------+-----------------------+----------------------------+
  126. | LCD_TFT R2 <-> PC.10 | LCD_TFT G2 <-> PA.06 | LCD_TFT B2 <-> PD.06 |
  127. | LCD_TFT R3 <-> PB.00 | LCD_TFT G3 <-> PG.10 | LCD_TFT B3 <-> PG.11 |
  128. | LCD_TFT R4 <-> PA.11 | LCD_TFT G4 <-> PB.10 | LCD_TFT B4 <-> PG.12 |
  129. | LCD_TFT R5 <-> PA.12 | LCD_TFT G5 <-> PB.11 | LCD_TFT B5 <-> PA.03 |
  130. | LCD_TFT R6 <-> PB.01 | LCD_TFT G6 <-> PC.07 | LCD_TFT B6 <-> PB.08 |
  131. | LCD_TFT R7 <-> PG.06 | LCD_TFT G7 <-> PD.03 | LCD_TFT B7 <-> PB.09 |
  132. -------------------------------------------------------------------------------
  133. | LCD_TFT HSYNC <-> PC.06 | LCDTFT VSYNC <-> PA.04 |
  134. | LCD_TFT CLK <-> PG.07 | LCD_TFT DE <-> PF.10 |
  135. -----------------------------------------------------
  136. */
  137. /* GPIOA configuration */
  138. GPIO_InitStructure.Pin = GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_6 |
  139. GPIO_PIN_11 | GPIO_PIN_12;
  140. GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
  141. GPIO_InitStructure.Pull = GPIO_NOPULL;
  142. GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
  143. GPIO_InitStructure.Alternate= GPIO_AF14_LTDC;
  144. HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
  145. /* GPIOB configuration */
  146. GPIO_InitStructure.Pin = GPIO_PIN_8 | \
  147. GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11;
  148. HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
  149. /* GPIOC configuration */
  150. GPIO_InitStructure.Pin = GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_10;
  151. HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
  152. /* GPIOD configuration */
  153. GPIO_InitStructure.Pin = GPIO_PIN_3 | GPIO_PIN_6;
  154. HAL_GPIO_Init(GPIOD, &GPIO_InitStructure);
  155. /* GPIOF configuration */
  156. GPIO_InitStructure.Pin = GPIO_PIN_10;
  157. HAL_GPIO_Init(GPIOF, &GPIO_InitStructure);
  158. /* GPIOG configuration */
  159. GPIO_InitStructure.Pin = GPIO_PIN_6 | GPIO_PIN_7 | \
  160. GPIO_PIN_11;
  161. HAL_GPIO_Init(GPIOG, &GPIO_InitStructure);
  162. /* GPIOB configuration */
  163. GPIO_InitStructure.Pin = GPIO_PIN_0 | GPIO_PIN_1;
  164. GPIO_InitStructure.Alternate= GPIO_AF9_LTDC;
  165. HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
  166. /* GPIOG configuration */
  167. GPIO_InitStructure.Pin = GPIO_PIN_10 | GPIO_PIN_12;
  168. HAL_GPIO_Init(GPIOG, &GPIO_InitStructure);
  169. }
  170. /**
  171. * @brief SPIx error treatment function.
  172. */
  173. static void SPIx_Error(void)
  174. {
  175. /* De-initialize the SPI communication BUS */
  176. HAL_SPI_DeInit(&hspi5);
  177. /* Re- Initialize the SPI communication BUS */
  178. /* SPI5 parameter configuration*/
  179. hspi5.Instance = SPI5;
  180. hspi5.Init.Mode = SPI_MODE_MASTER;
  181. hspi5.Init.Direction = SPI_DIRECTION_2LINES;
  182. hspi5.Init.DataSize = SPI_DATASIZE_8BIT;
  183. hspi5.Init.CLKPolarity = SPI_POLARITY_LOW;
  184. hspi5.Init.CLKPhase = SPI_PHASE_1EDGE;
  185. hspi5.Init.NSS = SPI_NSS_SOFT;
  186. hspi5.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
  187. hspi5.Init.FirstBit = SPI_FIRSTBIT_MSB;
  188. hspi5.Init.TIMode = SPI_TIMODE_DISABLE;
  189. hspi5.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  190. hspi5.Init.CRCPolynomial = 10;
  191. if (HAL_SPI_Init(&hspi5) != HAL_OK)
  192. {
  193. //_Error_Handler(__FILE__, __LINE__);
  194. }
  195. }
  196. /**
  197. * @brief Writes a byte to device.
  198. * @param Value: value to be written
  199. */
  200. static void SPIx_Write(uint16_t Value)
  201. {
  202. HAL_StatusTypeDef status = HAL_OK;
  203. status = HAL_SPI_Transmit(&hspi5, (uint8_t*) &Value, 1, 0x1000);
  204. /* Check the communication status */
  205. if(status != HAL_OK)
  206. {
  207. /* Re-Initialize the BUS */
  208. SPIx_Error();
  209. }
  210. }
  211. /**
  212. * @brief Reads 4 bytes from device.
  213. * @param ReadSize: Number of bytes to read (max 4 bytes)
  214. * @retval Value read on the SPI
  215. */
  216. static uint32_t SPIx_Read(uint8_t ReadSize)
  217. {
  218. HAL_StatusTypeDef status = HAL_OK;
  219. uint32_t readvalue;
  220. status = HAL_SPI_Receive(&hspi5, (uint8_t*) &readvalue, ReadSize, 0x1000);
  221. /* Check the communication status */
  222. if(status != HAL_OK)
  223. {
  224. /* Re-Initialize the BUS */
  225. SPIx_Error();
  226. }
  227. return readvalue;
  228. }
  229. /**
  230. * @brief Configures the LCD_SPI interface.
  231. */
  232. void LCD_IO_Init(void)
  233. {
  234. GPIO_InitTypeDef GPIO_InitStructure;
  235. /* Configure NCS in Output Push-Pull mode */
  236. LCD_WRX_GPIO_CLK_ENABLE();
  237. GPIO_InitStructure.Pin = LCD_WRX_PIN;
  238. GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
  239. GPIO_InitStructure.Pull = GPIO_NOPULL;
  240. GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
  241. HAL_GPIO_Init(LCD_WRX_GPIO_PORT, &GPIO_InitStructure);
  242. LCD_RDX_GPIO_CLK_ENABLE();
  243. GPIO_InitStructure.Pin = LCD_RDX_PIN;
  244. GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
  245. GPIO_InitStructure.Pull = GPIO_NOPULL;
  246. GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
  247. HAL_GPIO_Init(LCD_RDX_GPIO_PORT, &GPIO_InitStructure);
  248. /* Configure the LCD Control pins ----------------------------------------*/
  249. LCD_NCS_GPIO_CLK_ENABLE();
  250. /* Configure NCS in Output Push-Pull mode */
  251. GPIO_InitStructure.Pin = LCD_NCS_PIN;
  252. GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
  253. GPIO_InitStructure.Pull = GPIO_NOPULL;
  254. GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
  255. HAL_GPIO_Init(LCD_NCS_GPIO_PORT, &GPIO_InitStructure);
  256. /* Set or Reset the control line */
  257. LCD_CS_LOW();
  258. LCD_CS_HIGH();
  259. /* SPI configuration -----------------------------------------------------*/
  260. SpiHandle.Instance = DISCOVERY_SPIx;
  261. /* SPI baudrate is set to 5.6 MHz (PCLK2/SPI_BaudRatePrescaler = 90/16 = 5.625 MHz)
  262. to verify these constraints:
  263. - ILI9341 LCD SPI interface max baudrate is 10MHz for write and 6.66MHz for read
  264. - l3gd20 SPI interface max baudrate is 10MHz for write/read
  265. - PCLK2 frequency is set to 90 MHz
  266. */
  267. SpiHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
  268. /* On STM32F429I-Discovery, LCD ID cannot be read then keep a common configuration */
  269. /* for LCD and GYRO (SPI_DIRECTION_2LINES) */
  270. /* Note: To read a register a LCD, SPI_DIRECTION_1LINE should be set */
  271. SpiHandle.Init.Direction = SPI_DIRECTION_2LINES;
  272. SpiHandle.Init.CLKPhase = SPI_PHASE_1EDGE;
  273. SpiHandle.Init.CLKPolarity = SPI_POLARITY_LOW;
  274. SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
  275. SpiHandle.Init.CRCPolynomial = 7;
  276. SpiHandle.Init.DataSize = SPI_DATASIZE_8BIT;
  277. SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB;
  278. SpiHandle.Init.NSS = SPI_NSS_SOFT;
  279. SpiHandle.Init.TIMode = SPI_TIMODE_DISABLED;
  280. SpiHandle.Init.Mode = SPI_MODE_MASTER;
  281. SPIx_MspInit(&SpiHandle);
  282. HAL_SPI_Init(&SpiHandle);
  283. }
  284. /**
  285. * @brief Writes to the selected LCD register.
  286. * @param LCD_Reg: address of the selected register.
  287. * @retval None
  288. */
  289. void ili9341_WriteReg(uint8_t LCD_Reg)
  290. {
  291. /* Reset WRX to send command */
  292. LCD_WRX_LOW();
  293. /* Reset LCD control line(/CS) and Send command */
  294. LCD_CS_LOW();
  295. SPIx_Write(LCD_Reg);
  296. /* Deselect: Chip Select high */
  297. LCD_CS_HIGH();
  298. }
  299. /**
  300. * @brief Writes data to the selected LCD register.
  301. * @param LCD_Reg: address of the selected register.
  302. * @retval None
  303. */
  304. void ili9341_WriteData(uint16_t RegValue)
  305. {
  306. /* Set WRX to send data */
  307. LCD_WRX_HIGH();
  308. /* Reset LCD control line(/CS) and Send data */
  309. LCD_CS_LOW();
  310. SPIx_Write(RegValue);
  311. /* Deselect: Chip Select high */
  312. LCD_CS_HIGH();
  313. }
  314. /**
  315. * @brief Reads the selected LCD Register.
  316. * @param RegValue: Address of the register to read
  317. * @param ReadSize: Number of bytes to read
  318. * @retval LCD Register Value.
  319. */
  320. uint32_t ili9341_ReadData(uint16_t RegValue, uint8_t ReadSize)
  321. {
  322. uint32_t readvalue = 0;
  323. /* Select: Chip Select low */
  324. LCD_CS_LOW();
  325. /* Reset WRX to send command */
  326. LCD_WRX_LOW();
  327. SPIx_Write(RegValue);
  328. readvalue = SPIx_Read(ReadSize);
  329. /* Set WRX to send data */
  330. LCD_WRX_HIGH();
  331. /* Deselect: Chip Select high */
  332. LCD_CS_HIGH();
  333. return readvalue;
  334. }
  335. /**
  336. * @brief Enables the Display.
  337. */
  338. void BSP_LCD_DisplayOn(void)
  339. {
  340. /* Display On */
  341. ili9341_WriteReg(LCD_DISPLAY_ON);
  342. }
  343. /**
  344. * @brief Disables the Display.
  345. */
  346. void BSP_LCD_DisplayOff(void)
  347. {
  348. /* Display Off */
  349. ili9341_WriteReg(LCD_DISPLAY_OFF);
  350. }
  351. void ili9341_Init(void)
  352. {
  353. /* Initialize ILI9341 low level bus layer ----------------------------------*/
  354. LCD_IO_Init();
  355. /* Configure LCD */
  356. ili9341_WriteReg(0xCA);
  357. ili9341_WriteData(0xC3);
  358. ili9341_WriteData(0x08);
  359. ili9341_WriteData(0x50);
  360. ili9341_WriteReg(LCD_POWERB);
  361. ili9341_WriteData(0x00);
  362. ili9341_WriteData(0xC1);
  363. ili9341_WriteData(0x30);
  364. ili9341_WriteReg(LCD_POWER_SEQ);
  365. ili9341_WriteData(0x64);
  366. ili9341_WriteData(0x03);
  367. ili9341_WriteData(0x12);
  368. ili9341_WriteData(0x81);
  369. ili9341_WriteReg(LCD_DTCA);
  370. ili9341_WriteData(0x85);
  371. ili9341_WriteData(0x00);
  372. ili9341_WriteData(0x78);
  373. ili9341_WriteReg(LCD_POWERA);
  374. ili9341_WriteData(0x39);
  375. ili9341_WriteData(0x2C);
  376. ili9341_WriteData(0x00);
  377. ili9341_WriteData(0x34);
  378. ili9341_WriteData(0x02);
  379. ili9341_WriteReg(LCD_PRC);
  380. ili9341_WriteData(0x20);
  381. ili9341_WriteReg(LCD_DTCB);
  382. ili9341_WriteData(0x00);
  383. ili9341_WriteData(0x00);
  384. ili9341_WriteReg(LCD_FRMCTR1);
  385. ili9341_WriteData(0x00);
  386. ili9341_WriteData(0x1B);
  387. ili9341_WriteReg(LCD_DFC);
  388. ili9341_WriteData(0x0A);
  389. ili9341_WriteData(0xA2);
  390. ili9341_WriteReg(LCD_POWER1);
  391. ili9341_WriteData(0x10);
  392. ili9341_WriteReg(LCD_POWER2);
  393. ili9341_WriteData(0x10);
  394. ili9341_WriteReg(LCD_VCOM1);
  395. ili9341_WriteData(0x45);
  396. ili9341_WriteData(0x15);
  397. ili9341_WriteReg(LCD_VCOM2);
  398. ili9341_WriteData(0x90);
  399. ili9341_WriteReg(LCD_MAC);
  400. ili9341_WriteData(0xC8);
  401. ili9341_WriteReg(LCD_3GAMMA_EN);
  402. ili9341_WriteData(0x00);
  403. ili9341_WriteReg(LCD_RGB_INTERFACE);
  404. ili9341_WriteData(0xC2);
  405. ili9341_WriteReg(LCD_DFC);
  406. ili9341_WriteData(0x0A);
  407. ili9341_WriteData(0xA7);
  408. ili9341_WriteData(0x27);
  409. ili9341_WriteData(0x04);
  410. /* Colomn address set */
  411. ili9341_WriteReg(LCD_COLUMN_ADDR);
  412. ili9341_WriteData(0x00);
  413. ili9341_WriteData(0x00);
  414. ili9341_WriteData(0x00);
  415. ili9341_WriteData(0xEF);
  416. /* Page address set */
  417. ili9341_WriteReg(LCD_PAGE_ADDR);
  418. ili9341_WriteData(0x00);
  419. ili9341_WriteData(0x00);
  420. ili9341_WriteData(0x01);
  421. ili9341_WriteData(0x3F);
  422. ili9341_WriteReg(LCD_INTERFACE);
  423. ili9341_WriteData(0x01);
  424. ili9341_WriteData(0x00);
  425. ili9341_WriteData(0x06);
  426. ili9341_WriteReg(LCD_GRAM);
  427. delay_ms(200);
  428. ili9341_WriteReg(LCD_GAMMA);
  429. ili9341_WriteData(0x01);
  430. ili9341_WriteReg(LCD_PGAMMA);
  431. ili9341_WriteData(0x0F);
  432. ili9341_WriteData(0x29);
  433. ili9341_WriteData(0x24);
  434. ili9341_WriteData(0x0C);
  435. ili9341_WriteData(0x0E);
  436. ili9341_WriteData(0x09);
  437. ili9341_WriteData(0x4E);
  438. ili9341_WriteData(0x78);
  439. ili9341_WriteData(0x3C);
  440. ili9341_WriteData(0x09);
  441. ili9341_WriteData(0x13);
  442. ili9341_WriteData(0x05);
  443. ili9341_WriteData(0x17);
  444. ili9341_WriteData(0x11);
  445. ili9341_WriteData(0x00);
  446. ili9341_WriteReg(LCD_NGAMMA);
  447. ili9341_WriteData(0x00);
  448. ili9341_WriteData(0x16);
  449. ili9341_WriteData(0x1B);
  450. ili9341_WriteData(0x04);
  451. ili9341_WriteData(0x11);
  452. ili9341_WriteData(0x07);
  453. ili9341_WriteData(0x31);
  454. ili9341_WriteData(0x33);
  455. ili9341_WriteData(0x42);
  456. ili9341_WriteData(0x05);
  457. ili9341_WriteData(0x0C);
  458. ili9341_WriteData(0x0A);
  459. ili9341_WriteData(0x28);
  460. ili9341_WriteData(0x2F);
  461. ili9341_WriteData(0x0F);
  462. ili9341_WriteReg(LCD_SLEEP_OUT);
  463. delay_ms(200);
  464. ili9341_WriteReg(LCD_DISPLAY_ON);
  465. /* GRAM start writing */
  466. ili9341_WriteReg(LCD_GRAM);
  467. }
  468. /**
  469. * @brief Initializes the LCD layers.
  470. * @param LayerIndex: the layer foreground or background.
  471. * @param FB_Address: the layer frame buffer.
  472. */
  473. void BSP_LCD_LayerDefaultInit(uint16_t LayerIndex, uint32_t FB_Address)
  474. {
  475. LTDC_LayerCfgTypeDef Layercfg;
  476. /* Layer Init */
  477. Layercfg.WindowX0 = 0;
  478. Layercfg.WindowX1 = 240;
  479. Layercfg.WindowY0 = 0;
  480. Layercfg.WindowY1 = 320;
  481. Layercfg.PixelFormat = LTDC_PIXEL_FORMAT_RGB565;
  482. Layercfg.FBStartAdress = FB_Address;
  483. Layercfg.Alpha = 255;
  484. Layercfg.Alpha0 = 0;
  485. Layercfg.Backcolor.Blue = 0;
  486. Layercfg.Backcolor.Green = 0;
  487. Layercfg.Backcolor.Red = 0;
  488. Layercfg.BlendingFactor1 = LTDC_BLENDING_FACTOR1_PAxCA;
  489. Layercfg.BlendingFactor2 = LTDC_BLENDING_FACTOR2_PAxCA;
  490. Layercfg.ImageWidth = 240;
  491. Layercfg.ImageHeight = 320;
  492. HAL_LTDC_ConfigLayer(&LtdcHandler, &Layercfg, LayerIndex);
  493. //DrawProp[LayerIndex].BackColor = LCD_COLOR_WHITE;
  494. //DrawProp[LayerIndex].pFont = &Font24;
  495. //DrawProp[LayerIndex].TextColor = LCD_COLOR_BLACK;
  496. /* Dithering activation */
  497. HAL_LTDC_EnableDither(&LtdcHandler);
  498. }
  499. uint8_t BSP_LCD_Init(void)
  500. {
  501. /* On STM32F429I-DISCO, it is not possible to read ILI9341 ID because */
  502. /* PIN EXTC is not connected to VDD and then LCD_READ_ID4 is not accessible. */
  503. /* In this case, ReadID function is bypassed.*/
  504. /*if(ili9341_drv.ReadID() == ILI9341_ID)*/
  505. /* LTDC Configuration ----------------------------------------------------*/
  506. LtdcHandler.Instance = LTDC;
  507. /* Timing configuration (Typical configuration from ILI9341 datasheet)
  508. HSYNC=10 (9+1)
  509. HBP=20 (29-10+1)
  510. ActiveW=240 (269-20-10+1)
  511. HFP=10 (279-240-20-10+1)
  512. VSYNC=2 (1+1)
  513. VBP=2 (3-2+1)
  514. ActiveH=320 (323-2-2+1)
  515. VFP=4 (327-320-2-2+1)
  516. */
  517. /* Configure horizontal synchronization width */
  518. LtdcHandler.Init.HorizontalSync = ILI9341_HSYNC;
  519. /* Configure vertical synchronization height */
  520. LtdcHandler.Init.VerticalSync = ILI9341_VSYNC;
  521. /* Configure accumulated horizontal back porch */
  522. LtdcHandler.Init.AccumulatedHBP = ILI9341_HBP;
  523. /* Configure accumulated vertical back porch */
  524. LtdcHandler.Init.AccumulatedVBP = ILI9341_VBP;
  525. /* Configure accumulated active width */
  526. LtdcHandler.Init.AccumulatedActiveW = 269;
  527. /* Configure accumulated active height */
  528. LtdcHandler.Init.AccumulatedActiveH = 323;
  529. /* Configure total width */
  530. LtdcHandler.Init.TotalWidth = 279;
  531. /* Configure total height */
  532. LtdcHandler.Init.TotalHeigh = 327;
  533. /* Configure R,G,B component values for LCD background color */
  534. LtdcHandler.Init.Backcolor.Red= 0;
  535. LtdcHandler.Init.Backcolor.Blue= 0;
  536. LtdcHandler.Init.Backcolor.Green= 0;
  537. /* LCD clock configuration */
  538. /* PLLSAI_VCO Input = HSE_VALUE/PLL_M = 1 Mhz */
  539. /* PLLSAI_VCO Output = PLLSAI_VCO Input * PLLSAIN = 192 Mhz */
  540. /* PLLLCDCLK = PLLSAI_VCO Output/PLLSAIR = 192/4 = 48 Mhz */
  541. /* LTDC clock frequency = PLLLCDCLK / LTDC_PLLSAI_DIVR_8 = 48/4 = 6Mhz */
  542. PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LTDC;
  543. PeriphClkInitStruct.PLLSAI.PLLSAIN = 192;
  544. PeriphClkInitStruct.PLLSAI.PLLSAIR = 4;
  545. PeriphClkInitStruct.PLLSAIDivR = RCC_PLLSAIDIVR_8;
  546. HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
  547. /* Polarity */
  548. LtdcHandler.Init.HSPolarity = LTDC_HSPOLARITY_AL;
  549. LtdcHandler.Init.VSPolarity = LTDC_VSPOLARITY_AL;
  550. LtdcHandler.Init.DEPolarity = LTDC_DEPOLARITY_AL;
  551. LtdcHandler.Init.PCPolarity = LTDC_PCPOLARITY_IPC;
  552. BSP_LCD_MspInit();
  553. HAL_LTDC_Init(&LtdcHandler);
  554. /* Select the device */
  555. //LcdDrv = &ili9341_drv;
  556. /* LCD Init */
  557. ili9341_Init();
  558. /* Initialize the SDRAM */
  559. //BSP_SDRAM_Init();
  560. /* Initialize the font */
  561. //BSP_LCD_SetFont(&LCD_DEFAULT_FONT);
  562. return 0;
  563. }
  564. void BSP_LCD_DrawPixel(uint16_t Xpos, uint16_t Ypos, uint32_t RGB_Code)
  565. {
  566. /* Write data value to all SDRAM memory */
  567. *(__IO uint32_t*) (LtdcHandler.LayerCfg[ActiveLayer].FBStartAdress + (2*(Ypos*240 + Xpos))) = RGB_Code;
  568. }
  569. void BSP_LCD_DrawLine(uint32_t pixel, uint16_t X1, uint16_t Y1, uint16_t X2, uint16_t Y2)
  570. {
  571. int16_t deltax = 0, deltay = 0, x = 0, y = 0, xinc1 = 0, xinc2 = 0,
  572. yinc1 = 0, yinc2 = 0, den = 0, num = 0, numadd = 0, numpixels = 0,
  573. curpixel = 0;
  574. deltax = ABS(X2 - X1); /* The difference between the x's */
  575. deltay = ABS(Y2 - Y1); /* The difference between the y's */
  576. x = X1; /* Start x off at the first pixel */
  577. y = Y1; /* Start y off at the first pixel */
  578. if (X2 >= X1) /* The x-values are increasing */
  579. {
  580. xinc1 = 1;
  581. xinc2 = 1;
  582. }
  583. else /* The x-values are decreasing */
  584. {
  585. xinc1 = -1;
  586. xinc2 = -1;
  587. }
  588. if (Y2 >= Y1) /* The y-values are increasing */
  589. {
  590. yinc1 = 1;
  591. yinc2 = 1;
  592. }
  593. else /* The y-values are decreasing */
  594. {
  595. yinc1 = -1;
  596. yinc2 = -1;
  597. }
  598. if (deltax >= deltay) /* There is at least one x-value for every y-value */
  599. {
  600. xinc1 = 0; /* Don't change the x when numerator >= denominator */
  601. yinc2 = 0; /* Don't change the y for every iteration */
  602. den = deltax;
  603. num = deltax / 2;
  604. numadd = deltay;
  605. numpixels = deltax; /* There are more x-values than y-values */
  606. }
  607. else /* There is at least one y-value for every x-value */
  608. {
  609. xinc2 = 0; /* Don't change the x for every iteration */
  610. yinc1 = 0; /* Don't change the y when numerator >= denominator */
  611. den = deltay;
  612. num = deltay / 2;
  613. numadd = deltax;
  614. numpixels = deltay; /* There are more y-values than x-values */
  615. }
  616. for (curpixel = 0; curpixel <= numpixels; curpixel++)
  617. {
  618. BSP_LCD_DrawPixel(x, y, pixel); /* Draw the current pixel */
  619. num += numadd; /* Increase the numerator by the top of the fraction */
  620. if (num >= den) /* Check if numerator >= denominator */
  621. {
  622. num -= den; /* Calculate the new numerator value */
  623. x += xinc1; /* Change the x as appropriate */
  624. y += yinc1; /* Change the y as appropriate */
  625. }
  626. x += xinc2; /* Change the x as appropriate */
  627. y += yinc2; /* Change the y as appropriate */
  628. }
  629. }
  630. rt_uint16_t ili9341_bgr2rgb(rt_uint16_t value)
  631. {
  632. rt_uint16_t red, green, blue;
  633. blue = (value >> 0) & 0x1f;
  634. green = (value >> 5) & 0x3f;
  635. red = (value >> 11) & 0x1f;
  636. return (blue << 11) + (green << 5) + (red << 0);
  637. }
  638. //static void ili9341_set_scan_direction(rt_uint8_t dir)
  639. //{
  640. // rt_uint16_t regval = 0;
  641. // rt_uint16_t dirreg = 0;
  642. // rt_uint16_t temp;
  643. // switch (dir)
  644. // {
  645. // case L2R_U2D://从左到右,从上到下
  646. // regval |= (0 << 7) | (0 << 6) | (0 << 5);
  647. // break;
  648. // case L2R_D2U://从左到右,从下到上
  649. // regval |= (1 << 7) | (0 << 6) | (0 << 5);
  650. // break;
  651. // case R2L_U2D://从右到左,从上到下
  652. // regval |= (0 << 7) | (1 << 6) | (0 << 5);
  653. // break;
  654. // case R2L_D2U://从右到左,从下到上
  655. // regval |= (1 << 7) | (1 << 6) | (0 << 5);
  656. // break;
  657. // case U2D_L2R://从上到下,从左到右
  658. // regval |= (0 << 7) | (0 << 6) | (1 << 5);
  659. // break;
  660. // case U2D_R2L://从上到下,从右到左
  661. // regval |= (0 << 7) | (1 << 6) | (1 << 5);
  662. // break;
  663. // case D2U_L2R://从下到上,从左到右
  664. // regval |= (1 << 7) | (0 << 6) | (1 << 5);
  665. // break;
  666. // case D2U_R2L://从下到上,从右到左
  667. // regval |= (1 << 7) | (1 << 6) | (1 << 5);
  668. // break;
  669. // }
  670. // dirreg = 0X36;
  671. // ili9341_write_reg_with_value(dirreg, regval);
  672. // if (regval & 0X20)
  673. // {
  674. // if (lcddev.width < lcddev.height)//交换X,Y
  675. // {
  676. // temp = lcddev.width;
  677. // lcddev.width = lcddev.height;
  678. // lcddev.height = temp;
  679. // }
  680. // }
  681. // else
  682. // {
  683. // if (lcddev.width > lcddev.height)//交换X,Y
  684. // {
  685. // temp = lcddev.width;
  686. // lcddev.width = lcddev.height;
  687. // lcddev.height = temp;
  688. // }
  689. // }
  690. //
  691. // ili9341_write_reg(lcddev.setxcmd);
  692. // ili9341_write_data(0);
  693. // ili9341_write_data(0);
  694. // ili9341_write_data((lcddev.width - 1) >> 8);
  695. // ili9341_write_data((lcddev.width - 1) & 0XFF);
  696. // ili9341_write_reg(lcddev.setycmd);
  697. // ili9341_write_data(0);
  698. // ili9341_write_data(0);
  699. // ili9341_write_data((lcddev.height - 1) >> 8);
  700. // ili9341_write_data((lcddev.height - 1) & 0XFF);
  701. //}
  702. //void ili9341_set_backlight(rt_uint8_t pwm)
  703. //{
  704. // ili9341_write_reg(0xBE);
  705. // ili9341_write_data(0x05);
  706. // ili9341_write_data(pwm*2.55);
  707. // ili9341_write_data(0x01);
  708. // ili9341_write_data(0xFF);
  709. // ili9341_write_data(0x00);
  710. // ili9341_write_data(0x00);
  711. //}
  712. //void ili9341_set_display_direction(rt_uint8_t dir)
  713. //{
  714. // lcddev.dir = dir;
  715. // if (dir == 0)
  716. // {
  717. // lcddev.width = 240;
  718. // lcddev.height = 320;
  719. // }
  720. // else
  721. // {
  722. // lcddev.width = 320;
  723. // lcddev.height = 240;
  724. // }
  725. // lcddev.wramcmd = 0X2C;
  726. // lcddev.setxcmd = 0X2A;
  727. // lcddev.setycmd = 0X2B;
  728. // ili9341_set_scan_direction(DFT_SCAN_DIR);
  729. //}
  730. void _lcd_low_level_init(void)
  731. {
  732. GPIO_InitTypeDef GPIO_InitStruct;
  733. /* GPIO Ports Clock Enable */
  734. __HAL_RCC_GPIOC_CLK_ENABLE();
  735. __HAL_RCC_GPIOD_CLK_ENABLE();
  736. /*Configure GPIO pin Output Level */
  737. HAL_GPIO_WritePin(GPIOC, CSX_Pin, GPIO_PIN_RESET);
  738. /*Configure GPIO pin Output Level */
  739. HAL_GPIO_WritePin(GPIOD, RDX_Pin|WRX_DCX_Pin, GPIO_PIN_RESET);
  740. /*Configure GPIO pins : NCS_MEMS_SPI_Pin CSX_Pin OTG_FS_PSO_Pin */
  741. GPIO_InitStruct.Pin = CSX_Pin;
  742. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  743. GPIO_InitStruct.Pull = GPIO_NOPULL;
  744. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  745. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  746. /*Configure GPIO pins : RDX_Pin WRX_DCX_Pin */
  747. GPIO_InitStruct.Pin = RDX_Pin|WRX_DCX_Pin;
  748. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  749. GPIO_InitStruct.Pull = GPIO_NOPULL;
  750. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  751. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  752. BSP_LCD_Init();
  753. BSP_LCD_LayerDefaultInit(0,0xD0000000);
  754. BSP_LCD_SelectLayer(0);
  755. BSP_LCD_DisplayOn();
  756. lcddev.width = 240;
  757. lcddev.height = 320;
  758. //ili9341_set_display_direction(0);
  759. //HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_SET);
  760. }
  761. static rt_err_t lcd_init(rt_device_t dev)
  762. {
  763. return RT_EOK;
  764. }
  765. static rt_err_t lcd_open(rt_device_t dev, rt_uint16_t oflag)
  766. {
  767. return RT_EOK;
  768. }
  769. static rt_err_t lcd_close(rt_device_t dev)
  770. {
  771. return RT_EOK;
  772. }
  773. static rt_err_t lcd_control(rt_device_t dev, int cmd, void *args)
  774. {
  775. switch (cmd)
  776. {
  777. case RTGRAPHIC_CTRL_GET_INFO:
  778. {
  779. struct rt_device_graphic_info *info;
  780. info = (struct rt_device_graphic_info*) args;
  781. RT_ASSERT(info != RT_NULL);
  782. info->bits_per_pixel = 16;
  783. info->pixel_format = RTGRAPHIC_PIXEL_FORMAT_RGB565;
  784. info->framebuffer = RT_NULL;
  785. info->width = 240;
  786. info->height = 320;
  787. }
  788. break;
  789. case RTGRAPHIC_CTRL_RECT_UPDATE:
  790. /* nothong to be done */
  791. break;
  792. default:
  793. break;
  794. }
  795. return RT_EOK;
  796. }
  797. static void ili9341_lcd_set_pixel(const char* pixel, int x, int y)
  798. {
  799. *(__IO uint16_t*)(LtdcHandler.LayerCfg[ActiveLayer].FBStartAdress + (2*(y * 240 + x))) = *(uint16_t *)pixel;
  800. }
  801. #ifdef RT_USING_FINSH
  802. static void lcd_set_pixel(uint16_t color, int x, int y)
  803. {
  804. rt_kprintf("lcd set pixel, color: %X, x: %d, y: %d", color, x, y);
  805. ili9341_lcd_set_pixel((const char *)&color, x, y);
  806. }
  807. FINSH_FUNCTION_EXPORT(lcd_set_pixel, set pixel in lcd display);
  808. #endif
  809. static void ili9341_lcd_get_pixel(char* pixel, int x, int y)
  810. {
  811. uint32_t ret = 0;
  812. if(LtdcHandler.LayerCfg[ActiveLayer].PixelFormat == LTDC_PIXEL_FORMAT_ARGB8888)
  813. {
  814. /* Read data value from SDRAM memory */
  815. ret = *(__IO uint32_t*) (LtdcHandler.LayerCfg[ActiveLayer].FBStartAdress + (4*(y * 240 + x)));
  816. }
  817. else if(LtdcHandler.LayerCfg[ActiveLayer].PixelFormat == LTDC_PIXEL_FORMAT_RGB888)
  818. {
  819. /* Read data value from SDRAM memory */
  820. ret = (*(__IO uint32_t*) (LtdcHandler.LayerCfg[ActiveLayer].FBStartAdress + (4*(y*240 + x))) & 0x00FFFFFF);
  821. }
  822. else if((LtdcHandler.LayerCfg[ActiveLayer].PixelFormat == LTDC_PIXEL_FORMAT_RGB565) || \
  823. (LtdcHandler.LayerCfg[ActiveLayer].PixelFormat == LTDC_PIXEL_FORMAT_ARGB4444) || \
  824. (LtdcHandler.LayerCfg[ActiveLayer].PixelFormat == LTDC_PIXEL_FORMAT_AL88))
  825. {
  826. /* Read data value from SDRAM memory */
  827. ret = *(__IO uint16_t*) (LtdcHandler.LayerCfg[ActiveLayer].FBStartAdress + (2*(y*240 + x)));
  828. }
  829. else
  830. {
  831. /* Read data value from SDRAM memory */
  832. ret = *(__IO uint8_t*) (LtdcHandler.LayerCfg[ActiveLayer].FBStartAdress + (2*(y*240 + x)));
  833. }
  834. *pixel = ret;
  835. }
  836. #ifdef RT_USING_FINSH
  837. static void lcd_get_pixel(int x, int y)
  838. {
  839. uint16_t pixel;
  840. ili9341_lcd_get_pixel((char *)&pixel, x, y);
  841. rt_kprintf("lcd get pixel, pixel: 0x%X, x: %d, y: %d", pixel, x, y);
  842. }
  843. FINSH_FUNCTION_EXPORT(lcd_get_pixel, get pixel in lcd display);
  844. #endif
  845. static void ili9341_lcd_draw_hline(const char* pixel, int x1, int x2, int y)
  846. {
  847. BSP_LCD_DrawLine(*pixel, x1, y, x2, y);
  848. }
  849. #ifdef RT_USING_FINSH
  850. static void lcd_draw_hline(uint16_t pixel, int x1, int x2, int y)
  851. {
  852. ili9341_lcd_draw_hline((const char *)&pixel, x1, x2, y);
  853. rt_kprintf("lcd draw hline, pixel: 0x%X, x1: %d, x2: %d, y: %d", pixel, x1, x2, y);
  854. }
  855. FINSH_FUNCTION_EXPORT(lcd_draw_hline, draw hline in lcd display);
  856. #endif
  857. static void ili9341_lcd_draw_vline(const char* pixel, int x, int y1, int y2)
  858. {
  859. BSP_LCD_DrawLine(*pixel, x, y1, x, y2);
  860. }
  861. #ifdef RT_USING_FINSH
  862. static void lcd_draw_vline(uint16_t pixel, int x, int y1, int y2)
  863. {
  864. ili9341_lcd_draw_vline((const char *)&pixel, x, y1, y2);
  865. rt_kprintf("lcd draw hline, pixel: 0x%X, x: %d, y: %d", pixel, y1, y2);
  866. }
  867. FINSH_FUNCTION_EXPORT(lcd_draw_vline, draw vline in lcd display);
  868. #endif
  869. static void ili9341_lcd_blit_line(const char* pixels, int x, int y, rt_size_t size)
  870. {
  871. int i = 0;
  872. while(size--)
  873. *(__IO uint16_t*) (LtdcHandler.LayerCfg[ActiveLayer].FBStartAdress + (2*(y*240 + x + i++))) = *(uint16_t *)pixels++;
  874. }
  875. #ifdef RT_USING_FINSH
  876. #define LINE_LEN 30
  877. static void lcd_blit_line(int x, int y)
  878. {
  879. uint16_t pixels[LINE_LEN];
  880. int i;
  881. for (i = 0; i < LINE_LEN; i++)
  882. {
  883. pixels[i] = i * 40 + 50;
  884. }
  885. ili9341_lcd_blit_line((const char *)pixels, x, y, LINE_LEN);
  886. rt_kprintf("lcd blit line, x: %d, y: %d", x, y);
  887. }
  888. FINSH_FUNCTION_EXPORT(lcd_blit_line, draw blit line in lcd display);
  889. #endif
  890. static int rt_hw_lcd_init(void)
  891. {
  892. _lcd_low_level_init();
  893. static struct rt_device lcd_device;
  894. static struct rt_device_graphic_ops ili9341_ops =
  895. {
  896. ili9341_lcd_set_pixel,
  897. ili9341_lcd_get_pixel,
  898. ili9341_lcd_draw_hline,
  899. ili9341_lcd_draw_vline,
  900. ili9341_lcd_blit_line
  901. };
  902. /* register lcd device */
  903. lcd_device.type = RT_Device_Class_Graphic;
  904. lcd_device.init = lcd_init;
  905. lcd_device.open = lcd_open;
  906. lcd_device.close = lcd_close;
  907. lcd_device.control = lcd_control;
  908. lcd_device.read = RT_NULL;
  909. lcd_device.write = RT_NULL;
  910. lcd_device.user_data = &ili9341_ops;
  911. /* register graphic device driver */
  912. rt_device_register(&lcd_device, "lcd",
  913. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);
  914. return 0;
  915. }
  916. INIT_BOARD_EXPORT(rt_hw_lcd_init);