u8g2_periphery.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. uint8_t u8g2_gpio_and_delay_stm32(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int, void* arg_ptr) {
  10. switch(msg) {
  11. //Initialize SPI peripheral
  12. case U8X8_MSG_GPIO_AND_DELAY_INIT:
  13. /* HAL initialization contains all what we need so we can skip this part. */
  14. break;
  15. //Function which implements a delay, arg_int contains the amount of ms
  16. case U8X8_MSG_DELAY_MILLI:
  17. osDelay(arg_int);
  18. break;
  19. //Function which delays 10us
  20. case U8X8_MSG_DELAY_10MICRO:
  21. delay_us(10);
  22. break;
  23. //Function which delays 100ns
  24. case U8X8_MSG_DELAY_100NANO:
  25. asm("nop");
  26. break;
  27. // Function to define the logic level of the RESET line
  28. case U8X8_MSG_GPIO_RESET:
  29. #ifdef DEBUG
  30. fuprintf(log, "[u8g2] rst %d\n", arg_int);
  31. #endif
  32. // TODO change it to FuriRecord pin
  33. HAL_GPIO_WritePin(
  34. DISPLAY_RST_GPIO_Port, DISPLAY_RST_Pin, arg_int ? GPIO_PIN_SET : GPIO_PIN_RESET);
  35. break;
  36. default:
  37. #ifdef DEBUG
  38. fufuprintf(log, "[u8g2] unknown io %d\n", msg);
  39. #endif
  40. return 0; //A message was received which is not implemented, return 0 to indicate an error
  41. }
  42. return 1; // command processed successfully.
  43. }
  44. uint8_t u8x8_hw_spi_stm32(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int, void* arg_ptr) {
  45. switch(msg) {
  46. case U8X8_MSG_BYTE_SEND:
  47. #ifdef DEBUG
  48. fuprintf(log, "[u8g2] send %d bytes %02X\n", arg_int, ((uint8_t*)arg_ptr)[0]);
  49. #endif
  50. // TODO change it to FuriRecord SPI
  51. HAL_SPI_Transmit(&SPI_D, (uint8_t*)arg_ptr, arg_int, 10000);
  52. break;
  53. case U8X8_MSG_BYTE_SET_DC:
  54. #ifdef DEBUG
  55. fuprintf(log, "[u8g2] dc %d\n", arg_int);
  56. #endif
  57. // TODO change it to FuriRecord pin
  58. HAL_GPIO_WritePin(
  59. DISPLAY_DI_GPIO_Port, DISPLAY_DI_Pin, arg_int ? GPIO_PIN_SET : GPIO_PIN_RESET);
  60. break;
  61. case U8X8_MSG_BYTE_INIT:
  62. #ifdef DEBUG
  63. fuprintf(log, "[u8g2] init\n");
  64. #endif
  65. // TODO change it to FuriRecord pin
  66. HAL_GPIO_WritePin(DISPLAY_CS_GPIO_Port, DISPLAY_CS_Pin, GPIO_PIN_RESET);
  67. break;
  68. case U8X8_MSG_BYTE_START_TRANSFER:
  69. #ifdef DEBUG
  70. fuprintf(log, "[u8g2] start\n");
  71. #endif
  72. // TODO: SPI manager
  73. api_hal_spi_lock(&SPI_D);
  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. // TODO: SPI manager
  86. api_hal_spi_unlock(&SPI_D);
  87. break;
  88. default:
  89. #ifdef DEBUG
  90. fuprintf(log, "[u8g2] unknown xfer %d\n", msg);
  91. #endif
  92. return 0;
  93. }
  94. return 1;
  95. }