display-u8g2.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #include "u8g2/u8g2.h"
  2. #include "flipper.h"
  3. extern SPI_HandleTypeDef hspi1;
  4. // #define DEBUG 1
  5. // TODO rewrite u8g2 to pass thread-local context in this handlers
  6. static uint8_t
  7. u8g2_gpio_and_delay_stm32(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int, void* arg_ptr) {
  8. switch(msg) {
  9. //Initialize SPI peripheral
  10. case U8X8_MSG_GPIO_AND_DELAY_INIT:
  11. /* HAL initialization contains all what we need so we can skip this part. */
  12. break;
  13. //Function which implements a delay, arg_int contains the amount of ms
  14. case U8X8_MSG_DELAY_MILLI:
  15. osDelay(arg_int);
  16. break;
  17. //Function which delays 10us
  18. case U8X8_MSG_DELAY_10MICRO:
  19. delay_us(10);
  20. break;
  21. //Function which delays 100ns
  22. case U8X8_MSG_DELAY_100NANO:
  23. asm("nop");
  24. break;
  25. // Function to define the logic level of the RESET line
  26. case U8X8_MSG_GPIO_RESET:
  27. #ifdef DEBUG
  28. fuprintf(log, "[u8g2] rst %d\n", arg_int);
  29. #endif
  30. // TODO change it to FuriRecord pin
  31. HAL_GPIO_WritePin(
  32. DISPLAY_RST_GPIO_Port, DISPLAY_RST_Pin, arg_int ? GPIO_PIN_SET : GPIO_PIN_RESET);
  33. break;
  34. default:
  35. #ifdef DEBUG
  36. fufuprintf(log, "[u8g2] unknown io %d\n", msg);
  37. #endif
  38. return 0; //A message was received which is not implemented, return 0 to indicate an error
  39. }
  40. return 1; // command processed successfully.
  41. }
  42. static uint8_t u8x8_hw_spi_stm32(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int, void* arg_ptr) {
  43. switch(msg) {
  44. case U8X8_MSG_BYTE_SEND:
  45. #ifdef DEBUG
  46. fuprintf(log, "[u8g2] send %d bytes %02X\n", arg_int, ((uint8_t*)arg_ptr)[0]);
  47. #endif
  48. // TODO change it to FuriRecord SPI
  49. HAL_SPI_Transmit(&hspi1, (uint8_t*)arg_ptr, arg_int, 10000);
  50. break;
  51. case U8X8_MSG_BYTE_SET_DC:
  52. #ifdef DEBUG
  53. fuprintf(log, "[u8g2] dc %d\n", arg_int);
  54. #endif
  55. // TODO change it to FuriRecord pin
  56. HAL_GPIO_WritePin(
  57. DISPLAY_DI_GPIO_Port, DISPLAY_DI_Pin, arg_int ? GPIO_PIN_SET : GPIO_PIN_RESET);
  58. break;
  59. case U8X8_MSG_BYTE_INIT:
  60. #ifdef DEBUG
  61. fuprintf(log, "[u8g2] init\n");
  62. #endif
  63. // TODO change it to FuriRecord pin
  64. HAL_GPIO_WritePin(DISPLAY_CS_GPIO_Port, DISPLAY_CS_Pin, GPIO_PIN_RESET);
  65. break;
  66. case U8X8_MSG_BYTE_START_TRANSFER:
  67. #ifdef DEBUG
  68. fuprintf(log, "[u8g2] start\n");
  69. #endif
  70. // TODO change it to FuriRecord pin
  71. HAL_GPIO_WritePin(DISPLAY_CS_GPIO_Port, DISPLAY_CS_Pin, GPIO_PIN_RESET);
  72. asm("nop");
  73. break;
  74. case U8X8_MSG_BYTE_END_TRANSFER:
  75. #ifdef DEBUG
  76. fuprintf(log, "[u8g2] end\n");
  77. #endif
  78. asm("nop");
  79. // TODO change it to FuriRecord pin
  80. HAL_GPIO_WritePin(DISPLAY_CS_GPIO_Port, DISPLAY_CS_Pin, GPIO_PIN_SET);
  81. break;
  82. default:
  83. #ifdef DEBUG
  84. fuprintf(log, "[u8g2] unknown xfer %d\n", msg);
  85. #endif
  86. return 0;
  87. }
  88. return 1;
  89. }
  90. typedef struct {
  91. SemaphoreHandle_t update; // queue to pass events from callback to app thread
  92. FuriRecordSubscriber* log; // app logger
  93. } DisplayCtx;
  94. static void handle_fb_change(const void* fb, size_t fb_size, void* raw_ctx) {
  95. DisplayCtx* ctx = (DisplayCtx*)raw_ctx; // make right type
  96. fuprintf(ctx->log, "[display_u8g2] change fb\n");
  97. // send update to app thread
  98. xSemaphoreGive(ctx->update);
  99. }
  100. void display_u8g2(void* p) {
  101. FuriRecordSubscriber* log = get_default_log();
  102. // TODO we need different app to contol backlight
  103. HAL_GPIO_WritePin(DISPLAY_BACKLIGHT_GPIO_Port, DISPLAY_BACKLIGHT_Pin, GPIO_PIN_SET);
  104. u8g2_t _u8g2;
  105. u8g2_Setup_st7565_erc12864_alt_f(
  106. &_u8g2, U8G2_R0, u8x8_hw_spi_stm32, u8g2_gpio_and_delay_stm32);
  107. u8g2_InitDisplay(
  108. &_u8g2); // send init sequence to the display, display is in sleep mode after this
  109. u8g2_SetContrast(&_u8g2, 36);
  110. if(!furi_create("u8g2_fb", (void*)&_u8g2, sizeof(_u8g2))) {
  111. fuprintf(log, "[display_u8g2] cannot create fb record\n");
  112. furiac_exit(NULL);
  113. }
  114. StaticSemaphore_t event_descriptor;
  115. // create stack-based counting semaphore
  116. SemaphoreHandle_t update = xSemaphoreCreateCountingStatic(255, 0, &event_descriptor);
  117. if(update == NULL) {
  118. fuprintf(log, "[display_u8g2] cannot create update semaphore\n");
  119. furiac_exit(NULL);
  120. }
  121. // save log and event queue in context structure
  122. DisplayCtx ctx = {.update = update, .log = log};
  123. // subscribe to record. ctx will be passed to handle_fb_change
  124. FuriRecordSubscriber* fb_record =
  125. furi_open("u8g2_fb", false, false, handle_fb_change, NULL, &ctx);
  126. if(fb_record == NULL) {
  127. fuprintf(log, "[display] cannot open fb record\n");
  128. furiac_exit(NULL);
  129. }
  130. u8g2_t* u8g2 = (u8g2_t*)furi_take(fb_record);
  131. u8g2_SetPowerSave(u8g2, 0); // wake up display
  132. u8g2_SendBuffer(u8g2);
  133. furi_give(fb_record);
  134. // we ready to work
  135. furiac_ready();
  136. while(1) {
  137. // wait for event
  138. if(xSemaphoreTake(update, 10000) == pdTRUE) {
  139. HAL_GPIO_WritePin(DISPLAY_BACKLIGHT_GPIO_Port, DISPLAY_BACKLIGHT_Pin, GPIO_PIN_SET);
  140. u8g2_t* u8g2 = (u8g2_t*)furi_take(fb_record);
  141. u8g2_SetPowerSave(u8g2, 0); // wake up display
  142. u8g2_SendBuffer(u8g2);
  143. furi_give(fb_record);
  144. } else {
  145. // TODO we need different app to contol backlight
  146. HAL_GPIO_WritePin(DISPLAY_BACKLIGHT_GPIO_Port, DISPLAY_BACKLIGHT_Pin, GPIO_PIN_RESET);
  147. }
  148. }
  149. }