drv_lcd.c 30 KB

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