drv_lcd.c 27 KB

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