board_ST7565.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * This file is subject to the terms of the GFX License. If a copy of
  3. * the license was not distributed with this file, you can obtain one at:
  4. *
  5. * http://ugfx.org/license.html
  6. */
  7. #ifndef _GDISP_LLD_BOARD_H
  8. #define _GDISP_LLD_BOARD_H
  9. #include "print.h"
  10. #define ST7565_LCD_BIAS ST7565_LCD_BIAS_9 // actually 6
  11. #define ST7565_ADC ST7565_ADC_NORMAL
  12. #define ST7565_COM_SCAN ST7565_COM_SCAN_DEC
  13. #define ST7565_PAGE_ORDER 0,1,2,3
  14. /*
  15. * Custom page order for several LCD boards, e.g. HEM12864-99
  16. * #define ST7565_PAGE_ORDER 4,5,6,7,0,1,2,3
  17. */
  18. #define ST7565_GPIOPORT GPIOC
  19. #define ST7565_PORT PORTC
  20. #define ST7565_A0_PIN 7
  21. #define ST7565_RST_PIN 8
  22. #define ST7565_MOSI_PIN 6
  23. #define ST7565_SLCK_PIN 5
  24. #define ST7565_SS_PIN 4
  25. #define palSetPadModeRaw(portname, bits) \
  26. ST7565_PORT->PCR[ST7565_##portname##_PIN] = bits
  27. #define palSetPadModeNamed(portname, portmode) \
  28. palSetPadMode(ST7565_GPIOPORT, ST7565_##portname##_PIN, portmode)
  29. #define ST7565_SPI_MODE PORTx_PCRn_DSE | PORTx_PCRn_MUX(2)
  30. // DSPI Clock and Transfer Attributes
  31. // Frame Size: 8 bits
  32. // MSB First
  33. // CLK Low by default
  34. static const SPIConfig spi1config = {
  35. NULL,
  36. /* HW dependent part.*/
  37. ST7565_GPIOPORT,
  38. ST7565_SS_PIN,
  39. SPIx_CTARn_FMSZ(7)
  40. | SPIx_CTARn_ASC(7)
  41. | SPIx_CTARn_DT(7)
  42. | SPIx_CTARn_CSSCK(7)
  43. | SPIx_CTARn_PBR(0)
  44. | SPIx_CTARn_BR(7)
  45. //SPI_CR1_BR_0
  46. };
  47. static bool_t st7565_is_data_mode = 1;
  48. static GFXINLINE void init_board(GDisplay *g) {
  49. (void) g;
  50. palSetPadModeNamed(A0, PAL_MODE_OUTPUT_PUSHPULL);
  51. palSetPad(ST7565_GPIOPORT, ST7565_A0_PIN);
  52. st7565_is_data_mode = 1;
  53. palSetPadModeNamed(RST, PAL_MODE_OUTPUT_PUSHPULL);
  54. palSetPad(ST7565_GPIOPORT, ST7565_RST_PIN);
  55. palSetPadModeRaw(MOSI, ST7565_SPI_MODE);
  56. palSetPadModeRaw(SLCK, ST7565_SPI_MODE);
  57. palSetPadModeRaw(SS, ST7565_SPI_MODE);
  58. spiInit();
  59. spiStart(&SPID1, &spi1config);
  60. spiSelect(&SPID1);
  61. }
  62. static GFXINLINE void post_init_board(GDisplay *g) {
  63. (void) g;
  64. }
  65. static GFXINLINE void setpin_reset(GDisplay *g, bool_t state) {
  66. (void) g;
  67. if (state) {
  68. palClearPad(ST7565_GPIOPORT, ST7565_RST_PIN);
  69. }
  70. else {
  71. palSetPad(ST7565_GPIOPORT, ST7565_RST_PIN);
  72. }
  73. }
  74. static GFXINLINE void acquire_bus(GDisplay *g) {
  75. (void) g;
  76. // Only the LCD is using the SPI bus, so no need to acquire
  77. // spiAcquireBus(&SPID1);
  78. }
  79. static GFXINLINE void release_bus(GDisplay *g) {
  80. (void) g;
  81. // Only the LCD is using the SPI bus, so no need to release
  82. //spiReleaseBus(&SPID1);
  83. }
  84. static GFXINLINE void write_cmd(GDisplay *g, uint8_t cmd) {
  85. (void) g;
  86. if (st7565_is_data_mode) {
  87. // The sleeps need to be at lest 10 vs 25 ns respectively
  88. // So let's sleep two ticks, one tick might not be enough
  89. // if we are at the end of the tick
  90. chThdSleep(2);
  91. palClearPad(ST7565_GPIOPORT, ST7565_A0_PIN);
  92. chThdSleep(2);
  93. st7565_is_data_mode = 0;
  94. }
  95. spiSend(&SPID1, 1, &cmd);
  96. }
  97. static GFXINLINE void write_data(GDisplay *g, uint8_t* data, uint16_t length) {
  98. (void) g;
  99. if (!st7565_is_data_mode) {
  100. // The sleeps need to be at lest 10 vs 25 ns respectively
  101. // So let's sleep two ticks, one tick might not be enough
  102. // if we are at the end of the tick
  103. chThdSleep(2);
  104. palSetPad(ST7565_GPIOPORT, ST7565_A0_PIN);
  105. chThdSleep(2);
  106. st7565_is_data_mode = 1;
  107. }
  108. spiSend(&SPID1, length, data);
  109. }
  110. #endif /* _GDISP_LLD_BOARD_H */