display-u8g2.c 5.5 KB

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