drv_lcd.c 27 KB

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