µGFX library fork

gdisp_lld_SSD1351.c 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. #include "gfx.h"
  8. #if GFX_USE_GDISP
  9. #if defined(GDISP_SCREEN_HEIGHT) || defined(GDISP_SCREEN_HEIGHT)
  10. #if GFX_COMPILER_WARNING_TYPE == GFX_COMPILER_WARNING_DIRECT
  11. #warning "GDISP: This low level driver does not support setting a screen size. It is being ignored."
  12. #elif GFX_COMPILER_WARNING_TYPE == GFX_COMPILER_WARNING_MACRO
  13. COMPILER_WARNING("GDISP: This low level driver does not support setting a screen size. It is being ignored.")
  14. #endif
  15. #undef GDISP_SCREEN_WIDTH
  16. #undef GDISP_SCREEN_HEIGHT
  17. #endif
  18. #define GDISP_DRIVER_VMT GDISPVMT_SSD1351
  19. #include "gdisp_lld_config.h"
  20. #include "../../../src/gdisp/gdisp_driver.h"
  21. #include "board_SSD1351.h"
  22. /*===========================================================================*/
  23. /* Driver local definitions. */
  24. /*===========================================================================*/
  25. #ifndef GDISP_SCREEN_HEIGHT
  26. #define GDISP_SCREEN_HEIGHT 128
  27. #endif
  28. #ifndef GDISP_SCREEN_WIDTH
  29. #define GDISP_SCREEN_WIDTH 128
  30. #endif
  31. #ifndef GDISP_INITIAL_CONTRAST
  32. #define GDISP_INITIAL_CONTRAST 100
  33. #endif
  34. #ifndef GDISP_INITIAL_BACKLIGHT
  35. #define GDISP_INITIAL_BACKLIGHT 100
  36. #endif
  37. #include "SSD1351.h"
  38. /*===========================================================================*/
  39. /* Driver local functions. */
  40. /*===========================================================================*/
  41. // Some common routines and macros
  42. #define dummy_read(g) { volatile uint16_t dummy; dummy = read_data(g); (void) dummy; }
  43. #define write_reg(g, reg, data) { write_cmd(g, reg); write_data(g, data); }
  44. /*===========================================================================*/
  45. /* Driver exported functions. */
  46. /*===========================================================================*/
  47. LLDSPEC bool_t gdisp_lld_init(GDisplay *g) {
  48. // No private area for this controller
  49. g->priv = 0;
  50. // Initialise the board interface
  51. init_board(g);
  52. // Hardware reset
  53. setpin_reset(g, TRUE);
  54. gfxSleepMilliseconds(20);
  55. setpin_reset(g, FALSE);
  56. gfxSleepMilliseconds(20);
  57. // Get the bus for the following initialisation commands
  58. acquire_bus(g);
  59. write_reg(g, 0xFD, 0x12); // unlock OLED driver IC
  60. write_reg(g, 0xFD, 0xB1); // make commands A1, B1, B3, BB, BE, C1 accesible in unlocked state
  61. write_cmd(g, 0xAE); // sleep mode ON (display off)
  62. write_reg(g, 0xB3, 0xF1); // Front clock divider / osc freq - Osc = 0xF; div = 2
  63. write_reg(g, 0xCA, 127); // set MUX ratio
  64. write_reg(g, 0xA0, 0b01110100); // Set re-map / color depth
  65. // [0] : address increment (0: horizontal, 1: vertical, reset 0)
  66. // [1] : column remap (0: 0..127, 1: 127..0, reset 0)
  67. // [2] : color remap (0: A->B->C, 1: C->B->A, reset 0)
  68. // [3] : reserved
  69. // [4] : column scan direction (0: top->down, 1: bottom->up, reset 0)
  70. // [5] : odd/even split COM (0: disable, 1: enable, reset 1)
  71. // [6..7] : color depth (00,01: 65k, 10: 262k, 11: 262k format 2)
  72. write_cmd(g, 0x15); // Set Column address
  73. write_data(g, 0x00); // start
  74. write_data(g, 0x7F); // end
  75. write_cmd(g, 0x75); // set row address
  76. write_data(g, 0x00); // start
  77. write_data(g, 0x7F); // end
  78. write_reg(g, 0xA1, 0x00); // set display start line - 0
  79. write_reg(g, 0xA2, 0x00); // set display offset - 0
  80. write_reg(g, 0xB5, 0x00); // set GPIO - both HiZ, input disabled
  81. write_reg(g, 0xAB, 0x01); // enable internal VDD regulator
  82. write_reg(g, 0xB1, 0x32); // set reset / pre-charge period - phase 2: 3 DCLKs, phase 1: 5 DCLKs
  83. write_reg(g, 0xBE, 0x05); // set VComH voltage - 0.82*Vcc
  84. write_reg(g, 0xBB, 0x17); // set pre-charge voltage - 0.6*Vcc
  85. write_cmd(g, 0xA6); // set display mode: reset to normal display
  86. write_cmd(g, 0xC1); // set contrast current for A,B,C
  87. write_data(g, 0xC8);
  88. write_data(g, 0x80);
  89. write_data(g, 0xC8);
  90. write_reg(g, 0xC7, 0x0F); // master contrast current control - no change
  91. write_cmd(g, 0xB4); // set segment low voltage
  92. write_data(g, 0xA0); // external VSL
  93. write_data(g, 0xB5); // hard value
  94. write_data(g, 0x55); // hard value
  95. write_reg(g, 0xB6, 0x01); // set second pre-charge period - 1 DCLKs
  96. write_cmd(g, 0xAF); // sleep mode OFF (display on)
  97. write_cmd(g, 0x5C); // write to RAM
  98. // Finish Init
  99. post_init_board(g);
  100. // Release the bus
  101. release_bus(g);
  102. /* Turn on the back-light */
  103. set_backlight(g, GDISP_INITIAL_BACKLIGHT);
  104. /* Initialise the GDISP structure */
  105. g->g.Width = GDISP_SCREEN_WIDTH;
  106. g->g.Height = GDISP_SCREEN_HEIGHT;
  107. g->g.Orientation = GDISP_ROTATE_0;
  108. g->g.Powermode = powerOn;
  109. g->g.Backlight = GDISP_INITIAL_BACKLIGHT;
  110. g->g.Contrast = GDISP_INITIAL_CONTRAST;
  111. return TRUE;
  112. }
  113. #if GDISP_HARDWARE_STREAM_WRITE
  114. LLDSPEC void gdisp_lld_write_start(GDisplay *g) {
  115. acquire_bus(g);
  116. write_cmd(g, SSD1351_SET_COLUMN_ADDRESS);
  117. write_data(g, g->p.x);
  118. write_data(g, g->p.x + g->p.cx - 1);
  119. write_cmd(g, SSD1351_SET_ROW_ADDRESS);
  120. write_data(g, g->p.y);
  121. write_data(g, g->p.y + g->p.cy - 1);
  122. write_cmd(g, SSD1351_WRITE_RAM);
  123. }
  124. LLDSPEC void gdisp_lld_write_color(GDisplay *g) {
  125. LLDCOLOR_TYPE c;
  126. c = gdispColor2Native(g->p.color);
  127. write_data(g, c >> 8);
  128. write_data(g, c & 0xFF);
  129. }
  130. LLDSPEC void gdisp_lld_write_stop(GDisplay *g) {
  131. release_bus(g);
  132. }
  133. #endif
  134. #if GDISP_HARDWARE_STREAM_READ
  135. #error "SSD1351 - Stream Read is not supported yet"
  136. LLDSPEC void gdisp_lld_read_start(GDisplay *g) {
  137. acquire_bus(g);
  138. //set_viewport(g);
  139. //write_index(g, 0x2E);
  140. setreadmode(g);
  141. //dummy_read(g);
  142. }
  143. LLDSPEC color_t gdisp_lld_read_color(GDisplay *g) {
  144. uint16_t data;
  145. data = read_data(g);
  146. return gdispNative2Color(data);
  147. }
  148. LLDSPEC void gdisp_lld_read_stop(GDisplay *g) {
  149. setwritemode(g);
  150. release_bus(g);
  151. }
  152. #endif
  153. #if GDISP_NEED_CONTROL && GDISP_HARDWARE_CONTROL
  154. #error "SSD1351 - Hardware control is not supported yet"
  155. LLDSPEC void gdisp_lld_control(GDisplay *g) {
  156. switch(g->p.x) {
  157. case GDISP_CONTROL_POWER:
  158. if (g->g.Powermode == (powermode_t)g->p.ptr)
  159. return;
  160. switch((powermode_t)g->p.ptr) {
  161. case powerOff:
  162. case powerSleep:
  163. case powerDeepSleep:
  164. acquire_bus(g);
  165. //TODO
  166. release_bus(g);
  167. break;
  168. case powerOn:
  169. acquire_bus(g);
  170. //TODO
  171. release_bus(g);
  172. break;
  173. default:
  174. return;
  175. }
  176. g->g.Powermode = (powermode_t)g->p.ptr;
  177. return;
  178. case GDISP_CONTROL_ORIENTATION:
  179. if (g->g.Orientation == (orientation_t)g->p.ptr)
  180. return;
  181. switch((orientation_t)g->p.ptr) {
  182. case GDISP_ROTATE_0:
  183. acquire_bus(g);
  184. //TODO
  185. release_bus(g);
  186. g->g.Height = GDISP_SCREEN_HEIGHT;
  187. g->g.Width = GDISP_SCREEN_WIDTH;
  188. break;
  189. case GDISP_ROTATE_90:
  190. acquire_bus(g);
  191. //TODO
  192. release_bus(g);
  193. g->g.Height = GDISP_SCREEN_WIDTH;
  194. g->g.Width = GDISP_SCREEN_HEIGHT;
  195. break;
  196. case GDISP_ROTATE_180:
  197. acquire_bus(g);
  198. //TODO
  199. release_bus(g);
  200. g->g.Height = GDISP_SCREEN_HEIGHT;
  201. g->g.Width = GDISP_SCREEN_WIDTH;
  202. break;
  203. case GDISP_ROTATE_270:
  204. acquire_bus(g);
  205. //TODO
  206. release_bus(g);
  207. g->g.Height = GDISP_SCREEN_WIDTH;
  208. g->g.Width = GDISP_SCREEN_HEIGHT;
  209. break;
  210. default:
  211. return;
  212. }
  213. g->g.Orientation = (orientation_t)g->p.ptr;
  214. return;
  215. case GDISP_CONTROL_BACKLIGHT:
  216. if ((unsigned)g->p.ptr > 100)
  217. g->p.ptr = (void *)100;
  218. set_backlight(g, (unsigned)g->p.ptr);
  219. g->g.Backlight = (unsigned)g->p.ptr;
  220. return;
  221. //case GDISP_CONTROL_CONTRAST:
  222. default:
  223. return;
  224. }
  225. }
  226. #endif
  227. #endif /* GFX_USE_GDISP */