display-u8g2.c 5.7 KB

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