lcmComm_dgus.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * lcmComm_dgus.c
  3. *
  4. * Created on : 2020-10-20
  5. * Update on : 2022-03-01
  6. * Author : Folus Wen, Eason Yang
  7. * Version : V0.27
  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. if(write(fd, tx, tx_len) >= ARRAY_SIZE(tx))
  40. {
  41. if(tx[3] == CMD_REG_WRITE_DATA)
  42. {
  43. usleep(100);
  44. len = read(fd, rx, rx_len);
  45. if(len > 0)
  46. {
  47. if((rx[0] == CMD_HEADER_1) && (rx[1] == CMD_HEADER_2))
  48. {
  49. if((rx[3] == CMD_REG_WRITE_DATA) && ((rx[4] == CMD_ACK_VALUE_1) && (rx[5] == CMD_ACK_VALUE_2)))
  50. {
  51. #ifdef isDebugPrint
  52. displayMessageDgus(rx, len, YES);
  53. #endif
  54. result = PASS;
  55. }
  56. else
  57. {}
  58. }
  59. else
  60. {}
  61. }
  62. else
  63. {}
  64. }
  65. else if(tx[3] == CMD_REG_READ_DATA)
  66. {
  67. usleep(100);
  68. len = read(fd, rx, rx_len);
  69. if(len > 0)
  70. {
  71. if((rx[0] == CMD_HEADER_1) && (rx[1] == CMD_HEADER_2))
  72. {
  73. if(rx[3] == CMD_REG_READ_DATA)
  74. {
  75. #ifdef isDebugPrint
  76. displayMessageDgus(rx, len, YES);
  77. #endif
  78. result = PASS;
  79. }
  80. else
  81. {}
  82. }
  83. else
  84. {}
  85. }
  86. else
  87. {}
  88. }
  89. else
  90. {}
  91. }
  92. else
  93. {}
  94. return result;
  95. }
  96. //=======================================
  97. // Call function (Write register value function)
  98. //=======================================
  99. int8_t lcdRegisterWrite(int32_t fd, uint8_t regType, uint16_t address, uint8_t *data, uint16_t dataLen)
  100. {
  101. int8_t result = FAIL;
  102. uint8_t tx[(regType == REG_TYPE_CONTROL? 8 : 6) + dataLen];
  103. uint8_t rx[128];
  104. memset(tx, 0x00, sizeof(tx));
  105. memset(rx, 0x00, sizeof(rx));
  106. tx[0] = CMD_HEADER_1;
  107. tx[1] = CMD_HEADER_2;
  108. tx[2] = (regType == REG_TYPE_CONTROL? 7 : (3 + dataLen));
  109. tx[3] = CMD_REG_WRITE_DATA;
  110. tx[4] = (address >> 8) & 0xff;
  111. tx[5] = (address >> 0) & 0xff;
  112. if(regType == REG_TYPE_CONTROL)
  113. {
  114. if(address == REG_ADDRESS_SET_PAGE_ID)
  115. {
  116. tx[6] = 0x5A;
  117. tx[7] = 0x01;
  118. memcpy(&tx[8], data, dataLen);
  119. }
  120. }
  121. else
  122. {
  123. memcpy(&tx[6], data, dataLen);
  124. }
  125. if(fd > 0)
  126. {
  127. if(transceiverDgus(fd, tx, ARRAY_SIZE(tx), rx, ARRAY_SIZE(rx)) == PASS)
  128. {
  129. result = PASS;
  130. }
  131. else
  132. {}
  133. }
  134. else
  135. {}
  136. return result;
  137. }
  138. //=======================================
  139. // Call function (Read register value function)
  140. //=======================================
  141. int8_t lcdRegisterRead(int32_t fd, uint8_t regType, uint16_t address, uint8_t *data, uint16_t dataLen)
  142. {
  143. int8_t result = FAIL;
  144. uint8_t tx[(regType == REG_TYPE_CONTROL? 7 : 8)];
  145. uint8_t rx[(regType == REG_TYPE_CONTROL? (7 + dataLen) : (8 + dataLen*2))];
  146. memset(tx, 0x00, sizeof(tx));
  147. memset(rx, 0x00, sizeof(rx));
  148. tx[0] = CMD_HEADER_1;
  149. tx[1] = CMD_HEADER_2;
  150. tx[2] = (regType == REG_TYPE_CONTROL? 4 : 5);
  151. tx[3] = CMD_REG_READ_DATA;
  152. tx[4] = (address >> 8) & 0xff;
  153. tx[5] = (address >> 0) & 0xff;
  154. if(regType == REG_TYPE_CONTROL)
  155. {
  156. tx[6] = dataLen;
  157. }
  158. else if (regType == REG_TYPE_SPECIAL_CONTROL)
  159. {
  160. tx[6] = (dataLen >> 8) & 0xff;
  161. tx[7] = (dataLen >> 0) & 0xff;
  162. }
  163. else
  164. {}
  165. if(fd > 0)
  166. {
  167. if(transceiverDgus(fd, tx, ARRAY_SIZE(tx), rx, ARRAY_SIZE(rx)))
  168. {
  169. if(regType == REG_TYPE_CONTROL)
  170. {
  171. if((rx[3] == CMD_REG_READ_DATA) && (((rx[4] << 8) | rx[5]) == address))
  172. {
  173. memcpy(data, rx + 7, dataLen);
  174. result = PASS;
  175. }
  176. else
  177. {}
  178. }
  179. else if(regType == REG_TYPE_SPECIAL_CONTROL)
  180. {
  181. if((rx[3] == CMD_REG_READ_DATA) && (((rx[4] << 8) | rx[5]) == address))
  182. {
  183. memcpy(data, rx + 8, dataLen*2);
  184. result = PASS;
  185. }
  186. else
  187. {}
  188. }
  189. else
  190. {}
  191. }
  192. else
  193. {}
  194. }
  195. else
  196. {}
  197. return result;
  198. }