lcmComm_dgus.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * lcmComm_dgus.c
  3. *
  4. * Created on : 2020-10-20
  5. * Update on : 2021-02-20
  6. * Author : Folus Wen, Eason Yang
  7. * Version : D0.01
  8. *
  9. */
  10. #include "lcmComm_dgus.h"
  11. //#define isDebugPrint
  12. //=======================================
  13. // Basic routine
  14. //=======================================
  15. void displayMessageDgus(uint8_t *data, uint16_t len, uint8_t isRX)
  16. {
  17. uint8_t output[8192];
  18. memset(output, 0x00, ARRAY_SIZE(output));
  19. sprintf((char*)output, "%s", (isRX?"RX: ":"TX: "));
  20. for(uint16_t idx = 0;idx<len;idx++)
  21. {
  22. sprintf((char*)output, "%s%02x ", output, data[idx]);
  23. }
  24. #ifdef isDebugPrint
  25. DEBUG_INFO("%s\n", output);
  26. #endif
  27. }
  28. //=======================================
  29. // Call function (Transmit message into LCD)
  30. //=======================================
  31. int transceiverDgus(int32_t fd, uint8_t *tx, uint16_t tx_len, uint8_t *rx, uint16_t rx_len)
  32. {
  33. int result = FAIL;
  34. int len;
  35. tcflush(fd,TCIOFLUSH);
  36. #ifdef isDebugPrint
  37. displayMessageDgus(tx, tx_len, NO);
  38. #endif
  39. usleep(10000);
  40. if(write(fd, tx, tx_len) >= ARRAY_SIZE(tx))
  41. {
  42. if(tx[3] == CMD_REG_WRITE_DATA)
  43. {
  44. result = PASS;
  45. }
  46. else if(tx[3] == CMD_REG_READ_DATA)
  47. {
  48. len = read(fd, rx, rx_len);
  49. if(len > 0)
  50. {
  51. if((rx[0] == CMD_HEADER_1) && (rx[1] == CMD_HEADER_2))
  52. {
  53. if(rx[3] == CMD_REG_READ_DATA)
  54. {
  55. displayMessageDgus(rx, len, YES);
  56. result = PASS;
  57. }
  58. else
  59. {}
  60. }
  61. else
  62. {}
  63. }
  64. else
  65. {}
  66. }
  67. else
  68. {}
  69. }
  70. else
  71. {}
  72. return result;
  73. }
  74. //=======================================
  75. // Call function (Write register value function)
  76. //=======================================
  77. int8_t lcdRegisterWrite(int32_t fd, uint8_t regType, uint16_t address, uint8_t *data, uint16_t dataLen)
  78. {
  79. int8_t result = FAIL;
  80. uint8_t tx[(regType == REG_TYPE_CONTROL? 8 : 6) + dataLen];
  81. uint8_t rx[128];
  82. memset(tx, 0x00, sizeof(tx));
  83. memset(rx, 0x00, sizeof(rx));
  84. tx[0] = CMD_HEADER_1;
  85. tx[1] = CMD_HEADER_2;
  86. tx[2] = (regType == REG_TYPE_CONTROL? 7 : (3 + dataLen));
  87. tx[3] = CMD_REG_WRITE_DATA;
  88. tx[4] = (address >> 8) & 0xff;
  89. tx[5] = (address >> 0) & 0xff;
  90. if(regType == REG_TYPE_CONTROL)
  91. {
  92. if(address == REG_ADDRESS_SET_PAGE_ID)
  93. {
  94. tx[6] = 0x5A;
  95. tx[7] = 0x01;
  96. memcpy(&tx[8], data, dataLen);
  97. }
  98. }
  99. else
  100. {
  101. memcpy(&tx[6], data, dataLen);
  102. }
  103. if(fd > 0)
  104. {
  105. if(transceiverDgus(fd, tx, ARRAY_SIZE(tx), rx, ARRAY_SIZE(rx)))
  106. {
  107. result = PASS;
  108. }
  109. else
  110. {}
  111. }
  112. else
  113. {}
  114. return result;
  115. }
  116. //=======================================
  117. // Call function (Read register value function)
  118. //=======================================
  119. int8_t lcdRegisterRead(int32_t fd, uint8_t regType, uint16_t address, uint8_t *data, uint16_t dataLen)
  120. {
  121. int8_t result = FAIL;
  122. uint8_t tx[(regType == REG_TYPE_CONTROL? 7 : 8)];
  123. uint8_t rx[(regType == REG_TYPE_CONTROL? (7 + dataLen) : (8 + dataLen*2))];
  124. memset(tx, 0x00, sizeof(tx));
  125. memset(rx, 0x00, sizeof(rx));
  126. tx[0] = CMD_HEADER_1;
  127. tx[1] = CMD_HEADER_2;
  128. tx[2] = (regType == REG_TYPE_CONTROL? 4 : 5);
  129. tx[3] = CMD_REG_READ_DATA;
  130. tx[4] = (address >> 8) & 0xff;
  131. tx[5] = (address >> 0) & 0xff;
  132. if(regType == REG_TYPE_CONTROL)
  133. {
  134. tx[6] = dataLen;
  135. }
  136. else if (regType == REG_TYPE_SPECIAL_CONTROL)
  137. {
  138. tx[6] = (dataLen >> 8) & 0xff;
  139. tx[7] = (dataLen >> 0) & 0xff;
  140. }
  141. else
  142. {}
  143. if(fd > 0)
  144. {
  145. if(transceiverDgus(fd, tx, ARRAY_SIZE(tx), rx, ARRAY_SIZE(rx)))
  146. {
  147. if(regType == REG_TYPE_CONTROL)
  148. {
  149. if((rx[3] == CMD_REG_READ_DATA) && (((rx[4] << 8) | rx[5]) == address))
  150. {
  151. memcpy(data, rx + 7, dataLen);
  152. result = PASS;
  153. }
  154. else
  155. {}
  156. }
  157. else if(regType == REG_TYPE_SPECIAL_CONTROL)
  158. {
  159. if((rx[3] == CMD_REG_READ_DATA) && (((rx[4] << 8) | rx[5]) == address))
  160. {
  161. memcpy(data, rx + 8, dataLen*2);
  162. result = PASS;
  163. }
  164. else
  165. {}
  166. }
  167. else
  168. {}
  169. }
  170. else
  171. {}
  172. }
  173. else
  174. {}
  175. return result;
  176. }