u8g2_glue.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #include "u8g2_glue.h"
  2. #include <furi_hal.h>
  3. uint8_t u8g2_gpio_and_delay_stm32(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int, void* arg_ptr) {
  4. switch(msg) {
  5. case U8X8_MSG_GPIO_AND_DELAY_INIT:
  6. /* HAL initialization contains all what we need so we can skip this part. */
  7. break;
  8. case U8X8_MSG_DELAY_MILLI:
  9. furi_hal_delay_ms(arg_int);
  10. break;
  11. case U8X8_MSG_DELAY_10MICRO:
  12. furi_hal_delay_us(10);
  13. break;
  14. case U8X8_MSG_DELAY_100NANO:
  15. asm("nop");
  16. break;
  17. case U8X8_MSG_GPIO_RESET:
  18. furi_hal_gpio_write(&gpio_display_rst, arg_int);
  19. break;
  20. default:
  21. return 0;
  22. }
  23. return 1;
  24. }
  25. uint8_t u8x8_hw_spi_stm32(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int, void* arg_ptr) {
  26. switch(msg) {
  27. case U8X8_MSG_BYTE_SEND:
  28. furi_hal_spi_bus_tx(&furi_hal_spi_bus_handle_display, (uint8_t*)arg_ptr, arg_int, 10000);
  29. break;
  30. case U8X8_MSG_BYTE_SET_DC:
  31. furi_hal_gpio_write(&gpio_display_di, arg_int);
  32. break;
  33. case U8X8_MSG_BYTE_INIT:
  34. break;
  35. case U8X8_MSG_BYTE_START_TRANSFER:
  36. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_display);
  37. break;
  38. case U8X8_MSG_BYTE_END_TRANSFER:
  39. furi_hal_spi_release(&furi_hal_spi_bus_handle_display);
  40. break;
  41. default:
  42. return 0;
  43. }
  44. return 1;
  45. }
  46. #define ST756X_CMD_ON_OFF 0b10101110 /**< 0:0 Switch Display ON/OFF: last bit */
  47. #define ST756X_CMD_SET_LINE 0b01000000 /**< 0:0 Set Start Line: last 6 bits */
  48. #define ST756X_CMD_SET_PAGE 0b10110000 /**< 0:0 Set Page address: last 4 bits */
  49. #define ST756X_CMD_SET_COLUMN_MSB 0b00010000 /**< 0:0 Set Column MSB: last 4 bits */
  50. #define ST756X_CMD_SET_COLUMN_LSB 0b00000000 /**< 0:0 Set Column LSB: last 4 bits */
  51. #define ST756X_CMD_SEG_DIRECTION 0b10100000 /**< 0:0 Reverse scan direction of SEG: last bit */
  52. #define ST756X_CMD_INVERSE_DISPLAY 0b10100110 /**< 0:0 Invert display: last bit */
  53. #define ST756X_CMD_ALL_PIXEL_ON 0b10100100 /**< 0:0 Set all pixel on: last bit */
  54. #define ST756X_CMD_BIAS_SELECT 0b10100010 /**< 0:0 Select 1/9(0) or 1/7(1) bias: last bit */
  55. #define ST756X_CMD_R_M_W 0b11100000 /**< 0:0 Enter Read Modify Write mode: read+0, write+1 */
  56. #define ST756X_CMD_END 0b11101110 /**< 0:0 Exit Read Modify Write mode */
  57. #define ST756X_CMD_RESET 0b11100010 /**< 0:0 Software Reset */
  58. #define ST756X_CMD_COM_DIRECTION 0b11000000 /**< 0:0 Com direction reverse: +0b1000 */
  59. #define ST756X_CMD_POWER_CONTROL 0b00101000 /**< 0:0 Power control: last 3 bits VB:VR:VF */
  60. #define ST756X_CMD_REGULATION_RATIO 0b00100000 /**< 0:0 Regulation resistor ration: last 3bits */
  61. #define ST756X_CMD_SET_EV 0b10000001 /**< 0:0 Set electronic volume: 5 bits in next byte */
  62. #define ST756X_CMD_SET_BOOSTER \
  63. 0b11111000 /**< 0:0 Set Booster level, 4X(0) or 5X(1): last bit in next byte */
  64. #define ST756X_CMD_NOP 0b11100011 /**< 0:0 No operation */
  65. static const uint8_t u8x8_d_st756x_powersave0_seq[] = {
  66. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  67. U8X8_C(ST756X_CMD_ALL_PIXEL_ON | 0b0), /* all pixel off */
  68. U8X8_C(ST756X_CMD_ON_OFF | 0b1), /* display on */
  69. U8X8_END_TRANSFER(), /* disable chip */
  70. U8X8_END() /* end of sequence */
  71. };
  72. static const uint8_t u8x8_d_st756x_powersave1_seq[] = {
  73. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  74. U8X8_C(ST756X_CMD_ON_OFF | 0b0), /* display off */
  75. U8X8_C(ST756X_CMD_ALL_PIXEL_ON | 0b1), /* all pixel on */
  76. U8X8_END_TRANSFER(), /* disable chip */
  77. U8X8_END() /* end of sequence */
  78. };
  79. static const uint8_t u8x8_d_st756x_flip0_seq[] = {
  80. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  81. U8X8_C(0x0a1), /* segment remap a0/a1*/
  82. U8X8_C(0x0c0), /* c0: scan dir normal, c8: reverse */
  83. U8X8_END_TRANSFER(), /* disable chip */
  84. U8X8_END() /* end of sequence */
  85. };
  86. static const uint8_t u8x8_d_st756x_flip1_seq[] = {
  87. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  88. U8X8_C(0x0a0), /* segment remap a0/a1*/
  89. U8X8_C(0x0c8), /* c0: scan dir normal, c8: reverse */
  90. U8X8_END_TRANSFER(), /* disable chip */
  91. U8X8_END() /* end of sequence */
  92. };
  93. static const u8x8_display_info_t u8x8_st756x_128x64_display_info = {
  94. .chip_enable_level = 0,
  95. .chip_disable_level = 1,
  96. .post_chip_enable_wait_ns = 150, /* st7565 datasheet, table 26, tcsh */
  97. .pre_chip_disable_wait_ns = 50, /* st7565 datasheet, table 26, tcss */
  98. .reset_pulse_width_ms = 1,
  99. .post_reset_wait_ms = 1,
  100. .sda_setup_time_ns = 50, /* st7565 datasheet, table 26, tsds */
  101. .sck_pulse_width_ns =
  102. 120, /* half of cycle time (100ns according to datasheet), AVR: below 70: 8 MHz, >= 70 --> 4MHz clock */
  103. .sck_clock_hz =
  104. 4000000UL, /* since Arduino 1.6.0, the SPI bus speed in Hz. Should be 1000000000/sck_pulse_width_ns */
  105. .spi_mode = 0, /* active high, rising edge */
  106. .i2c_bus_clock_100kHz = 4,
  107. .data_setup_time_ns = 40, /* st7565 datasheet, table 24, tds8 */
  108. .write_pulse_width_ns = 80, /* st7565 datasheet, table 24, tcclw */
  109. .tile_width = 16, /* width of 16*8=128 pixel */
  110. .tile_height = 8,
  111. .default_x_offset = 0,
  112. .flipmode_x_offset = 4,
  113. .pixel_width = 128,
  114. .pixel_height = 64};
  115. uint8_t u8x8_d_st756x_common(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int, void* arg_ptr) {
  116. uint8_t x, c;
  117. uint8_t* ptr;
  118. switch(msg) {
  119. case U8X8_MSG_DISPLAY_DRAW_TILE:
  120. u8x8_cad_StartTransfer(u8x8);
  121. x = ((u8x8_tile_t*)arg_ptr)->x_pos;
  122. x *= 8;
  123. x += u8x8->x_offset;
  124. u8x8_cad_SendCmd(u8x8, 0x010 | (x >> 4));
  125. u8x8_cad_SendCmd(u8x8, 0x000 | ((x & 15)));
  126. u8x8_cad_SendCmd(u8x8, 0x0b0 | (((u8x8_tile_t*)arg_ptr)->y_pos));
  127. c = ((u8x8_tile_t*)arg_ptr)->cnt;
  128. c *= 8;
  129. ptr = ((u8x8_tile_t*)arg_ptr)->tile_ptr;
  130. /*
  131. The following if condition checks the hardware limits of the st7565
  132. controller: It is not allowed to write beyond the display limits.
  133. This is in fact an issue within flip mode.
  134. */
  135. if(c + x > 132u) {
  136. c = 132u;
  137. c -= x;
  138. }
  139. do {
  140. u8x8_cad_SendData(
  141. u8x8, c, ptr); /* note: SendData can not handle more than 255 bytes */
  142. arg_int--;
  143. } while(arg_int > 0);
  144. u8x8_cad_EndTransfer(u8x8);
  145. break;
  146. case U8X8_MSG_DISPLAY_SET_POWER_SAVE:
  147. if(arg_int == 0)
  148. u8x8_cad_SendSequence(u8x8, u8x8_d_st756x_powersave0_seq);
  149. else
  150. u8x8_cad_SendSequence(u8x8, u8x8_d_st756x_powersave1_seq);
  151. break;
  152. #ifdef U8X8_WITH_SET_CONTRAST
  153. case U8X8_MSG_DISPLAY_SET_CONTRAST:
  154. u8x8_cad_StartTransfer(u8x8);
  155. u8x8_cad_SendCmd(u8x8, ST756X_CMD_SET_EV);
  156. u8x8_cad_SendArg(u8x8, arg_int >> 2); /* st7565 has range from 0 to 63 */
  157. u8x8_cad_EndTransfer(u8x8);
  158. break;
  159. #endif
  160. default:
  161. return 0;
  162. }
  163. return 1;
  164. }
  165. void u8x8_d_st756x_init(u8x8_t* u8x8, uint8_t contrast, uint8_t regulation_ratio, bool bias) {
  166. contrast = contrast & 0b00111111;
  167. regulation_ratio = regulation_ratio & 0b111;
  168. u8x8_cad_StartTransfer(u8x8);
  169. // Reset
  170. u8x8_cad_SendCmd(u8x8, ST756X_CMD_RESET);
  171. // Bias: 1/7(0b1) or 1/9(0b0)
  172. u8x8_cad_SendCmd(u8x8, ST756X_CMD_BIAS_SELECT | bias);
  173. // Page, Line and Segment config
  174. u8x8_cad_SendCmd(u8x8, ST756X_CMD_SEG_DIRECTION);
  175. u8x8_cad_SendCmd(u8x8, ST756X_CMD_COM_DIRECTION | 0b1000);
  176. u8x8_cad_SendCmd(u8x8, ST756X_CMD_SET_LINE);
  177. // Set Regulation Ratio
  178. u8x8_cad_SendCmd(u8x8, ST756X_CMD_REGULATION_RATIO | regulation_ratio);
  179. // Set EV
  180. u8x8_cad_SendCmd(u8x8, ST756X_CMD_SET_EV);
  181. u8x8_cad_SendArg(u8x8, contrast);
  182. // Enable power
  183. u8x8_cad_SendCmd(u8x8, ST756X_CMD_POWER_CONTROL | 0b111);
  184. u8x8_cad_EndTransfer(u8x8);
  185. }
  186. uint8_t u8x8_d_st756x_flipper(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int, void* arg_ptr) {
  187. /* call common procedure first and handle messages there */
  188. if(u8x8_d_st756x_common(u8x8, msg, arg_int, arg_ptr) == 0) {
  189. /* msg not handled, then try here */
  190. switch(msg) {
  191. case U8X8_MSG_DISPLAY_SETUP_MEMORY:
  192. u8x8_d_helper_display_setup_memory(u8x8, &u8x8_st756x_128x64_display_info);
  193. break;
  194. case U8X8_MSG_DISPLAY_INIT:
  195. u8x8_d_helper_display_init(u8x8);
  196. FuriHalVersionDisplay display = furi_hal_version_get_hw_display();
  197. if(display == FuriHalVersionDisplayMgg) {
  198. /* MGG v0+(ST7567)
  199. * EV = 32
  200. * RR = V0 / ((1 - (63 - EV) / 162) * 2.1)
  201. * RR = 10 / ((1 - (63 - 32) / 162) * 2.1) ~= 5.88 is 6 (0b110)
  202. * Bias = 1/9 (false)
  203. */
  204. u8x8_d_st756x_init(u8x8, 32, 0b110, false);
  205. } else {
  206. /* ERC v1(ST7565) and v2(ST7567)
  207. * EV = 33
  208. * RR = V0 / ((1 - (63 - EV) / 162) * 2.1)
  209. * RR = 9.3 / ((1 - (63 - 32) / 162) * 2.1) ~= 5.47 is 5.5 (0b101)
  210. * Bias = 1/9 (false)
  211. */
  212. u8x8_d_st756x_init(u8x8, 33, 0b101, false);
  213. }
  214. break;
  215. case U8X8_MSG_DISPLAY_SET_FLIP_MODE:
  216. if(arg_int == 0) {
  217. u8x8_cad_SendSequence(u8x8, u8x8_d_st756x_flip1_seq);
  218. u8x8->x_offset = u8x8->display_info->default_x_offset;
  219. } else {
  220. u8x8_cad_SendSequence(u8x8, u8x8_d_st756x_flip0_seq);
  221. u8x8->x_offset = u8x8->display_info->flipmode_x_offset;
  222. }
  223. break;
  224. default:
  225. /* msg unknown */
  226. return 0;
  227. }
  228. }
  229. return 1;
  230. }
  231. void u8g2_Setup_st756x_flipper(
  232. u8g2_t* u8g2,
  233. const u8g2_cb_t* rotation,
  234. u8x8_msg_cb byte_cb,
  235. u8x8_msg_cb gpio_and_delay_cb) {
  236. uint8_t tile_buf_height;
  237. uint8_t* buf;
  238. u8g2_SetupDisplay(u8g2, u8x8_d_st756x_flipper, u8x8_cad_001, byte_cb, gpio_and_delay_cb);
  239. buf = u8g2_m_16_8_f(&tile_buf_height);
  240. u8g2_SetupBuffer(u8g2, buf, tile_buf_height, u8g2_ll_hvline_vertical_top_lsb, rotation);
  241. }