R61581.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /**
  2. * @file R61581.c
  3. *
  4. */
  5. /*********************
  6. * INCLUDES
  7. *********************/
  8. #include "R61581.h"
  9. #if USE_R61581 != 0
  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 R61581_CMD_MODE 0
  18. #define R61581_DATA_MODE 1
  19. /**********************
  20. * TYPEDEFS
  21. **********************/
  22. /**********************
  23. * STATIC PROTOTYPES
  24. **********************/
  25. static void r61581_io_init(void);
  26. static void r61581_reset(void);
  27. static void r61581_set_tft_spec(void);
  28. static inline void r61581_cmd_mode(void);
  29. static inline void r61581_data_mode(void);
  30. static inline void r61581_cmd(uint8_t cmd);
  31. static inline void r61581_data(uint8_t data);
  32. /**********************
  33. * STATIC VARIABLES
  34. **********************/
  35. static bool cmd_mode = true;
  36. /**********************
  37. * MACROS
  38. **********************/
  39. /**********************
  40. * GLOBAL FUNCTIONS
  41. **********************/
  42. /**
  43. * Initialize the R61581 display controller
  44. * @return HW_RES_OK or any error from hw_res_t enum
  45. */
  46. void r61581_init(void)
  47. {
  48. r61581_io_init();
  49. /*Slow mode until the PLL is not started in the display controller*/
  50. LV_DRV_DISP_PAR_SLOW;
  51. r61581_reset();
  52. r61581_set_tft_spec();
  53. r61581_cmd(0x13); //SET display on
  54. r61581_cmd(0x29); //SET display on
  55. LV_DRV_DELAY_MS(30);
  56. /*Parallel to max speed*/
  57. LV_DRV_DISP_PAR_FAST;
  58. }
  59. void r61581_flush(int32_t x1, int32_t y1, int32_t x2, int32_t y2, const lv_color_t * color_p)
  60. {
  61. /*Return if the area is out the screen*/
  62. if(x2 < 0) return;
  63. if(y2 < 0) return;
  64. if(x1 > R61581_HOR_RES - 1) return;
  65. if(y1 > R61581_VER_RES - 1) return;
  66. /*Truncate the area to the screen*/
  67. int32_t act_x1 = x1 < 0 ? 0 : x1;
  68. int32_t act_y1 = y1 < 0 ? 0 : y1;
  69. int32_t act_x2 = x2 > R61581_HOR_RES - 1 ? R61581_HOR_RES - 1 : x2;
  70. int32_t act_y2 = y2 > R61581_VER_RES - 1 ? R61581_VER_RES - 1 : y2;
  71. //Set the rectangular area
  72. r61581_cmd(0x002A);
  73. r61581_data(act_x1 >> 8);
  74. r61581_data(0x00FF & act_x1);
  75. r61581_data(act_x2 >> 8);
  76. r61581_data(0x00FF & act_x2);
  77. r61581_cmd(0x002B);
  78. r61581_data(act_y1 >> 8);
  79. r61581_data(0x00FF & act_y1);
  80. r61581_data(act_y2 >> 8);
  81. r61581_data(0x00FF & act_y2);
  82. r61581_cmd(0x2c);
  83. int16_t i;
  84. uint16_t full_w = x2 - x1 + 1;
  85. r61581_data_mode();
  86. #if LV_COLOR_DEPTH == 16
  87. uint16_t act_w = act_x2 - act_x1 + 1;
  88. for(i = act_y1; i <= act_y2; i++) {
  89. LV_DRV_DISP_PAR_WR_ARRAY((uint16_t*)color_p, act_w);
  90. color_p += full_w;
  91. }
  92. #else
  93. int16_t j;
  94. for(i = act_y1; i <= act_y2; i++) {
  95. for(j = 0; j <= act_x2 - act_x1 + 1; j++) {
  96. LV_DRV_DISP_PAR_WR_WORD(lv_color_to16(color_p[j]));
  97. color_p += full_w;
  98. }
  99. }
  100. #endif
  101. lv_flush_ready();
  102. }
  103. void r61581_fill(int32_t x1, int32_t y1, int32_t x2, int32_t y2, lv_color_t color)
  104. {
  105. /*Return if the area is out the screen*/
  106. if(x2 < 0) return;
  107. if(y2 < 0) return;
  108. if(x1 > R61581_HOR_RES - 1) return;
  109. if(y1 > R61581_VER_RES - 1) return;
  110. /*Truncate the area to the screen*/
  111. int32_t act_x1 = x1 < 0 ? 0 : x1;
  112. int32_t act_y1 = y1 < 0 ? 0 : y1;
  113. int32_t act_x2 = x2 > R61581_HOR_RES - 1 ? R61581_HOR_RES - 1 : x2;
  114. int32_t act_y2 = y2 > R61581_VER_RES - 1 ? R61581_VER_RES - 1 : y2;
  115. //Set the rectangular area
  116. r61581_cmd(0x002A);
  117. r61581_data(act_x1 >> 8);
  118. r61581_data(0x00FF & act_x1);
  119. r61581_data(act_x2 >> 8);
  120. r61581_data(0x00FF & act_x2);
  121. r61581_cmd(0x002B);
  122. r61581_data(act_y1 >> 8);
  123. r61581_data(0x00FF & act_y1);
  124. r61581_data(act_y2 >> 8);
  125. r61581_data(0x00FF & act_y2);
  126. r61581_cmd(0x2c);
  127. r61581_data_mode();
  128. uint16_t color16 = lv_color_to16(color);
  129. uint32_t size = (act_x2 - act_x1 + 1) * (act_y2 - act_y1 + 1);
  130. uint32_t i;
  131. for(i = 0; i < size; i++) {
  132. LV_DRV_DISP_PAR_WR_WORD(color16);
  133. }
  134. }
  135. void r61581_map(int32_t x1, int32_t y1, int32_t x2, int32_t y2, const lv_color_t * color_p)
  136. {
  137. /*Return if the area is out the screen*/
  138. if(x2 < 0) return;
  139. if(y2 < 0) return;
  140. if(x1 > R61581_HOR_RES - 1) return;
  141. if(y1 > R61581_VER_RES - 1) return;
  142. /*Truncate the area to the screen*/
  143. int32_t act_x1 = x1 < 0 ? 0 : x1;
  144. int32_t act_y1 = y1 < 0 ? 0 : y1;
  145. int32_t act_x2 = x2 > R61581_HOR_RES - 1 ? R61581_HOR_RES - 1 : x2;
  146. int32_t act_y2 = y2 > R61581_VER_RES - 1 ? R61581_VER_RES - 1 : y2;
  147. //Set the rectangular area
  148. r61581_cmd(0x002A);
  149. r61581_data(act_x1 >> 8);
  150. r61581_data(0x00FF & act_x1);
  151. r61581_data(act_x2 >> 8);
  152. r61581_data(0x00FF & act_x2);
  153. r61581_cmd(0x002B);
  154. r61581_data(act_y1 >> 8);
  155. r61581_data(0x00FF & act_y1);
  156. r61581_data(act_y2 >> 8);
  157. r61581_data(0x00FF & act_y2);
  158. r61581_cmd(0x2c);
  159. int16_t i;
  160. uint16_t full_w = x2 - x1 + 1;
  161. r61581_data_mode();
  162. #if LV_COLOR_DEPTH == 16
  163. uint16_t act_w = act_x2 - act_x1 + 1;
  164. for(i = act_y1; i <= act_y2; i++) {
  165. LV_DRV_DISP_PAR_WR_ARRAY((uint16_t*)color_p, act_w);
  166. color_p += full_w;
  167. }
  168. #else
  169. int16_t j;
  170. for(i = act_y1; i <= act_y2; i++) {
  171. for(j = 0; j <= act_x2 - act_x1 + 1; j++) {
  172. LV_DRV_DISP_PAR_WR_WORD(lv_color_to16(color_p[j]));
  173. color_p += full_w;
  174. }
  175. }
  176. #endif
  177. }
  178. /**********************
  179. * STATIC FUNCTIONS
  180. **********************/
  181. /**
  182. * Io init
  183. */
  184. static void r61581_io_init(void)
  185. {
  186. LV_DRV_DISP_CMD_DATA(R61581_CMD_MODE)
  187. cmd_mode = true;
  188. }
  189. /**
  190. * Reset
  191. */
  192. static void r61581_reset(void)
  193. {
  194. /*Hardware reset*/
  195. LV_DRV_DISP_RST(1);
  196. LV_DRV_DELAY_MS(50);
  197. LV_DRV_DISP_RST(0);
  198. LV_DRV_DELAY_MS(50);
  199. LV_DRV_DISP_RST(1);
  200. LV_DRV_DELAY_MS(50);
  201. /*Chip enable*/
  202. LV_DRV_DISP_PAR_CS(1);
  203. LV_DRV_DELAY_MS(10);
  204. LV_DRV_DISP_PAR_CS(0);
  205. LV_DRV_DELAY_MS(5);
  206. /*Software reset*/
  207. r61581_cmd(0x01);
  208. LV_DRV_DELAY_MS(20);
  209. r61581_cmd(0x01);
  210. LV_DRV_DELAY_MS(20);
  211. r61581_cmd(0x01);
  212. LV_DRV_DELAY_MS(20);
  213. }
  214. /**
  215. * TFT specific initialization
  216. */
  217. static void r61581_set_tft_spec(void)
  218. {
  219. r61581_cmd(0xB0);
  220. r61581_data(0x00);
  221. r61581_cmd(0xB3);
  222. r61581_data(0x02);
  223. r61581_data(0x00);
  224. r61581_data(0x00);
  225. r61581_data(0x10);
  226. r61581_cmd(0xB4);
  227. r61581_data(0x00);//0X10
  228. r61581_cmd(0xB9); //PWM
  229. r61581_data(0x01);
  230. r61581_data(0xFF); //FF brightness
  231. r61581_data(0xFF);
  232. r61581_data(0x18);
  233. /*Panel Driving Setting*/
  234. r61581_cmd(0xC0);
  235. r61581_data(0x02);
  236. r61581_data(0x3B);
  237. r61581_data(0x00);
  238. r61581_data(0x00);
  239. r61581_data(0x00);
  240. r61581_data(0x01);
  241. r61581_data(0x00);//NW
  242. r61581_data(0x43);
  243. /*Display Timing Setting for Normal Mode */
  244. r61581_cmd(0xC1);
  245. r61581_data(0x08);
  246. r61581_data(0x15); //CLOCK
  247. r61581_data(R61581_VFP);
  248. r61581_data(R61581_VBP);
  249. /*Source/VCOM/Gate Driving Timing Setting*/
  250. r61581_cmd(0xC4);
  251. r61581_data(0x15);
  252. r61581_data(0x03);
  253. r61581_data(0x03);
  254. r61581_data(0x01);
  255. /*Interface Setting*/
  256. r61581_cmd(0xC6);
  257. r61581_data((R61581_DPL << 0) |
  258. (R61581_EPL << 1) |
  259. (R61581_HSPL << 4) |
  260. (R61581_VSPL << 5));
  261. /*Gamma Set*/
  262. r61581_cmd(0xC8);
  263. r61581_data(0x0c);
  264. r61581_data(0x05);
  265. r61581_data(0x0A);
  266. r61581_data(0x6B);
  267. r61581_data(0x04);
  268. r61581_data(0x06);
  269. r61581_data(0x15);
  270. r61581_data(0x10);
  271. r61581_data(0x00);
  272. r61581_data(0x31);
  273. r61581_cmd(0x36);
  274. if(R61581_ORI == 0) r61581_data(0xE0);
  275. else r61581_data(0x20);
  276. r61581_cmd(0x0C);
  277. r61581_data(0x55);
  278. r61581_cmd(0x3A);
  279. r61581_data(0x55);
  280. r61581_cmd(0x38);
  281. r61581_cmd(0xD0);
  282. r61581_data(0x07);
  283. r61581_data(0x07);
  284. r61581_data(0x14);
  285. r61581_data(0xA2);
  286. r61581_cmd(0xD1);
  287. r61581_data(0x03);
  288. r61581_data(0x5A);
  289. r61581_data(0x10);
  290. r61581_cmd(0xD2);
  291. r61581_data(0x03);
  292. r61581_data(0x04);
  293. r61581_data(0x04);
  294. r61581_cmd(0x11);
  295. LV_DRV_DELAY_MS(10);
  296. r61581_cmd(0x2A);
  297. r61581_data(0x00);
  298. r61581_data(0x00);
  299. r61581_data(((R61581_HOR_RES - 1) >> 8 ) & 0XFF);
  300. r61581_data((R61581_HOR_RES - 1) & 0XFF);
  301. r61581_cmd(0x2B);
  302. r61581_data(0x00);
  303. r61581_data(0x00);
  304. r61581_data(((R61581_VER_RES - 1) >> 8 ) & 0XFF);
  305. r61581_data((R61581_VER_RES- 1) & 0XFF);
  306. LV_DRV_DELAY_MS(10);
  307. r61581_cmd(0x29);
  308. LV_DRV_DELAY_MS(5);
  309. r61581_cmd(0x2C);
  310. LV_DRV_DELAY_MS(5);
  311. }
  312. /**
  313. * Command mode
  314. */
  315. static inline void r61581_cmd_mode(void)
  316. {
  317. if(cmd_mode == false) {
  318. LV_DRV_DISP_CMD_DATA(R61581_CMD_MODE)
  319. cmd_mode = true;
  320. }
  321. }
  322. /**
  323. * Data mode
  324. */
  325. static inline void r61581_data_mode(void)
  326. {
  327. if(cmd_mode != false) {
  328. LV_DRV_DISP_CMD_DATA(R61581_DATA_MODE);
  329. cmd_mode = false;
  330. }
  331. }
  332. /**
  333. * Write command
  334. * @param cmd the command
  335. */
  336. static inline void r61581_cmd(uint8_t cmd)
  337. {
  338. r61581_cmd_mode();
  339. LV_DRV_DISP_PAR_WR_WORD(cmd);
  340. }
  341. /**
  342. * Write data
  343. * @param data the data
  344. */
  345. static inline void r61581_data(uint8_t data)
  346. {
  347. r61581_data_mode();
  348. LV_DRV_DISP_PAR_WR_WORD(data);
  349. }
  350. #endif