u8g2_vendor.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include "u8g2_support.h"
  2. #include "main.h"
  3. #include "cmsis_os.h"
  4. #include "gpio.h"
  5. #include <stdio.h>
  6. extern SPI_HandleTypeDef hspi1;
  7. // #define DEBUG 1
  8. uint8_t u8g2_gpio_and_delay_stm32(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) {
  9. switch(msg){
  10. //Initialize SPI peripheral
  11. case U8X8_MSG_GPIO_AND_DELAY_INIT:
  12. /* HAL initialization contains all what we need so we can skip this part. */
  13. break;
  14. //Function which implements a delay, arg_int contains the amount of ms
  15. case U8X8_MSG_DELAY_MILLI:
  16. osDelay(arg_int);
  17. break;
  18. //Function which delays 10us
  19. case U8X8_MSG_DELAY_10MICRO:
  20. delay_us(10);
  21. break;
  22. //Function which delays 100ns
  23. case U8X8_MSG_DELAY_100NANO:
  24. asm("nop");
  25. break;
  26. //Function to define the logic level of the RESET line
  27. case U8X8_MSG_GPIO_RESET:
  28. #ifdef DEBUG
  29. printf("[u8g2] rst %d\n", arg_int);
  30. #endif
  31. HAL_GPIO_WritePin(DISPLAY_RST_GPIO_Port, DISPLAY_RST_Pin, arg_int ? GPIO_PIN_SET : GPIO_PIN_RESET);
  32. break;
  33. default:
  34. #ifdef DEBUG
  35. printf("[u8g2] unknown io %d\n", msg);
  36. #endif
  37. return 0; //A message was received which is not implemented, return 0 to indicate an error
  38. }
  39. return 1; // command processed successfully.
  40. }
  41. uint8_t u8x8_hw_spi_stm32(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr){
  42. switch (msg) {
  43. case U8X8_MSG_BYTE_SEND:
  44. #ifdef DEBUG
  45. printf("[u8g2] send %d bytes %02X\n", arg_int, ((uint8_t*)arg_ptr)[0]);
  46. #endif
  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. printf("[u8g2] dc %d\n", arg_int);
  52. #endif
  53. HAL_GPIO_WritePin(DISPLAY_DI_GPIO_Port, DISPLAY_DI_Pin, arg_int ? GPIO_PIN_SET : GPIO_PIN_RESET);
  54. break;
  55. case U8X8_MSG_BYTE_INIT:
  56. #ifdef DEBUG
  57. printf("[u8g2] init\n");
  58. #endif
  59. HAL_GPIO_WritePin(DISPLAY_CS_GPIO_Port, DISPLAY_CS_Pin, GPIO_PIN_RESET);
  60. break;
  61. case U8X8_MSG_BYTE_START_TRANSFER:
  62. #ifdef DEBUG
  63. printf("[u8g2] start\n");
  64. #endif
  65. HAL_GPIO_WritePin(DISPLAY_CS_GPIO_Port, DISPLAY_CS_Pin, GPIO_PIN_RESET);
  66. asm("nop");
  67. break;
  68. case U8X8_MSG_BYTE_END_TRANSFER:
  69. #ifdef DEBUG
  70. printf("[u8g2] end\n");
  71. #endif
  72. asm("nop");
  73. HAL_GPIO_WritePin(DISPLAY_CS_GPIO_Port, DISPLAY_CS_Pin, GPIO_PIN_SET);
  74. break;
  75. default:
  76. #ifdef DEBUG
  77. printf("[u8g2] unknown xfer %d\n", msg);
  78. #endif
  79. return 0;
  80. }
  81. return 1;
  82. }