lcdDrv.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. #include "lcdDrv.h"
  2. #include "osi_api.h"
  3. #include "osi_log.h"
  4. #include "hal_gouda.h"
  5. #include "drv_lcd.h"
  6. #include "gui.h"
  7. #include "log.h"
  8. #define LCD_DRV_ID_ST7735 0x7c89f0
  9. static void _lcdDelayMs(int ms_delay)
  10. {
  11. osiDelayUS(ms_delay * 1000);
  12. }
  13. const unsigned char Cmd_xC8[3]={0xff,0x93,0x42};
  14. const unsigned char Cmd_xB6[2]={0x0A,0Xe0};
  15. const unsigned char Cmd_x36[1]={0x00};//d0
  16. const unsigned char Cmd_x3A[1]={0x06};
  17. const unsigned char Cmd_xC0[1]={0x2c};
  18. const unsigned char Cmd_xC1[1]={0x02};
  19. const unsigned char Cmd_xC2[1]={0x01};
  20. const unsigned char Cmd_xC3[1]={0x1a};
  21. const unsigned char Cmd_xC4[1]={0x20};
  22. const unsigned char Cmd_xC5[1]={0xca};
  23. const unsigned char Cmd_xC6[1]={0x0f};
  24. const unsigned char Cmd_xD0[2]={0xA4,0xa1};
  25. const unsigned char Cmd_xB1[2]={0x00,0x1b};
  26. const unsigned char Cmd_xB2[5]={0x0c,0x0c,0x00,0x33,0x33};
  27. const unsigned char Cmd_xB4[1]={0x02};
  28. const unsigned char Cmd_xB7[1]={0x35};
  29. const unsigned char Cmd_xBB[1]={0x38};
  30. const unsigned char Cmd_xE0[15]={0x00,0x01,0x04,0x00,0x11,0x08,0x35,0x79,0x45,0x07,0x0D,0x09,0x16,0x17,0x0F};
  31. const unsigned char Cmd_xE1[15]={0x00,0x28,0x29,0x02,0x0F,0x06,0x3F,0x25,0x55,0x06,0x15,0x0F,0x38,0x38,0x0F};
  32. static void cmdDataSet(unsigned char cmd, unsigned char *data, int datalen){
  33. int i;
  34. LCD_CtrlWrite_ST7735(cmd);
  35. for(i=0;i<datalen;i++) LCD_DataWrite_ST7735(data[i]);
  36. }
  37. static void cmdDataSetOne(unsigned char cmd, unsigned char data){
  38. LCD_CtrlWrite_ST7735(cmd);
  39. LCD_DataWrite_ST7735(data);
  40. }
  41. /**************************************************************************************/
  42. // Description: initialize all LCD with LCDC MCU MODE and LCDC mcu mode
  43. /**************************************************************************************/
  44. static void _st7735Init(void)
  45. {
  46. char infoo[50];
  47. char lcdtype=0;
  48. unsigned char id[4];
  49. MSG_INFO(1, "lcd: _st7735Init ");
  50. LCD_DataRead_ST7735(0x04, id, 4);
  51. if(id[1]==0xf0 && id[2]==0x89 && id[3]==0x7c) lcdtype=0;//st7735
  52. else if(id[1]==0x06 && id[2]==0x91 && id[3]==0x00) lcdtype=1;//gc9106
  53. MSG_INFO(1, "id:%02x%02x%02x", id[1],id[2],id[3]);
  54. lcdtype=0;//test
  55. if(lcdtype==0){
  56. LCD_CtrlWrite_ST7735(0x11); //Sleep out
  57. _lcdDelayMs(10); //Delay 120ms
  58. #if 0
  59. //--------------------------------------Display Setting---------------------------------------//
  60. LCD_CtrlWrite_ST7735 (0x36);
  61. LCD_DataWrite_ST7735 (0xc8);
  62. LCD_CtrlWrite_ST7735 (0x3a);
  63. LCD_DataWrite_ST7735 (0x05);
  64. //--------------------------------ST7789S Frame rate setting----------------------------------//
  65. LCD_CtrlWrite_ST7735 (0xb2);
  66. LCD_DataWrite_ST7735 (0x0c);
  67. LCD_DataWrite_ST7735 (0x0c);
  68. LCD_DataWrite_ST7735 (0x00);
  69. LCD_DataWrite_ST7735 (0x33);
  70. LCD_DataWrite_ST7735 (0x33);
  71. LCD_CtrlWrite_ST7735 (0xb7);
  72. LCD_DataWrite_ST7735 (0x35);
  73. //---------------------------------ST7789S Power setting--------------------------------------//
  74. LCD_CtrlWrite_ST7735 (0xbb);
  75. LCD_DataWrite_ST7735 (0x35);
  76. LCD_CtrlWrite_ST7735 (0xc0);
  77. LCD_DataWrite_ST7735 (0x2c);
  78. LCD_CtrlWrite_ST7735 (0xc2);
  79. LCD_DataWrite_ST7735 (0x01);
  80. LCD_CtrlWrite_ST7735 (0xc3);
  81. LCD_DataWrite_ST7735 (0x11);
  82. LCD_CtrlWrite_ST7735 (0xc4);
  83. LCD_DataWrite_ST7735 (0x20);
  84. LCD_CtrlWrite_ST7735 (0xc6);
  85. LCD_DataWrite_ST7735 (0x0f);
  86. LCD_CtrlWrite_ST7735 (0xd0);
  87. LCD_DataWrite_ST7735 (0xa4);
  88. LCD_DataWrite_ST7735 (0xa1);
  89. //--------------------------------ST7789S gamma setting---------------------------------------//
  90. LCD_CtrlWrite_ST7735 (0xe0);
  91. LCD_DataWrite_ST7735 (0xd0);
  92. LCD_DataWrite_ST7735 (0x00);
  93. LCD_DataWrite_ST7735 (0x05);
  94. LCD_DataWrite_ST7735 (0x0e);
  95. LCD_DataWrite_ST7735 (0x15);
  96. LCD_DataWrite_ST7735 (0x0d);
  97. LCD_DataWrite_ST7735 (0x37);
  98. LCD_DataWrite_ST7735 (0x43);
  99. LCD_DataWrite_ST7735 (0x47);
  100. LCD_DataWrite_ST7735 (0x09);
  101. LCD_DataWrite_ST7735 (0x15);
  102. LCD_DataWrite_ST7735 (0x12);
  103. LCD_DataWrite_ST7735 (0x16);
  104. LCD_DataWrite_ST7735 (0x19);
  105. LCD_CtrlWrite_ST7735 (0xe1);
  106. LCD_DataWrite_ST7735 (0xd0);
  107. LCD_DataWrite_ST7735 (0x00);
  108. LCD_DataWrite_ST7735 (0x05);
  109. LCD_DataWrite_ST7735 (0x0d);
  110. LCD_DataWrite_ST7735 (0x0c);
  111. LCD_DataWrite_ST7735 (0x06);
  112. LCD_DataWrite_ST7735 (0x2d);
  113. LCD_DataWrite_ST7735 (0x44);
  114. LCD_DataWrite_ST7735 (0x40);
  115. LCD_DataWrite_ST7735 (0x0e);
  116. LCD_DataWrite_ST7735 (0x1c);
  117. LCD_DataWrite_ST7735 (0x18);
  118. LCD_DataWrite_ST7735 (0x16);
  119. LCD_DataWrite_ST7735 (0x19);
  120. LCD_CtrlWrite_ST7735 (0x29);
  121. #else
  122. LCD_CtrlWrite_ST7735(0x36);
  123. LCD_DataWrite_ST7735(0xc8);
  124. LCD_CtrlWrite_ST7735(0x3A);
  125. LCD_DataWrite_ST7735(0x05);
  126. LCD_CtrlWrite_ST7735(0x35);
  127. LCD_DataWrite_ST7735(0x00);
  128. LCD_CtrlWrite_ST7735(0x44);
  129. LCD_DataWrite_ST7735(0x00);
  130. LCD_DataWrite_ST7735(0x20);
  131. LCD_CtrlWrite_ST7735(0xB2);
  132. LCD_DataWrite_ST7735(0x0C);
  133. LCD_DataWrite_ST7735(0x0C);
  134. LCD_DataWrite_ST7735(0x00);
  135. LCD_DataWrite_ST7735(0x33);
  136. LCD_DataWrite_ST7735(0x33);
  137. LCD_CtrlWrite_ST7735(0xB7);
  138. LCD_DataWrite_ST7735(0x75);
  139. LCD_CtrlWrite_ST7735(0xBB);
  140. LCD_DataWrite_ST7735(0x1F);
  141. LCD_CtrlWrite_ST7735(0xC0);
  142. LCD_DataWrite_ST7735(0x2C);
  143. LCD_CtrlWrite_ST7735(0xC2);
  144. LCD_DataWrite_ST7735(0x01);
  145. LCD_CtrlWrite_ST7735(0xC3);
  146. LCD_DataWrite_ST7735(0x13);
  147. LCD_CtrlWrite_ST7735(0xC4);
  148. LCD_DataWrite_ST7735(0x20);
  149. LCD_CtrlWrite_ST7735(0xC6);
  150. LCD_DataWrite_ST7735(0x0F);
  151. LCD_CtrlWrite_ST7735(0xD0);
  152. LCD_DataWrite_ST7735(0xA4);
  153. LCD_DataWrite_ST7735(0xA1);
  154. LCD_CtrlWrite_ST7735(0xD6);
  155. LCD_DataWrite_ST7735(0xA1);
  156. LCD_CtrlWrite_ST7735(0x21);
  157. LCD_CtrlWrite_ST7735(0xE0);
  158. LCD_DataWrite_ST7735(0xD0);
  159. LCD_DataWrite_ST7735(0x08);
  160. LCD_DataWrite_ST7735(0x10);
  161. LCD_DataWrite_ST7735(0x0D);
  162. LCD_DataWrite_ST7735(0x0C);
  163. LCD_DataWrite_ST7735(0x07);
  164. LCD_DataWrite_ST7735(0x37);
  165. LCD_DataWrite_ST7735(0x53);
  166. LCD_DataWrite_ST7735(0x4C);
  167. LCD_DataWrite_ST7735(0x39);
  168. LCD_DataWrite_ST7735(0x15);
  169. LCD_DataWrite_ST7735(0x15);
  170. LCD_DataWrite_ST7735(0x2A);
  171. LCD_DataWrite_ST7735(0x2D);
  172. LCD_CtrlWrite_ST7735(0xE1);
  173. LCD_DataWrite_ST7735(0xD0);
  174. LCD_DataWrite_ST7735(0x0D);
  175. LCD_DataWrite_ST7735(0x12);
  176. LCD_DataWrite_ST7735(0x08);
  177. LCD_DataWrite_ST7735(0x08);
  178. LCD_DataWrite_ST7735(0x15);
  179. LCD_DataWrite_ST7735(0x34);
  180. LCD_DataWrite_ST7735(0x34);
  181. LCD_DataWrite_ST7735(0x4A);
  182. LCD_DataWrite_ST7735(0x36);
  183. LCD_DataWrite_ST7735(0x12);
  184. LCD_DataWrite_ST7735(0x13);
  185. LCD_DataWrite_ST7735(0x2B);
  186. LCD_DataWrite_ST7735(0x2F);
  187. LCD_CtrlWrite_ST7735(0x29);
  188. #endif
  189. _lcdDelayMs(2);
  190. //LCD_CtrlWrite_ST7735(0x2c);
  191. }else if(lcdtype==1){
  192. LCD_CtrlWrite_ST7735(0x01);
  193. LCD_CtrlWrite_ST7735(0xfe);
  194. LCD_CtrlWrite_ST7735(0xef);
  195. cmdDataSet(0xC8,Cmd_xC8, sizeof(Cmd_xC8));
  196. cmdDataSet(0xB6,Cmd_xB6, sizeof(Cmd_xB6));
  197. cmdDataSet(0x36,Cmd_x36, sizeof(Cmd_x36));
  198. cmdDataSet(0X3A,Cmd_x3A, sizeof(Cmd_x3A));
  199. cmdDataSet(0xc0,Cmd_xC0, sizeof(Cmd_xC0));
  200. cmdDataSet(0xC1,Cmd_xC1, sizeof(Cmd_xC1));
  201. cmdDataSet(0xC5,Cmd_xC5, sizeof(Cmd_xC5));
  202. cmdDataSet(0XB1,Cmd_xB1, sizeof(Cmd_xB1));
  203. cmdDataSet(0xB4,Cmd_xB4, sizeof(Cmd_xB4));
  204. cmdDataSet(0xE0,Cmd_xE0, sizeof(Cmd_xE0));
  205. cmdDataSet(0XE1,Cmd_xE1, sizeof(Cmd_xE1));
  206. LCD_CtrlWrite_ST7735(0xfe);
  207. LCD_CtrlWrite_ST7735(0xef);
  208. LCD_CtrlWrite_ST7735(0x11);
  209. _lcdDelayMs(120); //Delay 120ms
  210. LCD_CtrlWrite_ST7735(0x29); //Display on
  211. }
  212. }
  213. static const lcdOperations_t st7735sOperations =
  214. {
  215. _st7735Init,
  216. NULL,//_st7735SleepIn,
  217. NULL,//_st7735EsdCheck,
  218. NULL,//_st7735SetDisplayWindow,
  219. NULL,//_st7735InvalidateRect,
  220. NULL,//_st7735Invalidate,
  221. NULL,//_st7735Close,
  222. NULL,//_st7735RotationInvalidateRect,
  223. NULL,
  224. NULL,//_st7735ReadId,
  225. };
  226. const lcdSpec_t g_lcd_st7735s =
  227. {
  228. LCD_DRV_ID_ST7735,
  229. GLCD_WIDTH,
  230. GLCD_HEIGHT,
  231. HAL_GOUDA_SPI_LINE_4,
  232. LCD_CTRL_SPI,
  233. (lcdOperations_t *)&st7735sOperations,
  234. false,
  235. 0x2a000,
  236. 20000000,// 500000, //10000000, 10M
  237. };
  238. static lcdSpec_t *lcd_cfg_tab[] =
  239. {
  240. (lcdSpec_t *)&g_lcd_st7735s,
  241. };
  242. char lcdDrv_Init(char type){
  243. bool status;
  244. drvLcdInfoRegisterFromOpenCpu(lcd_cfg_tab, 0, 1, 0);
  245. LSAPI_PmuSetPowerLevel(OSI_MAKE_TAG('L', 'C', 'D', ' '), 2800);
  246. //LSAPI_OSI_ThreadSleep(100);
  247. LSAPI_PmuSwitchPower(OSI_MAKE_TAG('L', 'C', 'D', ' '), true, true);
  248. //LSAPI_OSI_ThreadSleep(100);
  249. LSAPI_PmuSetPowerLevel(OSI_MAKE_TAG('C', 'A', 'M', 'A'), 3200);//2800
  250. LSAPI_OSI_ThreadSleep(100);
  251. /*
  252. LSAPI_PmuSwitchPower(OSI_MAKE_TAG('C', 'A', 'M', 'A'), true, true);
  253. LSAPI_OSI_ThreadSleep(100);
  254. */
  255. drvLcdClose();
  256. status=drvLcdInit();
  257. lcdBackLightApi(0);
  258. MSG_INFO(1, "Lcd status:%d", status);
  259. if(status==false) return 1;
  260. else return 0;
  261. }
  262. void showBusErrInfo(int i){
  263. static int errCnt=0;
  264. if(i>=0) return;
  265. if(++errCnt%20) MSG_WARN(1,"lcd bus errcnt:%d",errCnt);
  266. }
  267. void LCD_CtrlWrite_ST7735(unsigned char cmd){
  268. int i=100;
  269. do{
  270. if(HAL_ERR_NO==halGoudaWriteCmd(cmd)) break;
  271. }while(--i>0);
  272. showBusErrInfo(i);
  273. }
  274. void LCD_DataWrite_ST7735(unsigned char data){
  275. int i=100;
  276. do{
  277. if(HAL_ERR_NO==halGoudaWriteData(data)) break;
  278. }while(--i>0);
  279. showBusErrInfo(i);
  280. }
  281. void LCD_DataRead_ST7735(unsigned char *reg, unsigned char *data, unsigned int readLen){
  282. halGoudaReadData(reg, data, readLen);
  283. }
  284. void lcdBackList(char onoff){
  285. bool write_value = onoff;
  286. if(onoff)LSAPI_PmuSwitchPower(OSI_MAKE_TAG('C', 'A', 'M', 'A'), onoff, true);
  287. else LSAPI_PmuSwitchPower(OSI_MAKE_TAG('C', 'A', 'M', 'A'), onoff, false);
  288. }