gd32f450z_lcd_eval.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*!
  2. \file gd32f450z_lcd_eval.c
  3. \brief firmware functions to manage LCD
  4. */
  5. /*
  6. Copyright (C) 2016 GigaDevice
  7. 2016-10-19, V1.0.0, firmware for GD32F450Z
  8. */
  9. #include "gd32f450z_lcd_eval.h"
  10. static void delay(uint32_t time);
  11. /*!
  12. \brief enable the LCD
  13. \param[in] none
  14. \param[out] none
  15. \retval none
  16. */
  17. void lcd_enable(void)
  18. {
  19. gpio_bit_set(LCD_CS_GPIO_PORT, LCD_CS_PIN);
  20. }
  21. /*!
  22. \brief disable the LCD
  23. \param[in] none
  24. \param[out] none
  25. \retval none
  26. */
  27. void lcd_disable(void)
  28. {
  29. gpio_bit_reset(LCD_CS_GPIO_PORT, LCD_CS_PIN);
  30. }
  31. /*!
  32. \brief configure the LCD control line
  33. \param[in] none
  34. \param[out] none
  35. \retval none
  36. */
  37. void lcd_ctrl_line_config(void)
  38. {
  39. /* enable GPIOs clock*/
  40. rcu_periph_clock_enable(LCD_CS_GPIO_CLK);
  41. rcu_periph_clock_enable(LCD_RS_GPIO_CLK);
  42. /* configure LCD_CS_GPIO_PORT(PD11) and LCD_RS_GPIO_PORT(PE3) */
  43. gpio_mode_set(LCD_CS_GPIO_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LCD_CS_PIN);
  44. gpio_output_options_set(LCD_CS_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,LCD_CS_PIN);
  45. gpio_mode_set(LCD_RS_GPIO_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LCD_RS_PIN);
  46. gpio_output_options_set(LCD_RS_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,LCD_RS_PIN);
  47. /* set the chip select pin */
  48. lcd_ctrl_line_set(LCD_CS_GPIO_PORT, LCD_CS_PIN);
  49. }
  50. /*!
  51. \brief set the LCD control line
  52. \param[in] gpiox: control line GPIO
  53. \arg LCD_CS_GPIO_PORT: LCD chip select GPIO
  54. \arg LCD_RS_GPIO_PORT: LCD register/RAM selection GPIO
  55. \param[in] gpiopin: control line pin
  56. \arg LCD_CS_PIN: LCD chip select pin
  57. \arg LCD_RS_PIN: LCD register/RAM selection pin
  58. \param[out] none
  59. \retval none
  60. */
  61. void lcd_ctrl_line_set(uint32_t gpiox, uint16_t gpiopin)
  62. {
  63. gpio_bit_set(gpiox, gpiopin);
  64. }
  65. /*!
  66. \brief reset the LCD control line
  67. \param[in] gpiox: control line GPIO
  68. \arg LCD_CS_GPIO_PORT: LCD chip select GPIO
  69. \arg LCD_RS_GPIO_PORT: LCD register/RAM selection GPIO
  70. \param[in] gpiopin: control line pin
  71. \arg LCD_CS_PIN: LCD chip select pin
  72. \arg LCD_RS_PIN: LCD register/RAM selection pin
  73. \param[out] none
  74. \retval none
  75. */
  76. void lcd_ctrl_line_reset(uint32_t gpiox, uint16_t gpiopin)
  77. {
  78. gpio_bit_reset(gpiox, gpiopin);
  79. }
  80. /*!
  81. \brief configure the LCD SPI and it's GPIOs
  82. \param[in] none
  83. \param[out] none
  84. \retval none
  85. */
  86. void lcd_spi_config(void)
  87. {
  88. spi_parameter_struct spi_init_struct;
  89. rcu_periph_clock_enable(RCU_GPIOG);
  90. rcu_periph_clock_enable(RCU_SPI5);
  91. /* configure SPI5_SCK(PG13) and SPI5_MOSI(PG14) */
  92. gpio_af_set(GPIOG,GPIO_AF_5,GPIO_PIN_13);
  93. gpio_af_set(GPIOG,GPIO_AF_5,GPIO_PIN_14);
  94. gpio_mode_set(GPIOG, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_13|GPIO_PIN_14);
  95. gpio_output_options_set(GPIOG, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13|GPIO_PIN_14);
  96. spi_i2s_deinit(SPI5);
  97. if(0 == (SPI_CTL0(LCD_SPI) & SPI_CTL0_SPIEN)){
  98. spi_init_struct.trans_mode = SPI_TRANSMODE_FULLDUPLEX;
  99. spi_init_struct.device_mode = SPI_MASTER;
  100. spi_init_struct.frame_size = SPI_FRAMESIZE_8BIT;
  101. spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;
  102. spi_init_struct.nss = SPI_NSS_SOFT;
  103. spi_init_struct.prescale = SPI_PSC_16;
  104. spi_init_struct.endian = SPI_ENDIAN_MSB;
  105. spi_init(LCD_SPI, &spi_init_struct);
  106. spi_enable(LCD_SPI);
  107. }
  108. }
  109. /*!
  110. \brief write command to select LCD register
  111. \param[in] lcd_register: the address of the selected register
  112. \param[out] none
  113. \retval none
  114. */
  115. void lcd_command_write(uint8_t lcd_register)
  116. {
  117. /* reset LCD_RS to send command */
  118. lcd_ctrl_line_reset(LCD_RS_GPIO_PORT, LCD_RS_PIN);
  119. /* reset LCD control line and send command */
  120. lcd_disable();
  121. while(RESET == spi_i2s_flag_get(LCD_SPI, SPI_FLAG_TBE)) ;
  122. spi_i2s_data_transmit(LCD_SPI, lcd_register);
  123. /* wait until a data is sent */
  124. while(RESET != spi_i2s_flag_get(LCD_SPI, SPI_FLAG_TRANS));
  125. lcd_enable();
  126. }
  127. /*!
  128. \brief write data to select LCD register
  129. \param[in] value: the value that will be written to the selected register
  130. \param[out] none
  131. \retval none
  132. */
  133. void lcd_data_write(uint8_t value)
  134. {
  135. /* set LCD_RS to send data */
  136. lcd_ctrl_line_set(LCD_RS_GPIO_PORT, LCD_RS_PIN);
  137. /* reset LCD control line and send data */
  138. lcd_disable();
  139. while(RESET == spi_i2s_flag_get(LCD_SPI, SPI_FLAG_TBE)) ;
  140. spi_i2s_data_transmit(LCD_SPI, value);
  141. /* wait until a data is sent */
  142. while(RESET != spi_i2s_flag_get(LCD_SPI, SPI_FLAG_TRANS)) ;
  143. lcd_enable();
  144. }
  145. /*!
  146. \brief configure the LCD based on the power on sequence
  147. \param[in] none
  148. \param[out] none
  149. \retval none
  150. */
  151. void lcd_power_on(void)
  152. {
  153. lcd_command_write(0x11);
  154. delay(120);
  155. lcd_command_write(0x36);
  156. lcd_data_write(0x48);
  157. lcd_command_write(0x3A);
  158. lcd_data_write(0x55);
  159. lcd_command_write(0xB4);
  160. lcd_data_write(0x11);
  161. lcd_command_write(0xB3);
  162. lcd_data_write(0x00);
  163. lcd_data_write(0x00);
  164. lcd_data_write(0x00);
  165. lcd_data_write(0x20);
  166. lcd_command_write(0xC0);
  167. lcd_data_write(0x10);
  168. lcd_data_write(0x3B);
  169. lcd_data_write(0x00);
  170. lcd_data_write(0x12);
  171. lcd_data_write(0x01);
  172. lcd_command_write(0xC5);
  173. lcd_data_write(0x07);
  174. lcd_command_write(0xC8);
  175. lcd_data_write(0x01 );
  176. lcd_data_write(0x36);
  177. lcd_data_write(0x00);
  178. lcd_data_write(0x02);
  179. lcd_data_write(0x00);
  180. lcd_data_write(0x1C);
  181. lcd_data_write(0x77);
  182. lcd_data_write(0x14);
  183. lcd_data_write(0x67);
  184. lcd_data_write(0x20);
  185. lcd_data_write(0x0E);
  186. lcd_data_write(0x00);
  187. lcd_command_write(0xD0);
  188. lcd_data_write(0x44);
  189. lcd_data_write(0x41 );
  190. lcd_data_write(0x08);
  191. lcd_data_write(0xC2);
  192. lcd_command_write(0xD1);
  193. lcd_data_write(0x50);
  194. lcd_data_write(0x11);
  195. lcd_command_write(0xD2);
  196. lcd_data_write(0x05);
  197. lcd_data_write(0x12);
  198. lcd_command_write(0xC6);
  199. lcd_data_write(0x83);
  200. lcd_command_write(0x29);
  201. delay(5);
  202. }
  203. /**
  204. * @brief New Version 3.5" TFT RGB Hardware needs add this initilize funtion ---By xufei 2016.10.21
  205. Modified by GAO HAIYANG, test pass, 17, Nov, 2016
  206. * @param None
  207. * @retval None
  208. */
  209. void lcd_power_on3(void)
  210. {
  211. lcd_command_write(0xC0);//power control1 command/w/
  212. lcd_data_write(0x0A); // P-Gamma level//4.1875v
  213. lcd_data_write(0x0A); // N-Gamma level
  214. lcd_command_write(0xC1); // BT & VC Setting//power contrl2 command/w/
  215. lcd_data_write(0x41);
  216. lcd_data_write(0x07); // VCI1 = 2.5V
  217. lcd_command_write(0xC2); // DC1.DC0 Setting//power control3 for normal mode
  218. lcd_data_write(0x33);
  219. lcd_command_write(0xC5);//VCOM control
  220. lcd_data_write(0x00); //NV memory is not programmed
  221. lcd_data_write(0x42); // VCM Setting
  222. lcd_data_write(0x80); // VCM Register Enable
  223. lcd_command_write(0xB0); //interface mode control //Polarity Setting
  224. lcd_data_write(0x02);
  225. lcd_command_write(0xB1);//frame rate control for normal mode
  226. lcd_data_write(0xB0); // Frame Rate Setting//70 frame per second//no division for internal clocks
  227. lcd_data_write(0x11);//17 clocks per line period for idle mode at cpu interface
  228. lcd_command_write(0xB4);//dispaly inversion control
  229. lcd_data_write(0x00); // disable Z-inversion , column inversion
  230. lcd_command_write(0xB6); //display function control// RM.DM Setting
  231. lcd_data_write(0x70);////0xF0
  232. lcd_data_write(0x02);//direction of gate scan: G1->G480 one by one, source scan: S1->S960, scan cycle if interval scan in non-display area
  233. lcd_data_write(0x3B); // number of lines to drive LCD: 8*(0x3C) = 480
  234. lcd_command_write(0xB7); // Entry Mode
  235. lcd_data_write(0x07); // disable low voltage detection, normal display,
  236. lcd_command_write(0xF0); // Enter ENG , must be set before gamma setting
  237. lcd_data_write(0x36);
  238. lcd_data_write(0xA5);
  239. lcd_data_write(0xD3);
  240. lcd_command_write(0xE5); // Open gamma function , must be set before gamma setting
  241. lcd_data_write(0x80);
  242. lcd_command_write(0xE5); // Page 1
  243. lcd_data_write(0x01);
  244. lcd_command_write(0XB3); // WEMODE=0(Page 1) , pixels over window setting will be ignored.//frame rate control in partial mode/full colors
  245. lcd_data_write(0x00);
  246. lcd_command_write(0xE5); // Page 0
  247. lcd_data_write(0x00);
  248. lcd_command_write(0xF0); // Exit ENG , must be set before gamma setting
  249. lcd_data_write(0x36);
  250. lcd_data_write(0xA5);
  251. lcd_data_write(0x53);
  252. lcd_command_write(0xE0); // Gamma setting
  253. //y fine adjustment register for positive polarity
  254. lcd_data_write(0x00);
  255. lcd_data_write(0x35);
  256. lcd_data_write(0x33);
  257. //y gradient adjustment register for positive polarity
  258. lcd_data_write(0x00);
  259. //y amplitude adjustment register for positive polarity
  260. lcd_data_write(0x00);
  261. lcd_data_write(0x00);
  262. //y fine adjustment register for negative polarity
  263. lcd_data_write(0x00);
  264. lcd_data_write(0x35);
  265. lcd_data_write(0x33);
  266. //y gradient adjustment register for negative polarity
  267. lcd_data_write(0x00);
  268. //y amplitude adjustment register for negative polarity
  269. lcd_data_write(0x00);
  270. lcd_data_write(0x00);
  271. lcd_command_write(0x36); // memory data access control //
  272. lcd_data_write(0x48);//
  273. lcd_command_write(0x3A); // interface pixel format setting
  274. lcd_data_write(0x55);//16-bits
  275. lcd_command_write(0x11); // Exit sleep mode
  276. lcd_command_write(0x29); // Display on
  277. delay(10);
  278. }
  279. /*!
  280. \brief insert a delay time
  281. \param[in] time: delay time length
  282. \param[out] none
  283. \retval none
  284. */
  285. static void delay(uint32_t time)
  286. {
  287. uint32_t timecount = time;
  288. while(0 != timecount--);
  289. }