µGFX library fork

gdisp_lld_RA6963.c 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * This file is subject to the terms of the GFX License. If a copy of
  3. * the license was not distributed with this file, you can obtain one at:
  4. *
  5. * http://ugfx.org/license.html
  6. */
  7. /* Robert Offner 2017
  8. * This is a driver for the RA6963 or T6963 Displays.
  9. * It should work with displays up to 240 pixel in width and 64 pixel in height
  10. * ATTENTION some newer displays on ebay look like T6963 Type but they're not!
  11. * They use a new controller: ST7920. These are easy to identify: NO CS pin but
  12. * a PSB Pin which switches from parallel to serial communication mode.
  13. * If it cost less than 10.- it is probably a ST7920.
  14. *
  15. * Version: 0.1
  16. *
  17. * History:
  18. *
  19. * v0.1 Initial Release
  20. * .) tested with readback and without on a 72MHz STM32F103. Display ws a 128x64
  21. * type Graphic LCD (Looks like KS0108 Types). STREAM_READ is not implemented.
  22. */
  23. #include "gfx.h"
  24. #if GFX_USE_GDISP
  25. #define GDISP_DRIVER_VMT GDISPVMT_RA6963
  26. #include "gdisp_lld_config.h"
  27. #include "src/gdisp/gdisp_driver.h"
  28. /*===========================================================================*/
  29. /* Driver local definitions. */
  30. /*===========================================================================*/
  31. #if defined(GDISP_SCREEN_HEIGHT) || defined(GDISP_SCREEN_HEIGHT)
  32. #if GFX_COMPILER_WARNING_TYPE == GFX_COMPILER_WARNING_DIRECT
  33. #warning "GDISP: This low level driver does not support setting a screen size. It is being ignored."
  34. #elif GFX_COMPILER_WARNING_TYPE == GFX_COMPILER_WARNING_MACRO
  35. COMPILER_WARNING("GDISP: This low level driver does not support setting a screen size. It is being ignored.")
  36. #endif
  37. #undef GDISP_SCREEN_WIDTH
  38. #undef GDISP_SCREEN_HEIGHT
  39. #endif
  40. #include "board_RA6963.h"
  41. // RA6963 Commands
  42. //Register Setting
  43. #define RA6963_SET_CURSOR_POINTER 0x21
  44. #define RA6963_SET_OFFSET_REGISTER 0x22
  45. #define RA6963_SET_ADDRESS_POINTER 0x24
  46. //Set Control Word
  47. #define RA6963_SET_TEXT_HOME_ADDRESS 0x40
  48. #define RA6963_SET_TEXT_AREA 0x41
  49. #define RA6963_SET_GRAPHIC_HOME_ADDRESS 0x42
  50. #define RA6963_SET_GRAPHIC_AREA 0x43
  51. //Mode Set
  52. #define RA6963_MODE_SET 0x80
  53. #define RA6963_MODE_OR 0x00
  54. #define RA6963_MODE_XOR 0x01
  55. #define RA6963_MODE_AND 0x03
  56. #define RA6963_MODE_TEXT_ATTRIBUTE 0x04
  57. #define RA6963_MODE_INTERNAL_CGROM 0x00
  58. #define RA6963_MODE_EXTERNAL_CGRAM 0x08
  59. //Display Mode
  60. #define RA6963_DISPLAY_MODE 0x90
  61. #define RA6963_CURSOR_BLINK_ON 0x01
  62. #define RA6963_CURSOR_DISPLAY_ON 0x02
  63. #define RA6963_TEXT_DISPLAY_ON 0x04
  64. #define RA6963_GRAPHIC_DISPLAY_ON 0x08
  65. //Cursor Pattern Select
  66. #define RA6963_CURSOR_PATTERN_SELECT 0xA0
  67. #define RA6963_CURSOR_1_LINE 0x00
  68. #define RA6963_CURSOR_2_LINE 0x01
  69. #define RA6963_CURSOR_3_LINE 0x02
  70. #define RA6963_CURSOR_4_LINE 0x03
  71. #define RA6963_CURSOR_5_LINE 0x04
  72. #define RA6963_CURSOR_6_LINE 0x05
  73. #define RA6963_CURSOR_7_LINE 0x06
  74. #define RA6963_CURSOR_8_LINE 0x07
  75. //Data Read/Write
  76. #define RA6963_DATA_WRITE_AND_INCREMENT 0xC0
  77. #define RA6963_DATA_READ_AND_INCREMENT 0xC1
  78. #define RA6963_DATA_WRITE_AND_DECREMENT 0xC2
  79. #define RA6963_DATA_READ_AND_DECREMENT 0xC3
  80. #define RA6963_DATA_WRITE_AND_NONVARIALBE 0xC4
  81. #define RA6963_DATA_READ_AND_NONVARIABLE 0xC5
  82. //Data auto Read/Write
  83. #define RA6963_SET_DATA_AUTO_WRITE 0xB0
  84. #define RA6963_SET_DATA_AUTO_READ 0xB1
  85. #define RA6963_AUTO_RESET 0xB2
  86. //Screen Peek
  87. #define RA6963_SCREEN_PEEK 0xE0
  88. //Screen copy
  89. #define RA6963_SCREEN_COPY 0xE8
  90. // Status Register Bits
  91. #define RA6963_STATUS_BUSY1 0x01
  92. #define RA6963_STATUS_BUSY2 0x02
  93. #define RA6963_STATUS_DARRDY 0x04
  94. #define RA6963_STATUS_DAWRDY 0x08
  95. #ifndef GDISP_SCREEN_HEIGHT
  96. #define GDISP_SCREEN_HEIGHT 64
  97. #endif
  98. #ifndef GDISP_SCREEN_WIDTH
  99. #define GDISP_SCREEN_WIDTH 128
  100. #endif
  101. #define RA6963_FONT_WIDTH 8
  102. #define RA6963_GRAPHIC_AREA (GDISP_SCREEN_WIDTH / RA6963_FONT_WIDTH) //16
  103. #define RA6963_TEXT_AREA (GDISP_SCREEN_WIDTH / RA6963_FONT_WIDTH) //16
  104. #define RA6963_GRAPHIC_SIZE (RA6963_GRAPHIC_AREA * GDISP_SCREEN_HEIGHT) //1024
  105. #define RA6963_TEXT_SIZE (RA6963_TEXT_AREA * (GDISP_SCREEN_HEIGHT/8))//128
  106. //#define RA6963_TEXT_HOME 0
  107. //#define RA6963_GRAPHIC_HOME (RA6963_TEXT_HOME + RA6963_TEXT_SIZE) //128
  108. #define RA6963_GRAPHIC_HOME 0
  109. #define RA6963_TEXT_HOME (RA6963_GRAPHIC_HOME + RA6963_GRAPHIC_SIZE)
  110. //#define RA6963_OFFSET_REGISTER 2
  111. #if (RA6963_NEED_READ == FALSE)
  112. #define BUFFSZ (RA6963_GRAPHIC_SIZE)
  113. #define RAM(g) ((uint8_t *)g->priv)
  114. #define POS (((g->p.x) / RA6963_FONT_WIDTH) + ((g->p.y) * RA6963_GRAPHIC_AREA))
  115. #endif
  116. #ifndef GDISP_INITIAL_CONTRAST
  117. #define GDISP_INITIAL_CONTRAST 50
  118. #endif
  119. #ifndef GDISP_INITIAL_BACKLIGHT
  120. #define GDISP_INITIAL_BACKLIGHT 100
  121. #endif
  122. /*===========================================================================*/
  123. /* Driver local functions. */
  124. /*===========================================================================*/
  125. #ifndef write_data_repeat
  126. #define write_data_repeat(g, data, count) { int i; for (i = 0; i < count; ++i) write_data (g, data); }
  127. #endif
  128. /*===========================================================================*/
  129. /* Driver exported functions. */
  130. /*===========================================================================*/
  131. LLDSPEC bool_t gdisp_lld_init(GDisplay *g) {
  132. #if (RA6963_NEED_READ == FALSE)
  133. // The private area is the display surface.
  134. g->priv = gfxAlloc(BUFFSZ);
  135. #else
  136. g->priv = 0;
  137. #endif
  138. // Initialise the board interface
  139. init_board(g);
  140. #if RA6963_HAS_RESET //Make Hardware Reset
  141. setpin_reset(g, TRUE);
  142. gfxSleepMilliseconds(100);
  143. setpin_reset(g, FALSE);
  144. #endif
  145. gfxSleepMilliseconds(50);
  146. //RA6963 needs Data first THEN command!
  147. write_data(g, (uint8_t)RA6963_GRAPHIC_HOME); //0
  148. write_data(g, (uint8_t)(RA6963_GRAPHIC_HOME>>8)); //0
  149. write_cmd(g, RA6963_SET_GRAPHIC_HOME_ADDRESS); //0x42
  150. write_data(g, (uint8_t)RA6963_GRAPHIC_AREA); //16
  151. write_data(g, 0); //0
  152. write_cmd(g, RA6963_SET_GRAPHIC_AREA); //0x43
  153. write_data(g, (uint8_t)RA6963_TEXT_HOME); //0x00
  154. write_data(g, (uint8_t)(RA6963_TEXT_HOME>>8)); //0x04 = 0x400
  155. write_cmd(g, RA6963_SET_TEXT_HOME_ADDRESS); //0x40
  156. write_data(g, (uint8_t)RA6963_TEXT_AREA); //16
  157. write_data(g, (uint8_t)(RA6963_TEXT_AREA>>8)); //0
  158. write_cmd(g, RA6963_SET_TEXT_AREA); //0x41
  159. /* write_data(g, (uint8_t)RA6963_OFFSET_REGISTER);
  160. write_data(g, 0);
  161. write_cmd(g, RA6963_SET_OFFSET_REGISTER);*/
  162. write_data(g, 0x00);
  163. write_data(g, 0x00);
  164. write_cmd(g, RA6963_SET_ADDRESS_POINTER);
  165. write_data(g, 0x00);
  166. write_data(g, 0x00);
  167. write_cmd(g, RA6963_SET_CURSOR_POINTER);
  168. //write_cmd(g, RA6963_CURSOR_PATTERN_SELECT | RA6963_CURSOR_1_LINE);
  169. write_cmd(g, RA6963_MODE_SET | RA6963_MODE_OR);
  170. //write_cmd(g, RA6963_DISPLAY_MODE | RA6963_TEXT_DISPLAY_ON | RA6963_GRAPHIC_DISPLAY_ON);
  171. write_cmd(g, RA6963_DISPLAY_MODE | RA6963_GRAPHIC_DISPLAY_ON);
  172. // Finish Init
  173. post_init_board(g);
  174. #if RA6963_NEED_BACKLIGHT
  175. // Turn on the back-light
  176. set_backlight(g, GDISP_INITIAL_BACKLIGHT);
  177. #endif
  178. // Initialise the GDISP structure
  179. g->g.Width = GDISP_SCREEN_WIDTH;
  180. g->g.Height = GDISP_SCREEN_HEIGHT;
  181. g->g.Orientation = GDISP_ROTATE_0;
  182. g->g.Powermode = powerOn;
  183. g->g.Backlight = GDISP_INITIAL_BACKLIGHT;
  184. g->g.Contrast = GDISP_INITIAL_CONTRAST;
  185. return TRUE;
  186. }
  187. static void set_viewport(GDisplay *g) {
  188. if ((g->p.x >= GDISP_SCREEN_WIDTH) || (g->p.y >= GDISP_SCREEN_HEIGHT))
  189. return; // 0 + ( x / 8 ) + ( y * 16 )
  190. //uint16_t addr = (RA6963_GRAPHIC_HOME + ((g->p.x) / RA6963_FONT_WIDTH) + ((g->p.y) * RA6963_GRAPHIC_AREA));
  191. uint16_t addr = (RA6963_GRAPHIC_HOME + ((g->p.x >> 3) + (g->p.y << 4)));
  192. #if GFX_COMPILER_WARNING_TYPE == GFX_COMPILER_WARNING_DIRECT
  193. #warning "check function set_viewport about the shift operations if you change the resolution!"
  194. #elif GFX_COMPILER_WARNING_TYPE == GFX_COMPILER_WARNING_MACRO
  195. COMPILER_WARNING("check function set_viewport about the shift operations if you change the resolution!")
  196. #endif
  197. write_data(g, (uint8_t)addr);
  198. write_data(g, (uint8_t)(addr>>8));
  199. write_cmd(g, RA6963_SET_ADDRESS_POINTER);
  200. }
  201. LLDSPEC void gdisp_lld_write_color(GDisplay *g) {
  202. uint8_t temp;
  203. #if (RA6963_NEED_READ == FALSE)
  204. temp = RAM(g)[POS];
  205. #else
  206. temp = read_data(g);
  207. #endif
  208. if (g->p.color != White) {
  209. temp |= (1 << (RA6963_FONT_WIDTH - 1 - ((g->p.x) % RA6963_FONT_WIDTH)));
  210. }
  211. else {
  212. temp &= ~(1 << (RA6963_FONT_WIDTH - 1 - ((g->p.x) % RA6963_FONT_WIDTH)));
  213. }
  214. write_data(g, temp);
  215. #if (RA6963_NEED_READ == FALSE)
  216. RAM(g)[POS] = temp;
  217. #endif
  218. //write_cmd(g, RA6963_DATA_WRITE_AND_INCREMENT);
  219. write_cmd(g, RA6963_DATA_WRITE_AND_NONVARIALBE);
  220. }
  221. LLDSPEC void gdisp_lld_draw_pixel(GDisplay *g) {
  222. set_viewport(g);
  223. gdisp_lld_write_color(g);
  224. }
  225. #if GDISP_HARDWARE_STREAM_WRITE
  226. LLDSPEC void gdisp_lld_write_start(GDisplay *g) {
  227. //acquire_bus(g);
  228. set_viewport(g);
  229. }
  230. LLDSPEC void gdisp_lld_write_stop(GDisplay *g) {
  231. //release_bus(g);
  232. }
  233. #endif
  234. #if GDISP_HARDWARE_STREAM_READ
  235. LLDSPEC void gdisp_lld_read_start(GDisplay *g) {
  236. acquire_bus(g);
  237. set_viewport(g);
  238. setreadmode(g);
  239. dummy_read(g);
  240. }
  241. LLDSPEC color_t gdisp_lld_read_color(GDisplay *g) {
  242. uint16_t data;
  243. data = read_data(g);
  244. return gdispNative2Color(data);
  245. }
  246. LLDSPEC void gdisp_lld_read_stop(GDisplay *g) {
  247. setwritemode(g);
  248. release_bus(g);
  249. }
  250. #endif
  251. #if GDISP_HARDWARE_FILLS
  252. LLDSPEC void gdisp_lld_fill_area(GDisplay *g) {
  253. uint8_t data, j;
  254. set_viewport(g);
  255. if (g->p.color != White) {
  256. data = 0xFF; // set dot
  257. }
  258. else {
  259. data = 0; // clr dot
  260. }
  261. for (j=0; j < (g->p.cy)/8; j++) {
  262. write_data_repeat(g, data, (g->p.cx));
  263. (g->p.cy) +=8;
  264. set_viewport(g);
  265. }
  266. }
  267. #endif // GDISP_HARDWARE_FILLS
  268. #if GDISP_NEED_CONTROL && GDISP_HARDWARE_CONTROL
  269. LLDSPEC void gdisp_lld_control(GDisplay *g) {
  270. switch(g->p.x) {
  271. case GDISP_CONTROL_POWER:
  272. if (g->g.Powermode == (powermode_t)g->p.ptr)
  273. return;
  274. switch((powermode_t)g->p.ptr) {
  275. case powerOff:
  276. acquire_bus(g);
  277. write_index(g, 0x28);
  278. gfxSleepMilliseconds(10);
  279. write_index(g, 0x10);
  280. release_bus(g);
  281. break;
  282. case powerOn:
  283. acquire_bus(g);
  284. write_index(g, 0x11);
  285. gfxSleepMilliseconds(120);
  286. write_index(g, 0x29);
  287. release_bus(g);
  288. if (g->g.Powermode != powerSleep)
  289. gdisp_lld_init(g);
  290. break;
  291. case powerSleep:
  292. acquire_bus(g);
  293. write_index(g, 0x28);
  294. gfxSleepMilliseconds(10);
  295. write_index(g, 0x10);
  296. release_bus(g);
  297. break;
  298. default:
  299. return;
  300. }
  301. g->g.Powermode = (powermode_t)g->p.ptr;
  302. return;
  303. case GDISP_CONTROL_ORIENTATION:
  304. if (g->g.Orientation == (orientation_t)g->p.ptr)
  305. return;
  306. switch((orientation_t)g->p.ptr) {
  307. case GDISP_ROTATE_0:
  308. acquire_bus(g);
  309. write_index(g, 0x36);
  310. write_data(g, 0x08);
  311. release_bus(g);
  312. g->g.Height = GDISP_SCREEN_HEIGHT;
  313. g->g.Width = GDISP_SCREEN_WIDTH;
  314. break;
  315. case GDISP_ROTATE_90:
  316. acquire_bus(g);
  317. write_index(g, 0x36);
  318. write_data(g, 0x68);
  319. release_bus(g);
  320. g->g.Height = GDISP_SCREEN_WIDTH;
  321. g->g.Width = GDISP_SCREEN_HEIGHT;
  322. break;
  323. case GDISP_ROTATE_180:
  324. acquire_bus(g);
  325. write_index(g, 0x36);
  326. write_data(g, 0xC8);
  327. release_bus(g);
  328. g->g.Height = GDISP_SCREEN_HEIGHT;
  329. g->g.Width = GDISP_SCREEN_WIDTH;
  330. break;
  331. case GDISP_ROTATE_270:
  332. acquire_bus(g);
  333. write_index(g, 0x36);
  334. write_data(g, 0xA8);
  335. release_bus(g);
  336. g->g.Height = GDISP_SCREEN_WIDTH;
  337. g->g.Width = GDISP_SCREEN_HEIGHT;
  338. break;
  339. default:
  340. return;
  341. }
  342. g->g.Orientation = (orientation_t)g->p.ptr;
  343. return;
  344. default:
  345. return;
  346. }
  347. }
  348. #endif
  349. #endif /* GFX_USE_GDISP */