u8x8_d_st7528.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*
  2. u8x8_d_st7528.c
  3. Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
  4. Copyright (c) 2019, olikraus@gmail.com
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright notice, this list
  9. of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright notice, this
  11. list of conditions and the following disclaimer in the documentation and/or other
  12. materials provided with the distribution.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  14. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  15. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  16. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  23. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. ST7528: 16 Graylevel Controller
  27. https://github.com/olikraus/u8g2/issues/986
  28. I2C Address: 0x03f (0x7e)
  29. ERC16004
  30. https://www.buydisplay.com/default/2-inch-lcd-160x64-graphic-module-serial-spi-display-st7528-black-on-white
  31. */
  32. #include "u8x8.h"
  33. static const uint8_t u8x8_d_st7528_powersave0_seq[] = {
  34. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  35. U8X8_CA(0x038, 0x074), /* ext mode 0*/
  36. U8X8_C(0x0af), /* display on */
  37. U8X8_END_TRANSFER(), /* disable chip */
  38. U8X8_END() /* end of sequence */
  39. };
  40. static const uint8_t u8x8_d_st7528_powersave1_seq[] = {
  41. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  42. U8X8_CA(0x038, 0x074), /* ext mode 0*/
  43. U8X8_C(0x0ae), /* display off */
  44. U8X8_END_TRANSFER(), /* disable chip */
  45. U8X8_END() /* end of sequence */
  46. };
  47. static const uint8_t u8x8_d_st7528_flip0_seq[] = {
  48. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  49. U8X8_C(0x0a0), /* ADC */
  50. U8X8_C(0x0c8), /* SHL */
  51. U8X8_END_TRANSFER(), /* disable chip */
  52. U8X8_END() /* end of sequence */
  53. };
  54. static const uint8_t u8x8_d_st7528_flip1_seq[] = {
  55. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  56. U8X8_C(0x0a1), /* ADC */
  57. U8X8_C(0x0c0), /* SHL */
  58. U8X8_END_TRANSFER(), /* disable chip */
  59. U8X8_END() /* end of sequence */
  60. };
  61. /*
  62. input:
  63. one tile (8 Bytes)
  64. output:
  65. Tile for st7528 (32 Bytes)
  66. */
  67. static uint8_t u8x8_st7528_8to32_dest_buf[32];
  68. static uint8_t *u8x8_st7528_8to32(U8X8_UNUSED u8x8_t *u8x8, uint8_t *ptr)
  69. {
  70. uint8_t j;
  71. uint8_t *dest;
  72. dest = u8x8_st7528_8to32_dest_buf;
  73. for( j = 0; j < 8; j++ )
  74. {
  75. *dest++ =*ptr;
  76. *dest++ =*ptr;
  77. *dest++ =*ptr;
  78. *dest++ =*ptr;
  79. ptr++;
  80. }
  81. return u8x8_st7528_8to32_dest_buf;
  82. }
  83. static uint8_t u8x8_d_st7528_generic(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  84. {
  85. uint8_t x;
  86. uint8_t y, c;
  87. uint8_t *ptr;
  88. switch(msg)
  89. {
  90. /* handled by the calling function
  91. case U8X8_MSG_DISPLAY_SETUP_MEMORY:
  92. u8x8_d_helper_display_setup_memory(u8x8, &u8x8_st7528_display_info);
  93. break;
  94. case U8X8_MSG_DISPLAY_INIT:
  95. u8x8_d_helper_display_init(u8x8);
  96. u8x8_cad_SendSequence(u8x8, u8x8_d_st7528_nhd_c160100_init_seq);
  97. */
  98. break;
  99. case U8X8_MSG_DISPLAY_SET_POWER_SAVE:
  100. if ( arg_int == 0 )
  101. u8x8_cad_SendSequence(u8x8, u8x8_d_st7528_powersave0_seq);
  102. else
  103. u8x8_cad_SendSequence(u8x8, u8x8_d_st7528_powersave1_seq);
  104. break;
  105. case U8X8_MSG_DISPLAY_SET_FLIP_MODE:
  106. if ( arg_int == 0 )
  107. {
  108. u8x8_cad_SendSequence(u8x8, u8x8_d_st7528_flip0_seq);
  109. u8x8->x_offset = u8x8->display_info->default_x_offset;
  110. }
  111. else
  112. {
  113. u8x8_cad_SendSequence(u8x8, u8x8_d_st7528_flip1_seq);
  114. u8x8->x_offset = u8x8->display_info->flipmode_x_offset;
  115. }
  116. break;
  117. #ifdef U8X8_WITH_SET_CONTRAST
  118. case U8X8_MSG_DISPLAY_SET_CONTRAST:
  119. u8x8_cad_StartTransfer(u8x8);
  120. u8x8_cad_SendCmd(u8x8, 0x081 );
  121. u8x8_cad_SendArg(u8x8, arg_int ); /* ssd1326 has range from 0 to 255 */
  122. u8x8_cad_EndTransfer(u8x8);
  123. break;
  124. #endif
  125. case U8X8_MSG_DISPLAY_DRAW_TILE:
  126. u8x8_cad_StartTransfer(u8x8);
  127. x = ((u8x8_tile_t *)arg_ptr)->x_pos;
  128. x *= 8; // not clear
  129. y = (((u8x8_tile_t *)arg_ptr)->y_pos);
  130. do
  131. {
  132. c = ((u8x8_tile_t *)arg_ptr)->cnt;
  133. ptr = ((u8x8_tile_t *)arg_ptr)->tile_ptr;
  134. do
  135. {
  136. u8x8_cad_SendCmd(u8x8, 0xb0 | y ); /* set page address */
  137. u8x8_cad_SendCmd(u8x8, 0x10| (x>>4) ); /* set col msb*/
  138. u8x8_cad_SendCmd(u8x8, 0x00| (x&15) ); /* set col lsb*/
  139. u8x8_cad_SendData(u8x8, 32, u8x8_st7528_8to32(u8x8, ptr));
  140. ptr += 8;
  141. x += 8;
  142. c--;
  143. } while( c > 0 );
  144. arg_int--;
  145. } while( arg_int > 0 );
  146. u8x8_cad_EndTransfer(u8x8);
  147. break;
  148. default:
  149. return 0;
  150. }
  151. return 1;
  152. }
  153. static void u8x8_d_st7528_graylevel_init(u8x8_t *u8x8, uint8_t mode0)
  154. {
  155. uint8_t i;
  156. u8x8_cad_StartTransfer(u8x8);
  157. u8x8_cad_SendCmd(u8x8, 0x38 );
  158. u8x8_cad_SendArg(u8x8, mode0+1 );
  159. for( i = 0; i < 64; i++ )
  160. {
  161. u8x8_cad_SendCmd(u8x8, i+0x080 );
  162. u8x8_cad_SendArg(u8x8, i & 0xfc);
  163. }
  164. u8x8_cad_SendCmd(u8x8, 0x38 );
  165. u8x8_cad_SendArg(u8x8, mode0 );
  166. u8x8_cad_EndTransfer(u8x8);
  167. }
  168. /*===============================================================*/
  169. /* NHD C160100 */
  170. static const uint8_t u8x8_d_st7528_nhd_c160100_init_seq[] = {
  171. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  172. /*
  173. I2C_out(0x48);//partial display duty ratio
  174. I2C_out(0x64);// 1/100 duty
  175. I2C_out(0xA0);//ADC select
  176. I2C_out(0xC8);//SHL select
  177. I2C_out(0x44);//initial Com0 register
  178. I2C_out(0x00);//scan from Com0
  179. I2C_out(0xAB);//OSC on
  180. I2C_out(0x26);//
  181. I2C_out(0x81); //set electronic volume
  182. I2C_out(0x15);//vopcode=0x1C
  183. I2C_out(0x56);//set 1/11 bias
  184. I2C_out(0x64);//3x
  185. delay(2);
  186. I2C_out(0x2C);//
  187. I2C_out(0x66);//5x
  188. delay(2);
  189. I2C_out(0x2E);//
  190. delay(2);
  191. I2C_out(0x2F);//power control
  192. I2C_out(0xF3);//bias save circuit
  193. I2C_out(0x00);//
  194. I2C_out(0x96);//frc and pwm
  195. I2C_out(0x38);//external mode
  196. I2C_out(0x75);//
  197. I2C_out(0x97);//3frc, 45 pwm THIS IS A MODE0 CMD, IT IS USELESS HERE
  198. I2C_out(0x80);//start 16-level grayscale settings
  199. */
  200. U8X8_CA(0x038, 0x064), /* ext mode 0*/
  201. U8X8_CA(0x048, 0x064), /* partial display duty ratio, 1/100 duty*/
  202. U8X8_C(0x0a0), /* ADC */
  203. U8X8_C(0x0c8), /* SHL */
  204. U8X8_CA(0x044, 0x000), /* initial Com0 */
  205. U8X8_CA(0x040, 0x000), /* y shift, issue https://github.com/olikraus/u8g2/issues/1828 */
  206. U8X8_C(0x0ab), /* start oscillator */
  207. U8X8_C(0x026), /* Select the internal resistance ratio of the regulator resistor */
  208. U8X8_CA(0x081, 0x015), /* volumn */
  209. U8X8_C(0x056), /* LCD Bias */
  210. U8X8_C(0x064), /* DC DC step up */
  211. U8X8_DLY(2),
  212. U8X8_C(0x02c), /* Power Control */
  213. U8X8_C(0x066), /* DC DC step up */
  214. U8X8_DLY(2),
  215. U8X8_C(0x02e), /* Power Control */
  216. U8X8_DLY(2),
  217. U8X8_C(0x02f), /* Power Control */
  218. U8X8_CA(0x0f3, 0x000), /* bias power save */
  219. U8X8_C(0x096), /* frc and pwm */
  220. U8X8_END_TRANSFER(), /* disable chip */
  221. U8X8_END() /* end of sequence */
  222. };
  223. static const u8x8_display_info_t u8x8_st7528_160x100_display_info =
  224. {
  225. /* chip_enable_level = */ 0,
  226. /* chip_disable_level = */ 1,
  227. /* post_chip_enable_wait_ns = */ 20,
  228. /* pre_chip_disable_wait_ns = */ 20,
  229. /* reset_pulse_width_ms = */ 5,
  230. /* post_reset_wait_ms = */ 5, /**/
  231. /* sda_setup_time_ns = */ 20, /* st7528 */
  232. /* sck_pulse_width_ns = */ 25, /* st7528 */
  233. /* sck_clock_hz = */ 8000000UL, /* since Arduino 1.6.0, the SPI bus speed in Hz. Should be 1000000000/sck_pulse_width_ns */
  234. /* st7528 actually allows 20MHz according to the datasheet */
  235. /* spi_mode = */ 0, /* active high, rising edge */
  236. /* i2c_bus_clock_100kHz = */ 4,
  237. /* data_setup_time_ns = */ 40,
  238. /* write_pulse_width_ns = */ 80, /* st7528 */
  239. /* tile_width = */ 20,
  240. /* tile_height = */ 13,
  241. /* default_x_offset = */ 0, /* x_offset is used as y offset for the ssd1326 */
  242. /* flipmode_x_offset = */ 0, /* x_offset is used as y offset for the ssd1326 */
  243. /* pixel_width = */ 160,
  244. /* pixel_height = */ 100
  245. };
  246. uint8_t u8x8_d_st7528_nhd_c160100(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  247. {
  248. if ( msg == U8X8_MSG_DISPLAY_SETUP_MEMORY )
  249. {
  250. u8x8_d_helper_display_setup_memory(u8x8, &u8x8_st7528_160x100_display_info);
  251. return 1;
  252. }
  253. if ( msg == U8X8_MSG_DISPLAY_INIT )
  254. {
  255. u8x8_d_helper_display_init(u8x8);
  256. u8x8_cad_SendSequence(u8x8, u8x8_d_st7528_nhd_c160100_init_seq);
  257. u8x8_d_st7528_graylevel_init(u8x8, 0x074);
  258. return 1;
  259. }
  260. return u8x8_d_st7528_generic(u8x8, msg, arg_int, arg_ptr);
  261. }
  262. /*===============================================================*/
  263. /* ERC16064, https://www.buydisplay.com/2-inch-lcd-160x64-graphic-module-serial-spi-display-st7528-black-on-white */
  264. /*
  265. #define ModeSet 0x38
  266. #define ModeSetP1 0x64 //EXT=0
  267. #define ModeSetP2 0x65 //EXT=1
  268. #define Display_on 0xaf
  269. #define Display_off 0xae
  270. #define Regulator 0x26
  271. #define Contrast_level 0x81
  272. #define PowerControll_on1 0x2c
  273. #define PowerControll_on2 0x2e
  274. #define PowerControll_on3 0x2f
  275. #define LcdBias_9 0x54
  276. #define InterOsc_on 0xab
  277. #define EntireDisp_on 0xa5
  278. #define EntireDisp_off 0xa4
  279. #define BoostLevel_5 0x66
  280. #define Duty_set 0x48
  281. #define Duty_setP 0x40
  282. #define Start_columnlsb 0x00
  283. #define Start_columnmsb 0x10
  284. #define Start_page 0xb0
  285. #define StartLine_set 0x40
  286. //#define StartLine_setP 0x00
  287. #define Set_Initial_COM0 0x44
  288. #define Set_Initial_COM0P 0x12
  289. #define Entire_Displa_ON 0xA5
  290. #define Entire_Displa_OFF 0xA4
  291. #define FrcPwm_set 0x92
  292. #define NLineInversion_on 0x4c
  293. #define NLineInversion_onP 0x1f
  294. #define NLineInversion_off 0xe4
  295. #define ReverseDisp_on 0xa7
  296. #define ReverseDisp_off 0xa6
  297. #define AdcSelect 0xa0
  298. #define ComScanDirection 0xc8
  299. Write_command(ModeSet); 0x38, 0x64
  300. Write_command(ModeSetP1);
  301. Write_command(InterOsc_on); 0xab
  302. Write_command(Set_Initial_COM0); 0x44, 0x12
  303. Write_command(Set_Initial_COM0P);
  304. Write_command(AdcSelect); 0xa0
  305. Write_command(ComScanDirection); 0xc8
  306. Write_command(BoostLevel_5); 0x66
  307. Delay(1000);
  308. Write_command(LcdBias_9); 0x54
  309. Write_command(Duty_set); 0x48, 0x40
  310. Write_command(Duty_setP);
  311. Write_command(Regulator); 0x26
  312. Write_command(Contrast_level); 0x81, 0x0b
  313. Write_command(Contrast_levelP);
  314. Write_command(PowerControll_on1); 0x2c
  315. Delay(10000);
  316. Write_command(PowerControll_on2); 0x2e
  317. Delay(1000);
  318. Write_command(PowerControll_on3); 0x2f
  319. Delay(1000);
  320. Write_command(FrcPwm_set); 0x92
  321. Write_command(ModeSet);
  322. Write_command(ModeSetP2);
  323. for(i=0;i<64;i++)
  324. {
  325. Write_command(0x80+i);
  326. Write_command(Gray_Parameters[i]);
  327. }
  328. Write_command(ModeSet);
  329. Write_command(ModeSetP1);
  330. */
  331. static const uint8_t u8x8_d_st7528_erc16064_init_seq[] = {
  332. U8X8_START_TRANSFER(), /* enable chip, delay is part of the transfer start */
  333. U8X8_CA(0x038, 0x064), /* ext mode 0*/
  334. U8X8_C(0x0ab), /* start oscillator */
  335. U8X8_CA(0x044, 0x012), /* initial Com0 */
  336. U8X8_C(0x0a0), /* ADC */
  337. U8X8_C(0x0c8), /* SHL */
  338. U8X8_C(0x066), /* boost level, ERC16064: 0x066 */
  339. U8X8_DLY(1),
  340. U8X8_C(0x054), /* LCD Bias, ERC16064: 0x054 */
  341. U8X8_CA(0x048, 0x040), /* partial display duty ratio */
  342. U8X8_C(0x026), /* Select the internal resistance ratio of the regulator resistor */
  343. U8X8_CA(0x081, 0x00b), /* contrast, ERC16064: 0x00b */
  344. U8X8_C(0x02c), /* Power Control */
  345. U8X8_DLY(2),
  346. U8X8_C(0x02e), /* Power Control */
  347. U8X8_DLY(2),
  348. U8X8_C(0x02f), /* Power Control */
  349. U8X8_DLY(2),
  350. U8X8_C(0x092), /* frc and pwm, ERC160624: 0x092 */
  351. U8X8_END_TRANSFER(), /* disable chip */
  352. U8X8_END() /* end of sequence */
  353. };
  354. static const u8x8_display_info_t u8x8_st7528_erc16064_display_info =
  355. {
  356. /* chip_enable_level = */ 0,
  357. /* chip_disable_level = */ 1,
  358. /* post_chip_enable_wait_ns = */ 20,
  359. /* pre_chip_disable_wait_ns = */ 20,
  360. /* reset_pulse_width_ms = */ 5,
  361. /* post_reset_wait_ms = */ 5, /**/
  362. /* sda_setup_time_ns = */ 20, /* st7528 */
  363. /* sck_pulse_width_ns = */ 25, /* st7528 */
  364. /* sck_clock_hz = */ 8000000UL, /* since Arduino 1.6.0, the SPI bus speed in Hz. Should be 1000000000/sck_pulse_width_ns */
  365. /* st7528 actually allows 20MHz according to the datasheet */
  366. /* spi_mode = */ 0, /* active high, rising edge */
  367. /* i2c_bus_clock_100kHz = */ 4,
  368. /* data_setup_time_ns = */ 40,
  369. /* write_pulse_width_ns = */ 80, /* st7528 */
  370. /* tile_width = */ 20,
  371. /* tile_height = */ 8,
  372. /* default_x_offset = */ 0, /* x_offset is used as y offset for the ssd1326 */
  373. /* flipmode_x_offset = */ 0, /* x_offset is used as y offset for the ssd1326 */
  374. /* pixel_width = */ 160,
  375. /* pixel_height = */ 64
  376. };
  377. uint8_t u8x8_d_st7528_erc16064(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr)
  378. {
  379. if ( msg == U8X8_MSG_DISPLAY_SETUP_MEMORY )
  380. {
  381. u8x8_d_helper_display_setup_memory(u8x8, &u8x8_st7528_erc16064_display_info);
  382. return 1;
  383. }
  384. if ( msg == U8X8_MSG_DISPLAY_INIT )
  385. {
  386. u8x8_d_helper_display_init(u8x8);
  387. u8x8_cad_SendSequence(u8x8, u8x8_d_st7528_erc16064_init_seq);
  388. u8x8_d_st7528_graylevel_init(u8x8, 0x064);
  389. return 1;
  390. }
  391. return u8x8_d_st7528_generic(u8x8, msg, arg_int, arg_ptr);
  392. }