u8g2_periphery.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include "u8g2/u8g2.h"
  2. #include <furi.h>
  3. #include <api-hal.h>
  4. #include <main.h>
  5. extern SPI_HandleTypeDef SPI_D;
  6. // TODO: fix log
  7. #ifdef DEBUG
  8. #undef DEBUG
  9. #endif
  10. uint8_t 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. printf("[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. printf("[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. 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. printf("[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(&SPI_D, (uint8_t*)arg_ptr, arg_int, 10000);
  53. break;
  54. case U8X8_MSG_BYTE_SET_DC:
  55. #ifdef DEBUG
  56. printf("[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. printf("[u8g2] init\r\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. printf("[u8g2] start\r\n");
  72. #endif
  73. // TODO: SPI manager
  74. api_hal_spi_lock_device(&display_spi);
  75. // TODO change it to FuriRecord pin
  76. HAL_GPIO_WritePin(DISPLAY_CS_GPIO_Port, DISPLAY_CS_Pin, GPIO_PIN_RESET);
  77. asm("nop");
  78. break;
  79. case U8X8_MSG_BYTE_END_TRANSFER:
  80. #ifdef DEBUG
  81. printf("[u8g2] end\r\n");
  82. #endif
  83. asm("nop");
  84. // TODO change it to FuriRecord pin
  85. HAL_GPIO_WritePin(DISPLAY_CS_GPIO_Port, DISPLAY_CS_Pin, GPIO_PIN_SET);
  86. // TODO: SPI manager
  87. api_hal_spi_unlock_device(&display_spi);
  88. break;
  89. default:
  90. #ifdef DEBUG
  91. printf("[u8g2] unknown xfer %d\n", msg);
  92. #endif
  93. return 0;
  94. }
  95. return 1;
  96. }