u8g2_glue.c 10 KB

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