SSD1963.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /**
  2. * @file SSD1963.c
  3. *
  4. */
  5. /*********************
  6. * INCLUDES
  7. *********************/
  8. #include "SSD1963.h"
  9. #if USE_SSD1963
  10. #include <stdbool.h>
  11. #include "lvgl/lv_core/lv_vdb.h"
  12. #include LV_DRV_DISP_INCLUDE
  13. #include LV_DRV_DELAY_INCLUDE
  14. /*********************
  15. * DEFINES
  16. *********************/
  17. #define SSD1963_CMD_MODE 0
  18. #define SSD1963_DATA_MODE 1
  19. /**********************
  20. * TYPEDEFS
  21. **********************/
  22. /**********************
  23. * STATIC PROTOTYPES
  24. **********************/
  25. static inline void ssd1963_cmd_mode(void);
  26. static inline void ssd1963_data_mode(void);
  27. static inline void ssd1963_cmd(uint8_t cmd);
  28. static inline void ssd1963_data(uint8_t data);
  29. static void ssd1963_io_init(void);
  30. static void ssd1963_reset(void);
  31. static void ssd1963_set_clk(void);
  32. static void ssd1963_set_tft_spec(void);
  33. static void ssd1963_init_bl(void);
  34. /**********************
  35. * STATIC VARIABLES
  36. **********************/
  37. static bool cmd_mode = true;
  38. /**********************
  39. * MACROS
  40. **********************/
  41. /**********************
  42. * GLOBAL FUNCTIONS
  43. **********************/
  44. void ssd1963_init(void)
  45. {
  46. ssd1963_io_init();
  47. ssd1963_reset();
  48. ssd1963_set_clk();
  49. ssd1963_set_tft_spec();
  50. ssd1963_init_bl();
  51. ssd1963_cmd(0x13); //SET display on
  52. ssd1963_cmd(0x29); //SET display on
  53. LV_DRV_DELAY_MS(30);
  54. }
  55. void ssd1963_flush(int32_t x1, int32_t y1, int32_t x2, int32_t y2, const lv_color_t * color_p)
  56. {
  57. /*Return if the area is out the screen*/
  58. if(x2 < 0) return;
  59. if(y2 < 0) return;
  60. if(x1 > SSD1963_HOR_RES - 1) return;
  61. if(y1 > SSD1963_VER_RES - 1) return;
  62. /*Truncate the area to the screen*/
  63. int32_t act_x1 = x1 < 0 ? 0 : x1;
  64. int32_t act_y1 = y1 < 0 ? 0 : y1;
  65. int32_t act_x2 = x2 > SSD1963_HOR_RES - 1 ? SSD1963_HOR_RES - 1 : x2;
  66. int32_t act_y2 = y2 > SSD1963_VER_RES - 1 ? SSD1963_VER_RES - 1 : y2;
  67. //Set the rectangular area
  68. ssd1963_cmd(0x002A);
  69. ssd1963_data(act_x1 >> 8);
  70. ssd1963_data(0x00FF & act_x1);
  71. ssd1963_data(act_x2 >> 8);
  72. ssd1963_data(0x00FF & act_x2);
  73. ssd1963_cmd(0x002B);
  74. ssd1963_data(act_y1 >> 8);
  75. ssd1963_data(0x00FF & act_y1);
  76. ssd1963_data(act_y2 >> 8);
  77. ssd1963_data(0x00FF & act_y2);
  78. ssd1963_cmd(0x2c);
  79. int16_t i;
  80. uint16_t full_w = x2 - x1 + 1;
  81. ssd1963_data_mode();
  82. #if LV_COLOR_DEPTH == 16
  83. uint16_t act_w = act_x2 - act_x1 + 1;
  84. for(i = act_y1; i <= act_y2; i++) {
  85. LV_DRV_DISP_PAR_WR_ARRAY((uint16_t*)color_p, act_w);
  86. color_p += full_w;
  87. }
  88. #else
  89. int16_t j;
  90. for(i = act_y1; i <= act_y2; i++) {
  91. for(j = 0; j <= act_x2 - act_x1 + 1; j++) {
  92. LV_DRV_DISP_PAR_WR_WORD(color_p[j]);
  93. color_p += full_w;
  94. }
  95. }
  96. #endif
  97. lv_flush_ready();
  98. }
  99. void ssd1963_fill(int32_t x1, int32_t y1, int32_t x2, int32_t y2, lv_color_t color)
  100. {
  101. /*Return if the area is out the screen*/
  102. if(x2 < 0) return;
  103. if(y2 < 0) return;
  104. if(x1 > SSD1963_HOR_RES - 1) return;
  105. if(y1 > SSD1963_VER_RES - 1) return;
  106. /*Truncate the area to the screen*/
  107. int32_t act_x1 = x1 < 0 ? 0 : x1;
  108. int32_t act_y1 = y1 < 0 ? 0 : y1;
  109. int32_t act_x2 = x2 > SSD1963_HOR_RES - 1 ? SSD1963_HOR_RES - 1 : x2;
  110. int32_t act_y2 = y2 > SSD1963_VER_RES - 1 ? SSD1963_VER_RES - 1 : y2;
  111. //Set the rectangular area
  112. ssd1963_cmd(0x002A);
  113. ssd1963_data(act_x1 >> 8);
  114. ssd1963_data(0x00FF & act_x1);
  115. ssd1963_data(act_x2 >> 8);
  116. ssd1963_data(0x00FF & act_x2);
  117. ssd1963_cmd(0x002B);
  118. ssd1963_data(act_y1 >> 8);
  119. ssd1963_data(0x00FF & act_y1);
  120. ssd1963_data(act_y2 >> 8);
  121. ssd1963_data(0x00FF & act_y2);
  122. ssd1963_cmd(0x2c);
  123. ssd1963_data_mode();
  124. uint16_t color16 = lv_color_to16(color);
  125. uint32_t size = (act_x2 - act_x1 + 1) * (act_y2 - act_y1 + 1);
  126. uint32_t i;
  127. for(i = 0; i < size; i++) {
  128. LV_DRV_DISP_PAR_WR_WORD(color16);
  129. }
  130. }
  131. void ssd1963_map(int32_t x1, int32_t y1, int32_t x2, int32_t y2, const lv_color_t * color_p)
  132. {
  133. /*Return if the area is out the screen*/
  134. if(x2 < 0) return;
  135. if(y2 < 0) return;
  136. if(x1 > SSD1963_HOR_RES - 1) return;
  137. if(y1 > SSD1963_VER_RES - 1) return;
  138. /*Truncate the area to the screen*/
  139. int32_t act_x1 = x1 < 0 ? 0 : x1;
  140. int32_t act_y1 = y1 < 0 ? 0 : y1;
  141. int32_t act_x2 = x2 > SSD1963_HOR_RES - 1 ? SSD1963_HOR_RES - 1 : x2;
  142. int32_t act_y2 = y2 > SSD1963_VER_RES - 1 ? SSD1963_VER_RES - 1 : y2;
  143. //Set the rectangular area
  144. ssd1963_cmd(0x002A);
  145. ssd1963_data(act_x1 >> 8);
  146. ssd1963_data(0x00FF & act_x1);
  147. ssd1963_data(act_x2 >> 8);
  148. ssd1963_data(0x00FF & act_x2);
  149. ssd1963_cmd(0x002B);
  150. ssd1963_data(act_y1 >> 8);
  151. ssd1963_data(0x00FF & act_y1);
  152. ssd1963_data(act_y2 >> 8);
  153. ssd1963_data(0x00FF & act_y2);
  154. ssd1963_cmd(0x2c);
  155. int16_t i;
  156. uint16_t full_w = x2 - x1 + 1;
  157. ssd1963_data_mode();
  158. #if LV_COLOR_DEPTH == 16
  159. uint16_t act_w = act_x2 - act_x1 + 1;
  160. for(i = act_y1; i <= act_y2; i++) {
  161. LV_DRV_DISP_PAR_WR_ARRAY((uint16_t*)color_p, act_w);
  162. color_p += full_w;
  163. }
  164. #else
  165. int16_t j;
  166. for(i = act_y1; i <= act_y2; i++) {
  167. for(j = 0; j <= act_x2 - act_x1 + 1; j++) {
  168. LV_DRV_DISP_PAR_WR_WORD(color_p[j]);
  169. color_p += full_w;
  170. }
  171. }
  172. #endif
  173. }
  174. /**********************
  175. * STATIC FUNCTIONS
  176. **********************/
  177. static void ssd1963_io_init(void)
  178. {
  179. LV_DRV_DISP_CMD_DATA(SSD1963_CMD_MODE);
  180. cmd_mode = true;
  181. }
  182. static void ssd1963_reset(void)
  183. {
  184. /*Hardware reset*/
  185. LV_DRV_DISP_RST(1);
  186. LV_DRV_DELAY_MS(50);
  187. LV_DRV_DISP_RST(0);
  188. LV_DRV_DELAY_MS(50);
  189. LV_DRV_DISP_RST(1);
  190. LV_DRV_DELAY_MS(50);
  191. /*Chip enable*/
  192. LV_DRV_DISP_PAR_CS(0);
  193. LV_DRV_DELAY_MS(10);
  194. LV_DRV_DISP_PAR_CS(1);
  195. LV_DRV_DELAY_MS(5);
  196. /*Software reset*/
  197. ssd1963_cmd(0x01);
  198. LV_DRV_DELAY_MS(20);
  199. ssd1963_cmd(0x01);
  200. LV_DRV_DELAY_MS(20);
  201. ssd1963_cmd(0x01);
  202. LV_DRV_DELAY_MS(20);
  203. }
  204. static void ssd1963_set_clk(void)
  205. {
  206. /* Set PLL*/
  207. ssd1963_cmd(0xe2);
  208. ssd1963_data(0x23);
  209. ssd1963_data(0x05);
  210. ssd1963_data(0x54);
  211. /*Enable PLL*/
  212. ssd1963_cmd(0xe0);
  213. ssd1963_data(0x01);
  214. LV_DRV_DELAY_MS(20);
  215. /*Lock PLL*/
  216. ssd1963_cmd(0xe0);
  217. ssd1963_data(0x03);
  218. /*Software reset*/
  219. ssd1963_cmd(0x01);
  220. LV_DRV_DELAY_MS(20);
  221. /*Set PCLK freq*/
  222. ssd1963_cmd(0xe6);
  223. ssd1963_data(0x04);
  224. ssd1963_data(0x93);
  225. ssd1963_data(0xe0);
  226. }
  227. static void ssd1963_set_tft_spec(void)
  228. {
  229. ssd1963_cmd(0xB0); //LCD SPECIFICATION
  230. ssd1963_data(0x20);
  231. ssd1963_data(0x00);
  232. ssd1963_data(((SSD1963_HOR_RES - 1) >> 8) & 0XFF); //Set HDP
  233. ssd1963_data((SSD1963_HOR_RES - 1) & 0XFF);
  234. ssd1963_data(((SSD1963_VER_RES - 1) >> 8) & 0XFF); //Set VDP
  235. ssd1963_data((SSD1963_VER_RES - 1) & 0XFF);
  236. ssd1963_data(0x00);
  237. ssd1963_cmd(0xB4); //HSYNC
  238. ssd1963_data((SSD1963_HT >> 8) & 0XFF); //Set HT
  239. ssd1963_data(SSD1963_HT & 0XFF);
  240. ssd1963_data((SSD1963_HPS >> 8) & 0XFF); //Set HPS
  241. ssd1963_data(SSD1963_HPS & 0XFF);
  242. ssd1963_data(SSD1963_HPW); //Set HPW
  243. ssd1963_data((SSD1963_LPS >> 8) & 0XFF); //Set HPS
  244. ssd1963_data(SSD1963_LPS & 0XFF);
  245. ssd1963_data(0x00);
  246. ssd1963_cmd(0xB6); //VSYNC
  247. ssd1963_data((SSD1963_VT >> 8) & 0XFF); //Set VT
  248. ssd1963_data(SSD1963_VT & 0XFF);
  249. ssd1963_data((SSD1963_VPS >> 8) & 0XFF); //Set VPS
  250. ssd1963_data(SSD1963_VPS & 0XFF);
  251. ssd1963_data(SSD1963_VPW); //Set VPW
  252. ssd1963_data((SSD1963_FPS >> 8) & 0XFF); //Set FPS
  253. ssd1963_data(SSD1963_FPS & 0XFF);
  254. ssd1963_cmd(0xf0); //SET pixel data I/F format=16bit
  255. ssd1963_data(0x03);
  256. // ssd1963_cmd(0x3a); //RGB pixel format
  257. // ssd1963_data(0x66); //24bpp (0x60: 18bpp)
  258. ssd1963_cmd(0x36); //SET address mode=flip vertical
  259. #if SSD1963_ORI == 0
  260. ssd1963_data(0x03);
  261. #else
  262. ssd1963_data(0x00);
  263. #endif
  264. // ssd1963_cmd(0x26); //gamma curve
  265. // ssd1963_data(0x08);
  266. }
  267. static void ssd1963_init_bl(void)
  268. {
  269. ssd1963_cmd(0xBE); /*Set PWM configuration for back light control*/
  270. ssd1963_data(0x02); /*PWMF[7:0] = 2, PWM base freq = PLL/(256*(1+5))/256 =
  271. 300Hz for a PLL freq = 120MHz */
  272. ssd1963_data(0x20); /*Set duty cycle, from 0x00 (total pull-down) to 0xFF
  273. (99% pull-up , 255/256) */
  274. ssd1963_data(0x01); /*PWM enabled and controlled by host (mcu) */
  275. ssd1963_data(0x00);
  276. ssd1963_data(0x00);
  277. ssd1963_data(0x00);
  278. }
  279. /**
  280. * Command mode
  281. */
  282. static inline void ssd1963_cmd_mode(void)
  283. {
  284. if(cmd_mode == false) {
  285. LV_DRV_DISP_CMD_DATA(SSD1963_CMD_MODE);
  286. cmd_mode = true;
  287. }
  288. }
  289. /**
  290. * Data mode
  291. */
  292. static inline void ssd1963_data_mode(void)
  293. {
  294. if(cmd_mode != false) {
  295. LV_DRV_DISP_CMD_DATA(SSD1963_DATA_MODE);
  296. cmd_mode = false;
  297. }
  298. }
  299. /**
  300. * Write command
  301. * @param cmd the command
  302. */
  303. static inline void ssd1963_cmd(uint8_t cmd)
  304. {
  305. ssd1963_cmd_mode();
  306. LV_DRV_DISP_PAR_WR_WORD(cmd);
  307. }
  308. /**
  309. * Write data
  310. * @param data the data
  311. */
  312. static inline void ssd1963_data(uint8_t data)
  313. {
  314. ssd1963_data_mode();
  315. LV_DRV_DISP_PAR_WR_WORD(data);
  316. }
  317. #endif