lcdDrv.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "nwy_osi_api.h"
  2. #include "nwy_lcd_bus.h"
  3. #include "nwy_pm.h"
  4. #include "log.h"
  5. #include "board.h"
  6. #include "lcdDrv.h"
  7. static void _st7735Init(char type);
  8. static bool lcdInitYet=false;
  9. void lcdDrvPowerCtl(bool on_off){
  10. nwy_subpower_switch(NWY_POWER_LCD, on_off, on_off);
  11. }
  12. void lcdDrv_Init(char type){
  13. nwy_lcd_bus_config_t lcd_bus_config = {
  14. .cs = NWY_LCD_BUS_CS_0,
  15. .cs0Polarity = false,
  16. .cs1Polarity = false,
  17. .resetb = true,
  18. .rsPolarity = false,
  19. .wrPolarity = false,
  20. .rdPolarity = false,
  21. .highByte = false,
  22. .clk = 15000000,//总线时钟
  23. };
  24. if(type==0){//第一次初始化
  25. CTL_LCD_BL(0);
  26. nwy_subpower_switch(NWY_POWER_LCD, true, true);
  27. nwy_subpower_switch(NWY_POWER_BACK_LIGHT, true, true);
  28. nwy_subpower_switch(NWY_POWER_RGB_IB0, true, true);
  29. nwy_sleep(200);
  30. nwy_lcd_bus_init(&lcd_bus_config);
  31. nwy_sleep(32);
  32. _st7735Init(type);
  33. nwy_sleep(32);
  34. lcdInitYet=true;
  35. }else{//唤醒后重新初始化
  36. nwy_subpower_switch(NWY_POWER_LCD, true, true);
  37. nwy_lcd_bus_init(&lcd_bus_config);
  38. _st7735Init(type);
  39. }
  40. }
  41. void lcdDrv_DeInit(void){
  42. if(false==lcdInitYet) return;
  43. lcdInitYet = false;
  44. nwy_lcd_bus_deinit();
  45. nwy_subpower_switch(NWY_POWER_BACK_LIGHT, false, false);
  46. nwy_subpower_switch(NWY_POWER_RGB_IB0, false, false);
  47. nwy_subpower_switch(NWY_POWER_LCD, false, false);
  48. }
  49. const unsigned char Cmd_xB1[3]={0x05,0x3c,0x3c};
  50. const unsigned char Cmd_xB2[3]={0x05,0x3c,0x3c};
  51. const unsigned char Cmd_xB3[6]={0x05,0x3c,0x3c,0x05,0x3c,0x3c};
  52. const unsigned char Cmd_xB4[1]={0x03};
  53. const unsigned char Cmd_xC0[3]={0x28,0x08,0x04};
  54. const unsigned char Cmd_xC1[1]={0xC0};
  55. const unsigned char Cmd_xC2[2]={0x0D,0x00};
  56. const unsigned char Cmd_xC3[2]={0x8D,0x2A};
  57. const unsigned char Cmd_xC4[2]={0x8D,0xEE};
  58. const unsigned char Cmd_xC5[1]={0x1A};
  59. const unsigned char Cmd_x36[1]={0x68};//低4位,如果是0b 0000-->RGB 0b 1000-->BGR
  60. const unsigned char Cmd_xE0[16]={0x04,0x22,0x07,0x0A,0x2E,0x30,0x25,0x2A,0x28,0x26,0x2E,0x3A,0x00,0x01,0x03,0x13};
  61. const unsigned char Cmd_xE1[16]={0x04,0x16,0x06,0x0D,0x2D,0x26,0x23,0x27,0x27,0x25,0x2D,0x3B,0x00,0x01,0x04,0x13};
  62. const unsigned char Cmd_x3A[1]={0x05};
  63. static void cmdDataSet(unsigned char cmd, unsigned char *data, int datalen){
  64. int i;
  65. LCD_CtrlWrite_ST7735(cmd);
  66. for(i=0;i<datalen;i++) LCD_DataWrite_ST7735(data[i]);
  67. }
  68. static void _st7735Init(char type)
  69. {
  70. MSG_WARN(1,"lcd _st7735Init start");
  71. if(type==0) nwy_sleep(200);
  72. LCD_CtrlWrite_ST7735(0x11);
  73. if(type==0) nwy_sleep(120);
  74. cmdDataSet(0xB1,Cmd_xB1, sizeof(Cmd_xB1));
  75. cmdDataSet(0xB2,Cmd_xB2, sizeof(Cmd_xB2));
  76. cmdDataSet(0xB3,Cmd_xB3, sizeof(Cmd_xB3));
  77. cmdDataSet(0xB4,Cmd_xB4, sizeof(Cmd_xB4));
  78. cmdDataSet(0xC0,Cmd_xC0, sizeof(Cmd_xC0));
  79. cmdDataSet(0xC1,Cmd_xC1, sizeof(Cmd_xC1));
  80. cmdDataSet(0xC2,Cmd_xC2, sizeof(Cmd_xC2));
  81. cmdDataSet(0xC3,Cmd_xC3, sizeof(Cmd_xC3));
  82. cmdDataSet(0xC4,Cmd_xC4, sizeof(Cmd_xC4));
  83. cmdDataSet(0xC5,Cmd_xC5, sizeof(Cmd_xC5));
  84. cmdDataSet(0x36,Cmd_x36, sizeof(Cmd_x36));
  85. cmdDataSet(0xE0,Cmd_xE0, sizeof(Cmd_xE0));
  86. cmdDataSet(0xE1,Cmd_xE1, sizeof(Cmd_xE1));
  87. cmdDataSet(0x3A,Cmd_x3A, sizeof(Cmd_x3A));
  88. LCD_CtrlWrite_ST7735(0x29);
  89. }
  90. void recoverLightColor(void){
  91. cmdDataSet(0xE0,Cmd_xE0, sizeof(Cmd_xE0));
  92. }
  93. void showBusErrInfo(int i){
  94. static int errCnt=0;
  95. if(i>=0) return;
  96. if(++errCnt%20) MSG_WARN(1,"lcd bus errcnt:%d",errCnt);
  97. }
  98. void LCD_CtrlWrite_ST7735(unsigned char cmd){
  99. int i=100;
  100. do{
  101. if(true==nwy_lcd_bus_write_cmd(cmd)) break;
  102. }while(--i>0);
  103. showBusErrInfo(i);
  104. }
  105. void LCD_DataWrite_ST7735(unsigned char data){
  106. int i=100;
  107. do{
  108. if(true==nwy_lcd_bus_write_data(data)) break;
  109. }while(--i>0);
  110. showBusErrInfo(i);
  111. }