drv_lcd.c 25 KB

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