ili9320.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /**
  2. ******************************************************************************
  3. * @file ili9320.c
  4. * @author MCD Application Team
  5. * @version V1.2.2
  6. * @date 02-December-2014
  7. * @brief This file includes the LCD driver for ILI9320 LCD.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
  12. *
  13. * Redistribution and use in source and binary forms, with or without modification,
  14. * are permitted provided that the following conditions are met:
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  30. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  32. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ******************************************************************************
  36. */
  37. /* Includes ------------------------------------------------------------------*/
  38. #include "ili9320.h"
  39. /** @addtogroup BSP
  40. * @{
  41. */
  42. /** @addtogroup Components
  43. * @{
  44. */
  45. /** @addtogroup ili9320
  46. * @brief This file provides a set of functions needed to drive the
  47. * ILI9320 LCD.
  48. * @{
  49. */
  50. /** @defgroup ILI9320_Private_TypesDefinitions
  51. * @{
  52. */
  53. /**
  54. * @}
  55. */
  56. /** @defgroup ILI9320_Private_Defines
  57. * @{
  58. */
  59. /**
  60. * @}
  61. */
  62. /** @defgroup ILI9320_Private_Macros
  63. * @{
  64. */
  65. /**
  66. * @}
  67. */
  68. /** @defgroup ILI9320_Private_Variables
  69. * @{
  70. */
  71. LCD_DrvTypeDef ili9320_drv =
  72. {
  73. ili9320_Init,
  74. ili9320_ReadID,
  75. ili9320_DisplayOn,
  76. ili9320_DisplayOff,
  77. ili9320_SetCursor,
  78. ili9320_WritePixel,
  79. ili9320_ReadPixel,
  80. ili9320_SetDisplayWindow,
  81. ili9320_DrawHLine,
  82. ili9320_DrawVLine,
  83. ili9320_GetLcdPixelWidth,
  84. ili9320_GetLcdPixelHeight,
  85. ili9320_DrawBitmap,
  86. ili9320_DrawRGBImage,
  87. };
  88. static uint8_t Is_ili9320_Initialized = 0;
  89. static uint16_t ArrayRGB[320] = {0};
  90. /**
  91. * @}
  92. */
  93. /** @defgroup ILI9320_Private_FunctionPrototypes
  94. * @{
  95. */
  96. /**
  97. * @}
  98. */
  99. /** @defgroup ILI9320_Private_Functions
  100. * @{
  101. */
  102. /**
  103. * @brief Initialise the ILI9320 LCD Component.
  104. * @param None
  105. * @retval None
  106. */
  107. void ili9320_Init(void)
  108. {
  109. if(Is_ili9320_Initialized == 0)
  110. {
  111. Is_ili9320_Initialized = 1;
  112. /* Initialise ILI9320 low level bus layer --------------------------------*/
  113. LCD_IO_Init();
  114. /* Start Initial Sequence ------------------------------------------------*/
  115. ili9320_WriteReg(LCD_REG_229,0x8000); /* Set the internal vcore voltage */
  116. ili9320_WriteReg(LCD_REG_0, 0x0001); /* Start internal OSC. */
  117. ili9320_WriteReg(LCD_REG_1, 0x0100); /* set SS and SM bit */
  118. ili9320_WriteReg(LCD_REG_2, 0x0700); /* set 1 line inversion */
  119. ili9320_WriteReg(LCD_REG_3, 0x1030); /* set GRAM write direction and BGR=1. */
  120. ili9320_WriteReg(LCD_REG_4, 0x0000); /* Resize register */
  121. ili9320_WriteReg(LCD_REG_8, 0x0202); /* set the back porch and front porch */
  122. ili9320_WriteReg(LCD_REG_9, 0x0000); /* set non-display area refresh cycle ISC[3:0] */
  123. ili9320_WriteReg(LCD_REG_10, 0x0000); /* FMARK function */
  124. ili9320_WriteReg(LCD_REG_12, 0x0000); /* RGB interface setting */
  125. ili9320_WriteReg(LCD_REG_13, 0x0000); /* Frame marker Position */
  126. ili9320_WriteReg(LCD_REG_15, 0x0000); /* RGB interface polarity */
  127. /* Power On sequence -----------------------------------------------------*/
  128. ili9320_WriteReg(LCD_REG_16, 0x0000); /* SAP, BT[3:0], AP, DSTB, SLP, STB */
  129. ili9320_WriteReg(LCD_REG_17, 0x0000); /* DC1[2:0], DC0[2:0], VC[2:0] */
  130. ili9320_WriteReg(LCD_REG_18, 0x0000); /* VREG1OUT voltage */
  131. ili9320_WriteReg(LCD_REG_19, 0x0000); /* VDV[4:0] for VCOM amplitude */
  132. ili9320_WriteReg(LCD_REG_16, 0x17B0); /* SAP, BT[3:0], AP, DSTB, SLP, STB */
  133. ili9320_WriteReg(LCD_REG_17, 0x0137); /* DC1[2:0], DC0[2:0], VC[2:0] */
  134. ili9320_WriteReg(LCD_REG_18, 0x0139); /* VREG1OUT voltage */
  135. ili9320_WriteReg(LCD_REG_19, 0x1d00); /* VDV[4:0] for VCOM amplitude */
  136. ili9320_WriteReg(LCD_REG_41, 0x0013); /* VCM[4:0] for VCOMH */
  137. ili9320_WriteReg(LCD_REG_32, 0x0000); /* GRAM horizontal Address */
  138. ili9320_WriteReg(LCD_REG_33, 0x0000); /* GRAM Vertical Address */
  139. /* Adjust the Gamma Curve ------------------------------------------------*/
  140. ili9320_WriteReg(LCD_REG_48, 0x0007);
  141. ili9320_WriteReg(LCD_REG_49, 0x0007);
  142. ili9320_WriteReg(LCD_REG_50, 0x0007);
  143. ili9320_WriteReg(LCD_REG_53, 0x0007);
  144. ili9320_WriteReg(LCD_REG_54, 0x0007);
  145. ili9320_WriteReg(LCD_REG_55, 0x0700);
  146. ili9320_WriteReg(LCD_REG_56, 0x0700);
  147. ili9320_WriteReg(LCD_REG_57, 0x0700);
  148. ili9320_WriteReg(LCD_REG_60, 0x0700);
  149. ili9320_WriteReg(LCD_REG_61, 0x1F00);
  150. /* Set GRAM area ---------------------------------------------------------*/
  151. ili9320_WriteReg(LCD_REG_80, 0x0000); /* Horizontal GRAM Start Address */
  152. ili9320_WriteReg(LCD_REG_81, 0x00EF); /* Horizontal GRAM End Address */
  153. ili9320_WriteReg(LCD_REG_82, 0x0000); /* Vertical GRAM Start Address */
  154. ili9320_WriteReg(LCD_REG_83, 0x013F); /* Vertical GRAM End Address */
  155. ili9320_WriteReg(LCD_REG_96, 0x2700); /* Gate Scan Line */
  156. ili9320_WriteReg(LCD_REG_97, 0x0001); /* NDL,VLE, REV */
  157. ili9320_WriteReg(LCD_REG_106, 0x0000); /* set scrolling line */
  158. /* Partial Display Control -----------------------------------------------*/
  159. ili9320_WriteReg(LCD_REG_128, 0x0000);
  160. ili9320_WriteReg(LCD_REG_129, 0x0000);
  161. ili9320_WriteReg(LCD_REG_130, 0x0000);
  162. ili9320_WriteReg(LCD_REG_131, 0x0000);
  163. ili9320_WriteReg(LCD_REG_132, 0x0000);
  164. ili9320_WriteReg(LCD_REG_133, 0x0000);
  165. /* Panel Control ---------------------------------------------------------*/
  166. ili9320_WriteReg(LCD_REG_144, 0x0010);
  167. ili9320_WriteReg(LCD_REG_146, 0x0000);
  168. ili9320_WriteReg(LCD_REG_147, 0x0003);
  169. ili9320_WriteReg(LCD_REG_149, 0x0110);
  170. ili9320_WriteReg(LCD_REG_151, 0x0000);
  171. ili9320_WriteReg(LCD_REG_152, 0x0000);
  172. /* Set GRAM write direction and BGR = 1 */
  173. /* I/D=01 (Horizontal : increment, Vertical : decrement) */
  174. /* AM=1 (address is updated in vertical writing direction) */
  175. ili9320_WriteReg(LCD_REG_3, 0x1018);
  176. ili9320_WriteReg(LCD_REG_7, 0x0173); /* 262K color and display ON */
  177. }
  178. /* Set the Cursor */
  179. ili9320_SetCursor(0, 0);
  180. /* Prepare to write GRAM */
  181. LCD_IO_WriteReg(LCD_REG_34);
  182. }
  183. /**
  184. * @brief Enables the Display.
  185. * @param None
  186. * @retval None
  187. */
  188. void ili9320_DisplayOn(void)
  189. {
  190. /* Power On sequence ---------------------------------------------------------*/
  191. ili9320_WriteReg(LCD_REG_16, 0x0000); /* SAP, BT[3:0], AP, DSTB, SLP, STB */
  192. ili9320_WriteReg(LCD_REG_17, 0x0000); /* DC1[2:0], DC0[2:0], VC[2:0] */
  193. ili9320_WriteReg(LCD_REG_18, 0x0000); /* VREG1OUT voltage */
  194. ili9320_WriteReg(LCD_REG_19, 0x0000); /* VDV[4:0] for VCOM amplitude*/
  195. ili9320_WriteReg(LCD_REG_16, 0x17B0); /* SAP, BT[3:0], AP, DSTB, SLP, STB */
  196. ili9320_WriteReg(LCD_REG_17, 0x0137); /* DC1[2:0], DC0[2:0], VC[2:0] */
  197. ili9320_WriteReg(LCD_REG_18, 0x0139); /* VREG1OUT voltage */
  198. ili9320_WriteReg(LCD_REG_19, 0x1d00); /* VDV[4:0] for VCOM amplitude */
  199. ili9320_WriteReg(LCD_REG_41, 0x0013); /* VCM[4:0] for VCOMH */
  200. /* Display On */
  201. ili9320_WriteReg(LCD_REG_7, 0x0173); /* 262K color and display ON */
  202. }
  203. /**
  204. * @brief Disables the Display.
  205. * @param None
  206. * @retval None
  207. */
  208. void ili9320_DisplayOff(void)
  209. {
  210. /* Power Off sequence ---------------------------------------------------------*/
  211. ili9320_WriteReg(LCD_REG_16, 0x0000); /* SAP, BT[3:0], AP, DSTB, SLP, STB */
  212. ili9320_WriteReg(LCD_REG_17, 0x0000); /* DC1[2:0], DC0[2:0], VC[2:0] */
  213. ili9320_WriteReg(LCD_REG_18, 0x0000); /* VREG1OUT voltage */
  214. ili9320_WriteReg(LCD_REG_19, 0x0000); /* VDV[4:0] for VCOM amplitude*/
  215. ili9320_WriteReg(LCD_REG_41, 0x0000); /* VCM[4:0] for VCOMH */
  216. /* Display Off */
  217. ili9320_WriteReg(LCD_REG_7, 0x0);
  218. }
  219. /**
  220. * @brief Get the LCD pixel Width.
  221. * @param None
  222. * @retval The Lcd Pixel Width
  223. */
  224. uint16_t ili9320_GetLcdPixelWidth(void)
  225. {
  226. return (uint16_t)320;
  227. }
  228. /**
  229. * @brief Get the LCD pixel Height.
  230. * @param None
  231. * @retval The Lcd Pixel Height
  232. */
  233. uint16_t ili9320_GetLcdPixelHeight(void)
  234. {
  235. return (uint16_t)240;
  236. }
  237. /**
  238. * @brief Get the ILI9320 ID.
  239. * @param None
  240. * @retval The ILI9320 ID
  241. */
  242. uint16_t ili9320_ReadID(void)
  243. {
  244. if(Is_ili9320_Initialized == 0)
  245. {
  246. ili9320_Init();
  247. }
  248. return (ili9320_ReadReg(0x00));
  249. }
  250. /**
  251. * @brief Set Cursor position.
  252. * @param Xpos: specifies the X position.
  253. * @param Ypos: specifies the Y position.
  254. * @retval None
  255. */
  256. void ili9320_SetCursor(uint16_t Xpos, uint16_t Ypos)
  257. {
  258. ili9320_WriteReg(LCD_REG_32, Ypos);
  259. ili9320_WriteReg(LCD_REG_33, (ILI9320_LCD_PIXEL_WIDTH - 1 - Xpos));
  260. }
  261. /**
  262. * @brief Write pixel.
  263. * @param Xpos: specifies the X position.
  264. * @param Ypos: specifies the Y position.
  265. * @param RGBCode: the RGB pixel color
  266. * @retval None
  267. */
  268. void ili9320_WritePixel(uint16_t Xpos, uint16_t Ypos, uint16_t RGBCode)
  269. {
  270. /* Set Cursor */
  271. ili9320_SetCursor(Xpos, Ypos);
  272. /* Prepare to write GRAM */
  273. LCD_IO_WriteReg(LCD_REG_34);
  274. /* Write 16-bit GRAM Reg */
  275. LCD_IO_WriteMultipleData((uint8_t*)&RGBCode, 2);
  276. }
  277. /**
  278. * @brief Read pixel.
  279. * @param None
  280. * @retval the RGB pixel color
  281. */
  282. uint16_t ili9320_ReadPixel(uint16_t Xpos, uint16_t Ypos)
  283. {
  284. /* Set Cursor */
  285. ili9320_SetCursor(Xpos, Ypos);
  286. /* Prepare to write GRAM */
  287. LCD_IO_WriteReg(LCD_REG_34);
  288. /* Dummy read */
  289. LCD_IO_ReadData(0x00);
  290. /* Read 16-bit Reg */
  291. return (LCD_IO_ReadData(0x00));
  292. }
  293. /**
  294. * @brief Writes to the selected LCD register.
  295. * @param LCDReg: address of the selected register.
  296. * @param LCDRegValue: value to write to the selected register.
  297. * @retval None
  298. */
  299. void ili9320_WriteReg(uint8_t LCDReg, uint16_t LCDRegValue)
  300. {
  301. LCD_IO_WriteReg(LCDReg);
  302. /* Write 16-bit GRAM Reg */
  303. LCD_IO_WriteMultipleData((uint8_t*)&LCDRegValue, 2);
  304. }
  305. /**
  306. * @brief Reads the selected LCD Register.
  307. * @param LCDReg: address of the selected register.
  308. * @retval LCD Register Value.
  309. */
  310. uint16_t ili9320_ReadReg(uint8_t LCDReg)
  311. {
  312. /* Write 16-bit Index (then Read Reg) */
  313. LCD_IO_WriteReg(LCDReg);
  314. /* Read 16-bit Reg */
  315. return (LCD_IO_ReadData(0x00));
  316. }
  317. /**
  318. * @brief Sets a display window
  319. * @param Xpos: specifies the X bottom left position.
  320. * @param Ypos: specifies the Y bottom left position.
  321. * @param Height: display window height.
  322. * @param Width: display window width.
  323. * @retval None
  324. */
  325. void ili9320_SetDisplayWindow(uint16_t Xpos, uint16_t Ypos, uint16_t Width, uint16_t Height)
  326. {
  327. /* Horizontal GRAM Start Address */
  328. ili9320_WriteReg(LCD_REG_80, (Ypos));
  329. /* Horizontal GRAM End Address */
  330. ili9320_WriteReg(LCD_REG_81, (Ypos + Height - 1));
  331. /* Vertical GRAM Start Address */
  332. ili9320_WriteReg(LCD_REG_82, ILI9320_LCD_PIXEL_WIDTH - Xpos - Width);
  333. /* Vertical GRAM End Address */
  334. ili9320_WriteReg(LCD_REG_83, ILI9320_LCD_PIXEL_WIDTH - Xpos - 1);
  335. }
  336. /**
  337. * @brief Draw vertical line.
  338. * @param RGBCode: Specifies the RGB color
  339. * @param Xpos: specifies the X position.
  340. * @param Ypos: specifies the Y position.
  341. * @param Length: specifies the Line length.
  342. * @retval None
  343. */
  344. void ili9320_DrawHLine(uint16_t RGBCode, uint16_t Xpos, uint16_t Ypos, uint16_t Length)
  345. {
  346. uint16_t counter = 0;
  347. /* Set Cursor */
  348. ili9320_SetCursor(Xpos, Ypos);
  349. /* Prepare to write GRAM */
  350. LCD_IO_WriteReg(LCD_REG_34);
  351. /* Sent a complete line */
  352. for(counter = 0; counter < Length; counter++)
  353. {
  354. ArrayRGB[counter] = RGBCode;
  355. }
  356. LCD_IO_WriteMultipleData((uint8_t*)&ArrayRGB[0], Length * 2);
  357. }
  358. /**
  359. * @brief Draw vertical line.
  360. * @param RGBCode: Specifies the RGB color
  361. * @param Xpos: specifies the X position.
  362. * @param Ypos: specifies the Y position.
  363. * @param Length: specifies the Line length.
  364. * @retval None
  365. */
  366. void ili9320_DrawVLine(uint16_t RGBCode, uint16_t Xpos, uint16_t Ypos, uint16_t Length)
  367. {
  368. uint16_t counter = 0;
  369. /* set GRAM write direction and BGR = 1 */
  370. /* I/D=00 (Horizontal : increment, Vertical : decrement) */
  371. /* AM=1 (address is updated in vertical writing direction) */
  372. ili9320_WriteReg(LCD_REG_3, 0x1010);
  373. /* Set Cursor */
  374. ili9320_SetCursor(Xpos, Ypos);
  375. /* Prepare to write GRAM */
  376. LCD_IO_WriteReg(LCD_REG_34);
  377. /* Fill a complete vertical line */
  378. for(counter = 0; counter < Length; counter++)
  379. {
  380. ArrayRGB[counter] = RGBCode;
  381. }
  382. LCD_IO_WriteMultipleData((uint8_t*)&ArrayRGB[0], Length * 2);
  383. /* set GRAM write direction and BGR = 1 */
  384. /* I/D=00 (Horizontal : increment, Vertical : decrement) */
  385. /* AM=1 (address is updated in vertical writing direction) */
  386. ili9320_WriteReg(LCD_REG_3, 0x1018);
  387. }
  388. /**
  389. * @brief Displays a bitmap picture..
  390. * @param BmpAddress: Bmp picture address.
  391. * @param Xpos: Bmp X position in the LCD
  392. * @param Ypos: Bmp Y position in the LCD
  393. * @retval None
  394. */
  395. void ili9320_DrawBitmap(uint16_t Xpos, uint16_t Ypos, uint8_t *pbmp)
  396. {
  397. uint32_t index = 0, size = 0;
  398. /* Read bitmap size */
  399. size = *(volatile uint16_t *) (pbmp + 2);
  400. size |= (*(volatile uint16_t *) (pbmp + 4)) << 16;
  401. /* Get bitmap data address offset */
  402. index = *(volatile uint16_t *) (pbmp + 10);
  403. index |= (*(volatile uint16_t *) (pbmp + 12)) << 16;
  404. size = (size - index)/2;
  405. pbmp += index;
  406. /* Set GRAM write direction and BGR = 1 */
  407. /* I/D=00 (Horizontal : decrement, Vertical : decrement) */
  408. /* AM=1 (address is updated in vertical writing direction) */
  409. ili9320_WriteReg(LCD_REG_3, 0x1008);
  410. /* Set Cursor */
  411. ili9320_SetCursor(Xpos, Ypos);
  412. /* Prepare to write GRAM */
  413. LCD_IO_WriteReg(LCD_REG_34);
  414. LCD_IO_WriteMultipleData((uint8_t*)pbmp, size*2);
  415. /* Set GRAM write direction and BGR = 1 */
  416. /* I/D = 01 (Horizontal : increment, Vertical : decrement) */
  417. /* AM = 1 (address is updated in vertical writing direction) */
  418. ili9320_WriteReg(LCD_REG_3, 0x1018);
  419. }
  420. /**
  421. * @brief Displays picture..
  422. * @param pdata: picture address.
  423. * @param Xpos: Image X position in the LCD
  424. * @param Ypos: Image Y position in the LCD
  425. * @param Xsize: Image X size in the LCD
  426. * @param Ysize: Image Y size in the LCD
  427. * @retval None
  428. */
  429. void ili9320_DrawRGBImage(uint16_t Xpos, uint16_t Ypos, uint16_t Xsize, uint16_t Ysize, uint8_t *pdata)
  430. {
  431. uint32_t size = 0;
  432. size = (Xsize * Ysize);
  433. /* Set Cursor */
  434. ili9320_SetCursor(Xpos, Ypos);
  435. /* Prepare to write GRAM */
  436. LCD_IO_WriteReg(LCD_REG_34);
  437. LCD_IO_WriteMultipleData((uint8_t*)pdata, size*2);
  438. }
  439. /**
  440. * @}
  441. */
  442. /**
  443. * @}
  444. */
  445. /**
  446. * @}
  447. */
  448. /**
  449. * @}
  450. */
  451. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/