st7565.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* Copyright 2023 Dual Tachyon
  2. * https://github.com/DualTachyon
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <stdint.h>
  17. #include <stdio.h> // NULL
  18. #include "bsp/dp32g030/gpio.h"
  19. #include "bsp/dp32g030/spi.h"
  20. #include "driver/gpio.h"
  21. #include "driver/spi.h"
  22. #include "driver/st7565.h"
  23. #include "driver/system.h"
  24. #include "misc.h"
  25. #ifdef ENABLE_CONTRAST
  26. uint8_t contrast = 31; // 0 ~ 63
  27. #endif
  28. uint8_t gStatusLine[LCD_WIDTH];
  29. uint8_t gFrameBuffer[FRAME_LINES][LCD_WIDTH];
  30. static void DrawLine(uint8_t column, uint8_t line, const uint8_t * lineBuffer, unsigned size_defVal)
  31. {
  32. ST7565_SelectColumnAndLine(column + 4, line);
  33. GPIO_SetBit(&GPIOB->DATA, GPIOB_PIN_ST7565_A0);
  34. for (unsigned i = 0; i < size_defVal; i++) {
  35. while ((SPI0->FIFOST & SPI_FIFOST_TFF_MASK) != SPI_FIFOST_TFF_BITS_NOT_FULL) {}
  36. SPI0->WDR = lineBuffer ? lineBuffer[i] : size_defVal;
  37. }
  38. SPI_WaitForUndocumentedTxFifoStatusBit();
  39. }
  40. void ST7565_DrawLine(const unsigned int Column, const unsigned int Line, const uint8_t *pBitmap, const unsigned int Size)
  41. {
  42. SPI_ToggleMasterMode(&SPI0->CR, false);
  43. DrawLine(Column, Line, pBitmap, Size);
  44. SPI_ToggleMasterMode(&SPI0->CR, true);
  45. }
  46. void ST7565_BlitFullScreen(void)
  47. {
  48. SPI_ToggleMasterMode(&SPI0->CR, false);
  49. ST7565_WriteByte(0x40);
  50. for (unsigned line = 0; line < FRAME_LINES; line++) {
  51. DrawLine(0, line+1, gFrameBuffer[line], LCD_WIDTH);
  52. }
  53. SPI_ToggleMasterMode(&SPI0->CR, true);
  54. }
  55. void ST7565_BlitLine(unsigned line)
  56. {
  57. SPI_ToggleMasterMode(&SPI0->CR, false);
  58. ST7565_WriteByte(0x40); // start line ?
  59. DrawLine(0, line+1, gFrameBuffer[line], LCD_WIDTH);
  60. SPI_ToggleMasterMode(&SPI0->CR, true);
  61. }
  62. void ST7565_BlitStatusLine(void)
  63. { // the top small text line on the display
  64. SPI_ToggleMasterMode(&SPI0->CR, false);
  65. ST7565_WriteByte(0x40); // start line ?
  66. DrawLine(0, 0, gStatusLine, LCD_WIDTH);
  67. SPI_ToggleMasterMode(&SPI0->CR, true);
  68. }
  69. void ST7565_FillScreen(uint8_t value)
  70. {
  71. SPI_ToggleMasterMode(&SPI0->CR, false);
  72. for (unsigned i = 0; i < 8; i++) {
  73. DrawLine(0, i, NULL, value);
  74. }
  75. SPI_ToggleMasterMode(&SPI0->CR, true);
  76. }
  77. // Software reset
  78. const uint8_t ST7565_CMD_SOFTWARE_RESET = 0xE2;
  79. // Bias Select
  80. // 1 0 1 0 0 0 1 BS
  81. // Select bias setting 0=1/9; 1=1/7 (at 1/65 duty)
  82. const uint8_t ST7565_CMD_BIAS_SELECT = 0xA2;
  83. // COM Direction
  84. // 1 1 0 0 MY - - -
  85. // Set output direction of COM
  86. // MY=1, reverse direction
  87. // MY=0, normal direction
  88. const uint8_t ST7565_CMD_COM_DIRECTION = 0xC0;
  89. // SEG Direction
  90. // 1 0 1 0 0 0 0 MX
  91. // Set scan direction of SEG
  92. // MX=1, reverse direction
  93. // MX=0, normal direction
  94. const uint8_t ST7565_CMD_SEG_DIRECTION = 0xA0;
  95. // Inverse Display
  96. // 1 0 1 0 0 1 1 INV
  97. // INV =1, inverse display
  98. // INV =0, normal display
  99. const uint8_t ST7565_CMD_INVERSE_DISPLAY = 0xA6;
  100. // All Pixel ON
  101. // 1 0 1 0 0 1 0 AP
  102. // AP=1, set all pixel ON
  103. // AP=0, normal display
  104. const uint8_t ST7565_CMD_ALL_PIXEL_ON = 0xA4;
  105. // Regulation Ratio
  106. // 0 0 1 0 0 RR2 RR1 RR0
  107. // This instruction controls the regulation ratio of the built-in regulator
  108. const uint8_t ST7565_CMD_REGULATION_RATIO = 0x20;
  109. // Double command!! Set electronic volume (EV) level
  110. // Send next: 0 0 EV5 EV4 EV3 EV2 EV1 EV0 contrast 0-63
  111. const uint8_t ST7565_CMD_SET_EV = 0x81;
  112. // Control built-in power circuit ON/OFF - 0 0 1 0 1 VB VR VF
  113. // VB: Built-in Booster
  114. // VR: Built-in Regulator
  115. // VF: Built-in Follower
  116. const uint8_t ST7565_CMD_POWER_CIRCUIT = 0x28;
  117. // Set display start line 0-63
  118. // 0 0 0 1 S5 S4 S3 S2 S1 S0
  119. const uint8_t ST7565_CMD_SET_START_LINE = 0x40;
  120. // Display ON/OFF
  121. // 0 0 1 0 1 0 1 1 1 D
  122. // D=1, display ON
  123. // D=0, display OFF
  124. const uint8_t ST7565_CMD_DISPLAY_ON_OFF = 0xAE;
  125. uint8_t cmds[] = {
  126. ST7565_CMD_BIAS_SELECT | 0, // Select bias setting: 1/9
  127. ST7565_CMD_COM_DIRECTION | (0 << 3), // Set output direction of COM: normal
  128. ST7565_CMD_SEG_DIRECTION | 1, // Set scan direction of SEG: reverse
  129. ST7565_CMD_INVERSE_DISPLAY | 0, // Inverse Display: false
  130. ST7565_CMD_ALL_PIXEL_ON | 0, // All Pixel ON: false - normal display
  131. ST7565_CMD_REGULATION_RATIO | (4 << 0), // Regulation Ratio 5.0
  132. ST7565_CMD_SET_EV, // Set contrast
  133. 31,
  134. ST7565_CMD_POWER_CIRCUIT | 0b111, // Built-in power circuit ON/OFF: VB=1 VR=1 VF=1
  135. ST7565_CMD_SET_START_LINE | 0, // Set Start Line: 0
  136. ST7565_CMD_DISPLAY_ON_OFF | 1, // Display ON/OFF: ON
  137. };
  138. void ST7565_Init(void)
  139. {
  140. SPI0_Init();
  141. ST7565_HardwareReset();
  142. SPI_ToggleMasterMode(&SPI0->CR, false);
  143. ST7565_WriteByte(ST7565_CMD_SOFTWARE_RESET); // software reset
  144. SYSTEM_DelayMs(120);
  145. for(uint8_t i = 0; i < 8; i++)
  146. ST7565_WriteByte(cmds[i]);
  147. ST7565_WriteByte(ST7565_CMD_POWER_CIRCUIT | 0b011); // VB=0 VR=1 VF=1
  148. SYSTEM_DelayMs(1);
  149. ST7565_WriteByte(ST7565_CMD_POWER_CIRCUIT | 0b110); // VB=1 VR=1 VF=0
  150. SYSTEM_DelayMs(1);
  151. for(uint8_t i = 0; i < 4; i++) // why 4 times?
  152. ST7565_WriteByte(ST7565_CMD_POWER_CIRCUIT | 0b111); // VB=1 VR=1 VF=1
  153. SYSTEM_DelayMs(40);
  154. #ifdef ENABLE_CONTRAST
  155. ST7565_WriteByte(contrast); // brightness 0 ~ 63
  156. #else
  157. ST7565_WriteByte(31); // brightness 0 ~ 63
  158. #endif
  159. ST7565_WriteByte(ST7565_CMD_SET_START_LINE | 0); // line 0
  160. ST7565_WriteByte(ST7565_CMD_DISPLAY_ON_OFF | 1); // D=1
  161. SPI_WaitForUndocumentedTxFifoStatusBit();
  162. SPI_ToggleMasterMode(&SPI0->CR, true);
  163. ST7565_FillScreen(0x00);
  164. }
  165. void ST7565_FixInterfGlitch(void)
  166. {
  167. SPI_ToggleMasterMode(&SPI0->CR, false);
  168. for(uint8_t i = 0; i < ARRAY_SIZE(cmds); i++)
  169. ST7565_WriteByte(cmds[i]);
  170. SPI_WaitForUndocumentedTxFifoStatusBit();
  171. SPI_ToggleMasterMode(&SPI0->CR, true);
  172. }
  173. void ST7565_HardwareReset(void)
  174. {
  175. GPIO_SetBit(&GPIOB->DATA, GPIOB_PIN_ST7565_RES);
  176. SYSTEM_DelayMs(1);
  177. GPIO_ClearBit(&GPIOB->DATA, GPIOB_PIN_ST7565_RES);
  178. SYSTEM_DelayMs(20);
  179. GPIO_SetBit(&GPIOB->DATA, GPIOB_PIN_ST7565_RES);
  180. SYSTEM_DelayMs(120);
  181. }
  182. void ST7565_SelectColumnAndLine(uint8_t Column, uint8_t Line)
  183. {
  184. GPIO_ClearBit(&GPIOB->DATA, GPIOB_PIN_ST7565_A0);
  185. while ((SPI0->FIFOST & SPI_FIFOST_TFF_MASK) != SPI_FIFOST_TFF_BITS_NOT_FULL) {}
  186. SPI0->WDR = Line + 176;
  187. while ((SPI0->FIFOST & SPI_FIFOST_TFF_MASK) != SPI_FIFOST_TFF_BITS_NOT_FULL) {}
  188. SPI0->WDR = ((Column >> 4) & 0x0F) | 0x10;
  189. while ((SPI0->FIFOST & SPI_FIFOST_TFF_MASK) != SPI_FIFOST_TFF_BITS_NOT_FULL) {}
  190. SPI0->WDR = ((Column >> 0) & 0x0F);
  191. SPI_WaitForUndocumentedTxFifoStatusBit();
  192. }
  193. void ST7565_WriteByte(uint8_t Value)
  194. {
  195. GPIO_ClearBit(&GPIOB->DATA, GPIOB_PIN_ST7565_A0);
  196. while ((SPI0->FIFOST & SPI_FIFOST_TFF_MASK) != SPI_FIFOST_TFF_BITS_NOT_FULL) {}
  197. SPI0->WDR = Value;
  198. }
  199. #ifdef ENABLE_CONTRAST
  200. void ST7565_SetContrast(const uint8_t value)
  201. {
  202. contrast = (value > 45) ? 45 : (value < 26) ? 26 : value;
  203. }
  204. uint8_t ST7565_GetContrast(void)
  205. {
  206. return contrast;
  207. }
  208. #endif