u8g2_glue.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #include "u8g2_glue.h"
  2. #include <furi-hal.h>
  3. static FuriHalSpiDevice* u8g2_periphery_display = NULL;
  4. uint8_t u8g2_gpio_and_delay_stm32(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int, void* arg_ptr) {
  5. switch(msg) {
  6. case U8X8_MSG_GPIO_AND_DELAY_INIT:
  7. /* HAL initialization contains all what we need so we can skip this part. */
  8. break;
  9. case U8X8_MSG_DELAY_MILLI:
  10. delay(arg_int);
  11. break;
  12. case U8X8_MSG_DELAY_10MICRO:
  13. delay_us(10);
  14. break;
  15. case U8X8_MSG_DELAY_100NANO:
  16. asm("nop");
  17. break;
  18. case U8X8_MSG_GPIO_RESET:
  19. hal_gpio_write(&gpio_display_rst, arg_int);
  20. break;
  21. default:
  22. return 0;
  23. }
  24. return 1;
  25. }
  26. uint8_t u8x8_hw_spi_stm32(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int, void* arg_ptr) {
  27. switch(msg) {
  28. case U8X8_MSG_BYTE_SEND:
  29. furi_hal_spi_bus_tx(u8g2_periphery_display->bus, (uint8_t*)arg_ptr, arg_int, 10000);
  30. break;
  31. case U8X8_MSG_BYTE_SET_DC:
  32. hal_gpio_write(&gpio_display_di, arg_int);
  33. break;
  34. case U8X8_MSG_BYTE_INIT:
  35. break;
  36. case U8X8_MSG_BYTE_START_TRANSFER:
  37. furi_assert(u8g2_periphery_display == NULL);
  38. u8g2_periphery_display =
  39. (FuriHalSpiDevice*)furi_hal_spi_device_get(FuriHalSpiDeviceIdDisplay);
  40. hal_gpio_write(u8g2_periphery_display->chip_select, false);
  41. break;
  42. case U8X8_MSG_BYTE_END_TRANSFER:
  43. furi_assert(u8g2_periphery_display);
  44. hal_gpio_write(u8g2_periphery_display->chip_select, true);
  45. furi_hal_spi_device_return(u8g2_periphery_display);
  46. u8g2_periphery_display = NULL;
  47. break;
  48. default:
  49. return 0;
  50. }
  51. return 1;
  52. }
  53. static const uint8_t u8x8_d_st7565_powersave0_seq[] = {
  54. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  55. U8X8_C(0x0a4), /* all pixel off, issue 142 */
  56. U8X8_C(0x0af), /* display on */
  57. U8X8_END_TRANSFER(), /* disable chip */
  58. U8X8_END() /* end of sequence */
  59. };
  60. static const uint8_t u8x8_d_st7565_powersave1_seq[] = {
  61. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  62. U8X8_C(0x0ae), /* display off */
  63. U8X8_C(0x0a5), /* enter powersafe: all pixel on, issue 142 */
  64. U8X8_END_TRANSFER(), /* disable chip */
  65. U8X8_END() /* end of sequence */
  66. };
  67. static const uint8_t u8x8_d_st7565_flip0_seq[] = {
  68. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  69. U8X8_C(0x0a1), /* segment remap a0/a1*/
  70. U8X8_C(0x0c0), /* c0: scan dir normal, c8: reverse */
  71. U8X8_END_TRANSFER(), /* disable chip */
  72. U8X8_END() /* end of sequence */
  73. };
  74. static const uint8_t u8x8_d_st7565_flip1_seq[] = {
  75. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  76. U8X8_C(0x0a0), /* segment remap a0/a1*/
  77. U8X8_C(0x0c8), /* c0: scan dir normal, c8: reverse */
  78. U8X8_END_TRANSFER(), /* disable chip */
  79. U8X8_END() /* end of sequence */
  80. };
  81. static const u8x8_display_info_t u8x8_st756x_128x64_display_info = {
  82. .chip_enable_level = 0,
  83. .chip_disable_level = 1,
  84. .post_chip_enable_wait_ns = 150, /* st7565 datasheet, table 26, tcsh */
  85. .pre_chip_disable_wait_ns = 50, /* st7565 datasheet, table 26, tcss */
  86. .reset_pulse_width_ms = 1,
  87. .post_reset_wait_ms = 1,
  88. .sda_setup_time_ns = 50, /* st7565 datasheet, table 26, tsds */
  89. .sck_pulse_width_ns = 120, /* half of cycle time (100ns according to datasheet), AVR: below 70: 8 MHz, >= 70 --> 4MHz clock */
  90. .sck_clock_hz = 4000000UL, /* since Arduino 1.6.0, the SPI bus speed in Hz. Should be 1000000000/sck_pulse_width_ns */
  91. .spi_mode = 0, /* active high, rising edge */
  92. .i2c_bus_clock_100kHz = 4,
  93. .data_setup_time_ns = 40, /* st7565 datasheet, table 24, tds8 */
  94. .write_pulse_width_ns = 80, /* st7565 datasheet, table 24, tcclw */
  95. .tile_width = 16, /* width of 16*8=128 pixel */
  96. .tile_height = 8,
  97. .default_x_offset = 0,
  98. .flipmode_x_offset = 4,
  99. .pixel_width = 128,
  100. .pixel_height = 64
  101. };
  102. uint8_t u8x8_d_st7565_common(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
  103. uint8_t x, c;
  104. uint8_t *ptr;
  105. switch(msg) {
  106. case U8X8_MSG_DISPLAY_DRAW_TILE:
  107. u8x8_cad_StartTransfer(u8x8);
  108. x = ((u8x8_tile_t *)arg_ptr)->x_pos;
  109. x *= 8;
  110. x += u8x8->x_offset;
  111. u8x8_cad_SendCmd(u8x8, 0x010 | (x>>4) );
  112. u8x8_cad_SendCmd(u8x8, 0x000 | ((x&15)));
  113. u8x8_cad_SendCmd(u8x8, 0x0b0 | (((u8x8_tile_t *)arg_ptr)->y_pos));
  114. c = ((u8x8_tile_t *)arg_ptr)->cnt;
  115. c *= 8;
  116. ptr = ((u8x8_tile_t *)arg_ptr)->tile_ptr;
  117. /*
  118. The following if condition checks the hardware limits of the st7565
  119. controller: It is not allowed to write beyond the display limits.
  120. This is in fact an issue within flip mode.
  121. */
  122. if ( c + x > 132u ) {
  123. c = 132u;
  124. c -= x;
  125. }
  126. do {
  127. u8x8_cad_SendData(u8x8, c, ptr); /* note: SendData can not handle more than 255 bytes */
  128. arg_int--;
  129. } while( arg_int > 0 );
  130. u8x8_cad_EndTransfer(u8x8);
  131. break;
  132. case U8X8_MSG_DISPLAY_SET_POWER_SAVE:
  133. if ( arg_int == 0 )
  134. u8x8_cad_SendSequence(u8x8, u8x8_d_st7565_powersave0_seq);
  135. else
  136. u8x8_cad_SendSequence(u8x8, u8x8_d_st7565_powersave1_seq);
  137. break;
  138. #ifdef U8X8_WITH_SET_CONTRAST
  139. case U8X8_MSG_DISPLAY_SET_CONTRAST:
  140. u8x8_cad_StartTransfer(u8x8);
  141. u8x8_cad_SendCmd(u8x8, 0x081 );
  142. u8x8_cad_SendArg(u8x8, arg_int >> 2 ); /* st7565 has range from 0 to 63 */
  143. u8x8_cad_EndTransfer(u8x8);
  144. break;
  145. #endif
  146. default:
  147. return 0;
  148. }
  149. return 1;
  150. }
  151. static const uint8_t u8x8_d_st756x_erc_init_seq[] = {
  152. U8X8_START_TRANSFER(),
  153. U8X8_C(0x0e2), // soft reset
  154. U8X8_C(0xA3), // CMD_SET_BIAS_7
  155. U8X8_C(0xA0), // CMD_SET_ADC_NORMAL
  156. U8X8_C(0xC8), // CMD_SET_COM_REVERSE
  157. U8X8_C(0x40), // CMD_SET_DISP_START_LINE
  158. U8X8_C(0x28 | 0x4), // CMD_SET_POWER_CONTROL | 0x4
  159. U8X8_DLY(50),
  160. U8X8_C(0x28 | 0x6), // CMD_SET_POWER_CONTROL | 0x6
  161. U8X8_DLY(50),
  162. U8X8_C(0x28 | 0x7), // CMD_SET_POWER_CONTROL | 0x7
  163. U8X8_DLY(50),
  164. U8X8_C(0x20 | 0x6), // CMD_SET_RESISTOR_RATIO | 0x6
  165. U8X8_END_TRANSFER(),
  166. U8X8_END() // end of sequence
  167. };
  168. uint8_t u8x8_d_st756x_erc(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
  169. /* call common procedure first and handle messages there */
  170. if (u8x8_d_st7565_common(u8x8, msg, arg_int, arg_ptr) == 0) {
  171. /* msg not handled, then try here */
  172. switch(msg){
  173. case U8X8_MSG_DISPLAY_SETUP_MEMORY:
  174. u8x8_d_helper_display_setup_memory(u8x8, &u8x8_st756x_128x64_display_info);
  175. break;
  176. case U8X8_MSG_DISPLAY_INIT:
  177. u8x8_d_helper_display_init(u8x8);
  178. u8x8_cad_SendSequence(u8x8, u8x8_d_st756x_erc_init_seq);
  179. break;
  180. case U8X8_MSG_DISPLAY_SET_FLIP_MODE:
  181. if ( arg_int == 0 ) {
  182. u8x8_cad_SendSequence(u8x8, u8x8_d_st7565_flip1_seq);
  183. u8x8->x_offset = u8x8->display_info->default_x_offset;
  184. } else {
  185. u8x8_cad_SendSequence(u8x8, u8x8_d_st7565_flip0_seq);
  186. u8x8->x_offset = u8x8->display_info->flipmode_x_offset;
  187. }
  188. break;
  189. default:
  190. /* msg unknown */
  191. return 0;
  192. }
  193. }
  194. return 1;
  195. }
  196. void u8g2_Setup_st756x_erc(u8g2_t *u8g2, const u8g2_cb_t *rotation, u8x8_msg_cb byte_cb, u8x8_msg_cb gpio_and_delay_cb) {
  197. uint8_t tile_buf_height;
  198. uint8_t *buf;
  199. u8g2_SetupDisplay(u8g2, u8x8_d_st756x_erc, u8x8_cad_001, byte_cb, gpio_and_delay_cb);
  200. buf = u8g2_m_16_8_f(&tile_buf_height);
  201. u8g2_SetupBuffer(u8g2, buf, tile_buf_height, u8g2_ll_hvline_vertical_top_lsb, rotation);
  202. }