lcdDrv.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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_xB1[3]={0x05,0x3c,0x3c};
  14. const unsigned char Cmd_xB2[3]={0x05,0x3c,0x3c};
  15. const unsigned char Cmd_xB3[6]={0x05,0x3c,0x3c,0x05,0x3c,0x3c};
  16. const unsigned char Cmd_xB4[1]={0x03};
  17. const unsigned char Cmd_xC0[3]={0x28,0x08,0x04};
  18. const unsigned char Cmd_xC1[1]={0xC0};
  19. const unsigned char Cmd_xC2[2]={0x0D,0x00};
  20. const unsigned char Cmd_xC3[2]={0x8D,0x2A};
  21. const unsigned char Cmd_xC4[2]={0x8D,0xEE};
  22. const unsigned char Cmd_xC5[1]={0x1A};
  23. const unsigned char Cmd_x36[1]={0x68};//µÍ4λ,Èç¹ûÊÇ0b 0000-->RGB 0b 1000-->BGR
  24. const unsigned char Cmd_xE0[16]={0x04,0x22,0x07,0x0A,0x2E,0x30,0x25,0x2A,0x28,0x26,0x2E,0x3A,0x00,0x01,0x03,0x13};
  25. const unsigned char Cmd_xE1[16]={0x04,0x16,0x06,0x0D,0x2D,0x26,0x23,0x27,0x27,0x25,0x2D,0x3B,0x00,0x01,0x04,0x13};
  26. const unsigned char Cmd_x3A[1]={0x05};
  27. const unsigned char Cmd_xB3_1[1]={0x03};
  28. const unsigned char Cmd_xB4_1[1]={0x21};
  29. const unsigned char Cmd_xB6[1]={0x01};
  30. const unsigned char Cmd_xA3[1]={0x11};
  31. const unsigned char Cmd_xF0[14]={0x36,0x4c,0x1b,0x58,0x84,0x24,0x25,0x00,0x34,0x06,0x06,0x16,0x15,0x0f};
  32. const unsigned char Cmd_xF1[14]={0x12,0x2d,0x25,0x3f,0xa9,0x13,0x15,0x00,0x11,0x0a,0x0a,0x17,0x16,0x0f};
  33. static void cmdDataSet(unsigned char cmd, unsigned char *data, int datalen){
  34. int i;
  35. LCD_CtrlWrite_ST7735(cmd);
  36. for(i=0;i<datalen;i++) LCD_DataWrite_ST7735(data[i]);
  37. }
  38. void LCD_DataRead_ST7735(unsigned char *reg, unsigned char *data, unsigned int readLen){
  39. halGoudaReadData(reg, data, readLen);
  40. }
  41. /**************************************************************************************/
  42. // Description: initialize all LCD with LCDC MCU MODE and LCDC mcu mode
  43. /**************************************************************************************/
  44. static void _st7735Init(void)
  45. {
  46. char lcdtype;
  47. unsigned char id[4];
  48. MSG_INFO(1, "lcd: _st7735Init ");
  49. LCD_DataRead_ST7735(0x04, id, 4);
  50. if(id[1]==0xf0 && id[2]==0x89 && id[3]==0x7c) lcdtype=0;//st7735
  51. else if(id[1]==0x06 && id[2]==0x91 && id[3]==0x00) lcdtype=1;//gc9106
  52. MSG_INFO(1, "id:%02x%02x%02x", id[1],id[2],id[3]);
  53. if(lcdtype==0){
  54. LCD_CtrlWrite_ST7735(0x11); //Sleep out
  55. _lcdDelayMs(10); //Delay 120ms
  56. cmdDataSet(0xB1,Cmd_xB1, sizeof(Cmd_xB1));
  57. cmdDataSet(0xB2,Cmd_xB2, sizeof(Cmd_xB2));
  58. cmdDataSet(0xB3,Cmd_xB3, sizeof(Cmd_xB3));
  59. cmdDataSet(0xB4,Cmd_xB4, sizeof(Cmd_xB4));
  60. cmdDataSet(0xC0,Cmd_xC0, sizeof(Cmd_xC0));
  61. cmdDataSet(0xC1,Cmd_xC1, sizeof(Cmd_xC1));
  62. cmdDataSet(0xC2,Cmd_xC2, sizeof(Cmd_xC2));
  63. cmdDataSet(0xC3,Cmd_xC3, sizeof(Cmd_xC3));
  64. cmdDataSet(0xC4,Cmd_xC4, sizeof(Cmd_xC4));
  65. cmdDataSet(0xC5,Cmd_xC5, sizeof(Cmd_xC5));
  66. cmdDataSet(0x36,Cmd_x36, sizeof(Cmd_x36));
  67. cmdDataSet(0xE0,Cmd_xE0, sizeof(Cmd_xE0));
  68. cmdDataSet(0xE1,Cmd_xE1, sizeof(Cmd_xE1));
  69. cmdDataSet(0x3A,Cmd_x3A, sizeof(Cmd_x3A));
  70. LCD_CtrlWrite_ST7735(0x29); //Display on
  71. _lcdDelayMs(2);
  72. LCD_CtrlWrite_ST7735(0x2c);
  73. }else if(lcdtype==1){
  74. LCD_CtrlWrite_ST7735(0x01);
  75. LCD_CtrlWrite_ST7735(0xfe);
  76. LCD_CtrlWrite_ST7735(0xef);
  77. cmdDataSet(0xB3,Cmd_xB3_1, sizeof(Cmd_xB3_1));
  78. cmdDataSet(0xB6,Cmd_xB6, sizeof(Cmd_xB6));
  79. cmdDataSet(0xA3,Cmd_xA3, sizeof(Cmd_xA3));
  80. LCD_CtrlWrite_ST7735(0x21);
  81. cmdDataSet(0x36,Cmd_x36, sizeof(Cmd_x36));
  82. cmdDataSet(0x3a,Cmd_x3A, sizeof(Cmd_x3A));
  83. cmdDataSet(0xB4,Cmd_xB4_1, sizeof(Cmd_xB4_1));
  84. cmdDataSet(0xF0,Cmd_xF0, sizeof(Cmd_xF0));
  85. cmdDataSet(0xF1,Cmd_xF1, sizeof(Cmd_xF1));
  86. LCD_CtrlWrite_ST7735(0xfe);
  87. LCD_CtrlWrite_ST7735(0xef);
  88. LCD_CtrlWrite_ST7735(0x11);
  89. _lcdDelayMs(120); //Delay 120ms
  90. LCD_CtrlWrite_ST7735(0x29); //Display on
  91. }
  92. }
  93. static const lcdOperations_t st7735sOperations =
  94. {
  95. _st7735Init,
  96. NULL,//_st7735SleepIn,
  97. NULL,//_st7735EsdCheck,
  98. NULL,//_st7735SetDisplayWindow,
  99. NULL,//_st7735InvalidateRect,
  100. NULL,//_st7735Invalidate,
  101. NULL,//_st7735Close,
  102. NULL,//_st7735RotationInvalidateRect,
  103. NULL,
  104. NULL,//_st7735ReadId,
  105. };
  106. const lcdSpec_t g_lcd_st7735s =
  107. {
  108. LCD_DRV_ID_ST7735,
  109. GLCD_WIDTH,
  110. GLCD_HEIGHT,
  111. HAL_GOUDA_SPI_LINE_4,
  112. LCD_CTRL_SPI,
  113. (lcdOperations_t *)&st7735sOperations,
  114. false,
  115. 0x2a000,
  116. 10000000,// 500000, //10000000, 10M
  117. };
  118. static lcdSpec_t *lcd_cfg_tab[] =
  119. {
  120. (lcdSpec_t *)&g_lcd_st7735s,
  121. };
  122. char lcdDrv_Init(char type){
  123. bool status;
  124. drvLcdInfoRegisterFromOpenCpu(lcd_cfg_tab, 0, 1, 0);
  125. LSAPI_PmuSetPowerLevel(OSI_MAKE_TAG('L', 'C', 'D', ' '), 2800);
  126. //LSAPI_OSI_ThreadSleep(100);
  127. LSAPI_PmuSwitchPower(OSI_MAKE_TAG('L', 'C', 'D', ' '), true, true);
  128. //LSAPI_OSI_ThreadSleep(100);
  129. drvLcdClose();
  130. status=drvLcdInit();
  131. MSG_INFO(1, "Lcd status:%d", status);
  132. if(status==false) return 1;
  133. else return 0;
  134. }
  135. void showBusErrInfo(int i){
  136. static int errCnt=0;
  137. if(i>=0) return;
  138. if(++errCnt%20) MSG_WARN(1,"lcd bus errcnt:%d",errCnt);
  139. }
  140. void LCD_CtrlWrite_ST7735(unsigned char cmd){
  141. int i=100;
  142. do{
  143. if(HAL_ERR_NO==halGoudaWriteCmd(cmd)) break;
  144. }while(--i>0);
  145. showBusErrInfo(i);
  146. }
  147. void LCD_DataWrite_ST7735(unsigned char data){
  148. int i=100;
  149. do{
  150. if(HAL_ERR_NO==halGoudaWriteData(data)) break;
  151. }while(--i>0);
  152. showBusErrInfo(i);
  153. }