lcmComm_dgus.c 4.4 KB

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