drv_lcd.c 30 KB

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