ili9325.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /**
  2. ******************************************************************************
  3. * @file ili9325.c
  4. * @author MCD Application Team
  5. * @version V1.2.3
  6. * @date 03-May-2016
  7. * @brief This file includes the LCD driver for ILI9325 LCD.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT(c) 2016 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 "ili9325.h"
  39. /** @addtogroup BSP
  40. * @{
  41. */
  42. /** @addtogroup Components
  43. * @{
  44. */
  45. /** @addtogroup ili9325
  46. * @brief This file provides a set of functions needed to drive the
  47. * ILI9325 LCD.
  48. * @{
  49. */
  50. /** @defgroup ILI9325_Private_TypesDefinitions
  51. * @{
  52. */
  53. /**
  54. * @}
  55. */
  56. /** @defgroup ILI9325_Private_Defines
  57. * @{
  58. */
  59. /**
  60. * @}
  61. */
  62. /** @defgroup ILI9325_Private_Macros
  63. * @{
  64. */
  65. /**
  66. * @}
  67. */
  68. /** @defgroup ILI9325_Private_Variables
  69. * @{
  70. */
  71. LCD_DrvTypeDef ili9325_drv =
  72. {
  73. ili9325_Init,
  74. ili9325_ReadID,
  75. ili9325_DisplayOn,
  76. ili9325_DisplayOff,
  77. ili9325_SetCursor,
  78. ili9325_WritePixel,
  79. ili9325_ReadPixel,
  80. ili9325_SetDisplayWindow,
  81. ili9325_DrawHLine,
  82. ili9325_DrawVLine,
  83. ili9325_GetLcdPixelWidth,
  84. ili9325_GetLcdPixelHeight,
  85. ili9325_DrawBitmap,
  86. ili9325_DrawRGBImage,
  87. };
  88. static uint16_t ArrayRGB[320] = {0};
  89. /**
  90. * @}
  91. */
  92. /** @defgroup ILI9325_Private_FunctionPrototypes
  93. * @{
  94. */
  95. /**
  96. * @}
  97. */
  98. /** @defgroup ILI9325_Private_Functions
  99. * @{
  100. */
  101. /**
  102. * @brief Initialize the ILI9325 LCD Component.
  103. * @param None
  104. * @retval None
  105. */
  106. void ili9325_Init(void)
  107. {
  108. /* Initialize ILI9325 low level bus layer ----------------------------------*/
  109. LCD_IO_Init();
  110. /* Start Initial Sequence --------------------------------------------------*/
  111. ili9325_WriteReg(LCD_REG_0, 0x0001); /* Start internal OSC. */
  112. ili9325_WriteReg(LCD_REG_1, 0x0100); /* Set SS and SM bit */
  113. ili9325_WriteReg(LCD_REG_2, 0x0700); /* Set 1 line inversion */
  114. ili9325_WriteReg(LCD_REG_3, 0x1018); /* Set GRAM write direction and BGR=1. */
  115. ili9325_WriteReg(LCD_REG_4, 0x0000); /* Resize register */
  116. ili9325_WriteReg(LCD_REG_8, 0x0202); /* Set the back porch and front porch */
  117. ili9325_WriteReg(LCD_REG_9, 0x0000); /* Set non-display area refresh cycle ISC[3:0] */
  118. ili9325_WriteReg(LCD_REG_10, 0x0000); /* FMARK function */
  119. ili9325_WriteReg(LCD_REG_12, 0x0000); /* RGB interface setting */
  120. ili9325_WriteReg(LCD_REG_13, 0x0000); /* Frame marker Position */
  121. ili9325_WriteReg(LCD_REG_15, 0x0000); /* RGB interface polarity */
  122. /* Power On sequence -------------------------------------------------------*/
  123. ili9325_WriteReg(LCD_REG_16, 0x0000); /* SAP, BT[3:0], AP, DSTB, SLP, STB */
  124. ili9325_WriteReg(LCD_REG_17, 0x0000); /* DC1[2:0], DC0[2:0], VC[2:0] */
  125. ili9325_WriteReg(LCD_REG_18, 0x0000); /* VREG1OUT voltage */
  126. ili9325_WriteReg(LCD_REG_19, 0x0000); /* VDV[4:0] for VCOM amplitude */
  127. ili9325_WriteReg(LCD_REG_16, 0x17B0); /* SAP, BT[3:0], AP, DSTB, SLP, STB */
  128. ili9325_WriteReg(LCD_REG_17, 0x0137); /* DC1[2:0], DC0[2:0], VC[2:0] */
  129. ili9325_WriteReg(LCD_REG_18, 0x0139); /* VREG1OUT voltage */
  130. ili9325_WriteReg(LCD_REG_19, 0x1d00); /* VDV[4:0] for VCOM amplitude */
  131. ili9325_WriteReg(LCD_REG_41, 0x0013); /* VCM[4:0] for VCOMH */
  132. ili9325_WriteReg(LCD_REG_32, 0x0000); /* GRAM horizontal Address */
  133. ili9325_WriteReg(LCD_REG_33, 0x0000); /* GRAM Vertical Address */
  134. /* Adjust the Gamma Curve (ILI9325) ----------------------------------------*/
  135. ili9325_WriteReg(LCD_REG_48, 0x0007);
  136. ili9325_WriteReg(LCD_REG_49, 0x0302);
  137. ili9325_WriteReg(LCD_REG_50, 0x0105);
  138. ili9325_WriteReg(LCD_REG_53, 0x0206);
  139. ili9325_WriteReg(LCD_REG_54, 0x0808);
  140. ili9325_WriteReg(LCD_REG_55, 0x0206);
  141. ili9325_WriteReg(LCD_REG_56, 0x0504);
  142. ili9325_WriteReg(LCD_REG_57, 0x0007);
  143. ili9325_WriteReg(LCD_REG_60, 0x0105);
  144. ili9325_WriteReg(LCD_REG_61, 0x0808);
  145. /* Set GRAM area -----------------------------------------------------------*/
  146. ili9325_WriteReg(LCD_REG_80, 0x0000); /* Horizontal GRAM Start Address */
  147. ili9325_WriteReg(LCD_REG_81, 0x00EF); /* Horizontal GRAM End Address */
  148. ili9325_WriteReg(LCD_REG_82, 0x0000); /* Vertical GRAM Start Address */
  149. ili9325_WriteReg(LCD_REG_83, 0x013F); /* Vertical GRAM End Address */
  150. ili9325_WriteReg(LCD_REG_96, 0xA700); /* Gate Scan Line(GS=1, scan direction is G320~G1) */
  151. ili9325_WriteReg(LCD_REG_97, 0x0001); /* NDL,VLE, REV */
  152. ili9325_WriteReg(LCD_REG_106, 0x0000); /* set scrolling line */
  153. /* Partial Display Control -------------------------------------------------*/
  154. ili9325_WriteReg(LCD_REG_128, 0x0000);
  155. ili9325_WriteReg(LCD_REG_129, 0x0000);
  156. ili9325_WriteReg(LCD_REG_130, 0x0000);
  157. ili9325_WriteReg(LCD_REG_131, 0x0000);
  158. ili9325_WriteReg(LCD_REG_132, 0x0000);
  159. ili9325_WriteReg(LCD_REG_133, 0x0000);
  160. /* Panel Control -----------------------------------------------------------*/
  161. ili9325_WriteReg(LCD_REG_144, 0x0010);
  162. ili9325_WriteReg(LCD_REG_146, 0x0000);
  163. ili9325_WriteReg(LCD_REG_147, 0x0003);
  164. ili9325_WriteReg(LCD_REG_149, 0x0110);
  165. ili9325_WriteReg(LCD_REG_151, 0x0000);
  166. ili9325_WriteReg(LCD_REG_152, 0x0000);
  167. /* set GRAM write direction and BGR = 1 */
  168. /* I/D=00 (Horizontal : increment, Vertical : decrement) */
  169. /* AM=1 (address is updated in vertical writing direction) */
  170. ili9325_WriteReg(LCD_REG_3, 0x1018);
  171. /* 262K color and display ON */
  172. ili9325_WriteReg(LCD_REG_7, 0x0173);
  173. /* Set the Cursor */
  174. ili9325_SetCursor(0, 0);
  175. /* Prepare to write GRAM */
  176. LCD_IO_WriteReg(LCD_REG_34);
  177. }
  178. /**
  179. * @brief Enables the Display.
  180. * @param None
  181. * @retval None
  182. */
  183. void ili9325_DisplayOn(void)
  184. {
  185. /* Power On sequence -------------------------------------------------------*/
  186. ili9325_WriteReg(LCD_REG_16, 0x0000); /* SAP, BT[3:0], AP, DSTB, SLP, STB */
  187. ili9325_WriteReg(LCD_REG_17, 0x0000); /* DC1[2:0], DC0[2:0], VC[2:0] */
  188. ili9325_WriteReg(LCD_REG_18, 0x0000); /* VREG1OUT voltage */
  189. ili9325_WriteReg(LCD_REG_19, 0x0000); /* VDV[4:0] for VCOM amplitude*/
  190. ili9325_WriteReg(LCD_REG_16, 0x17B0); /* SAP, BT[3:0], AP, DSTB, SLP, STB */
  191. ili9325_WriteReg(LCD_REG_17, 0x0137); /* DC1[2:0], DC0[2:0], VC[2:0] */
  192. ili9325_WriteReg(LCD_REG_18, 0x0139); /* VREG1OUT voltage */
  193. ili9325_WriteReg(LCD_REG_19, 0x1d00); /* VDV[4:0] for VCOM amplitude */
  194. ili9325_WriteReg(LCD_REG_41, 0x0013); /* VCM[4:0] for VCOMH */
  195. /* Display On */
  196. ili9325_WriteReg(LCD_REG_7, 0x0173); /* 262K color and display ON */
  197. }
  198. /**
  199. * @brief Disables the Display.
  200. * @param None
  201. * @retval None
  202. */
  203. void ili9325_DisplayOff(void)
  204. {
  205. /* Power Off sequence ------------------------------------------------------*/
  206. ili9325_WriteReg(LCD_REG_16, 0x0000); /* SAP, BT[3:0], AP, DSTB, SLP, STB */
  207. ili9325_WriteReg(LCD_REG_17, 0x0000); /* DC1[2:0], DC0[2:0], VC[2:0] */
  208. ili9325_WriteReg(LCD_REG_18, 0x0000); /* VREG1OUT voltage */
  209. ili9325_WriteReg(LCD_REG_19, 0x0000); /* VDV[4:0] for VCOM amplitude*/
  210. ili9325_WriteReg(LCD_REG_41, 0x0000); /* VCM[4:0] for VCOMH */
  211. /* Display Off */
  212. ili9325_WriteReg(LCD_REG_7, 0x0);
  213. }
  214. /**
  215. * @brief Get the LCD pixel Width.
  216. * @param None
  217. * @retval The Lcd Pixel Width
  218. */
  219. uint16_t ili9325_GetLcdPixelWidth(void)
  220. {
  221. return (uint16_t)320;
  222. }
  223. /**
  224. * @brief Get the LCD pixel Height.
  225. * @param None
  226. * @retval The Lcd Pixel Height
  227. */
  228. uint16_t ili9325_GetLcdPixelHeight(void)
  229. {
  230. return (uint16_t)240;
  231. }
  232. /**
  233. * @brief Get the ILI9325 ID.
  234. * @param None
  235. * @retval The ILI9325 ID
  236. */
  237. uint16_t ili9325_ReadID(void)
  238. {
  239. LCD_IO_Init();
  240. return (ili9325_ReadReg(0x00));
  241. }
  242. /**
  243. * @brief Set Cursor position.
  244. * @param Xpos: specifies the X position.
  245. * @param Ypos: specifies the Y position.
  246. * @retval None
  247. */
  248. void ili9325_SetCursor(uint16_t Xpos, uint16_t Ypos)
  249. {
  250. ili9325_WriteReg(LCD_REG_32, Ypos);
  251. ili9325_WriteReg(LCD_REG_33, (ILI9325_LCD_PIXEL_WIDTH - 1 - Xpos));
  252. }
  253. /**
  254. * @brief Write pixel.
  255. * @param Xpos: specifies the X position.
  256. * @param Ypos: specifies the Y position.
  257. * @param RGBCode: the RGB pixel color
  258. * @retval None
  259. */
  260. void ili9325_WritePixel(uint16_t Xpos, uint16_t Ypos, uint16_t RGBCode)
  261. {
  262. /* Set Cursor */
  263. ili9325_SetCursor(Xpos, Ypos);
  264. /* Prepare to write GRAM */
  265. LCD_IO_WriteReg(LCD_REG_34);
  266. /* Write 16-bit GRAM Reg */
  267. LCD_IO_WriteMultipleData((uint8_t*)&RGBCode, 2);
  268. }
  269. /**
  270. * @brief Read pixel.
  271. * @param None
  272. * @retval The RGB pixel color
  273. */
  274. uint16_t ili9325_ReadPixel(uint16_t Xpos, uint16_t Ypos)
  275. {
  276. /* Set Cursor */
  277. ili9325_SetCursor(Xpos, Ypos);
  278. /* Read 16-bit Reg */
  279. return (LCD_IO_ReadData(LCD_REG_34));
  280. }
  281. /**
  282. * @brief Writes to the selected LCD register.
  283. * @param LCDReg: Address of the selected register.
  284. * @param LCDRegValue: Value to write to the selected register.
  285. * @retval None
  286. */
  287. void ili9325_WriteReg(uint8_t LCDReg, uint16_t LCDRegValue)
  288. {
  289. LCD_IO_WriteReg(LCDReg);
  290. /* Write 16-bit GRAM Reg */
  291. LCD_IO_WriteMultipleData((uint8_t*)&LCDRegValue, 2);
  292. }
  293. /**
  294. * @brief Reads the selected LCD Register.
  295. * @param LCDReg: address of the selected register.
  296. * @retval LCD Register Value.
  297. */
  298. uint16_t ili9325_ReadReg(uint8_t LCDReg)
  299. {
  300. /* Read 16-bit Reg */
  301. return (LCD_IO_ReadData(LCDReg));
  302. }
  303. /**
  304. * @brief Sets a display window
  305. * @param Xpos: specifies the X bottom left position.
  306. * @param Ypos: specifies the Y bottom left position.
  307. * @param Height: display window height.
  308. * @param Width: display window width.
  309. * @retval None
  310. */
  311. void ili9325_SetDisplayWindow(uint16_t Xpos, uint16_t Ypos, uint16_t Width, uint16_t Height)
  312. {
  313. /* Horizontal GRAM Start Address */
  314. ili9325_WriteReg(LCD_REG_80, (Ypos));
  315. /* Horizontal GRAM End Address */
  316. ili9325_WriteReg(LCD_REG_81, (Ypos + Height - 1));
  317. /* Vertical GRAM Start Address */
  318. ili9325_WriteReg(LCD_REG_82, ILI9325_LCD_PIXEL_WIDTH - Xpos - Width);
  319. /* Vertical GRAM End Address */
  320. ili9325_WriteReg(LCD_REG_83, ILI9325_LCD_PIXEL_WIDTH - Xpos - 1);
  321. }
  322. /**
  323. * @brief Draw vertical line.
  324. * @param RGBCode: Specifies the RGB color
  325. * @param Xpos: specifies the X position.
  326. * @param Ypos: specifies the Y position.
  327. * @param Length: specifies the Line length.
  328. * @retval None
  329. */
  330. void ili9325_DrawHLine(uint16_t RGBCode, uint16_t Xpos, uint16_t Ypos, uint16_t Length)
  331. {
  332. uint16_t counter = 0;
  333. /* Set Cursor */
  334. ili9325_SetCursor(Xpos, Ypos);
  335. /* Prepare to write GRAM */
  336. LCD_IO_WriteReg(LCD_REG_34);
  337. /* Sent a complete line */
  338. for(counter = 0; counter < Length; counter++)
  339. {
  340. ArrayRGB[counter] = RGBCode;
  341. }
  342. LCD_IO_WriteMultipleData((uint8_t*)&ArrayRGB[0], Length * 2);
  343. }
  344. /**
  345. * @brief Draw vertical line.
  346. * @param RGBCode: Specifies the RGB color
  347. * @param Xpos: specifies the X position.
  348. * @param Ypos: specifies the Y position.
  349. * @param Length: specifies the Line length.
  350. * @retval None
  351. */
  352. void ili9325_DrawVLine(uint16_t RGBCode, uint16_t Xpos, uint16_t Ypos, uint16_t Length)
  353. {
  354. uint16_t counter = 0;
  355. /* set GRAM write direction and BGR = 1 */
  356. /* I/D=00 (Horizontal : increment, Vertical : decrement) */
  357. /* AM=1 (address is updated in vertical writing direction) */
  358. ili9325_WriteReg(LCD_REG_3, 0x1010);
  359. /* Set Cursor */
  360. ili9325_SetCursor(Xpos, Ypos);
  361. /* Prepare to write GRAM */
  362. LCD_IO_WriteReg(LCD_REG_34);
  363. /* Fill a complete vertical line */
  364. for(counter = 0; counter < Length; counter++)
  365. {
  366. ArrayRGB[counter] = RGBCode;
  367. }
  368. /* Write 16-bit GRAM Reg */
  369. LCD_IO_WriteMultipleData((uint8_t*)&ArrayRGB[0], Length * 2);
  370. /* set GRAM write direction and BGR = 1 */
  371. /* I/D=00 (Horizontal : increment, Vertical : decrement) */
  372. /* AM=1 (address is updated in vertical writing direction) */
  373. ili9325_WriteReg(LCD_REG_3, 0x1018);
  374. }
  375. /**
  376. * @brief Displays a bitmap picture.
  377. * @param BmpAddress: Bmp picture address.
  378. * @param Xpos: Bmp X position in the LCD
  379. * @param Ypos: Bmp Y position in the LCD
  380. * @retval None
  381. */
  382. void ili9325_DrawBitmap(uint16_t Xpos, uint16_t Ypos, uint8_t *pbmp)
  383. {
  384. uint32_t index = 0, size = 0;
  385. /* Read bitmap size */
  386. size = *(volatile uint16_t *) (pbmp + 2);
  387. size |= (*(volatile uint16_t *) (pbmp + 4)) << 16;
  388. /* Get bitmap data address offset */
  389. index = *(volatile uint16_t *) (pbmp + 10);
  390. index |= (*(volatile uint16_t *) (pbmp + 12)) << 16;
  391. size = (size - index)/2;
  392. pbmp += index;
  393. /* Set GRAM write direction and BGR = 1 */
  394. /* I/D=00 (Horizontal : decrement, Vertical : decrement) */
  395. /* AM=1 (address is updated in vertical writing direction) */
  396. ili9325_WriteReg(LCD_REG_3, 0x1008);
  397. /* Set Cursor */
  398. ili9325_SetCursor(Xpos, Ypos);
  399. /* Prepare to write GRAM */
  400. LCD_IO_WriteReg(LCD_REG_34);
  401. LCD_IO_WriteMultipleData((uint8_t*)pbmp, size*2);
  402. /* Set GRAM write direction and BGR = 1 */
  403. /* I/D = 01 (Horizontal : increment, Vertical : decrement) */
  404. /* AM = 1 (address is updated in vertical writing direction) */
  405. ili9325_WriteReg(LCD_REG_3, 0x1018);
  406. }
  407. /**
  408. * @brief Displays picture.
  409. * @param pdata: picture address.
  410. * @param Xpos: Image X position in the LCD
  411. * @param Ypos: Image Y position in the LCD
  412. * @param Xsize: Image X size in the LCD
  413. * @param Ysize: Image Y size in the LCD
  414. * @retval None
  415. */
  416. void ili9325_DrawRGBImage(uint16_t Xpos, uint16_t Ypos, uint16_t Xsize, uint16_t Ysize, uint8_t *pdata)
  417. {
  418. uint32_t size = 0;
  419. size = (Xsize * Ysize);
  420. /* Set Cursor */
  421. ili9325_SetCursor(Xpos, Ypos);
  422. /* Prepare to write GRAM */
  423. LCD_IO_WriteReg(LCD_REG_34);
  424. LCD_IO_WriteMultipleData((uint8_t*)pdata, size*2);
  425. }
  426. /**
  427. * @}
  428. */
  429. /**
  430. * @}
  431. */
  432. /**
  433. * @}
  434. */
  435. /**
  436. * @}
  437. */
  438. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/