stm3210c_eval_lcd.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408
  1. /**
  2. ******************************************************************************
  3. * @file stm3210c_eval_lcd.c
  4. * @author MCD Application Team
  5. * @version V4.5.0
  6. * @date 07-March-2011
  7. * @brief This file includes the LCD driver for AM-240320L8TNQW00H (LCD_ILI9320)
  8. * Liquid Crystal Display Module of STM3210C-EVAL board.
  9. ******************************************************************************
  10. * @attention
  11. *
  12. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  13. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  14. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  15. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  16. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  17. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  18. *
  19. * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  20. ******************************************************************************
  21. */
  22. /* Includes ------------------------------------------------------------------*/
  23. #include "stm3210c_eval_lcd.h"
  24. #include "../Common/fonts.c"
  25. /** @addtogroup Utilities
  26. * @{
  27. */
  28. /** @addtogroup STM32_EVAL
  29. * @{
  30. */
  31. /** @addtogroup STM3210C_EVAL
  32. * @{
  33. */
  34. /** @defgroup STM3210C_EVAL_LCD
  35. * @brief This file includes the LCD driver for AM-240320L8TNQW00H (LCD_ILI9320)
  36. * Liquid Crystal Display Module of STM3210C-EVAL board.
  37. * @{
  38. */
  39. /** @defgroup STM3210C_EVAL_LCD_Private_TypesDefinitions
  40. * @{
  41. */
  42. /**
  43. * @}
  44. */
  45. /** @defgroup STM3210C_EVAL_LCD_Private_Defines
  46. * @{
  47. */
  48. #define START_BYTE 0x70
  49. #define SET_INDEX 0x00
  50. #define READ_STATUS 0x01
  51. #define LCD_WRITE_REG 0x02
  52. #define LCD_READ_REG 0x03
  53. #define MAX_POLY_CORNERS 200
  54. #define POLY_Y(Z) ((int32_t)((Points + Z)->X))
  55. #define POLY_X(Z) ((int32_t)((Points + Z)->Y))
  56. /**
  57. * @}
  58. */
  59. /** @defgroup STM3210C_EVAL_LCD_Private_Macros
  60. * @{
  61. */
  62. #define ABS(X) ((X) > 0 ? (X) : -(X))
  63. /**
  64. * @}
  65. */
  66. /** @defgroup STM3210C_EVAL_LCD_Private_Variables
  67. * @{
  68. */
  69. static sFONT *LCD_Currentfonts;
  70. /* Global variables to set the written text color */
  71. static __IO uint16_t TextColor = 0x0000, BackColor = 0xFFFF;
  72. /**
  73. * @}
  74. */
  75. /** @defgroup STM3210C_EVAL_LCD_Private_FunctionPrototypes
  76. * @{
  77. */
  78. #ifndef USE_Delay
  79. static void delay(__IO uint32_t nCount);
  80. #endif /* USE_Delay*/
  81. static void PutPixel(int16_t x, int16_t y);
  82. static void LCD_PolyLineRelativeClosed(pPoint Points, uint16_t PointCount, uint16_t Closed);
  83. /**
  84. * @}
  85. */
  86. /** @defgroup STM3210C_EVAL_LCD_Private_Functions
  87. * @{
  88. */
  89. /**
  90. * @brief DeInitializes the LCD.
  91. * @param None
  92. * @retval None
  93. */
  94. void LCD_DeInit(void)
  95. {
  96. GPIO_InitTypeDef GPIO_InitStructure;
  97. /*!< LCD Display Off */
  98. LCD_DisplayOff();
  99. /*!< LCD_SPI disable */
  100. SPI_Cmd(LCD_SPI, DISABLE);
  101. /*!< LCD_SPI DeInit */
  102. SPI_I2S_DeInit(LCD_SPI);
  103. /*!< Disable SPI clock */
  104. RCC_APB1PeriphClockCmd(LCD_SPI_CLK, DISABLE);
  105. GPIO_PinRemapConfig(GPIO_Remap_SPI3, DISABLE);
  106. /* Configure NCS in Output Push-Pull mode */
  107. GPIO_InitStructure.GPIO_Pin = LCD_NCS_PIN;
  108. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  109. GPIO_Init(LCD_NCS_GPIO_PORT, &GPIO_InitStructure);
  110. /* Configure SPI pins: SCK, MISO and MOSI */
  111. GPIO_InitStructure.GPIO_Pin = LCD_SPI_SCK_PIN;
  112. GPIO_Init(LCD_SPI_SCK_GPIO_PORT, &GPIO_InitStructure);
  113. GPIO_InitStructure.GPIO_Pin = LCD_SPI_MISO_PIN;
  114. GPIO_Init(LCD_SPI_MISO_GPIO_PORT, &GPIO_InitStructure);
  115. GPIO_InitStructure.GPIO_Pin = LCD_SPI_MOSI_PIN;
  116. GPIO_Init(LCD_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure);
  117. }
  118. /**
  119. * @brief Setups the LCD.
  120. * @param None
  121. * @retval None
  122. */
  123. void LCD_Setup(void)
  124. {
  125. /* Configure the LCD Control pins --------------------------------------------*/
  126. LCD_CtrlLinesConfig();
  127. /* Configure the LCD_SPI interface ----------------------------------------------*/
  128. LCD_SPIConfig();
  129. _delay_(5); /* Delay 50 ms */
  130. /* Start Initial Sequence ------------------------------------------------*/
  131. LCD_WriteReg(LCD_REG_229, 0x8000); /* Set the internal vcore voltage */
  132. LCD_WriteReg(LCD_REG_0, 0x0001); /* Start internal OSC. */
  133. LCD_WriteReg(LCD_REG_1, 0x0100); /* set SS and SM bit */
  134. LCD_WriteReg(LCD_REG_2, 0x0700); /* set 1 line inversion */
  135. LCD_WriteReg(LCD_REG_3, 0x1030); /* set GRAM write direction and BGR=1. */
  136. LCD_WriteReg(LCD_REG_4, 0x0000); /* Resize register */
  137. LCD_WriteReg(LCD_REG_8, 0x0202); /* set the back porch and front porch */
  138. LCD_WriteReg(LCD_REG_9, 0x0000); /* set non-display area refresh cycle ISC[3:0] */
  139. LCD_WriteReg(LCD_REG_10, 0x0000); /* FMARK function */
  140. LCD_WriteReg(LCD_REG_12, 0x0000); /* RGB interface setting */
  141. LCD_WriteReg(LCD_REG_13, 0x0000); /* Frame marker Position */
  142. LCD_WriteReg(LCD_REG_15, 0x0000); /* RGB interface polarity */
  143. /* Power On sequence -----------------------------------------------------*/
  144. LCD_WriteReg(LCD_REG_16, 0x0000); /* SAP, BT[3:0], AP, DSTB, SLP, STB */
  145. LCD_WriteReg(LCD_REG_17, 0x0000); /* DC1[2:0], DC0[2:0], VC[2:0] */
  146. LCD_WriteReg(LCD_REG_18, 0x0000); /* VREG1OUT voltage */
  147. LCD_WriteReg(LCD_REG_19, 0x0000); /* VDV[4:0] for VCOM amplitude */
  148. _delay_(20); /* Dis-charge capacitor power voltage (200ms) */
  149. LCD_WriteReg(LCD_REG_16, 0x17B0); /* SAP, BT[3:0], AP, DSTB, SLP, STB */
  150. LCD_WriteReg(LCD_REG_17, 0x0137); /* DC1[2:0], DC0[2:0], VC[2:0] */
  151. _delay_(5); /* Delay 50 ms */
  152. LCD_WriteReg(LCD_REG_18, 0x0139); /* VREG1OUT voltage */
  153. _delay_(5); /* Delay 50 ms */
  154. LCD_WriteReg(LCD_REG_19, 0x1d00); /* VDV[4:0] for VCOM amplitude */
  155. LCD_WriteReg(LCD_REG_41, 0x0013); /* VCM[4:0] for VCOMH */
  156. _delay_(5); /* Delay 50 ms */
  157. LCD_WriteReg(LCD_REG_32, 0x0000); /* GRAM horizontal Address */
  158. LCD_WriteReg(LCD_REG_33, 0x0000); /* GRAM Vertical Address */
  159. /* Adjust the Gamma Curve ------------------------------------------------*/
  160. LCD_WriteReg(LCD_REG_48, 0x0006);
  161. LCD_WriteReg(LCD_REG_49, 0x0101);
  162. LCD_WriteReg(LCD_REG_50, 0x0003);
  163. LCD_WriteReg(LCD_REG_53, 0x0106);
  164. LCD_WriteReg(LCD_REG_54, 0x0b02);
  165. LCD_WriteReg(LCD_REG_55, 0x0302);
  166. LCD_WriteReg(LCD_REG_56, 0x0707);
  167. LCD_WriteReg(LCD_REG_57, 0x0007);
  168. LCD_WriteReg(LCD_REG_60, 0x0600);
  169. LCD_WriteReg(LCD_REG_61, 0x020b);
  170. /* Set GRAM area ---------------------------------------------------------*/
  171. LCD_WriteReg(LCD_REG_80, 0x0000); /* Horizontal GRAM Start Address */
  172. LCD_WriteReg(LCD_REG_81, 0x00EF); /* Horizontal GRAM End Address */
  173. LCD_WriteReg(LCD_REG_82, 0x0000); /* Vertical GRAM Start Address */
  174. LCD_WriteReg(LCD_REG_83, 0x013F); /* Vertical GRAM End Address */
  175. LCD_WriteReg(LCD_REG_96, 0x2700); /* Gate Scan Line */
  176. LCD_WriteReg(LCD_REG_97, 0x0001); /* NDL,VLE, REV */
  177. LCD_WriteReg(LCD_REG_106, 0x0000); /* set scrolling line */
  178. /* Partial Display Control -----------------------------------------------*/
  179. LCD_WriteReg(LCD_REG_128, 0x0000);
  180. LCD_WriteReg(LCD_REG_129, 0x0000);
  181. LCD_WriteReg(LCD_REG_130, 0x0000);
  182. LCD_WriteReg(LCD_REG_131, 0x0000);
  183. LCD_WriteReg(LCD_REG_132, 0x0000);
  184. LCD_WriteReg(LCD_REG_133, 0x0000);
  185. /* Panel Control ---------------------------------------------------------*/
  186. LCD_WriteReg(LCD_REG_144, 0x0010);
  187. LCD_WriteReg(LCD_REG_146, 0x0000);
  188. LCD_WriteReg(LCD_REG_147, 0x0003);
  189. LCD_WriteReg(LCD_REG_149, 0x0110);
  190. LCD_WriteReg(LCD_REG_151, 0x0000);
  191. LCD_WriteReg(LCD_REG_152, 0x0000);
  192. /* Set GRAM write direction and BGR = 1 */
  193. /* I/D=01 (Horizontal : increment, Vertical : decrement) */
  194. /* AM=1 (address is updated in vertical writing direction) */
  195. LCD_WriteReg(LCD_REG_3, 0x1018);
  196. LCD_WriteReg(LCD_REG_7, 0x0173); /* 262K color and display ON */
  197. }
  198. /**
  199. * @brief Initializes the LCD.
  200. * @param None
  201. * @retval None
  202. */
  203. void STM3210C_LCD_Init(void)
  204. {
  205. /* Setups the LCD */
  206. LCD_Setup();
  207. LCD_SetFont(&LCD_DEFAULT_FONT);
  208. }
  209. /**
  210. * @brief Sets the LCD Text and Background colors.
  211. * @param _TextColor: specifies the Text Color.
  212. * @param _BackColor: specifies the Background Color.
  213. * @retval None
  214. */
  215. void LCD_SetColors(__IO uint16_t _TextColor, __IO uint16_t _BackColor)
  216. {
  217. TextColor = _TextColor;
  218. BackColor = _BackColor;
  219. }
  220. /**
  221. * @brief Gets the LCD Text and Background colors.
  222. * @param _TextColor: pointer to the variable that will contain the Text
  223. Color.
  224. * @param _BackColor: pointer to the variable that will contain the Background
  225. Color.
  226. * @retval None
  227. */
  228. void LCD_GetColors(__IO uint16_t *_TextColor, __IO uint16_t *_BackColor)
  229. {
  230. *_TextColor = TextColor; *_BackColor = BackColor;
  231. }
  232. /**
  233. * @brief Sets the Text color.
  234. * @param Color: specifies the Text color code RGB(5-6-5).
  235. * @retval None
  236. */
  237. void LCD_SetTextColor(__IO uint16_t Color)
  238. {
  239. TextColor = Color;
  240. }
  241. /**
  242. * @brief Sets the Background color.
  243. * @param Color: specifies the Background color code RGB(5-6-5).
  244. * @retval None
  245. */
  246. void LCD_SetBackColor(__IO uint16_t Color)
  247. {
  248. BackColor = Color;
  249. }
  250. /**
  251. * @brief Sets the Text Font.
  252. * @param fonts: specifies the font to be used.
  253. * @retval None
  254. */
  255. void LCD_SetFont(sFONT *fonts)
  256. {
  257. LCD_Currentfonts = fonts;
  258. }
  259. /**
  260. * @brief Gets the Text Font.
  261. * @param None.
  262. * @retval the used font.
  263. */
  264. sFONT *LCD_GetFont(void)
  265. {
  266. return LCD_Currentfonts;
  267. }
  268. /**
  269. * @brief Clears the selected line.
  270. * @param Line: the Line to be cleared.
  271. * This parameter can be one of the following values:
  272. * @arg Linex: where x can be 0..n
  273. * @retval None
  274. */
  275. void LCD_ClearLine(uint8_t Line)
  276. {
  277. uint16_t refcolumn = LCD_PIXEL_WIDTH - 1;
  278. /* Send the string character by character on lCD */
  279. while (((refcolumn + 1)& 0xFFFF) >= LCD_Currentfonts->Width)
  280. {
  281. /* Display one character on LCD */
  282. LCD_DisplayChar(Line, refcolumn, ' ');
  283. /* Decrement the column position by 16 */
  284. refcolumn -= LCD_Currentfonts->Width;
  285. }
  286. }
  287. /**
  288. * @brief Clears the hole LCD.
  289. * @param Color: the color of the background.
  290. * @retval None
  291. */
  292. void LCD_Clear(uint16_t Color)
  293. {
  294. uint32_t index = 0;
  295. LCD_SetCursor(0x00, 0x013F);
  296. LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */
  297. for(index = 0; index < 76800; index++)
  298. {
  299. LCD_WriteRAM(Color);
  300. }
  301. LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_SET);
  302. }
  303. /**
  304. * @brief Sets the cursor position.
  305. * @param Xpos: specifies the X position.
  306. * @param Ypos: specifies the Y position.
  307. * @retval None
  308. */
  309. void LCD_SetCursor(uint8_t Xpos, uint16_t Ypos)
  310. {
  311. LCD_WriteReg(LCD_REG_32, Xpos);
  312. LCD_WriteReg(LCD_REG_33, Ypos);
  313. }
  314. /**
  315. * @brief Draws a character on LCD.
  316. * @param Xpos: the Line where to display the character shape.
  317. * @param Ypos: start column address.
  318. * @param c: pointer to the character data.
  319. * @retval None
  320. */
  321. void LCD_DrawChar(uint8_t Xpos, uint16_t Ypos, const uint16_t *c)
  322. {
  323. uint32_t index = 0, i = 0;
  324. uint8_t Xaddress = 0;
  325. Xaddress = Xpos;
  326. LCD_SetCursor(Xaddress, Ypos);
  327. for(index = 0; index < LCD_Currentfonts->Height; index++)
  328. {
  329. LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */
  330. for(i = 0; i < LCD_Currentfonts->Width; i++)
  331. {
  332. if((((c[index] & ((0x80 << ((LCD_Currentfonts->Width / 12 ) * 8 ) ) >> i)) == 0x00) &&(LCD_Currentfonts->Width <= 12))||
  333. (((c[index] & (0x1 << i)) == 0x00)&&(LCD_Currentfonts->Width > 12 )))
  334. {
  335. LCD_WriteRAM(BackColor);
  336. }
  337. else
  338. {
  339. LCD_WriteRAM(TextColor);
  340. }
  341. }
  342. LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_SET);
  343. Xaddress++;
  344. LCD_SetCursor(Xaddress, Ypos);
  345. }
  346. }
  347. /**
  348. * @brief Displays one character (16dots width, 24dots height).
  349. * @param Line: the Line where to display the character shape .
  350. * This parameter can be one of the following values:
  351. * @arg Linex: where x can be 0..9
  352. * @param Column: start column address.
  353. * @param Ascii: character ascii code, must be between 0x20 and 0x7E.
  354. * @retval None
  355. */
  356. void LCD_DisplayChar(uint8_t Line, uint16_t Column, uint8_t Ascii)
  357. {
  358. Ascii -= 32;
  359. LCD_DrawChar(Line, Column, &LCD_Currentfonts->table[Ascii * LCD_Currentfonts->Height]);
  360. }
  361. /**
  362. * @brief Displays a maximum of 20 char on the LCD.
  363. * @param Line: the Line where to display the character shape .
  364. * This parameter can be one of the following values:
  365. * @arg Linex: where x can be 0..9
  366. * @param *ptr: pointer to string to display on LCD.
  367. * @retval None
  368. */
  369. void LCD_DisplayStringLine(uint8_t Line, uint8_t *ptr)
  370. {
  371. uint16_t refcolumn = LCD_PIXEL_WIDTH - 1;
  372. /* Send the string character by character on lCD */
  373. while ((*ptr != 0) & (((refcolumn + 1) & 0xFFFF) >= LCD_Currentfonts->Width))
  374. {
  375. /* Display one character on LCD */
  376. LCD_DisplayChar(Line, refcolumn, *ptr);
  377. /* Decrement the column position by 16 */
  378. refcolumn -= LCD_Currentfonts->Width;
  379. /* Point on the next character */
  380. ptr++;
  381. }
  382. }
  383. /**
  384. * @brief Sets a display window
  385. * @param Xpos: specifies the X buttom left position.
  386. * @param Ypos: specifies the Y buttom left position.
  387. * @param Height: display window height.
  388. * @param Width: display window width.
  389. * @retval None
  390. */
  391. void LCD_SetDisplayWindow(uint8_t Xpos, uint16_t Ypos, uint8_t Height, uint16_t Width)
  392. {
  393. /* Horizontal GRAM Start Address */
  394. if(Xpos >= Height)
  395. {
  396. LCD_WriteReg(LCD_REG_80, (Xpos - Height + 1));
  397. }
  398. else
  399. {
  400. LCD_WriteReg(LCD_REG_80, 0);
  401. }
  402. /* Horizontal GRAM End Address */
  403. LCD_WriteReg(LCD_REG_81, Xpos);
  404. /* Vertical GRAM Start Address */
  405. if(Ypos >= Width)
  406. {
  407. LCD_WriteReg(LCD_REG_82, (Ypos - Width + 1));
  408. }
  409. else
  410. {
  411. LCD_WriteReg(LCD_REG_82, 0);
  412. }
  413. /* Vertical GRAM End Address */
  414. LCD_WriteReg(LCD_REG_83, Ypos);
  415. LCD_SetCursor(Xpos, Ypos);
  416. }
  417. /**
  418. * @brief Disables LCD Window mode.
  419. * @param None
  420. * @retval None
  421. */
  422. void LCD_WindowModeDisable(void)
  423. {
  424. LCD_SetDisplayWindow(239, 0x13F, 240, 320);
  425. LCD_WriteReg(LCD_REG_3, 0x1018);
  426. }
  427. /**
  428. * @brief Displays a line.
  429. * @param Xpos: specifies the X position.
  430. * @param Ypos: specifies the Y position.
  431. * @param Length: line length.
  432. * @param Direction: line direction.
  433. * This parameter can be one of the following values: Vertical or Horizontal.
  434. * @retval None
  435. */
  436. void LCD_DrawLine(uint8_t Xpos, uint16_t Ypos, uint16_t Length, uint8_t Direction)
  437. {
  438. uint32_t i = 0;
  439. LCD_SetCursor(Xpos, Ypos);
  440. if(Direction == LCD_DIR_HORIZONTAL)
  441. {
  442. LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */
  443. for(i = 0; i < Length; i++)
  444. {
  445. LCD_WriteRAM(TextColor);
  446. }
  447. LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_SET);
  448. }
  449. else
  450. {
  451. for(i = 0; i < Length; i++)
  452. {
  453. LCD_WriteRAMWord(TextColor);
  454. Xpos++;
  455. LCD_SetCursor(Xpos, Ypos);
  456. }
  457. }
  458. }
  459. /**
  460. * @brief Displays a rectangle.
  461. * @param Xpos: specifies the X position.
  462. * @param Ypos: specifies the Y position.
  463. * @param Height: display rectangle height.
  464. * @param Width: display rectangle width.
  465. * @retval None
  466. */
  467. void LCD_DrawRect(uint8_t Xpos, uint16_t Ypos, uint8_t Height, uint16_t Width)
  468. {
  469. LCD_DrawLine(Xpos, Ypos, Width, LCD_DIR_HORIZONTAL);
  470. LCD_DrawLine((Xpos + Height), Ypos, Width, LCD_DIR_HORIZONTAL);
  471. LCD_DrawLine(Xpos, Ypos, Height, LCD_DIR_VERTICAL);
  472. LCD_DrawLine(Xpos, (Ypos - Width + 1), Height, LCD_DIR_VERTICAL);
  473. }
  474. /**
  475. * @brief Displays a circle.
  476. * @param Xpos: specifies the X position.
  477. * @param Ypos: specifies the Y position.
  478. * @param Radius
  479. * @retval None
  480. */
  481. void LCD_DrawCircle(uint8_t Xpos, uint16_t Ypos, uint16_t Radius)
  482. {
  483. s32 D;/* Decision Variable */
  484. uint32_t CurX;/* Current X Value */
  485. uint32_t CurY;/* Current Y Value */
  486. D = 3 - (Radius << 1);
  487. CurX = 0;
  488. CurY = Radius;
  489. while (CurX <= CurY)
  490. {
  491. LCD_SetCursor(Xpos + CurX, Ypos + CurY);
  492. LCD_WriteRAMWord(TextColor);
  493. LCD_SetCursor(Xpos + CurX, Ypos - CurY);
  494. LCD_WriteRAMWord(TextColor);
  495. LCD_SetCursor(Xpos - CurX, Ypos + CurY);
  496. LCD_WriteRAMWord(TextColor);
  497. LCD_SetCursor(Xpos - CurX, Ypos - CurY);
  498. LCD_WriteRAMWord(TextColor);
  499. LCD_SetCursor(Xpos + CurY, Ypos + CurX);
  500. LCD_WriteRAMWord(TextColor);
  501. LCD_SetCursor(Xpos + CurY, Ypos - CurX);
  502. LCD_WriteRAMWord(TextColor);
  503. LCD_SetCursor(Xpos - CurY, Ypos + CurX);
  504. LCD_WriteRAMWord(TextColor);
  505. LCD_SetCursor(Xpos - CurY, Ypos - CurX);
  506. LCD_WriteRAMWord(TextColor);
  507. if (D < 0)
  508. {
  509. D += (CurX << 2) + 6;
  510. }
  511. else
  512. {
  513. D += ((CurX - CurY) << 2) + 10;
  514. CurY--;
  515. }
  516. CurX++;
  517. }
  518. }
  519. /**
  520. * @brief Displays a monocolor picture.
  521. * @param Pict: pointer to the picture array.
  522. * @retval None
  523. */
  524. void LCD_DrawMonoPict(const uint32_t *Pict)
  525. {
  526. uint32_t index = 0, i = 0;
  527. LCD_SetCursor(0, (LCD_PIXEL_WIDTH - 1));
  528. LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */
  529. for(index = 0; index < 2400; index++)
  530. {
  531. for(i = 0; i < 32; i++)
  532. {
  533. if((Pict[index] & (1 << i)) == 0x00)
  534. {
  535. LCD_WriteRAM(BackColor);
  536. }
  537. else
  538. {
  539. LCD_WriteRAM(TextColor);
  540. }
  541. }
  542. }
  543. LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_SET);
  544. }
  545. #ifdef USE_LCD_DrawBMP
  546. /**
  547. * @brief Displays a bitmap picture loaded in the SPI Flash.
  548. * @param BmpAddress: Bmp picture address in the SPI Flash.
  549. * @retval None
  550. */
  551. //void LCD_DrawBMP(uint32_t BmpAddress)
  552. //{
  553. // uint32_t i = 0, size = 0;
  554. //
  555. // /* Read bitmap size */
  556. // sFLASH_ReadBuffer((uint8_t*)&size, BmpAddress + 2, 4);
  557. //
  558. // /* get bitmap data address offset */
  559. // sFLASH_ReadBuffer((uint8_t*)&i, BmpAddress + 10, 4);
  560. //
  561. // size = (size - i)/2;
  562. //
  563. // sFLASH_StartReadSequence(BmpAddress + i);
  564. //
  565. // /* Disable LCD_SPI */
  566. // SPI_Cmd(LCD_SPI, DISABLE);
  567. // /* SPI in 16-bit mode */
  568. // SPI_DataSizeConfig(LCD_SPI, SPI_DataSize_16b);
  569. //
  570. // /* Enable LCD_SPI */
  571. // SPI_Cmd(LCD_SPI, ENABLE);
  572. //
  573. // /* Set GRAM write direction and BGR = 1 */
  574. // /* I/D=00 (Horizontal : decrement, Vertical : decrement) */
  575. // /* AM=1 (address is updated in vertical writing direction) */
  576. // LCD_WriteReg(LCD_REG_3, 0x1008);
  577. //
  578. // LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */
  579. //
  580. // /* Read bitmap data from SPI Flash and send them to LCD */
  581. // for(i = 0; i < size; i++)
  582. // {
  583. // LCD_WriteRAM(__REV_HalfWord(sFLASH_SendHalfWord(0xA5A5)));
  584. // }
  585. //
  586. // LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_SET);
  587. //
  588. // /* Deselect the FLASH: Chip Select high */
  589. // sFLASH_CS_HIGH();
  590. //
  591. // /* Disable LCD_SPI */
  592. // SPI_Cmd(LCD_SPI, DISABLE);
  593. // /* SPI in 8-bit mode */
  594. // SPI_DataSizeConfig(LCD_SPI, SPI_DataSize_8b);
  595. //
  596. // /* Enable LCD_SPI */
  597. // SPI_Cmd(LCD_SPI, ENABLE);
  598. //
  599. // /* Set GRAM write direction and BGR = 1 */
  600. // /* I/D = 01 (Horizontal : increment, Vertical : decrement) */
  601. // /* AM = 1 (address is updated in vertical writing direction) */
  602. // LCD_WriteReg(LCD_REG_3, 0x1018);
  603. //}
  604. /**
  605. * @brief Displays a bitmap picture loaded in the Internal FLASH.
  606. * @param BmpAddress: Bmp picture address in the Internal FLASH.
  607. * @retval None
  608. */
  609. void LCD_DrawBMP(const uint16_t *BmpAddress)
  610. {
  611. uint32_t i = 0, size = 0;
  612. /* Read bitmap size */
  613. size = BmpAddress[1] | (BmpAddress[2] << 16);
  614. /* get bitmap data address offset */
  615. i = BmpAddress[5] | (BmpAddress[6] << 16);
  616. size = (size - i)/2;
  617. BmpAddress += i/2;
  618. /* Set GRAM write direction and BGR = 1 */
  619. /* I/D=00 (Horizontal : decrement, Vertical : decrement) */
  620. /* AM=1 (address is updated in vertical writing direction) */
  621. LCD_WriteReg(LCD_REG_3, 0x1008);
  622. LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */
  623. /* Read bitmap data from SPI Flash and send them to LCD */
  624. for(i = 0; i < size; i++)
  625. {
  626. LCD_WriteRAM(BmpAddress[i]);
  627. }
  628. LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_SET);
  629. /* Set GRAM write direction and BGR = 1 */
  630. /* I/D = 01 (Horizontal : increment, Vertical : decrement) */
  631. /* AM = 1 (address is updated in vertical writing direction) */
  632. LCD_WriteReg(LCD_REG_3, 0x1018);
  633. }
  634. #endif
  635. /**
  636. * @brief Displays a full rectangle.
  637. * @param Xpos: specifies the X position.
  638. * @param Ypos: specifies the Y position.
  639. * @param Height: rectangle height.
  640. * @param Width: rectangle width.
  641. * @retval None
  642. */
  643. void LCD_DrawFullRect(uint16_t Xpos, uint16_t Ypos, uint16_t Width, uint16_t Height)
  644. {
  645. LCD_SetTextColor(TextColor);
  646. LCD_DrawLine(Xpos, Ypos, Width, LCD_DIR_HORIZONTAL);
  647. LCD_DrawLine((Xpos + Height), Ypos, Width, LCD_DIR_HORIZONTAL);
  648. LCD_DrawLine(Xpos, Ypos, Height, LCD_DIR_VERTICAL);
  649. LCD_DrawLine(Xpos, (Ypos - Width + 1), Height, LCD_DIR_VERTICAL);
  650. Width -= 2;
  651. Height--;
  652. Ypos--;
  653. LCD_SetTextColor(BackColor);
  654. while(Height--)
  655. {
  656. LCD_DrawLine(++Xpos, Ypos, Width, LCD_DIR_HORIZONTAL);
  657. }
  658. LCD_SetTextColor(TextColor);
  659. }
  660. /**
  661. * @brief Displays a full circle.
  662. * @param Xpos: specifies the X position.
  663. * @param Ypos: specifies the Y position.
  664. * @param Radius
  665. * @retval None
  666. */
  667. void LCD_DrawFullCircle(uint16_t Xpos, uint16_t Ypos, uint16_t Radius)
  668. {
  669. int32_t D; /* Decision Variable */
  670. uint32_t CurX;/* Current X Value */
  671. uint32_t CurY;/* Current Y Value */
  672. D = 3 - (Radius << 1);
  673. CurX = 0;
  674. CurY = Radius;
  675. LCD_SetTextColor(BackColor);
  676. while (CurX <= CurY)
  677. {
  678. if(CurY > 0)
  679. {
  680. LCD_DrawLine(Xpos - CurX, Ypos + CurY, 2*CurY, LCD_DIR_HORIZONTAL);
  681. LCD_DrawLine(Xpos + CurX, Ypos + CurY, 2*CurY, LCD_DIR_HORIZONTAL);
  682. }
  683. if(CurX > 0)
  684. {
  685. LCD_DrawLine(Xpos - CurY, Ypos + CurX, 2*CurX, LCD_DIR_HORIZONTAL);
  686. LCD_DrawLine(Xpos + CurY, Ypos + CurX, 2*CurX, LCD_DIR_HORIZONTAL);
  687. }
  688. if (D < 0)
  689. {
  690. D += (CurX << 2) + 6;
  691. }
  692. else
  693. {
  694. D += ((CurX - CurY) << 2) + 10;
  695. CurY--;
  696. }
  697. CurX++;
  698. }
  699. LCD_SetTextColor(TextColor);
  700. LCD_DrawCircle(Xpos, Ypos, Radius);
  701. }
  702. /**
  703. * @brief Displays an uni line (between two points).
  704. * @param x1: specifies the point 1 x position.
  705. * @param y1: specifies the point 1 y position.
  706. * @param x2: specifies the point 2 x position.
  707. * @param y2: specifies the point 2 y position.
  708. * @retval None
  709. */
  710. void LCD_DrawUniLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
  711. {
  712. int16_t deltax = 0, deltay = 0, x = 0, y = 0, xinc1 = 0, xinc2 = 0,
  713. yinc1 = 0, yinc2 = 0, den = 0, num = 0, numadd = 0, numpixels = 0,
  714. curpixel = 0;
  715. deltax = ABS(x2 - x1); /* The difference between the x's */
  716. deltay = ABS(y2 - y1); /* The difference between the y's */
  717. x = x1; /* Start x off at the first pixel */
  718. y = y1; /* Start y off at the first pixel */
  719. if (x2 >= x1) /* The x-values are increasing */
  720. {
  721. xinc1 = 1;
  722. xinc2 = 1;
  723. }
  724. else /* The x-values are decreasing */
  725. {
  726. xinc1 = -1;
  727. xinc2 = -1;
  728. }
  729. if (y2 >= y1) /* The y-values are increasing */
  730. {
  731. yinc1 = 1;
  732. yinc2 = 1;
  733. }
  734. else /* The y-values are decreasing */
  735. {
  736. yinc1 = -1;
  737. yinc2 = -1;
  738. }
  739. if (deltax >= deltay) /* There is at least one x-value for every y-value */
  740. {
  741. xinc1 = 0; /* Don't change the x when numerator >= denominator */
  742. yinc2 = 0; /* Don't change the y for every iteration */
  743. den = deltax;
  744. num = deltax / 2;
  745. numadd = deltay;
  746. numpixels = deltax; /* There are more x-values than y-values */
  747. }
  748. else /* There is at least one y-value for every x-value */
  749. {
  750. xinc2 = 0; /* Don't change the x for every iteration */
  751. yinc1 = 0; /* Don't change the y when numerator >= denominator */
  752. den = deltay;
  753. num = deltay / 2;
  754. numadd = deltax;
  755. numpixels = deltay; /* There are more y-values than x-values */
  756. }
  757. for (curpixel = 0; curpixel <= numpixels; curpixel++)
  758. {
  759. PutPixel(x, y); /* Draw the current pixel */
  760. num += numadd; /* Increase the numerator by the top of the fraction */
  761. if (num >= den) /* Check if numerator >= denominator */
  762. {
  763. num -= den; /* Calculate the new numerator value */
  764. x += xinc1; /* Change the x as appropriate */
  765. y += yinc1; /* Change the y as appropriate */
  766. }
  767. x += xinc2; /* Change the x as appropriate */
  768. y += yinc2; /* Change the y as appropriate */
  769. }
  770. }
  771. /**
  772. * @brief Displays an polyline (between many points).
  773. * @param Points: pointer to the points array.
  774. * @param PointCount: Number of points.
  775. * @retval None
  776. */
  777. void LCD_PolyLine(pPoint Points, uint16_t PointCount)
  778. {
  779. int16_t X = 0, Y = 0;
  780. if(PointCount < 2)
  781. {
  782. return;
  783. }
  784. while(--PointCount)
  785. {
  786. X = Points->X;
  787. Y = Points->Y;
  788. Points++;
  789. LCD_DrawUniLine(X, Y, Points->X, Points->Y);
  790. }
  791. }
  792. /**
  793. * @brief Displays an relative polyline (between many points).
  794. * @param Points: pointer to the points array.
  795. * @param PointCount: Number of points.
  796. * @param Closed: specifies if the draw is closed or not.
  797. * 1: closed, 0 : not closed.
  798. * @retval None
  799. */
  800. static void LCD_PolyLineRelativeClosed(pPoint Points, uint16_t PointCount, uint16_t Closed)
  801. {
  802. int16_t X = 0, Y = 0;
  803. pPoint First = Points;
  804. if(PointCount < 2)
  805. {
  806. return;
  807. }
  808. X = Points->X;
  809. Y = Points->Y;
  810. while(--PointCount)
  811. {
  812. Points++;
  813. LCD_DrawUniLine(X, Y, X + Points->X, Y + Points->Y);
  814. X = X + Points->X;
  815. Y = Y + Points->Y;
  816. }
  817. if(Closed)
  818. {
  819. LCD_DrawUniLine(First->X, First->Y, X, Y);
  820. }
  821. }
  822. /**
  823. * @brief Displays a closed polyline (between many points).
  824. * @param Points: pointer to the points array.
  825. * @param PointCount: Number of points.
  826. * @retval None
  827. */
  828. void LCD_ClosedPolyLine(pPoint Points, uint16_t PointCount)
  829. {
  830. LCD_PolyLine(Points, PointCount);
  831. LCD_DrawUniLine(Points->X, Points->Y, (Points+PointCount-1)->X, (Points+PointCount-1)->Y);
  832. }
  833. /**
  834. * @brief Displays a relative polyline (between many points).
  835. * @param Points: pointer to the points array.
  836. * @param PointCount: Number of points.
  837. * @retval None
  838. */
  839. void LCD_PolyLineRelative(pPoint Points, uint16_t PointCount)
  840. {
  841. LCD_PolyLineRelativeClosed(Points, PointCount, 0);
  842. }
  843. /**
  844. * @brief Displays a closed relative polyline (between many points).
  845. * @param Points: pointer to the points array.
  846. * @param PointCount: Number of points.
  847. * @retval None
  848. */
  849. void LCD_ClosedPolyLineRelative(pPoint Points, uint16_t PointCount)
  850. {
  851. LCD_PolyLineRelativeClosed(Points, PointCount, 1);
  852. }
  853. /**
  854. * @brief Displays a full polyline (between many points).
  855. * @param Points: pointer to the points array.
  856. * @param PointCount: Number of points.
  857. * @retval None
  858. */
  859. void LCD_FillPolyLine(pPoint Points, uint16_t PointCount)
  860. {
  861. /* public-domain code by Darel Rex Finley, 2007 */
  862. uint16_t nodes = 0, nodeX[MAX_POLY_CORNERS], pixelX = 0, pixelY = 0, i = 0,
  863. j = 0, swap = 0;
  864. uint16_t IMAGE_LEFT = 0, IMAGE_RIGHT = 0, IMAGE_TOP = 0, IMAGE_BOTTOM = 0;
  865. IMAGE_LEFT = IMAGE_RIGHT = Points->X;
  866. IMAGE_TOP= IMAGE_BOTTOM = Points->Y;
  867. for(i = 1; i < PointCount; i++)
  868. {
  869. pixelX = POLY_X(i);
  870. if(pixelX < IMAGE_LEFT)
  871. {
  872. IMAGE_LEFT = pixelX;
  873. }
  874. if(pixelX > IMAGE_RIGHT)
  875. {
  876. IMAGE_RIGHT = pixelX;
  877. }
  878. pixelY = POLY_Y(i);
  879. if(pixelY < IMAGE_TOP)
  880. {
  881. IMAGE_TOP = pixelY;
  882. }
  883. if(pixelY > IMAGE_BOTTOM)
  884. {
  885. IMAGE_BOTTOM = pixelY;
  886. }
  887. }
  888. LCD_SetTextColor(BackColor);
  889. /* Loop through the rows of the image. */
  890. for (pixelY = IMAGE_TOP; pixelY < IMAGE_BOTTOM; pixelY++)
  891. {
  892. /* Build a list of nodes. */
  893. nodes = 0; j = PointCount-1;
  894. for (i = 0; i < PointCount; i++)
  895. {
  896. if (POLY_Y(i)<(double) pixelY && POLY_Y(j)>=(double) pixelY || POLY_Y(j)<(double) pixelY && POLY_Y(i)>=(double) pixelY)
  897. {
  898. nodeX[nodes++]=(int) (POLY_X(i)+((pixelY-POLY_Y(i))*(POLY_X(j)-POLY_X(i)))/(POLY_Y(j)-POLY_Y(i)));
  899. }
  900. j = i;
  901. }
  902. /* Sort the nodes, via a simple "Bubble" sort. */
  903. i = 0;
  904. while (i < nodes-1)
  905. {
  906. if (nodeX[i]>nodeX[i+1])
  907. {
  908. swap = nodeX[i];
  909. nodeX[i] = nodeX[i+1];
  910. nodeX[i+1] = swap;
  911. if(i)
  912. {
  913. i--;
  914. }
  915. }
  916. else
  917. {
  918. i++;
  919. }
  920. }
  921. /* Fill the pixels between node pairs. */
  922. for (i = 0; i < nodes; i+=2)
  923. {
  924. if(nodeX[i] >= IMAGE_RIGHT)
  925. {
  926. break;
  927. }
  928. if(nodeX[i+1] > IMAGE_LEFT)
  929. {
  930. if (nodeX[i] < IMAGE_LEFT)
  931. {
  932. nodeX[i]=IMAGE_LEFT;
  933. }
  934. if(nodeX[i+1] > IMAGE_RIGHT)
  935. {
  936. nodeX[i+1] = IMAGE_RIGHT;
  937. }
  938. LCD_SetTextColor(BackColor);
  939. LCD_DrawLine(pixelY, nodeX[i+1], nodeX[i+1] - nodeX[i], LCD_DIR_HORIZONTAL);
  940. LCD_SetTextColor(TextColor);
  941. PutPixel(pixelY, nodeX[i+1]);
  942. PutPixel(pixelY, nodeX[i]);
  943. /* for (j=nodeX[i]; j<nodeX[i+1]; j++) PutPixel(j,pixelY); */
  944. }
  945. }
  946. }
  947. /* draw the edges */
  948. LCD_SetTextColor(TextColor);
  949. }
  950. /**
  951. * @brief Reset LCD control line(/CS) and Send Start-Byte
  952. * @param Start_Byte: the Start-Byte to be sent
  953. * @retval None
  954. */
  955. void LCD_nCS_StartByte(uint8_t Start_Byte)
  956. {
  957. LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_RESET);
  958. SPI_I2S_SendData(LCD_SPI, Start_Byte);
  959. while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_BSY) != RESET)
  960. {
  961. }
  962. }
  963. /**
  964. * @brief Writes index to select the LCD register.
  965. * @param LCD_Reg: address of the selected register.
  966. * @retval None
  967. */
  968. void LCD_WriteRegIndex(uint8_t LCD_Reg)
  969. {
  970. /* Reset LCD control line(/CS) and Send Start-Byte */
  971. LCD_nCS_StartByte(START_BYTE | SET_INDEX);
  972. /* Write 16-bit Reg Index (High Byte is 0) */
  973. SPI_I2S_SendData(LCD_SPI, 0x00);
  974. while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_BSY) != RESET)
  975. {
  976. }
  977. SPI_I2S_SendData(LCD_SPI, LCD_Reg);
  978. while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_BSY) != RESET)
  979. {
  980. }
  981. LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_SET);
  982. }
  983. /**
  984. * @brief Reads the selected LCD Register.
  985. * @param None
  986. * @retval LCD Register Value.
  987. */
  988. uint16_t LCD_ReadReg(uint8_t LCD_Reg)
  989. {
  990. uint16_t tmp = 0;
  991. uint8_t i = 0;
  992. /* LCD_SPI prescaler: 4 */
  993. LCD_SPI->CR1 &= 0xFFC7;
  994. LCD_SPI->CR1 |= 0x0008;
  995. /* Write 16-bit Index (then Read Reg) */
  996. LCD_WriteRegIndex(LCD_Reg);
  997. /* Read 16-bit Reg */
  998. /* Reset LCD control line(/CS) and Send Start-Byte */
  999. LCD_nCS_StartByte(START_BYTE | LCD_READ_REG);
  1000. for(i = 0; i < 5; i++)
  1001. {
  1002. SPI_I2S_SendData(LCD_SPI, 0xFF);
  1003. while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_BSY) != RESET)
  1004. {
  1005. }
  1006. /* One byte of invalid dummy data read after the start byte */
  1007. while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_RXNE) == RESET)
  1008. {
  1009. }
  1010. SPI_I2S_ReceiveData(LCD_SPI);
  1011. }
  1012. SPI_I2S_SendData(LCD_SPI, 0xFF);
  1013. /* Read upper byte */
  1014. while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_BSY) != RESET)
  1015. {
  1016. }
  1017. /* Read lower byte */
  1018. while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_RXNE) == RESET)
  1019. {
  1020. }
  1021. tmp = SPI_I2S_ReceiveData(LCD_SPI);
  1022. SPI_I2S_SendData(LCD_SPI, 0xFF);
  1023. while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_BSY) != RESET)
  1024. {
  1025. }
  1026. /* Read lower byte */
  1027. while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_RXNE) == RESET)
  1028. {
  1029. }
  1030. tmp = ((tmp & 0xFF) << 8) | SPI_I2S_ReceiveData(LCD_SPI);
  1031. LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_SET);
  1032. /* LCD_SPI prescaler: 2 */
  1033. LCD_SPI->CR1 &= 0xFFC7;
  1034. return tmp;
  1035. }
  1036. /**
  1037. * @brief Prepare to write to the LCD RAM.
  1038. * @param None
  1039. * @retval None
  1040. */
  1041. void LCD_WriteRAM_Prepare(void)
  1042. {
  1043. LCD_WriteRegIndex(LCD_REG_34); /* Select GRAM Reg */
  1044. /* Reset LCD control line(/CS) and Send Start-Byte */
  1045. LCD_nCS_StartByte(START_BYTE | LCD_WRITE_REG);
  1046. }
  1047. /**
  1048. * @brief Writes 1 word to the LCD RAM.
  1049. * @param RGB_Code: the pixel color in RGB mode (5-6-5).
  1050. * @retval None
  1051. */
  1052. void LCD_WriteRAMWord(uint16_t RGB_Code)
  1053. {
  1054. LCD_WriteRAM_Prepare();
  1055. LCD_WriteRAM(RGB_Code);
  1056. LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_SET);
  1057. }
  1058. /**
  1059. * @brief Writes to the selected LCD register.
  1060. * @param LCD_Reg: address of the selected register.
  1061. * @param LCD_RegValue: value to write to the selected register.
  1062. * @retval None
  1063. */
  1064. void LCD_WriteReg(uint8_t LCD_Reg, uint16_t LCD_RegValue)
  1065. {
  1066. /* Write 16-bit Index (then Write Reg) */
  1067. LCD_WriteRegIndex(LCD_Reg);
  1068. /* Write 16-bit Reg */
  1069. /* Reset LCD control line(/CS) and Send Start-Byte */
  1070. LCD_nCS_StartByte(START_BYTE | LCD_WRITE_REG);
  1071. SPI_I2S_SendData(LCD_SPI, LCD_RegValue>>8);
  1072. while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_BSY) != RESET)
  1073. {
  1074. }
  1075. SPI_I2S_SendData(LCD_SPI, (LCD_RegValue & 0xFF));
  1076. while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_BSY) != RESET)
  1077. {
  1078. }
  1079. LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_SET);
  1080. }
  1081. /**
  1082. * @brief Writes to the LCD RAM.
  1083. * @param RGB_Code: the pixel color in RGB mode (5-6-5).
  1084. * @retval None
  1085. */
  1086. void LCD_WriteRAM(uint16_t RGB_Code)
  1087. {
  1088. SPI_I2S_SendData(LCD_SPI, RGB_Code >> 8);
  1089. while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_BSY) != RESET)
  1090. {
  1091. }
  1092. SPI_I2S_SendData(LCD_SPI, RGB_Code & 0xFF);
  1093. while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_BSY) != RESET)
  1094. {
  1095. }
  1096. }
  1097. /**
  1098. * @brief Power on the LCD.
  1099. * @param None
  1100. * @retval None
  1101. */
  1102. void LCD_PowerOn(void)
  1103. {
  1104. /* Power On sequence ---------------------------------------------------------*/
  1105. LCD_WriteReg(LCD_REG_16, 0x0000); /* SAP, BT[3:0], AP, DSTB, SLP, STB */
  1106. LCD_WriteReg(LCD_REG_17, 0x0000); /* DC1[2:0], DC0[2:0], VC[2:0] */
  1107. LCD_WriteReg(LCD_REG_18, 0x0000); /* VREG1OUT voltage */
  1108. LCD_WriteReg(LCD_REG_19, 0x0000); /* VDV[4:0] for VCOM amplitude */
  1109. _delay_(20); /* Dis-charge capacitor power voltage (200ms) */
  1110. LCD_WriteReg(LCD_REG_16, 0x17B0); /* SAP, BT[3:0], AP, DSTB, SLP, STB */
  1111. LCD_WriteReg(LCD_REG_17, 0x0137); /* DC1[2:0], DC0[2:0], VC[2:0] */
  1112. _delay_(5); /* Delay 50 ms */
  1113. LCD_WriteReg(LCD_REG_18, 0x0139); /* VREG1OUT voltage */
  1114. _delay_(5); /* delay 50 ms */
  1115. LCD_WriteReg(LCD_REG_19, 0x1d00); /* VDV[4:0] for VCOM amplitude */
  1116. LCD_WriteReg(LCD_REG_41, 0x0013); /* VCM[4:0] for VCOMH */
  1117. _delay_(5); /* delay 50 ms */
  1118. LCD_WriteReg(LCD_REG_7, 0x0173); /* 262K color and display ON */
  1119. }
  1120. /**
  1121. * @brief Enables the Display.
  1122. * @param None
  1123. * @retval None
  1124. */
  1125. void LCD_DisplayOn(void)
  1126. {
  1127. /* Display On */
  1128. LCD_WriteReg(LCD_REG_7, 0x0173); /* 262K color and display ON */
  1129. }
  1130. /**
  1131. * @brief Disables the Display.
  1132. * @param None
  1133. * @retval None
  1134. */
  1135. void LCD_DisplayOff(void)
  1136. {
  1137. /* Display Off */
  1138. LCD_WriteReg(LCD_REG_7, 0x0);
  1139. }
  1140. /**
  1141. * @brief Configures LCD control lines in Output Push-Pull mode.
  1142. * @param None
  1143. * @retval None
  1144. */
  1145. void LCD_CtrlLinesConfig(void)
  1146. {
  1147. GPIO_InitTypeDef GPIO_InitStructure;
  1148. /* Enable GPIO clock */
  1149. RCC_APB2PeriphClockCmd(LCD_NCS_GPIO_CLK, ENABLE);
  1150. /* Configure NCS in Output Push-Pull mode */
  1151. GPIO_InitStructure.GPIO_Pin = LCD_NCS_PIN;
  1152. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  1153. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  1154. GPIO_Init(LCD_NCS_GPIO_PORT, &GPIO_InitStructure);
  1155. }
  1156. /**
  1157. * @brief Sets or reset LCD control lines.
  1158. * @param GPIOx: where x can be B or D to select the GPIO peripheral.
  1159. * @param CtrlPins: the Control line. This parameter can be:
  1160. * @arg LCD_NCS_PIN: Chip Select pin
  1161. * @param BitVal: specifies the value to be written to the selected bit.
  1162. * This parameter can be:
  1163. * @arg Bit_RESET: to clear the port pin
  1164. * @arg Bit_SET: to set the port pin
  1165. * @retval None
  1166. */
  1167. void LCD_CtrlLinesWrite(GPIO_TypeDef* GPIOx, uint16_t CtrlPins, BitAction BitVal)
  1168. {
  1169. /* Set or Reset the control line */
  1170. GPIO_WriteBit(GPIOx, CtrlPins, BitVal);
  1171. }
  1172. /**
  1173. * @brief Configures the LCD_SPI interface.
  1174. * @param None
  1175. * @retval None
  1176. */
  1177. void LCD_SPIConfig(void)
  1178. {
  1179. SPI_InitTypeDef SPI_InitStructure;
  1180. GPIO_InitTypeDef GPIO_InitStructure;
  1181. /* Enable GPIO clock */
  1182. RCC_APB2PeriphClockCmd(LCD_SPI_SCK_GPIO_CLK | LCD_SPI_MISO_GPIO_CLK | LCD_SPI_MOSI_GPIO_CLK
  1183. | RCC_APB2Periph_AFIO, ENABLE);
  1184. GPIO_PinRemapConfig(GPIO_Remap_SPI3, ENABLE);
  1185. /* Enable SPI clock */
  1186. RCC_APB1PeriphClockCmd(LCD_SPI_CLK, ENABLE);
  1187. /* Configure SPI pins: SCK, MISO and MOSI */
  1188. GPIO_InitStructure.GPIO_Pin = LCD_SPI_SCK_PIN;
  1189. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  1190. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  1191. GPIO_Init(LCD_SPI_SCK_GPIO_PORT, &GPIO_InitStructure);
  1192. GPIO_InitStructure.GPIO_Pin = LCD_SPI_MISO_PIN;
  1193. GPIO_Init(LCD_SPI_MISO_GPIO_PORT, &GPIO_InitStructure);
  1194. GPIO_InitStructure.GPIO_Pin = LCD_SPI_MOSI_PIN;
  1195. GPIO_Init(LCD_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure);
  1196. SPI_I2S_DeInit(LCD_SPI);
  1197. /* SPI Config */
  1198. SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  1199. SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  1200. SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  1201. SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
  1202. SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
  1203. SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  1204. SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
  1205. SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  1206. SPI_Init(LCD_SPI, &SPI_InitStructure);
  1207. /* SPI enable */
  1208. SPI_Cmd(LCD_SPI, ENABLE);
  1209. }
  1210. /**
  1211. * @brief Displays a pixel.
  1212. * @param x: pixel x.
  1213. * @param y: pixel y.
  1214. * @retval None
  1215. */
  1216. static void PutPixel(int16_t x, int16_t y)
  1217. {
  1218. if(x < 0 || x > 239 || y < 0 || y > 319)
  1219. {
  1220. return;
  1221. }
  1222. LCD_DrawLine(x, y, 1, LCD_DIR_HORIZONTAL);
  1223. }
  1224. #ifndef USE_Delay
  1225. /**
  1226. * @brief Inserts a delay time.
  1227. * @param nCount: specifies the delay time length.
  1228. * @retval None
  1229. */
  1230. static void delay(__IO uint32_t nCount)
  1231. {
  1232. __IO uint32_t index = 0;
  1233. for(index = (100000 * nCount); index != 0; index--)
  1234. {
  1235. }
  1236. }
  1237. #endif /* USE_Delay*/
  1238. /**
  1239. * @}
  1240. */
  1241. /**
  1242. * @}
  1243. */
  1244. /**
  1245. * @}
  1246. */
  1247. /**
  1248. * @}
  1249. */
  1250. /**
  1251. * @}
  1252. */
  1253. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/