display-u8g2.c 5.5 KB

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