stm320518_eval_lcd.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896
  1. /**
  2. ******************************************************************************
  3. * @file stm320518_eval_lcd.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 20-April-2012
  7. * @brief This file includes the LCD driver for AM-240320L8TNQW00H (LCD_ILI9320),
  8. * AM-240320LDTNQW00H (LCD_SPFD5408B), AM240320D5TOQW01H (LCD_ILI9325)
  9. * and AM240320LGTNQW00H (HX8347-D) Liquid Crystal Display Module of
  10. * STM320518-EVAL board.
  11. ******************************************************************************
  12. * @attention
  13. *
  14. * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  15. *
  16. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  17. * You may not use this file except in compliance with the License.
  18. * You may obtain a copy of the License at:
  19. *
  20. * http://www.st.com/software_license_agreement_liberty_v2
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an "AS IS" BASIS,
  24. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. * See the License for the specific language governing permissions and
  26. * limitations under the License.
  27. *
  28. ******************************************************************************
  29. */
  30. /* Includes ------------------------------------------------------------------*/
  31. #include "stm320518_eval_lcd.h"
  32. #include "../Common/fonts.c"
  33. /** @addtogroup Utilities
  34. * @{
  35. */
  36. /** @addtogroup STM32_EVAL
  37. * @{
  38. */
  39. /** @addtogroup STM320518_EVAL
  40. * @{
  41. */
  42. /** @defgroup STM320518_EVAL_LCD
  43. * @brief This file includes the LCD driver for AM-240320L8TNQW00H (LCD_ILI9320),
  44. * AM-240320LDTNQW00H (LCD_SPFD5408B), AM240320D5TOQW01H (LCD_ILI9325) and
  45. * AM240320LGTNQW00H (HX8347-D) Liquid Crystal Display Module of
  46. * STM320518-EVAL board.
  47. * @{
  48. */
  49. /** @defgroup STM320518_EVAL_LCD_Private_Types
  50. * @{
  51. */
  52. /**
  53. * @}
  54. */
  55. /** @defgroup STM320518_EVAL_LCD_Private_Defines
  56. * @{
  57. */
  58. #define LCD_ILI9325 0x9325
  59. #define LCD_ILI9320 0x9320
  60. #define LCD_SPFD5408 0x5408
  61. #define LCD_HX8347D 0x0047
  62. #define START_BYTE 0x70
  63. #define SET_INDEX 0x00
  64. #define READ_STATUS 0x01
  65. #define LCD_WRITE_REG 0x02
  66. #define LCD_READ_REG 0x03
  67. #define MAX_POLY_CORNERS 200
  68. #define POLY_Y(Z) ((int32_t)((Points + Z)->X))
  69. #define POLY_X(Z) ((int32_t)((Points + Z)->Y))
  70. /**
  71. * @}
  72. */
  73. /** @defgroup STM320518_EVAL_LCD_Private_Macros
  74. * @{
  75. */
  76. #define ABS(X) ((X) > 0 ? (X) : -(X))
  77. /**
  78. * @}
  79. */
  80. /** @defgroup STM320518_EVAL_LCD_Private_Variables
  81. * @{
  82. */
  83. static sFONT *LCD_Currentfonts;
  84. /* Global variables to set the written text color */
  85. static __IO uint16_t TextColor = 0x0000, BackColor = 0xFFFF;
  86. __IO uint32_t LCDType = LCD_HX8347D;
  87. /**
  88. * @}
  89. */
  90. /** @defgroup STM320518_EVAL_LCD_Private_Function_Prototypes
  91. * @{
  92. */
  93. #ifndef USE_Delay
  94. static void delay(__IO uint32_t nCount);
  95. #endif /* USE_Delay*/
  96. static void PutPixel(int16_t x, int16_t y);
  97. static void LCD_PolyLineRelativeClosed(pPoint Points, uint16_t PointCount, uint16_t Closed);
  98. /**
  99. * @}
  100. */
  101. /** @defgroup STM320518_EVAL_LCD_Private_Functions
  102. * @{
  103. */
  104. /**
  105. * @brief DeInitializes the LCD.
  106. * @param None
  107. * @retval None
  108. */
  109. void LCD_DeInit(void)
  110. {
  111. GPIO_InitTypeDef GPIO_InitStructure;
  112. /*!< LCD Display Off */
  113. LCD_DisplayOff();
  114. /*!< LCD_SPI disable */
  115. SPI_Cmd(LCD_SPI, DISABLE);
  116. /*!< LCD_SPI DeInit */
  117. SPI_I2S_DeInit(LCD_SPI);
  118. /*!< Disable SPI clock */
  119. RCC_APB1PeriphClockCmd(LCD_SPI_CLK, DISABLE);
  120. /* Configure NCS in Output Push-Pull mode */
  121. GPIO_InitStructure.GPIO_Pin = LCD_NCS_PIN;
  122. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  123. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  124. GPIO_Init(LCD_NCS_GPIO_PORT, &GPIO_InitStructure);
  125. /* Configure NWR(RNW), RS */
  126. GPIO_InitStructure.GPIO_Pin = LCD_RS_PIN;
  127. GPIO_Init(LCD_RS_GPIO_PORT, &GPIO_InitStructure);
  128. GPIO_InitStructure.GPIO_Pin = LCD_NWR_PIN;
  129. GPIO_Init(LCD_NWR_GPIO_PORT, &GPIO_InitStructure);
  130. /* Configure SPI pins: SCK, MISO and MOSI */
  131. GPIO_InitStructure.GPIO_Pin = LCD_SPI_SCK_PIN;
  132. GPIO_Init(LCD_SPI_SCK_GPIO_PORT, &GPIO_InitStructure);
  133. GPIO_InitStructure.GPIO_Pin = LCD_SPI_MISO_PIN;
  134. GPIO_Init(LCD_SPI_MISO_GPIO_PORT, &GPIO_InitStructure);
  135. GPIO_InitStructure.GPIO_Pin = LCD_SPI_MOSI_PIN;
  136. GPIO_Init(LCD_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure);
  137. }
  138. /**
  139. * @brief Setups the LCD.
  140. * @param None
  141. * @retval None
  142. */
  143. void LCD_Setup(void)
  144. {
  145. /* Configure the LCD Control pins --------------------------------------------*/
  146. LCD_CtrlLinesConfig();
  147. /* Configure the LCD_SPI interface ----------------------------------------------*/
  148. LCD_SPIConfig();
  149. /* Check if the LCD is HX8347D Controller */
  150. if(LCDType == LCD_HX8347D)
  151. {
  152. /* Driving ability setting */
  153. LCD_WriteReg(LCD_REG_234, 0x00);
  154. LCD_WriteReg(LCD_REG_235, 0x20);
  155. LCD_WriteReg(LCD_REG_236, 0x0C);
  156. LCD_WriteReg(LCD_REG_237, 0xC4);
  157. LCD_WriteReg(LCD_REG_232, 0x40);
  158. LCD_WriteReg(LCD_REG_233, 0x38);
  159. LCD_WriteReg(LCD_REG_241, 0x01); /* RGB 18-bit interface ;0x0110 */
  160. LCD_WriteReg(LCD_REG_242, 0x10);
  161. LCD_WriteReg(LCD_REG_39, 0xA3);
  162. /* Adjust the Gamma Curve */
  163. LCD_WriteReg(LCD_REG_64, 0x01);
  164. LCD_WriteReg(LCD_REG_65, 0x00);
  165. LCD_WriteReg(LCD_REG_66, 0x00);
  166. LCD_WriteReg(LCD_REG_67, 0x10);
  167. LCD_WriteReg(LCD_REG_68, 0x0E);
  168. LCD_WriteReg(LCD_REG_69, 0x24);
  169. LCD_WriteReg(LCD_REG_70, 0x04);
  170. LCD_WriteReg(LCD_REG_71, 0x50);
  171. LCD_WriteReg(LCD_REG_72, 0x02);
  172. LCD_WriteReg(LCD_REG_73, 0x13);
  173. LCD_WriteReg(LCD_REG_74, 0x19);
  174. LCD_WriteReg(LCD_REG_75, 0x19);
  175. LCD_WriteReg(LCD_REG_76, 0x16);
  176. LCD_WriteReg(LCD_REG_80, 0x1B);
  177. LCD_WriteReg(LCD_REG_81, 0x31);
  178. LCD_WriteReg(LCD_REG_82, 0x2F);
  179. LCD_WriteReg(LCD_REG_83, 0x3F);
  180. LCD_WriteReg(LCD_REG_84, 0x3F);
  181. LCD_WriteReg(LCD_REG_85, 0x3E);
  182. LCD_WriteReg(LCD_REG_86, 0x2F);
  183. LCD_WriteReg(LCD_REG_87, 0x7B);
  184. LCD_WriteReg(LCD_REG_88, 0x09);
  185. LCD_WriteReg(LCD_REG_89, 0x06);
  186. LCD_WriteReg(LCD_REG_90, 0x06);
  187. LCD_WriteReg(LCD_REG_91, 0x0C);
  188. LCD_WriteReg(LCD_REG_92, 0x1D);
  189. LCD_WriteReg(LCD_REG_93, 0xCC);
  190. /* Power voltage setting */
  191. LCD_WriteReg(LCD_REG_27, 0x1B);
  192. LCD_WriteReg(LCD_REG_26, 0x01);
  193. LCD_WriteReg(LCD_REG_36, 0x2F);
  194. LCD_WriteReg(LCD_REG_37, 0x57);
  195. /*****VCOM offset ****/
  196. LCD_WriteReg(LCD_REG_35, 0x86);
  197. /* Power on setting */
  198. LCD_WriteReg(LCD_REG_24, 0x36); /* Display frame rate:75Hz(2.85MHz X 117%) */
  199. LCD_WriteReg(LCD_REG_25, 0x01); /* Internal oscillator start to oscillate */
  200. LCD_WriteReg(LCD_REG_1,0x00);
  201. LCD_WriteReg(LCD_REG_31, 0x88); /* Step-up Circuit 1 on,open abnormal power-off monitor */
  202. _delay_(2);
  203. LCD_WriteReg(LCD_REG_31, 0x80); /* Step-up Circuit 1 off */
  204. _delay_(2);
  205. LCD_WriteReg(LCD_REG_31, 0x90); /* VCOML voltage can output to negative voltage,
  206. (1.0V ~ VCL+0.5V) */
  207. _delay_(2);
  208. LCD_WriteReg(LCD_REG_31, 0xD0); /* Step-up Circuit 2 on */
  209. _delay_(2);
  210. LCD_WriteReg(LCD_REG_23, 0x05); /* COLMOD control */
  211. /* Set GRAM Area - Partial Display Control */
  212. LCD_WriteReg(LCD_REG_1, 0x00); /* Scroll off */
  213. LCD_WriteReg(LCD_REG_2, 0x00);
  214. LCD_WriteReg(LCD_REG_3, 0x00);
  215. LCD_WriteReg(LCD_REG_4, 0x01); /* X,Y swap */
  216. LCD_WriteReg(LCD_REG_5, 0x3F); /* X,Y swap */
  217. LCD_WriteReg(LCD_REG_6, 0x00);
  218. LCD_WriteReg(LCD_REG_7, 0x00);
  219. LCD_WriteReg(LCD_REG_8, 0x00); /* X,Y swap */
  220. LCD_WriteReg(LCD_REG_9, 0xEF); /* X,Y swap */
  221. /* Memory access control */
  222. /* bit7 controls left,right swap(X) */
  223. /* bit6 controls up,down swap(Y) */
  224. /* bit5 controls X,Y swap */
  225. LCD_WriteReg(LCD_REG_22, 0x28);
  226. /* SET PANEL */
  227. LCD_WriteReg(LCD_REG_54, 0x00); /* Panel characteristic control */
  228. LCD_WriteReg(LCD_REG_54, 0x04); /* Panel characteristic control: gate driver shift reverse[work] */
  229. LCD_WriteReg(LCD_REG_40, 0x38); /* Display control3: source output->PT(0,0) */
  230. _delay_(6);
  231. LCD_WriteReg(LCD_REG_40, 0x3C); /* Display control3: source output->Display */
  232. }
  233. else if(LCDType == LCD_ILI9320)
  234. {
  235. _delay_(5); /* Delay 50 ms */
  236. /* Start Initial Sequence ------------------------------------------------*/
  237. LCD_WriteReg(LCD_REG_229, 0x8000); /* Set the internal vcore voltage */
  238. LCD_WriteReg(LCD_REG_0, 0x0001); /* Start internal OSC. */
  239. LCD_WriteReg(LCD_REG_1, 0x0100); /* set SS and SM bit */
  240. LCD_WriteReg(LCD_REG_2, 0x0700); /* set 1 line inversion */
  241. LCD_WriteReg(LCD_REG_3, 0x1030); /* set GRAM write direction and BGR=1. */
  242. LCD_WriteReg(LCD_REG_4, 0x0000); /* Resize register */
  243. LCD_WriteReg(LCD_REG_8, 0x0202); /* set the back porch and front porch */
  244. LCD_WriteReg(LCD_REG_9, 0x0000); /* set non-display area refresh cycle ISC[3:0] */
  245. LCD_WriteReg(LCD_REG_10, 0x0000); /* FMARK function */
  246. LCD_WriteReg(LCD_REG_12, 0x0000); /* RGB interface setting */
  247. LCD_WriteReg(LCD_REG_13, 0x0000); /* Frame marker Position */
  248. LCD_WriteReg(LCD_REG_15, 0x0000); /* RGB interface polarity */
  249. /* Power On sequence -----------------------------------------------------*/
  250. LCD_WriteReg(LCD_REG_16, 0x0000); /* SAP, BT[3:0], AP, DSTB, SLP, STB */
  251. LCD_WriteReg(LCD_REG_17, 0x0000); /* DC1[2:0], DC0[2:0], VC[2:0] */
  252. LCD_WriteReg(LCD_REG_18, 0x0000); /* VREG1OUT voltage */
  253. LCD_WriteReg(LCD_REG_19, 0x0000); /* VDV[4:0] for VCOM amplitude */
  254. _delay_(20); /* Dis-charge capacitor power voltage (200ms) */
  255. LCD_WriteReg(LCD_REG_16, 0x17B0); /* SAP, BT[3:0], AP, DSTB, SLP, STB */
  256. LCD_WriteReg(LCD_REG_17, 0x0137); /* DC1[2:0], DC0[2:0], VC[2:0] */
  257. _delay_(5); /* Delay 50 ms */
  258. LCD_WriteReg(LCD_REG_18, 0x0139); /* VREG1OUT voltage */
  259. _delay_(5); /* Delay 50 ms */
  260. LCD_WriteReg(LCD_REG_19, 0x1d00); /* VDV[4:0] for VCOM amplitude */
  261. LCD_WriteReg(LCD_REG_41, 0x0013); /* VCM[4:0] for VCOMH */
  262. _delay_(5); /* Delay 50 ms */
  263. LCD_WriteReg(LCD_REG_32, 0x0000); /* GRAM horizontal Address */
  264. LCD_WriteReg(LCD_REG_33, 0x0000); /* GRAM Vertical Address */
  265. /* Adjust the Gamma Curve ------------------------------------------------*/
  266. LCD_WriteReg(LCD_REG_48, 0x0006);
  267. LCD_WriteReg(LCD_REG_49, 0x0101);
  268. LCD_WriteReg(LCD_REG_50, 0x0003);
  269. LCD_WriteReg(LCD_REG_53, 0x0106);
  270. LCD_WriteReg(LCD_REG_54, 0x0b02);
  271. LCD_WriteReg(LCD_REG_55, 0x0302);
  272. LCD_WriteReg(LCD_REG_56, 0x0707);
  273. LCD_WriteReg(LCD_REG_57, 0x0007);
  274. LCD_WriteReg(LCD_REG_60, 0x0600);
  275. LCD_WriteReg(LCD_REG_61, 0x020b);
  276. /* Set GRAM area ---------------------------------------------------------*/
  277. LCD_WriteReg(LCD_REG_80, 0x0000); /* Horizontal GRAM Start Address */
  278. LCD_WriteReg(LCD_REG_81, 0x00EF); /* Horizontal GRAM End Address */
  279. LCD_WriteReg(LCD_REG_82, 0x0000); /* Vertical GRAM Start Address */
  280. LCD_WriteReg(LCD_REG_83, 0x013F); /* Vertical GRAM End Address */
  281. LCD_WriteReg(LCD_REG_96, 0x2700); /* Gate Scan Line */
  282. LCD_WriteReg(LCD_REG_97, 0x0001); /* NDL,VLE, REV */
  283. LCD_WriteReg(LCD_REG_106, 0x0000); /* set scrolling line */
  284. /* Partial Display Control -----------------------------------------------*/
  285. LCD_WriteReg(LCD_REG_128, 0x0000);
  286. LCD_WriteReg(LCD_REG_129, 0x0000);
  287. LCD_WriteReg(LCD_REG_130, 0x0000);
  288. LCD_WriteReg(LCD_REG_131, 0x0000);
  289. LCD_WriteReg(LCD_REG_132, 0x0000);
  290. LCD_WriteReg(LCD_REG_133, 0x0000);
  291. /* Panel Control ---------------------------------------------------------*/
  292. LCD_WriteReg(LCD_REG_144, 0x0010);
  293. LCD_WriteReg(LCD_REG_146, 0x0000);
  294. LCD_WriteReg(LCD_REG_147, 0x0003);
  295. LCD_WriteReg(LCD_REG_149, 0x0110);
  296. LCD_WriteReg(LCD_REG_151, 0x0000);
  297. LCD_WriteReg(LCD_REG_152, 0x0000);
  298. /* Set GRAM write direction and BGR = 1 */
  299. /* I/D=01 (Horizontal : increment, Vertical : decrement) */
  300. /* AM=1 (address is updated in vertical writing direction) */
  301. LCD_WriteReg(LCD_REG_3, 0x1018);
  302. LCD_WriteReg(LCD_REG_7, 0x0173); /* 262K color and display ON */
  303. }
  304. else if(LCDType == LCD_SPFD5408)
  305. {
  306. /* Start Initial Sequence --------------------------------------------------*/
  307. LCD_WriteReg(LCD_REG_227, 0x3008); /* Set internal timing */
  308. LCD_WriteReg(LCD_REG_231, 0x0012); /* Set internal timing */
  309. LCD_WriteReg(LCD_REG_239, 0x1231); /* Set internal timing */
  310. LCD_WriteReg(LCD_REG_1, 0x0100); /* Set SS and SM bit */
  311. LCD_WriteReg(LCD_REG_2, 0x0700); /* Set 1 line inversion */
  312. LCD_WriteReg(LCD_REG_3, 0x1030); /* Set GRAM write direction and BGR=1. */
  313. LCD_WriteReg(LCD_REG_4, 0x0000); /* Resize register */
  314. LCD_WriteReg(LCD_REG_8, 0x0202); /* Set the back porch and front porch */
  315. LCD_WriteReg(LCD_REG_9, 0x0000); /* Set non-display area refresh cycle ISC[3:0] */
  316. LCD_WriteReg(LCD_REG_10, 0x0000); /* FMARK function */
  317. LCD_WriteReg(LCD_REG_12, 0x0000); /* RGB interface setting */
  318. LCD_WriteReg(LCD_REG_13, 0x0000); /* Frame marker Position */
  319. LCD_WriteReg(LCD_REG_15, 0x0000); /* RGB interface polarity */
  320. /* Power On sequence -------------------------------------------------------*/
  321. LCD_WriteReg(LCD_REG_16, 0x0000); /* SAP, BT[3:0], AP, DSTB, SLP, STB */
  322. LCD_WriteReg(LCD_REG_17, 0x0000); /* DC1[2:0], DC0[2:0], VC[2:0] */
  323. LCD_WriteReg(LCD_REG_18, 0x0000); /* VREG1OUT voltage */
  324. LCD_WriteReg(LCD_REG_19, 0x0000); /* VDV[4:0] for VCOM amplitude */
  325. _delay_(20); /* Dis-charge capacitor power voltage (200ms) */
  326. LCD_WriteReg(LCD_REG_17, 0x0007); /* DC1[2:0], DC0[2:0], VC[2:0] */
  327. _delay_(5); /* Delay 50 ms */
  328. LCD_WriteReg(LCD_REG_16, 0x12B0); /* SAP, BT[3:0], AP, DSTB, SLP, STB */
  329. _delay_(5); /* Delay 50 ms */
  330. LCD_WriteReg(LCD_REG_18, 0x01BD); /* External reference voltage= Vci */
  331. _delay_(5); /* Delay 50 ms */
  332. LCD_WriteReg(LCD_REG_19, 0x1400); /* VDV[4:0] for VCOM amplitude */
  333. LCD_WriteReg(LCD_REG_41, 0x000E); /* VCM[4:0] for VCOMH */
  334. _delay_(5); /* Delay 50 ms */
  335. LCD_WriteReg(LCD_REG_32, 0x0000); /* GRAM horizontal Address */
  336. LCD_WriteReg(LCD_REG_33, 0x013F); /* GRAM Vertical Address */
  337. /* Adjust the Gamma Curve --------------------------------------------------*/
  338. LCD_WriteReg(LCD_REG_48, 0x0007);
  339. LCD_WriteReg(LCD_REG_49, 0x0302);
  340. LCD_WriteReg(LCD_REG_50, 0x0105);
  341. LCD_WriteReg(LCD_REG_53, 0x0206);
  342. LCD_WriteReg(LCD_REG_54, 0x0808);
  343. LCD_WriteReg(LCD_REG_55, 0x0206);
  344. LCD_WriteReg(LCD_REG_56, 0x0504);
  345. LCD_WriteReg(LCD_REG_57, 0x0007);
  346. LCD_WriteReg(LCD_REG_60, 0x0105);
  347. LCD_WriteReg(LCD_REG_61, 0x0808);
  348. /* Set GRAM area -----------------------------------------------------------*/
  349. LCD_WriteReg(LCD_REG_80, 0x0000); /* Horizontal GRAM Start Address */
  350. LCD_WriteReg(LCD_REG_81, 0x00EF); /* Horizontal GRAM End Address */
  351. LCD_WriteReg(LCD_REG_82, 0x0000); /* Vertical GRAM Start Address */
  352. LCD_WriteReg(LCD_REG_83, 0x013F); /* Vertical GRAM End Address */
  353. LCD_WriteReg(LCD_REG_96, 0xA700); /* Gate Scan Line */
  354. LCD_WriteReg(LCD_REG_97, 0x0001); /* NDL,VLE, REV */
  355. LCD_WriteReg(LCD_REG_106, 0x0000); /* Set scrolling line */
  356. /* Partial Display Control -------------------------------------------------*/
  357. LCD_WriteReg(LCD_REG_128, 0x0000);
  358. LCD_WriteReg(LCD_REG_129, 0x0000);
  359. LCD_WriteReg(LCD_REG_130, 0x0000);
  360. LCD_WriteReg(LCD_REG_131, 0x0000);
  361. LCD_WriteReg(LCD_REG_132, 0x0000);
  362. LCD_WriteReg(LCD_REG_133, 0x0000);
  363. /* Panel Control -----------------------------------------------------------*/
  364. LCD_WriteReg(LCD_REG_144, 0x0010);
  365. LCD_WriteReg(LCD_REG_146, 0x0000);
  366. LCD_WriteReg(LCD_REG_147, 0x0003);
  367. LCD_WriteReg(LCD_REG_149, 0x0110);
  368. LCD_WriteReg(LCD_REG_151, 0x0000);
  369. LCD_WriteReg(LCD_REG_152, 0x0000);
  370. /* Set GRAM write direction and BGR = 1
  371. I/D=01 (Horizontal : increment, Vertical : decrement)
  372. AM=1 (address is updated in vertical writing direction) */
  373. LCD_WriteReg(LCD_REG_3, 0x1018);
  374. LCD_WriteReg(LCD_REG_7, 0x0112); /* 262K color and display ON */
  375. }
  376. else if(LCDType == LCD_ILI9325)
  377. {
  378. /* Start Initial Sequence ------------------------------------------------*/
  379. LCD_WriteReg(LCD_REG_0, 0x0001); /* Start internal OSC. */
  380. LCD_WriteReg(LCD_REG_1, 0x0100); /* Set SS and SM bit */
  381. LCD_WriteReg(LCD_REG_2, 0x0700); /* Set 1 line inversion */
  382. LCD_WriteReg(LCD_REG_3, 0x1018); /* Set GRAM write direction and BGR=1. */
  383. LCD_WriteReg(LCD_REG_4, 0x0000); /* Resize register */
  384. LCD_WriteReg(LCD_REG_8, 0x0202); /* Set the back porch and front porch */
  385. LCD_WriteReg(LCD_REG_9, 0x0000); /* Set non-display area refresh cycle ISC[3:0] */
  386. LCD_WriteReg(LCD_REG_10, 0x0000); /* FMARK function */
  387. LCD_WriteReg(LCD_REG_12, 0x0000); /* RGB interface setting */
  388. LCD_WriteReg(LCD_REG_13, 0x0000); /* Frame marker Position */
  389. LCD_WriteReg(LCD_REG_15, 0x0000); /* RGB interface polarity */
  390. /* Power On sequence -----------------------------------------------------*/
  391. LCD_WriteReg(LCD_REG_16, 0x0000); /* SAP, BT[3:0], AP, DSTB, SLP, STB */
  392. LCD_WriteReg(LCD_REG_17, 0x0000); /* DC1[2:0], DC0[2:0], VC[2:0] */
  393. LCD_WriteReg(LCD_REG_18, 0x0000); /* VREG1OUT voltage */
  394. LCD_WriteReg(LCD_REG_19, 0x0000); /* VDV[4:0] for VCOM amplitude */
  395. _delay_(20); /* Dis-charge capacitor power voltage (200ms) */
  396. LCD_WriteReg(LCD_REG_16, 0x17B0); /* SAP, BT[3:0], AP, DSTB, SLP, STB */
  397. LCD_WriteReg(LCD_REG_17, 0x0137); /* DC1[2:0], DC0[2:0], VC[2:0] */
  398. _delay_(5); /* Delay 50 ms */
  399. LCD_WriteReg(LCD_REG_18, 0x0139); /* VREG1OUT voltage */
  400. _delay_(5); /* Delay 50 ms */
  401. LCD_WriteReg(LCD_REG_19, 0x1d00); /* VDV[4:0] for VCOM amplitude */
  402. LCD_WriteReg(LCD_REG_41, 0x0013); /* VCM[4:0] for VCOMH */
  403. _delay_(5); /* Delay 50 ms */
  404. LCD_WriteReg(LCD_REG_32, 0x0000); /* GRAM horizontal Address */
  405. LCD_WriteReg(LCD_REG_33, 0x0000); /* GRAM Vertical Address */
  406. /* Adjust the Gamma Curve (ILI9325)---------------------------------------*/
  407. LCD_WriteReg(LCD_REG_48, 0x0007);
  408. LCD_WriteReg(LCD_REG_49, 0x0302);
  409. LCD_WriteReg(LCD_REG_50, 0x0105);
  410. LCD_WriteReg(LCD_REG_53, 0x0206);
  411. LCD_WriteReg(LCD_REG_54, 0x0808);
  412. LCD_WriteReg(LCD_REG_55, 0x0206);
  413. LCD_WriteReg(LCD_REG_56, 0x0504);
  414. LCD_WriteReg(LCD_REG_57, 0x0007);
  415. LCD_WriteReg(LCD_REG_60, 0x0105);
  416. LCD_WriteReg(LCD_REG_61, 0x0808);
  417. /* Set GRAM area ---------------------------------------------------------*/
  418. LCD_WriteReg(LCD_REG_80, 0x0000); /* Horizontal GRAM Start Address */
  419. LCD_WriteReg(LCD_REG_81, 0x00EF); /* Horizontal GRAM End Address */
  420. LCD_WriteReg(LCD_REG_82, 0x0000); /* Vertical GRAM Start Address */
  421. LCD_WriteReg(LCD_REG_83, 0x013F); /* Vertical GRAM End Address */
  422. LCD_WriteReg(LCD_REG_96, 0xA700); /* Gate Scan Line(GS=1, scan direction is G320~G1) */
  423. LCD_WriteReg(LCD_REG_97, 0x0001); /* NDL,VLE, REV */
  424. LCD_WriteReg(LCD_REG_106, 0x0000); /* set scrolling line */
  425. /* Partial Display Control -----------------------------------------------*/
  426. LCD_WriteReg(LCD_REG_128, 0x0000);
  427. LCD_WriteReg(LCD_REG_129, 0x0000);
  428. LCD_WriteReg(LCD_REG_130, 0x0000);
  429. LCD_WriteReg(LCD_REG_131, 0x0000);
  430. LCD_WriteReg(LCD_REG_132, 0x0000);
  431. LCD_WriteReg(LCD_REG_133, 0x0000);
  432. /* Panel Control ---------------------------------------------------------*/
  433. LCD_WriteReg(LCD_REG_144, 0x0010);
  434. LCD_WriteReg(LCD_REG_146, 0x0000);
  435. LCD_WriteReg(LCD_REG_147, 0x0003);
  436. LCD_WriteReg(LCD_REG_149, 0x0110);
  437. LCD_WriteReg(LCD_REG_151, 0x0000);
  438. LCD_WriteReg(LCD_REG_152, 0x0000);
  439. /* set GRAM write direction and BGR = 1 */
  440. /* I/D=00 (Horizontal : increment, Vertical : decrement) */
  441. /* AM=1 (address is updated in vertical writing direction) */
  442. LCD_WriteReg(LCD_REG_3, 0x1018);
  443. LCD_WriteReg(LCD_REG_7, 0x0133); /* 262K color and display ON */
  444. }
  445. }
  446. /**
  447. * @brief Initializes the LCD.
  448. * @param None
  449. * @retval None
  450. */
  451. void STM320518_LCD_Init(void)
  452. {
  453. __IO uint32_t lcdid = 0;
  454. /* Setups the LCD */
  455. LCD_Setup();
  456. /* Read the LCD ID */
  457. lcdid = LCD_ReadReg(0x00);
  458. if (lcdid == LCD_SPFD5408)
  459. {
  460. LCDType = LCD_SPFD5408;
  461. /* Setups the LCD */
  462. LCD_Setup();
  463. }
  464. else if (lcdid == LCD_ILI9325)
  465. {
  466. LCDType = LCD_ILI9325;
  467. /* Setups the LCD */
  468. LCD_Setup();
  469. }
  470. else if (lcdid == LCD_ILI9320)
  471. {
  472. LCDType = LCD_ILI9320;
  473. /* Setups the LCD */
  474. LCD_Setup();
  475. }
  476. else
  477. {
  478. LCDType = LCD_HX8347D;
  479. }
  480. LCD_SetFont(&LCD_DEFAULT_FONT);
  481. }
  482. /**
  483. * @brief Sets the LCD Text and Background colors.
  484. * @param _TextColor: specifies the Text Color.
  485. * @param _BackColor: specifies the Background Color.
  486. * @retval None
  487. */
  488. void LCD_SetColors(__IO uint16_t _TextColor, __IO uint16_t _BackColor)
  489. {
  490. TextColor = _TextColor;
  491. BackColor = _BackColor;
  492. }
  493. /**
  494. * @brief Gets the LCD Text and Background colors.
  495. * @param _TextColor: pointer to the variable that will contain the Text
  496. Color.
  497. * @param _BackColor: pointer to the variable that will contain the Background
  498. Color.
  499. * @retval None
  500. */
  501. void LCD_GetColors(__IO uint16_t *_TextColor, __IO uint16_t *_BackColor)
  502. {
  503. *_TextColor = TextColor; *_BackColor = BackColor;
  504. }
  505. /**
  506. * @brief Sets the Text color.
  507. * @param Color: specifies the Text color code RGB(5-6-5).
  508. * @retval None
  509. */
  510. void LCD_SetTextColor(__IO uint16_t Color)
  511. {
  512. TextColor = Color;
  513. }
  514. /**
  515. * @brief Sets the Background color.
  516. * @param Color: specifies the Background color code RGB(5-6-5).
  517. * @retval None
  518. */
  519. void LCD_SetBackColor(__IO uint16_t Color)
  520. {
  521. BackColor = Color;
  522. }
  523. /**
  524. * @brief Sets the Text Font.
  525. * @param fonts: specifies the font to be used.
  526. * @retval None
  527. */
  528. void LCD_SetFont(sFONT *fonts)
  529. {
  530. LCD_Currentfonts = fonts;
  531. }
  532. /**
  533. * @brief Gets the Text Font.
  534. * @param None.
  535. * @retval the used font.
  536. */
  537. sFONT *LCD_GetFont(void)
  538. {
  539. return LCD_Currentfonts;
  540. }
  541. /**
  542. * @brief Clears the selected line.
  543. * @param Line: the Line to be cleared.
  544. * This parameter can be one of the following values:
  545. * @arg Linex: where x can be 0..n
  546. * @retval None
  547. */
  548. void LCD_ClearLine(uint16_t Line)
  549. {
  550. uint16_t refcolumn = LCD_PIXEL_WIDTH - 1;
  551. if(LCDType == LCD_HX8347D)
  552. {
  553. refcolumn = 0;
  554. /* Send the string character by character on LCD */
  555. while ((LCD_PIXEL_WIDTH - (refcolumn & 0xFFFF)) >= LCD_Currentfonts->Width)
  556. {
  557. /* Display one character on LCD */
  558. LCD_DisplayChar(Line, refcolumn, ' ');
  559. /* Increment the column position by 16 */
  560. refcolumn += LCD_Currentfonts->Width;
  561. }
  562. }
  563. else
  564. {
  565. /* Send the string character by character on lCD */
  566. while (((refcolumn + 1) & 0xFFFF) >= LCD_Currentfonts->Width)
  567. {
  568. /* Display one character on LCD */
  569. LCD_DisplayChar(Line, refcolumn, ' ');
  570. /* Decrement the column position by 16 */
  571. refcolumn -= LCD_Currentfonts->Width;
  572. }
  573. }
  574. }
  575. /**
  576. * @brief Clears the hole LCD.
  577. * @param Color: the color of the background.
  578. * @retval None
  579. */
  580. void LCD_Clear(uint16_t Color)
  581. {
  582. uint32_t index = 0;
  583. if(LCDType == LCD_HX8347D)
  584. {
  585. LCD_SetCursor(0x00, 0x0000);
  586. }
  587. else
  588. {
  589. LCD_SetCursor(0x00, 0x013F);
  590. }
  591. LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */
  592. for(index = 0; index < 76800; index++)
  593. {
  594. LCD_WriteRAM(Color);
  595. }
  596. LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_SET);
  597. }
  598. /**
  599. * @brief Sets the cursor position.
  600. * @param Xpos: specifies the X position.
  601. * @param Ypos: specifies the Y position.
  602. * @retval None
  603. */
  604. void LCD_SetCursor(uint16_t Xpos, uint16_t Ypos)
  605. {
  606. if(LCDType == LCD_HX8347D)
  607. {
  608. LCD_WriteReg(LCD_REG_2, Ypos >> 8);
  609. LCD_WriteReg(LCD_REG_3, Ypos & 0xFF);
  610. LCD_WriteReg(LCD_REG_6, 0x00);
  611. LCD_WriteReg(LCD_REG_7, Xpos);
  612. }
  613. else
  614. {
  615. LCD_WriteReg(LCD_REG_32, Xpos);
  616. LCD_WriteReg(LCD_REG_33, Ypos);
  617. }
  618. }
  619. /**
  620. * @brief Draws a character on LCD.
  621. * @param Xpos: the Line where to display the character shape.
  622. * @param Ypos: start column address.
  623. * @param c: pointer to the character data.
  624. * @retval None
  625. */
  626. void LCD_DrawChar(uint16_t Xpos, uint16_t Ypos, const uint16_t *c)
  627. {
  628. uint32_t index = 0, i = 0;
  629. uint16_t Xaddress = 0;
  630. Xaddress = Xpos;
  631. LCD_SetCursor(Xaddress, Ypos);
  632. for(index = 0; index < LCD_Currentfonts->Height; index++)
  633. {
  634. LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */
  635. for(i = 0; i < LCD_Currentfonts->Width; i++)
  636. {
  637. if((((c[index] & ((0x80 << ((LCD_Currentfonts->Width / 12 ) * 8 ) ) >> i)) == 0x00) &&(LCD_Currentfonts->Width <= 12))||
  638. (((c[index] & (0x1 << i)) == 0x00)&&(LCD_Currentfonts->Width > 12 )))
  639. {
  640. LCD_WriteRAM(BackColor);
  641. }
  642. else
  643. {
  644. LCD_WriteRAM(TextColor);
  645. }
  646. }
  647. LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_SET);
  648. Xaddress++;
  649. LCD_SetCursor(Xaddress, Ypos);
  650. }
  651. }
  652. /**
  653. * @brief Displays one character (16dots width, 24dots height).
  654. * @param Line: the Line where to display the character shape .
  655. * This parameter can be one of the following values:
  656. * @arg Linex: where x can be 0..9
  657. * @param Column: start column address.
  658. * @param Ascii: character ascii code, must be between 0x20 and 0x7E.
  659. * @retval None
  660. */
  661. void LCD_DisplayChar(uint16_t Line, uint16_t Column, uint8_t Ascii)
  662. {
  663. Ascii -= 32;
  664. LCD_DrawChar(Line, Column, &LCD_Currentfonts->table[Ascii * LCD_Currentfonts->Height]);
  665. }
  666. /**
  667. * @brief Displays a maximum of 20 char on the LCD.
  668. * @param Line: the Line where to display the character shape .
  669. * This parameter can be one of the following values:
  670. * @arg Linex: where x can be 0..9
  671. * @param *ptr: pointer to string to display on LCD.
  672. * @retval None
  673. */
  674. void LCD_DisplayStringLine(uint16_t Line, uint8_t *ptr)
  675. {
  676. uint16_t refcolumn = LCD_PIXEL_WIDTH - 1;
  677. if(LCDType == LCD_HX8347D)
  678. {
  679. refcolumn = 0;
  680. /* Send the string character by character on LCD */
  681. while ((*ptr != 0) & ((LCD_PIXEL_WIDTH - (refcolumn & 0xFFFF)) >= LCD_Currentfonts->Width))
  682. {
  683. /* Display one character on LCD */
  684. LCD_DisplayChar(Line, refcolumn, *ptr);
  685. /* Increment the column position by 16 */
  686. refcolumn += LCD_Currentfonts->Width;
  687. /* Point on the next character */
  688. ptr++;
  689. }
  690. }
  691. else
  692. {
  693. /* Send the string character by character on lCD */
  694. while ((*ptr != 0) & (((refcolumn + 1) & 0xFFFF) >= LCD_Currentfonts->Width))
  695. {
  696. /* Display one character on LCD */
  697. LCD_DisplayChar(Line, refcolumn, *ptr);
  698. /* Decrement the column position by 16 */
  699. refcolumn -= LCD_Currentfonts->Width;
  700. /* Point on the next character */
  701. ptr++;
  702. }
  703. }
  704. }
  705. /**
  706. * @brief Sets a display window
  707. * @param Xpos: specifies the X buttom left position.
  708. * @param Ypos: specifies the Y buttom left position.
  709. * @param Height: display window height.
  710. * @param Width: display window width.
  711. * @retval None
  712. */
  713. void LCD_SetDisplayWindow(uint16_t Xpos, uint16_t Ypos, uint8_t Height, uint16_t Width)
  714. {
  715. if(LCDType == LCD_HX8347D)
  716. {
  717. LCD_WriteReg(LCD_REG_2, (319 - Ypos) >> 8); /* SC */
  718. LCD_WriteReg(LCD_REG_3, (319 - Ypos) & 0xFF); /* SC */
  719. LCD_WriteReg(LCD_REG_4, (319 - (Ypos - Width + 1)) >> 8); /* EC */
  720. LCD_WriteReg(LCD_REG_5, (319 - (Ypos - Width + 1)) & 0xFF); /* EC */
  721. LCD_WriteReg(LCD_REG_6, 0x0); /* SP */
  722. LCD_WriteReg(LCD_REG_7, (239 - Xpos) & 0xFF); /* SP */
  723. LCD_WriteReg(LCD_REG_8, 0x0); /* EP */
  724. LCD_WriteReg(LCD_REG_9, (239 - (Xpos - Height + 1)) & 0xFF); /* EP */
  725. LCD_SetCursor(Xpos, Ypos);
  726. }
  727. else
  728. {
  729. /* Horizontal GRAM Start Address */
  730. if(Xpos >= Height)
  731. {
  732. LCD_WriteReg(LCD_REG_80, (Xpos - Height + 1));
  733. }
  734. else
  735. {
  736. LCD_WriteReg(LCD_REG_80, 0);
  737. }
  738. /* Horizontal GRAM End Address */
  739. LCD_WriteReg(LCD_REG_81, Xpos);
  740. /* Vertical GRAM Start Address */
  741. if(Ypos >= Width)
  742. {
  743. LCD_WriteReg(LCD_REG_82, (Ypos - Width + 1));
  744. }
  745. else
  746. {
  747. LCD_WriteReg(LCD_REG_82, 0);
  748. }
  749. /* Vertical GRAM End Address */
  750. LCD_WriteReg(LCD_REG_83, Ypos);
  751. }
  752. LCD_SetCursor(Xpos, Ypos);
  753. }
  754. /**
  755. * @brief Disables LCD Window mode.
  756. * @param None
  757. * @retval None
  758. */
  759. void LCD_WindowModeDisable(void)
  760. {
  761. LCD_SetDisplayWindow(239, 0x13F, 240, 320);
  762. if(LCDType != LCD_HX8347D)
  763. {
  764. LCD_WriteReg(LCD_REG_3, 0x1018);
  765. }
  766. }
  767. /**
  768. * @brief Displays a line.
  769. * @param Xpos: specifies the X position.
  770. * @param Ypos: specifies the Y position.
  771. * @param Length: line length.
  772. * @param Direction: line direction.
  773. * This parameter can be one of the following values: Vertical or Horizontal.
  774. * @retval None
  775. */
  776. void LCD_DrawLine(uint16_t Xpos, uint16_t Ypos, uint16_t Length, uint8_t Direction)
  777. {
  778. uint32_t i = 0;
  779. if(LCDType == LCD_HX8347D)
  780. {
  781. Ypos = (LCD_PIXEL_WIDTH - 1) - Ypos;
  782. }
  783. LCD_SetCursor(Xpos, Ypos);
  784. if(Direction == LCD_DIR_HORIZONTAL)
  785. {
  786. LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */
  787. for(i = 0; i < Length; i++)
  788. {
  789. LCD_WriteRAM(TextColor);
  790. }
  791. LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_SET);
  792. }
  793. else
  794. {
  795. for(i = 0; i < Length; i++)
  796. {
  797. LCD_WriteRAMWord(TextColor);
  798. Xpos++;
  799. LCD_SetCursor(Xpos, Ypos);
  800. }
  801. }
  802. }
  803. /**
  804. * @brief Displays a rectangle.
  805. * @param Xpos: specifies the X position.
  806. * @param Ypos: specifies the Y position.
  807. * @param Height: display rectangle height.
  808. * @param Width: display rectangle width.
  809. * @retval None
  810. */
  811. void LCD_DrawRect(uint16_t Xpos, uint16_t Ypos, uint8_t Height, uint16_t Width)
  812. {
  813. LCD_DrawLine(Xpos, Ypos, Width, LCD_DIR_HORIZONTAL);
  814. LCD_DrawLine((Xpos + Height), Ypos, Width, LCD_DIR_HORIZONTAL);
  815. LCD_DrawLine(Xpos, Ypos, Height, LCD_DIR_VERTICAL);
  816. LCD_DrawLine(Xpos, (Ypos - Width + 1), Height, LCD_DIR_VERTICAL);
  817. }
  818. /**
  819. * @brief Displays a circle.
  820. * @param Xpos: specifies the X position.
  821. * @param Ypos: specifies the Y position.
  822. * @param Radius
  823. * @retval None
  824. */
  825. void LCD_DrawCircle(uint16_t Xpos, uint16_t Ypos, uint16_t Radius)
  826. {
  827. int32_t D;/* Decision Variable */
  828. uint32_t CurX;/* Current X Value */
  829. uint32_t CurY;/* Current Y Value */
  830. if(LCDType == LCD_HX8347D)
  831. {
  832. Ypos = (LCD_PIXEL_WIDTH - 1) - Ypos;
  833. }
  834. D = 3 - (Radius << 1);
  835. CurX = 0;
  836. CurY = Radius;
  837. while (CurX <= CurY)
  838. {
  839. LCD_SetCursor(Xpos + CurX, Ypos + CurY);
  840. LCD_WriteRAMWord(TextColor);
  841. LCD_SetCursor(Xpos + CurX, Ypos - CurY);
  842. LCD_WriteRAMWord(TextColor);
  843. LCD_SetCursor(Xpos - CurX, Ypos + CurY);
  844. LCD_WriteRAMWord(TextColor);
  845. LCD_SetCursor(Xpos - CurX, Ypos - CurY);
  846. LCD_WriteRAMWord(TextColor);
  847. LCD_SetCursor(Xpos + CurY, Ypos + CurX);
  848. LCD_WriteRAMWord(TextColor);
  849. LCD_SetCursor(Xpos + CurY, Ypos - CurX);
  850. LCD_WriteRAMWord(TextColor);
  851. LCD_SetCursor(Xpos - CurY, Ypos + CurX);
  852. LCD_WriteRAMWord(TextColor);
  853. LCD_SetCursor(Xpos - CurY, Ypos - CurX);
  854. LCD_WriteRAMWord(TextColor);
  855. if (D < 0)
  856. {
  857. D += (CurX << 2) + 6;
  858. }
  859. else
  860. {
  861. D += ((CurX - CurY) << 2) + 10;
  862. CurY--;
  863. }
  864. CurX++;
  865. }
  866. }
  867. /**
  868. * @brief Displays a monocolor picture.
  869. * @param Pict: pointer to the picture array.
  870. * @retval None
  871. */
  872. void LCD_DrawMonoPict(const uint32_t *Pict)
  873. {
  874. uint32_t index = 0, i = 0;
  875. LCD_SetCursor(0, (LCD_PIXEL_WIDTH - 1));
  876. if(LCDType == LCD_HX8347D)
  877. {
  878. LCD_SetCursor(0, 0);
  879. }
  880. else
  881. {
  882. LCD_SetCursor(0, (LCD_PIXEL_WIDTH - 1));
  883. }
  884. LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */
  885. for(index = 0; index < 2400; index++)
  886. {
  887. for(i = 0; i < 32; i++)
  888. {
  889. if((Pict[index] & (1 << i)) == 0x00)
  890. {
  891. LCD_WriteRAM(BackColor);
  892. }
  893. else
  894. {
  895. LCD_WriteRAM(TextColor);
  896. }
  897. }
  898. }
  899. LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_SET);
  900. }
  901. #ifdef USE_LCD_DrawBMP
  902. /**
  903. * @brief Displays a bitmap picture loaded in the SPI Flash.
  904. * @param BmpAddress: Bmp picture address in the SPI Flash.
  905. * @retval None
  906. */
  907. void LCD_DrawBMP(uint32_t BmpAddress)
  908. {
  909. uint32_t i = 0, size = 0;
  910. /* Read bitmap size */
  911. sFLASH_ReadBuffer((uint8_t*)&size, BmpAddress + 2, 4);
  912. /* get bitmap data address offset */
  913. sFLASH_ReadBuffer((uint8_t*)&i, BmpAddress + 10, 4);
  914. size = (size - i)/2;
  915. sFLASH_StartReadSequence(BmpAddress + i);
  916. /* Disable LCD_SPI */
  917. SPI_Cmd(LCD_SPI, DISABLE);
  918. /* SPI in 16-bit mode */
  919. /* Configure the RX FIFO Threshold to Half Full */
  920. SPI_RxFIFOThresholdConfig(LCD_SPI, SPI_RxFIFOThreshold_HF);
  921. SPI_DataSizeConfig(LCD_SPI, SPI_DataSize_16b);
  922. /* Enable LCD_SPI */
  923. SPI_Cmd(LCD_SPI, ENABLE);
  924. if((LCDType == LCD_ILI9320) || (LCDType == LCD_SPFD5408))
  925. {
  926. /* Set GRAM write direction and BGR = 1 */
  927. /* I/D=00 (Horizontal : decrement, Vertical : decrement) */
  928. /* AM=1 (address is updated in vertical writing direction) */
  929. LCD_WriteReg(LCD_REG_3, 0x1008);
  930. LCD_WriteRAM_Prepare(); /* Prepare to write GRAM */
  931. }
  932. /* Read bitmap data from SPI Flash and send them to LCD */
  933. for(i = 0; i < size; i++)
  934. {
  935. LCD_WriteRAM(__REV16(sFLASH_SendHalfWord(0xA5A5)));
  936. }
  937. if((LCDType == LCD_ILI9320) || (LCDType == LCD_SPFD5408))
  938. {
  939. LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_SET);
  940. }
  941. /* Deselect the FLASH: Chip Select high */
  942. sFLASH_CS_HIGH();
  943. /* Disable LCD_SPI */
  944. SPI_Cmd(LCD_SPI, DISABLE);
  945. /* SPI in 8-bit mode */
  946. /* Configure the RX FIFO Threshold to Quarter Full */
  947. SPI_RxFIFOThresholdConfig(LCD_SPI, SPI_RxFIFOThreshold_QF);
  948. SPI_DataSizeConfig(LCD_SPI, SPI_DataSize_8b);
  949. /* Enable LCD_SPI */
  950. SPI_Cmd(LCD_SPI, ENABLE);
  951. if((LCDType == LCD_ILI9320) || (LCDType == LCD_SPFD5408))
  952. {
  953. /* Set GRAM write direction and BGR = 1 */
  954. /* I/D = 01 (Horizontal : increment, Vertical : decrement) */
  955. /* AM = 1 (address is updated in vertical writing direction) */
  956. LCD_WriteReg(LCD_REG_3, 0x1018);
  957. }
  958. }
  959. #endif /* USE_LCD_DrawBMP */
  960. /**
  961. * @brief Displays a full rectangle.
  962. * @param Xpos: specifies the X position.
  963. * @param Ypos: specifies the Y position.
  964. * @param Height: rectangle height.
  965. * @param Width: rectangle width.
  966. * @retval None
  967. */
  968. void LCD_DrawFullRect(uint16_t Xpos, uint16_t Ypos, uint16_t Width, uint16_t Height)
  969. {
  970. LCD_SetTextColor(TextColor);
  971. LCD_DrawLine(Xpos, Ypos, Width, LCD_DIR_HORIZONTAL);
  972. LCD_DrawLine((Xpos + Height), Ypos, Width, LCD_DIR_HORIZONTAL);
  973. LCD_DrawLine(Xpos, Ypos, Height, LCD_DIR_VERTICAL);
  974. LCD_DrawLine(Xpos, (Ypos - Width + 1), Height, LCD_DIR_VERTICAL);
  975. Width -= 2;
  976. Height--;
  977. Ypos--;
  978. LCD_SetTextColor(BackColor);
  979. while(Height--)
  980. {
  981. LCD_DrawLine(++Xpos, Ypos, Width, LCD_DIR_HORIZONTAL);
  982. }
  983. LCD_SetTextColor(TextColor);
  984. }
  985. /**
  986. * @brief Displays a full circle.
  987. * @param Xpos: specifies the X position.
  988. * @param Ypos: specifies the Y position.
  989. * @param Radius
  990. * @retval None
  991. */
  992. void LCD_DrawFullCircle(uint16_t Xpos, uint16_t Ypos, uint16_t Radius)
  993. {
  994. int32_t D; /* Decision Variable */
  995. uint32_t CurX;/* Current X Value */
  996. uint32_t CurY;/* Current Y Value */
  997. D = 3 - (Radius << 1);
  998. CurX = 0;
  999. CurY = Radius;
  1000. LCD_SetTextColor(BackColor);
  1001. while (CurX <= CurY)
  1002. {
  1003. if(CurY > 0)
  1004. {
  1005. LCD_DrawLine(Xpos - CurX, Ypos + CurY, 2*CurY, LCD_DIR_HORIZONTAL);
  1006. LCD_DrawLine(Xpos + CurX, Ypos + CurY, 2*CurY, LCD_DIR_HORIZONTAL);
  1007. }
  1008. if(CurX > 0)
  1009. {
  1010. LCD_DrawLine(Xpos - CurY, Ypos + CurX, 2*CurX, LCD_DIR_HORIZONTAL);
  1011. LCD_DrawLine(Xpos + CurY, Ypos + CurX, 2*CurX, LCD_DIR_HORIZONTAL);
  1012. }
  1013. if (D < 0)
  1014. {
  1015. D += (CurX << 2) + 6;
  1016. }
  1017. else
  1018. {
  1019. D += ((CurX - CurY) << 2) + 10;
  1020. CurY--;
  1021. }
  1022. CurX++;
  1023. }
  1024. LCD_SetTextColor(TextColor);
  1025. LCD_DrawCircle(Xpos, Ypos, Radius);
  1026. }
  1027. /**
  1028. * @brief Displays an uni line (between two points).
  1029. * @param x1: specifies the point 1 x position.
  1030. * @param y1: specifies the point 1 y position.
  1031. * @param x2: specifies the point 2 x position.
  1032. * @param y2: specifies the point 2 y position.
  1033. * @retval None
  1034. */
  1035. void LCD_DrawUniLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
  1036. {
  1037. int16_t deltax = 0, deltay = 0, x = 0, y = 0, xinc1 = 0, xinc2 = 0,
  1038. yinc1 = 0, yinc2 = 0, den = 0, num = 0, numadd = 0, numpixels = 0,
  1039. curpixel = 0;
  1040. deltax = ABS(x2 - x1); /* The difference between the x's */
  1041. deltay = ABS(y2 - y1); /* The difference between the y's */
  1042. x = x1; /* Start x off at the first pixel */
  1043. y = y1; /* Start y off at the first pixel */
  1044. if (x2 >= x1) /* The x-values are increasing */
  1045. {
  1046. xinc1 = 1;
  1047. xinc2 = 1;
  1048. }
  1049. else /* The x-values are decreasing */
  1050. {
  1051. xinc1 = -1;
  1052. xinc2 = -1;
  1053. }
  1054. if (y2 >= y1) /* The y-values are increasing */
  1055. {
  1056. yinc1 = 1;
  1057. yinc2 = 1;
  1058. }
  1059. else /* The y-values are decreasing */
  1060. {
  1061. yinc1 = -1;
  1062. yinc2 = -1;
  1063. }
  1064. if (deltax >= deltay) /* There is at least one x-value for every y-value */
  1065. {
  1066. xinc1 = 0; /* Don't change the x when numerator >= denominator */
  1067. yinc2 = 0; /* Don't change the y for every iteration */
  1068. den = deltax;
  1069. num = deltax / 2;
  1070. numadd = deltay;
  1071. numpixels = deltax; /* There are more x-values than y-values */
  1072. }
  1073. else /* There is at least one y-value for every x-value */
  1074. {
  1075. xinc2 = 0; /* Don't change the x for every iteration */
  1076. yinc1 = 0; /* Don't change the y when numerator >= denominator */
  1077. den = deltay;
  1078. num = deltay / 2;
  1079. numadd = deltax;
  1080. numpixels = deltay; /* There are more y-values than x-values */
  1081. }
  1082. for (curpixel = 0; curpixel <= numpixels; curpixel++)
  1083. {
  1084. PutPixel(x, y); /* Draw the current pixel */
  1085. num += numadd; /* Increase the numerator by the top of the fraction */
  1086. if (num >= den) /* Check if numerator >= denominator */
  1087. {
  1088. num -= den; /* Calculate the new numerator value */
  1089. x += xinc1; /* Change the x as appropriate */
  1090. y += yinc1; /* Change the y as appropriate */
  1091. }
  1092. x += xinc2; /* Change the x as appropriate */
  1093. y += yinc2; /* Change the y as appropriate */
  1094. }
  1095. }
  1096. /**
  1097. * @brief Displays an polyline (between many points).
  1098. * @param Points: pointer to the points array.
  1099. * @param PointCount: Number of points.
  1100. * @retval None
  1101. */
  1102. void LCD_PolyLine(pPoint Points, uint16_t PointCount)
  1103. {
  1104. int16_t X = 0, Y = 0;
  1105. if(PointCount < 2)
  1106. {
  1107. return;
  1108. }
  1109. while(--PointCount)
  1110. {
  1111. X = Points->X;
  1112. Y = Points->Y;
  1113. Points++;
  1114. LCD_DrawUniLine(X, Y, Points->X, Points->Y);
  1115. }
  1116. }
  1117. /**
  1118. * @brief Displays an relative polyline (between many points).
  1119. * @param Points: pointer to the points array.
  1120. * @param PointCount: Number of points.
  1121. * @param Closed: specifies if the draw is closed or not.
  1122. * 1: closed, 0 : not closed.
  1123. * @retval None
  1124. */
  1125. static void LCD_PolyLineRelativeClosed(pPoint Points, uint16_t PointCount, uint16_t Closed)
  1126. {
  1127. int16_t X = 0, Y = 0;
  1128. pPoint First = Points;
  1129. if(PointCount < 2)
  1130. {
  1131. return;
  1132. }
  1133. X = Points->X;
  1134. Y = Points->Y;
  1135. while(--PointCount)
  1136. {
  1137. Points++;
  1138. LCD_DrawUniLine(X, Y, X + Points->X, Y + Points->Y);
  1139. X = X + Points->X;
  1140. Y = Y + Points->Y;
  1141. }
  1142. if(Closed)
  1143. {
  1144. LCD_DrawUniLine(First->X, First->Y, X, Y);
  1145. }
  1146. }
  1147. /**
  1148. * @brief Displays a closed polyline (between many points).
  1149. * @param Points: pointer to the points array.
  1150. * @param PointCount: Number of points.
  1151. * @retval None
  1152. */
  1153. void LCD_ClosedPolyLine(pPoint Points, uint16_t PointCount)
  1154. {
  1155. LCD_PolyLine(Points, PointCount);
  1156. LCD_DrawUniLine(Points->X, Points->Y, (Points+PointCount-1)->X, (Points+PointCount-1)->Y);
  1157. }
  1158. /**
  1159. * @brief Displays a relative polyline (between many points).
  1160. * @param Points: pointer to the points array.
  1161. * @param PointCount: Number of points.
  1162. * @retval None
  1163. */
  1164. void LCD_PolyLineRelative(pPoint Points, uint16_t PointCount)
  1165. {
  1166. LCD_PolyLineRelativeClosed(Points, PointCount, 0);
  1167. }
  1168. /**
  1169. * @brief Displays a closed relative polyline (between many points).
  1170. * @param Points: pointer to the points array.
  1171. * @param PointCount: Number of points.
  1172. * @retval None
  1173. */
  1174. void LCD_ClosedPolyLineRelative(pPoint Points, uint16_t PointCount)
  1175. {
  1176. LCD_PolyLineRelativeClosed(Points, PointCount, 1);
  1177. }
  1178. /**
  1179. * @brief Displays a full polyline (between many points).
  1180. * @param Points: pointer to the points array.
  1181. * @param PointCount: Number of points.
  1182. * @retval None
  1183. */
  1184. void LCD_FillPolyLine(pPoint Points, uint16_t PointCount)
  1185. {
  1186. /* public-domain code by Darel Rex Finley, 2007 */
  1187. uint16_t nodes = 0, nodeX[MAX_POLY_CORNERS], pixelX = 0, pixelY = 0, i = 0,
  1188. j = 0, swap = 0;
  1189. uint16_t IMAGE_LEFT = 0, IMAGE_RIGHT = 0, IMAGE_TOP = 0, IMAGE_BOTTOM = 0;
  1190. IMAGE_LEFT = IMAGE_RIGHT = Points->X;
  1191. IMAGE_TOP= IMAGE_BOTTOM = Points->Y;
  1192. for(i = 1; i < PointCount; i++)
  1193. {
  1194. pixelX = POLY_X(i);
  1195. if(pixelX < IMAGE_LEFT)
  1196. {
  1197. IMAGE_LEFT = pixelX;
  1198. }
  1199. if(pixelX > IMAGE_RIGHT)
  1200. {
  1201. IMAGE_RIGHT = pixelX;
  1202. }
  1203. pixelY = POLY_Y(i);
  1204. if(pixelY < IMAGE_TOP)
  1205. {
  1206. IMAGE_TOP = pixelY;
  1207. }
  1208. if(pixelY > IMAGE_BOTTOM)
  1209. {
  1210. IMAGE_BOTTOM = pixelY;
  1211. }
  1212. }
  1213. LCD_SetTextColor(BackColor);
  1214. /* Loop through the rows of the image. */
  1215. for (pixelY = IMAGE_TOP; pixelY < IMAGE_BOTTOM; pixelY++)
  1216. {
  1217. /* Build a list of nodes. */
  1218. nodes = 0; j = PointCount-1;
  1219. for (i = 0; i < PointCount; i++)
  1220. {
  1221. if (((POLY_Y(i)<(double) pixelY) && (POLY_Y(j)>=(double) pixelY)) || \
  1222. ((POLY_Y(j)<(double) pixelY) && (POLY_Y(i)>=(double) pixelY)))
  1223. {
  1224. nodeX[nodes++]=(int) (POLY_X(i)+((pixelY-POLY_Y(i))*(POLY_X(j)-POLY_X(i)))/(POLY_Y(j)-POLY_Y(i)));
  1225. }
  1226. j = i;
  1227. }
  1228. /* Sort the nodes, via a simple "Bubble" sort. */
  1229. i = 0;
  1230. while (i < nodes-1)
  1231. {
  1232. if (nodeX[i]>nodeX[i+1])
  1233. {
  1234. swap = nodeX[i];
  1235. nodeX[i] = nodeX[i+1];
  1236. nodeX[i+1] = swap;
  1237. if(i)
  1238. {
  1239. i--;
  1240. }
  1241. }
  1242. else
  1243. {
  1244. i++;
  1245. }
  1246. }
  1247. /* Fill the pixels between node pairs. */
  1248. for (i = 0; i < nodes; i+=2)
  1249. {
  1250. if(nodeX[i] >= IMAGE_RIGHT)
  1251. {
  1252. break;
  1253. }
  1254. if(nodeX[i+1] > IMAGE_LEFT)
  1255. {
  1256. if (nodeX[i] < IMAGE_LEFT)
  1257. {
  1258. nodeX[i]=IMAGE_LEFT;
  1259. }
  1260. if(nodeX[i+1] > IMAGE_RIGHT)
  1261. {
  1262. nodeX[i+1] = IMAGE_RIGHT;
  1263. }
  1264. LCD_SetTextColor(BackColor);
  1265. LCD_DrawLine(pixelY, nodeX[i+1], nodeX[i+1] - nodeX[i], LCD_DIR_HORIZONTAL);
  1266. LCD_SetTextColor(TextColor);
  1267. PutPixel(pixelY, nodeX[i+1]);
  1268. PutPixel(pixelY, nodeX[i]);
  1269. /* for (j=nodeX[i]; j<nodeX[i+1]; j++) PutPixel(j,pixelY); */
  1270. }
  1271. }
  1272. }
  1273. /* draw the edges */
  1274. LCD_SetTextColor(TextColor);
  1275. }
  1276. /**
  1277. * @brief Reset LCD control line(/CS) and Send Start-Byte
  1278. * @param Start_Byte: the Start-Byte to be sent
  1279. * @retval None
  1280. */
  1281. void LCD_nCS_StartByte(uint8_t Start_Byte)
  1282. {
  1283. LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_RESET);
  1284. SPI_SendData8(LCD_SPI, Start_Byte);
  1285. while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_BSY) != RESET)
  1286. {
  1287. }
  1288. }
  1289. /**
  1290. * @brief Writes index to select the LCD register.
  1291. * @param LCD_Reg: address of the selected register.
  1292. * @retval None
  1293. */
  1294. void LCD_WriteRegIndex(uint8_t LCD_Reg)
  1295. {
  1296. /* Reset LCD control line(/CS) and Send Start-Byte */
  1297. LCD_nCS_StartByte(START_BYTE | SET_INDEX);
  1298. /* Write 16-bit Reg Index (High Byte is 0) */
  1299. SPI_SendData8(LCD_SPI, 0x00);
  1300. while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_BSY) != RESET)
  1301. {
  1302. }
  1303. SPI_SendData8(LCD_SPI, LCD_Reg);
  1304. while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_BSY) != RESET)
  1305. {
  1306. }
  1307. LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_SET);
  1308. }
  1309. /**
  1310. * @brief Reads the selected LCD Register.
  1311. * @param LCD_Reg: address of the selected register.
  1312. * @retval LCD Register Value.
  1313. */
  1314. uint16_t LCD_ReadReg(uint8_t LCD_Reg)
  1315. {
  1316. // uint16_t tmp = 0;
  1317. // uint8_t i = 0;
  1318. //
  1319. // // Write 16-bit Index (then Read Reg)
  1320. // LCD_WriteRegIndex(LCD_Reg);
  1321. //
  1322. // // Read 16-bit Reg //
  1323. // // Reset LCD control line(/CS) and Send Start-Byte
  1324. // LCD_nCS_StartByte(0x73);
  1325. // for(i=0;i<5;i++)
  1326. // {
  1327. // while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_TXE) == RESET);
  1328. // SPI_SendData8(LCD_SPI, 0xFF);
  1329. // }
  1330. // // One byte of invalid dummy data read after the start byte
  1331. // while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_RXNE)== SET)
  1332. // SPI_ReceiveData8(LCD_SPI);
  1333. //
  1334. // // trial for HX8347-D
  1335. // LCD_SPI->CR1 &= ~SPI_CR1_BIDIOE;
  1336. // // Read upper byte
  1337. // while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_TXE) == RESET);
  1338. // SPI_SendData8(LCD_SPI, 0xFF);
  1339. // while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_TXE) == RESET);
  1340. //// while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_RXNE)== RESET);
  1341. // tmp = SPI_ReceiveData8(LCD_SPI);
  1342. // // Read lower byte
  1343. // while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_TXE) == RESET);
  1344. // SPI_SendData8(LCD_SPI, 0xFF);
  1345. // while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_TXE) == RESET);
  1346. //// while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_RXNE)== RESET);
  1347. // tmp = ((tmp & 0xFF) << 8) | SPI_ReceiveData8(LCD_SPI);
  1348. //
  1349. // // trial for HX8347-D
  1350. // LCD_SPI->CR1 |= SPI_CR1_BIDIOE;
  1351. //
  1352. // LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_SET);
  1353. // return tmp;
  1354. uint16_t tmp = 0;
  1355. uint8_t i = 0;
  1356. /* LCD_SPI prescaler: 4 */
  1357. LCD_SPI->CR1 &= 0xFFC7;
  1358. LCD_SPI->CR1 |= 0x0008;
  1359. /* Write 16-bit Index (then Read Reg) */
  1360. LCD_WriteRegIndex(LCD_Reg);
  1361. /* Read 16-bit Reg */
  1362. /* Reset LCD control line(/CS) and Send Start-Byte */
  1363. LCD_nCS_StartByte(START_BYTE | LCD_READ_REG);
  1364. for(i = 0; i < 5; i++)
  1365. {
  1366. SPI_SendData8(LCD_SPI, 0xFF);
  1367. while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_BSY) != RESET)
  1368. {
  1369. }
  1370. /* One byte of invalid dummy data read after the start byte */
  1371. while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_RXNE) == RESET)
  1372. {
  1373. }
  1374. SPI_ReceiveData8(LCD_SPI);
  1375. }
  1376. SPI_SendData8(LCD_SPI, 0xFF);
  1377. /* Read upper byte */
  1378. while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_BSY) != RESET)
  1379. {
  1380. }
  1381. /* Read lower byte */
  1382. while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_RXNE) == RESET)
  1383. {
  1384. }
  1385. SPI_SendData8(LCD_SPI, 0xFF);
  1386. while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_BSY) != RESET)
  1387. {
  1388. }
  1389. /* Read lower byte */
  1390. while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_RXNE) == RESET)
  1391. {
  1392. }
  1393. tmp = SPI_I2S_ReceiveData16(LCD_SPI);
  1394. LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_SET);
  1395. /* LCD_SPI prescaler: 2 */
  1396. LCD_SPI->CR1 &= 0xFFC7;
  1397. return tmp;
  1398. }
  1399. /**
  1400. * @brief Prepare to write to the LCD RAM.
  1401. * @param None
  1402. * @retval None
  1403. */
  1404. void LCD_WriteRAM_Prepare(void)
  1405. {
  1406. LCD_WriteRegIndex(LCD_REG_34); /* Select GRAM Reg */
  1407. /* Reset LCD control line(/CS) and Send Start-Byte */
  1408. LCD_nCS_StartByte(START_BYTE | LCD_WRITE_REG);
  1409. }
  1410. /**
  1411. * @brief Writes 1 word to the LCD RAM.
  1412. * @param RGB_Code: the pixel color in RGB mode (5-6-5).
  1413. * @retval None
  1414. */
  1415. void LCD_WriteRAMWord(uint16_t RGB_Code)
  1416. {
  1417. LCD_WriteRAM_Prepare();
  1418. LCD_WriteRAM(RGB_Code);
  1419. LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_SET);
  1420. }
  1421. /**
  1422. * @brief Writes to the selected LCD register.
  1423. * @param LCD_Reg: address of the selected register.
  1424. * @param LCD_RegValue: value to write to the selected register.
  1425. * @retval None
  1426. */
  1427. void LCD_WriteReg(uint8_t LCD_Reg, uint16_t LCD_RegValue)
  1428. {
  1429. /* Write 16-bit Index (then Write Reg) */
  1430. LCD_WriteRegIndex(LCD_Reg);
  1431. /* Write 16-bit Reg */
  1432. /* Reset LCD control line(/CS) and Send Start-Byte */
  1433. LCD_nCS_StartByte(START_BYTE | LCD_WRITE_REG);
  1434. SPI_SendData8(LCD_SPI, LCD_RegValue>>8);
  1435. while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_BSY) != RESET)
  1436. {
  1437. }
  1438. SPI_SendData8(LCD_SPI, (LCD_RegValue & 0xFF));
  1439. while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_BSY) != RESET)
  1440. {
  1441. }
  1442. LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_SET);
  1443. }
  1444. /**
  1445. * @brief Writes to the LCD RAM.
  1446. * @param RGB_Code: the pixel color in RGB mode (5-6-5).
  1447. * @retval None
  1448. */
  1449. void LCD_WriteRAM(uint16_t RGB_Code)
  1450. {
  1451. SPI_SendData8(LCD_SPI, RGB_Code >> 8);
  1452. while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_BSY) != RESET)
  1453. {
  1454. }
  1455. SPI_SendData8(LCD_SPI, RGB_Code & 0xFF);
  1456. while(SPI_I2S_GetFlagStatus(LCD_SPI, SPI_I2S_FLAG_BSY) != RESET)
  1457. {
  1458. }
  1459. }
  1460. /**
  1461. * @brief Power on the LCD.
  1462. * @param None
  1463. * @retval None
  1464. */
  1465. void LCD_PowerOn(void)
  1466. {
  1467. /* Power On sequence ---------------------------------------------------------*/
  1468. LCD_WriteReg(LCD_REG_16, 0x0000); /* SAP, BT[3:0], AP, DSTB, SLP, STB */
  1469. LCD_WriteReg(LCD_REG_17, 0x0000); /* DC1[2:0], DC0[2:0], VC[2:0] */
  1470. LCD_WriteReg(LCD_REG_18, 0x0000); /* VREG1OUT voltage */
  1471. LCD_WriteReg(LCD_REG_19, 0x0000); /* VDV[4:0] for VCOM amplitude */
  1472. _delay_(20); /* Dis-charge capacitor power voltage (200ms) */
  1473. LCD_WriteReg(LCD_REG_16, 0x17B0); /* SAP, BT[3:0], AP, DSTB, SLP, STB */
  1474. LCD_WriteReg(LCD_REG_17, 0x0137); /* DC1[2:0], DC0[2:0], VC[2:0] */
  1475. _delay_(5); /* Delay 50 ms */
  1476. LCD_WriteReg(LCD_REG_18, 0x0139); /* VREG1OUT voltage */
  1477. _delay_(5); /* delay 50 ms */
  1478. LCD_WriteReg(LCD_REG_19, 0x1d00); /* VDV[4:0] for VCOM amplitude */
  1479. LCD_WriteReg(LCD_REG_41, 0x0013); /* VCM[4:0] for VCOMH */
  1480. _delay_(5); /* delay 50 ms */
  1481. LCD_WriteReg(LCD_REG_7, 0x0173); /* 262K color and display ON */
  1482. }
  1483. /**
  1484. * @brief Enables the Display.
  1485. * @param None
  1486. * @retval None
  1487. */
  1488. void LCD_DisplayOn(void)
  1489. {
  1490. if(LCDType == LCD_HX8347D)
  1491. {
  1492. LCD_WriteReg(LCD_REG_40, 0x38);
  1493. _delay_(6);
  1494. LCD_WriteReg(LCD_REG_40, 0x3C);
  1495. }
  1496. else
  1497. {
  1498. /* Display On */
  1499. LCD_WriteReg(LCD_REG_7, 0x0173); /* 262K color and display ON */
  1500. }
  1501. }
  1502. /**
  1503. * @brief Disables the Display.
  1504. * @param None
  1505. * @retval None
  1506. */
  1507. void LCD_DisplayOff(void)
  1508. {
  1509. if(LCDType == LCD_HX8347D)
  1510. {
  1511. LCD_WriteReg(LCD_REG_40, 0x38);
  1512. _delay_(6);
  1513. LCD_WriteReg(LCD_REG_40, 0x04);
  1514. }
  1515. else
  1516. {
  1517. /* Display Off */
  1518. LCD_WriteReg(LCD_REG_7, 0x0);
  1519. }
  1520. }
  1521. /**
  1522. * @brief Configures LCD control lines in Output Push-Pull mode.
  1523. * @param None
  1524. * @retval None
  1525. */
  1526. void LCD_CtrlLinesConfig(void)
  1527. {
  1528. GPIO_InitTypeDef GPIO_InitStructure;
  1529. RCC_AHBPeriphClockCmd(LCD_NCS_GPIO_CLK, ENABLE);
  1530. /* Configure NCS (PF.02) in Output Push-Pull mode */
  1531. GPIO_InitStructure.GPIO_Pin = LCD_NCS_PIN;
  1532. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  1533. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  1534. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  1535. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  1536. GPIO_Init(LCD_NCS_GPIO_PORT, &GPIO_InitStructure);
  1537. LCD_CtrlLinesWrite(LCD_NCS_GPIO_PORT, LCD_NCS_PIN, Bit_SET);
  1538. /* Configure NWR(RNW), RS in Output Push-Pull mode */
  1539. GPIO_InitStructure.GPIO_Pin = LCD_RS_PIN;
  1540. GPIO_Init(LCD_RS_GPIO_PORT, &GPIO_InitStructure);
  1541. GPIO_InitStructure.GPIO_Pin = LCD_NWR_PIN;
  1542. GPIO_Init(LCD_NWR_GPIO_PORT, &GPIO_InitStructure);
  1543. LCD_CtrlLinesWrite(LCD_NWR_GPIO_PORT, LCD_NWR_PIN, Bit_SET);
  1544. LCD_CtrlLinesWrite(LCD_RS_GPIO_PORT, LCD_RS_PIN, Bit_SET);
  1545. }
  1546. /**
  1547. * @brief Sets or reset LCD control lines.
  1548. * @param GPIOx: where x can be B or D to select the GPIO peripheral.
  1549. * @param CtrlPins: the Control line.
  1550. * This parameter can be:
  1551. * @arg LCD_NCS_PIN: Chip Select pin
  1552. * @arg LCD_NWR_PIN: Read/Write Selection pin
  1553. * @arg LCD_RS_PIN: Register/RAM Selection pin
  1554. * @param BitVal: specifies the value to be written to the selected bit.
  1555. * This parameter can be:
  1556. * @arg Bit_RESET: to clear the port pin
  1557. * @arg Bit_SET: to set the port pin
  1558. * @retval None
  1559. */
  1560. void LCD_CtrlLinesWrite(GPIO_TypeDef* GPIOx, uint16_t CtrlPins, BitAction BitVal)
  1561. {
  1562. /* Set or Reset the control line */
  1563. GPIO_WriteBit(GPIOx, CtrlPins, BitVal);
  1564. }
  1565. /**
  1566. * @brief Configures the LCD_SPI interface.
  1567. * @param None
  1568. * @retval None
  1569. */
  1570. void LCD_SPIConfig(void)
  1571. {
  1572. SPI_InitTypeDef SPI_InitStructure;
  1573. GPIO_InitTypeDef GPIO_InitStructure;
  1574. /* Enable LCD_SPI_SCK_GPIO_CLK, LCD_SPI_MISO_GPIO_CLK and LCD_SPI_MOSI_GPIO_CLK clock */
  1575. RCC_AHBPeriphClockCmd(LCD_SPI_SCK_GPIO_CLK | LCD_SPI_MISO_GPIO_CLK | LCD_SPI_MOSI_GPIO_CLK, ENABLE);
  1576. /* Enable SPI and SYSCFG clock */
  1577. RCC_APB2PeriphClockCmd(LCD_SPI_CLK | RCC_APB2Periph_SYSCFG, ENABLE);
  1578. /* Configure LCD_SPI SCK pin */
  1579. GPIO_InitStructure.GPIO_Pin = LCD_SPI_SCK_PIN;
  1580. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  1581. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  1582. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  1583. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  1584. GPIO_Init(LCD_SPI_SCK_GPIO_PORT, &GPIO_InitStructure);
  1585. /* Configure LCD_SPI MISO pin */
  1586. GPIO_InitStructure.GPIO_Pin = LCD_SPI_MISO_PIN;
  1587. GPIO_Init(LCD_SPI_MISO_GPIO_PORT, &GPIO_InitStructure);
  1588. /* Configure LCD_SPI MOSI pin */
  1589. GPIO_InitStructure.GPIO_Pin = LCD_SPI_MOSI_PIN;
  1590. GPIO_Init(LCD_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure);
  1591. /* Connect PE.13 to SPI SCK */
  1592. GPIO_PinAFConfig(LCD_SPI_SCK_GPIO_PORT, LCD_SPI_SCK_SOURCE, LCD_SPI_SCK_AF);
  1593. /* Connect PE.14 to SPI MISO */
  1594. GPIO_PinAFConfig(LCD_SPI_MISO_GPIO_PORT, LCD_SPI_MISO_SOURCE, LCD_SPI_MISO_AF);
  1595. /* Connect PE.15 to SPI MOSI */
  1596. GPIO_PinAFConfig(LCD_SPI_MOSI_GPIO_PORT, LCD_SPI_MOSI_SOURCE, LCD_SPI_MOSI_AF);
  1597. SPI_I2S_DeInit(LCD_SPI);
  1598. /* SPI Config */
  1599. SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  1600. SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  1601. SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  1602. SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
  1603. SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
  1604. SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  1605. SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
  1606. SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  1607. SPI_InitStructure.SPI_CRCPolynomial = 7;
  1608. SPI_Init(LCD_SPI, &SPI_InitStructure);
  1609. /* Configure the RX FIFO Threshold to Quarter Full */
  1610. SPI_RxFIFOThresholdConfig(LCD_SPI, SPI_RxFIFOThreshold_QF);
  1611. /* SPI enable */
  1612. SPI_Cmd(LCD_SPI, ENABLE);
  1613. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  1614. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  1615. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  1616. GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
  1617. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  1618. GPIO_Init(GPIOB, &GPIO_InitStructure);
  1619. GPIOB->ODR |= GPIO_Pin_2;
  1620. }
  1621. /**
  1622. * @brief Displays a pixel.
  1623. * @param x: pixel x.
  1624. * @param y: pixel y.
  1625. * @retval None
  1626. */
  1627. static void PutPixel(int16_t x, int16_t y)
  1628. {
  1629. if(x < 0 || x > 239 || y < 0 || y > 319)
  1630. {
  1631. return;
  1632. }
  1633. LCD_DrawLine(x, y, 1, LCD_DIR_HORIZONTAL);
  1634. }
  1635. #ifndef USE_Delay
  1636. /**
  1637. * @brief Inserts a delay time.
  1638. * @param nCount: specifies the delay time length.
  1639. * @retval None
  1640. */
  1641. static void delay(__IO uint32_t nCount)
  1642. {
  1643. __IO uint32_t index = 0;
  1644. for(index = (34000 * nCount); index != 0; index--)
  1645. {
  1646. }
  1647. }
  1648. #endif /* USE_Delay*/
  1649. /**
  1650. * @}
  1651. */
  1652. /**
  1653. * @}
  1654. */
  1655. /**
  1656. * @}
  1657. */
  1658. /**
  1659. * @}
  1660. */
  1661. /**
  1662. * @}
  1663. */
  1664. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/