EPD_2in9_V2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*****************************************************************************
  2. * | File : EPD_2in9_V2.c
  3. * | Author : Waveshare team
  4. * | Function : 2.9inch e-paper V2
  5. * | Info :
  6. *----------------
  7. * | This version: V1.0
  8. * | Date : 2020-12-09
  9. * | Info :
  10. * -----------------------------------------------------------------------------
  11. #
  12. # Permission is hereby granted, free of charge, to any person obtaining a copy
  13. # of this software and associated documnetation files (the "Software"), to deal
  14. # in the Software without restriction, including without limitation the rights
  15. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  16. # copies of the Software, and to permit persons to whom the Software is
  17. # furished to do so, subject to the following conditions:
  18. #
  19. # The above copyright notice and this permission notice shall be included in
  20. # all copies or substantial portions of the Software.
  21. #
  22. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  28. # THE SOFTWARE.
  29. #
  30. ******************************************************************************/
  31. #include "EPD_2in9_V2.h"
  32. #include "Debug.h"
  33. UBYTE _WF_PARTIAL_2IN9[159] =
  34. {
  35. 0x0,0x40,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  36. 0x80,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  37. 0x40,0x40,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  38. 0x0,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  39. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  40. 0x0A,0x0,0x0,0x0,0x0,0x0,0x2,
  41. 0x1,0x0,0x0,0x0,0x0,0x0,0x0,
  42. 0x1,0x0,0x0,0x0,0x0,0x0,0x0,
  43. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  44. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  45. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  46. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  47. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  48. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  49. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  50. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  51. 0x0,0x0,0x0,0x0,0x0,0x0,0x0,
  52. 0x22,0x22,0x22,0x22,0x22,0x22,0x0,0x0,0x0,
  53. 0x22,0x17,0x41,0xB0,0x32,0x36,
  54. };
  55. UBYTE WS_20_30[159] =
  56. {
  57. 0x80, 0x66, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x40, 0x0, 0x0, 0x0,
  58. 0x10, 0x66, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x20, 0x0, 0x0, 0x0,
  59. 0x80, 0x66, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x40, 0x0, 0x0, 0x0,
  60. 0x10, 0x66, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x20, 0x0, 0x0, 0x0,
  61. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  62. 0x14, 0x8, 0x0, 0x0, 0x0, 0x0, 0x1,
  63. 0xA, 0xA, 0x0, 0xA, 0xA, 0x0, 0x1,
  64. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  65. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  66. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  67. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  68. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  69. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  70. 0x14, 0x8, 0x0, 0x1, 0x0, 0x0, 0x1,
  71. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1,
  72. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  73. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  74. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x0, 0x0, 0x0,
  75. 0x22, 0x17, 0x41, 0x0, 0x32, 0x36
  76. };
  77. /******************************************************************************
  78. function : Software reset
  79. parameter:
  80. ******************************************************************************/
  81. static void EPD_2IN9_V2_Reset(void)
  82. {
  83. DEV_Digital_Write(EPD_RST_PIN, 1);
  84. DEV_Delay_ms(100);
  85. DEV_Digital_Write(EPD_RST_PIN, 0);
  86. DEV_Delay_ms(2);
  87. DEV_Digital_Write(EPD_RST_PIN, 1);
  88. DEV_Delay_ms(100);
  89. }
  90. /******************************************************************************
  91. function : send command
  92. parameter:
  93. Reg : Command register
  94. ******************************************************************************/
  95. static void EPD_2IN9_V2_SendCommand(UBYTE Reg)
  96. {
  97. DEV_Digital_Write(EPD_DC_PIN, 0);
  98. DEV_Digital_Write(EPD_CS_PIN, 0);
  99. DEV_SPI_WriteByte(Reg);
  100. DEV_Digital_Write(EPD_CS_PIN, 1);
  101. }
  102. /******************************************************************************
  103. function : send data
  104. parameter:
  105. Data : Write data
  106. ******************************************************************************/
  107. static void EPD_2IN9_V2_SendData(UBYTE Data)
  108. {
  109. DEV_Digital_Write(EPD_DC_PIN, 1);
  110. DEV_Digital_Write(EPD_CS_PIN, 0);
  111. DEV_SPI_WriteByte(Data);
  112. DEV_Digital_Write(EPD_CS_PIN, 1);
  113. }
  114. /******************************************************************************
  115. function : Wait until the busy_pin goes LOW
  116. parameter:
  117. ******************************************************************************/
  118. void EPD_2IN9_V2_ReadBusy(void)
  119. {
  120. Debug("e-Paper busy\r\n");
  121. while(1)
  122. { //=1 BUSY
  123. if(DEV_Digital_Read(EPD_BUSY_PIN)==0)
  124. break;
  125. DEV_Delay_ms(50);
  126. }
  127. DEV_Delay_ms(50);
  128. Debug("e-Paper busy release\r\n");
  129. }
  130. static void EPD_2IN9_V2_LUT(UBYTE *lut)
  131. {
  132. UBYTE count;
  133. EPD_2IN9_V2_SendCommand(0x32);
  134. for(count=0; count<153; count++)
  135. EPD_2IN9_V2_SendData(lut[count]);
  136. EPD_2IN9_V2_ReadBusy();
  137. }
  138. static void EPD_2IN9_V2_LUT_by_host(UBYTE *lut)
  139. {
  140. EPD_2IN9_V2_LUT((UBYTE *)lut); //lut
  141. EPD_2IN9_V2_SendCommand(0x3f);
  142. EPD_2IN9_V2_SendData(*(lut+153));
  143. EPD_2IN9_V2_SendCommand(0x03); // gate voltage
  144. EPD_2IN9_V2_SendData(*(lut+154));
  145. EPD_2IN9_V2_SendCommand(0x04); // source voltage
  146. EPD_2IN9_V2_SendData(*(lut+155)); // VSH
  147. EPD_2IN9_V2_SendData(*(lut+156)); // VSH2
  148. EPD_2IN9_V2_SendData(*(lut+157)); // VSL
  149. EPD_2IN9_V2_SendCommand(0x2c); // VCOM
  150. EPD_2IN9_V2_SendData(*(lut+158));
  151. }
  152. /******************************************************************************
  153. function : Turn On Display
  154. parameter:
  155. ******************************************************************************/
  156. static void EPD_2IN9_V2_TurnOnDisplay(void)
  157. {
  158. EPD_2IN9_V2_SendCommand(0x22); //Display Update Control
  159. EPD_2IN9_V2_SendData(0xc7);
  160. EPD_2IN9_V2_SendCommand(0x20); //Activate Display Update Sequence
  161. EPD_2IN9_V2_ReadBusy();
  162. }
  163. static void EPD_2IN9_V2_TurnOnDisplay_Partial(void)
  164. {
  165. EPD_2IN9_V2_SendCommand(0x22); //Display Update Control
  166. EPD_2IN9_V2_SendData(0x0F);
  167. EPD_2IN9_V2_SendCommand(0x20); //Activate Display Update Sequence
  168. EPD_2IN9_V2_ReadBusy();
  169. }
  170. /******************************************************************************
  171. function : Setting the display window
  172. parameter:
  173. ******************************************************************************/
  174. static void EPD_2IN9_V2_SetWindows(UWORD Xstart, UWORD Ystart, UWORD Xend, UWORD Yend)
  175. {
  176. EPD_2IN9_V2_SendCommand(0x44); // SET_RAM_X_ADDRESS_START_END_POSITION
  177. EPD_2IN9_V2_SendData((Xstart>>3) & 0xFF);
  178. EPD_2IN9_V2_SendData((Xend>>3) & 0xFF);
  179. EPD_2IN9_V2_SendCommand(0x45); // SET_RAM_Y_ADDRESS_START_END_POSITION
  180. EPD_2IN9_V2_SendData(Ystart & 0xFF);
  181. EPD_2IN9_V2_SendData((Ystart >> 8) & 0xFF);
  182. EPD_2IN9_V2_SendData(Yend & 0xFF);
  183. EPD_2IN9_V2_SendData((Yend >> 8) & 0xFF);
  184. }
  185. /******************************************************************************
  186. function : Set Cursor
  187. parameter:
  188. ******************************************************************************/
  189. static void EPD_2IN9_V2_SetCursor(UWORD Xstart, UWORD Ystart)
  190. {
  191. EPD_2IN9_V2_SendCommand(0x4E); // SET_RAM_X_ADDRESS_COUNTER
  192. EPD_2IN9_V2_SendData(Xstart & 0xFF);
  193. EPD_2IN9_V2_SendCommand(0x4F); // SET_RAM_Y_ADDRESS_COUNTER
  194. EPD_2IN9_V2_SendData(Ystart & 0xFF);
  195. EPD_2IN9_V2_SendData((Ystart >> 8) & 0xFF);
  196. }
  197. /******************************************************************************
  198. function : Initialize the e-Paper register
  199. parameter:
  200. ******************************************************************************/
  201. void EPD_2IN9_V2_Init(void)
  202. {
  203. EPD_2IN9_V2_Reset();
  204. DEV_Delay_ms(100);
  205. EPD_2IN9_V2_ReadBusy();
  206. EPD_2IN9_V2_SendCommand(0x12); //SWRESET
  207. EPD_2IN9_V2_ReadBusy();
  208. EPD_2IN9_V2_SendCommand(0x01); //Driver output control
  209. EPD_2IN9_V2_SendData(0x27);
  210. EPD_2IN9_V2_SendData(0x01);
  211. EPD_2IN9_V2_SendData(0x00);
  212. EPD_2IN9_V2_SendCommand(0x11); //data entry mode
  213. EPD_2IN9_V2_SendData(0x03);
  214. EPD_2IN9_V2_SetWindows(0, 0, EPD_2IN9_V2_WIDTH-1, EPD_2IN9_V2_HEIGHT-1);
  215. // EPD_2IN9_V2_SendCommand(0x3C); //BorderWavefrom
  216. // EPD_2IN9_V2_SendData(0x05);
  217. EPD_2IN9_V2_SendCommand(0x21); // Display update control
  218. EPD_2IN9_V2_SendData(0x00);
  219. EPD_2IN9_V2_SendData(0x80);
  220. // EPD_2IN9_V2_SendCommand(0x18); //Read built-in temperature sensor
  221. // EPD_2IN9_V2_SendData(0x80);
  222. EPD_2IN9_V2_SetCursor(0, 0);
  223. EPD_2IN9_V2_ReadBusy();
  224. EPD_2IN9_V2_LUT_by_host(WS_20_30);
  225. }
  226. /******************************************************************************
  227. function : Clear screen
  228. parameter:
  229. ******************************************************************************/
  230. void EPD_2IN9_V2_Clear(void)
  231. {
  232. UWORD i;
  233. EPD_2IN9_V2_SendCommand(0x24); //write RAM for black(0)/white (1)
  234. for(i=0;i<4736;i++)
  235. {
  236. EPD_2IN9_V2_SendData(0xff);
  237. }
  238. EPD_2IN9_V2_TurnOnDisplay();
  239. }
  240. /******************************************************************************
  241. function : Sends the image buffer in RAM to e-Paper and displays
  242. parameter:
  243. ******************************************************************************/
  244. void EPD_2IN9_V2_Display(UBYTE *Image)
  245. {
  246. UWORD i;
  247. EPD_2IN9_V2_SendCommand(0x24); //write RAM for black(0)/white (1)
  248. for(i=0;i<4736;i++)
  249. {
  250. EPD_2IN9_V2_SendData(Image[i]);
  251. }
  252. EPD_2IN9_V2_TurnOnDisplay();
  253. }
  254. void EPD_2IN9_V2_Display_Base(UBYTE *Image)
  255. {
  256. UWORD i;
  257. EPD_2IN9_V2_SendCommand(0x24); //Write Black and White image to RAM
  258. for(i=0;i<4736;i++)
  259. {
  260. EPD_2IN9_V2_SendData(Image[i]);
  261. }
  262. EPD_2IN9_V2_SendCommand(0x26); //Write Black and White image to RAM
  263. for(i=0;i<4736;i++)
  264. {
  265. EPD_2IN9_V2_SendData(Image[i]);
  266. }
  267. EPD_2IN9_V2_TurnOnDisplay();
  268. }
  269. void EPD_2IN9_V2_Display_Partial(UBYTE *Image)
  270. {
  271. UWORD i;
  272. //Reset
  273. DEV_Digital_Write(EPD_RST_PIN, 0);
  274. DEV_Delay_ms(2);
  275. DEV_Digital_Write(EPD_RST_PIN, 1);
  276. DEV_Delay_ms(2);
  277. EPD_2IN9_V2_LUT(_WF_PARTIAL_2IN9);
  278. EPD_2IN9_V2_SendCommand(0x37);
  279. EPD_2IN9_V2_SendData(0x00);
  280. EPD_2IN9_V2_SendData(0x00);
  281. EPD_2IN9_V2_SendData(0x00);
  282. EPD_2IN9_V2_SendData(0x00);
  283. EPD_2IN9_V2_SendData(0x00);
  284. EPD_2IN9_V2_SendData(0x40);
  285. EPD_2IN9_V2_SendData(0x00);
  286. EPD_2IN9_V2_SendData(0x00);
  287. EPD_2IN9_V2_SendData(0x00);
  288. EPD_2IN9_V2_SendData(0x00);
  289. EPD_2IN9_V2_SendCommand(0x3C); //BorderWavefrom
  290. EPD_2IN9_V2_SendData(0x80);
  291. EPD_2IN9_V2_SendCommand(0x22);
  292. EPD_2IN9_V2_SendData(0xC0);
  293. EPD_2IN9_V2_SendCommand(0x20);
  294. EPD_2IN9_V2_ReadBusy();
  295. EPD_2IN9_V2_SetWindows(0, 0, EPD_2IN9_V2_WIDTH-1, EPD_2IN9_V2_HEIGHT-1);
  296. EPD_2IN9_V2_SetCursor(0, 0);
  297. EPD_2IN9_V2_SendCommand(0x24); //Write Black and White image to RAM
  298. for(i=0;i<4736;i++)
  299. {
  300. EPD_2IN9_V2_SendData(Image[i]);
  301. }
  302. EPD_2IN9_V2_TurnOnDisplay_Partial();
  303. }
  304. /******************************************************************************
  305. function : Enter sleep mode
  306. parameter:
  307. ******************************************************************************/
  308. void EPD_2IN9_V2_Sleep(void)
  309. {
  310. EPD_2IN9_V2_SendCommand(0x10); //enter deep sleep
  311. EPD_2IN9_V2_SendData(0x01);
  312. DEV_Delay_ms(100);
  313. }