board_st7565_template.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #define ST7565_LCD_BIAS ST7565_LCD_BIAS_9 // actually 6
  10. #define ST7565_ADC ST7565_ADC_NORMAL
  11. #define ST7565_COM_SCAN ST7565_COM_SCAN_DEC
  12. #define ST7565_PAGE_ORDER 0, 1, 2, 3
  13. /*
  14. * Custom page order for several LCD boards, e.g. HEM12864-99
  15. * #define ST7565_PAGE_ORDER 4,5,6,7,0,1,2,3
  16. */
  17. #define ST7565_GPIOPORT GPIOC
  18. #define ST7565_PORT PORTC
  19. #define ST7565_A0_PIN 7
  20. #define ST7565_RST_PIN 8
  21. #define ST7565_MOSI_PIN 6
  22. #define ST7565_SLCK_PIN 5
  23. #define ST7565_SS_PIN 4
  24. #define palSetPadModeRaw(portname, bits) ST7565_PORT->PCR[ST7565_##portname##_PIN] = bits
  25. #define palSetPadModeNamed(portname, portmode) palSetPadMode(ST7565_GPIOPORT, ST7565_##portname##_PIN, portmode)
  26. #define ST7565_SPI_MODE PORTx_PCRn_DSE | PORTx_PCRn_MUX(2)
  27. // DSPI Clock and Transfer Attributes
  28. // Frame Size: 8 bits
  29. // MSB First
  30. // CLK Low by default
  31. static const SPIConfig spi1config = {
  32. // Operation complete callback or @p NULL.
  33. .end_cb = NULL,
  34. // The chip select line port - when not using pcs.
  35. .ssport = ST7565_GPIOPORT,
  36. // brief The chip select line pad number - when not using pcs.
  37. .sspad = ST7565_SS_PIN,
  38. // SPI initialization data.
  39. .tar0 = SPIx_CTARn_FMSZ(7) // Frame size = 8 bytes
  40. | SPIx_CTARn_ASC(1) // After SCK Delay Scaler (min 50 ns) = 55.56ns
  41. | SPIx_CTARn_DT(0) // Delay After Transfer Scaler (no minimum)= 27.78ns
  42. | SPIx_CTARn_CSSCK(0) // PCS to SCK Delay Scaler (min 20 ns) = 27.78ns
  43. | SPIx_CTARn_PBR(0) // Baud Rate Prescaler = 2
  44. | SPIx_CTARn_BR(0) // Baud rate (min 50ns) = 55.56ns
  45. };
  46. static GFXINLINE void acquire_bus(GDisplay *g) {
  47. (void)g;
  48. // Only the LCD is using the SPI bus, so no need to acquire
  49. // spiAcquireBus(&SPID1);
  50. spiSelect(&SPID1);
  51. }
  52. static GFXINLINE void release_bus(GDisplay *g) {
  53. (void)g;
  54. // Only the LCD is using the SPI bus, so no need to release
  55. // spiReleaseBus(&SPID1);
  56. spiUnselect(&SPID1);
  57. }
  58. static GFXINLINE void init_board(GDisplay *g) {
  59. (void)g;
  60. palSetPadModeNamed(A0, PAL_MODE_OUTPUT_PUSHPULL);
  61. palSetPad(ST7565_GPIOPORT, ST7565_A0_PIN);
  62. palSetPadModeNamed(RST, PAL_MODE_OUTPUT_PUSHPULL);
  63. palSetPad(ST7565_GPIOPORT, ST7565_RST_PIN);
  64. palSetPadModeRaw(MOSI, ST7565_SPI_MODE);
  65. palSetPadModeRaw(SLCK, ST7565_SPI_MODE);
  66. palSetPadModeNamed(SS, PAL_MODE_OUTPUT_PUSHPULL);
  67. spiInit();
  68. spiStart(&SPID1, &spi1config);
  69. release_bus(g);
  70. }
  71. static GFXINLINE void post_init_board(GDisplay *g) { (void)g; }
  72. static GFXINLINE void setpin_reset(GDisplay *g, bool_t state) {
  73. (void)g;
  74. if (state) {
  75. palClearPad(ST7565_GPIOPORT, ST7565_RST_PIN);
  76. } else {
  77. palSetPad(ST7565_GPIOPORT, ST7565_RST_PIN);
  78. }
  79. }
  80. static GFXINLINE void enter_data_mode(GDisplay *g) { palSetPad(ST7565_GPIOPORT, ST7565_A0_PIN); }
  81. static GFXINLINE void enter_cmd_mode(GDisplay *g) { palClearPad(ST7565_GPIOPORT, ST7565_A0_PIN); }
  82. static GFXINLINE void write_data(GDisplay *g, uint8_t *data, uint16_t length) {
  83. (void)g;
  84. spiSend(&SPID1, length, data);
  85. }
  86. #endif /* _GDISP_LLD_BOARD_H */