Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. #include <thread>
  2. #include <chrono>
  3. #include "common.hpp"
  4.  
  5. static bool fifo_600mode;
  6. static thread measure_thread;
  7. static thread write_thread;
  8. static thread read_thread;
  9. static const int BUFFER_LEN = 32*1024;
  10. static unsigned int check=0;
  11.  
  12. static char hex_val[16] = { '0', '1', '2', '3', '4', '5','6','7','8','9','A','B', 'C', 'D', 'E', 'F'};
  13.  
  14. unsigned long long dec2bin(unsigned int c)
  15. {
  16. int i = 0;
  17. unsigned long long bin=0;
  18. for(i = 7; i >= 0; i--){
  19. if((c & (1 << i)) != 0){
  20. bin=bin*10 + 1;
  21. }else{
  22. bin=bin*10;
  23. }
  24. }
  25. return bin;
  26. }
  27.  
  28. static void write_test(FT_HANDLE handle)
  29. {
  30. unique_ptr<uint8_t[]> buf(new uint8_t[BUFFER_LEN]);
  31.  
  32. while (!do_exit) {
  33. for (uint8_t channel = 0; channel < out_ch_cnt; channel++) {
  34. ULONG count = 0;
  35. if (FT_OK != FT_WritePipeEx(handle, channel,
  36. buf.get(), BUFFER_LEN, &count, 1000)) {
  37. do_exit = true;
  38. break;
  39. }
  40. tx_count += count;
  41. }
  42. }
  43. printf("Write stopped\r\n");
  44. }
  45.  
  46. static void read_test(FT_HANDLE handle)
  47. {
  48. //unsigned int buffer_value;
  49. unique_ptr<uint8_t[]> buf(new uint8_t[BUFFER_LEN]);
  50.  
  51. while (!do_exit) {
  52. for (uint8_t channel = 0; channel < in_ch_cnt; channel++) {
  53. ULONG count = 0;
  54. if (FT_OK != FT_ReadPipeEx(handle, channel,
  55. buf.get(), BUFFER_LEN, &count, 1000)) {
  56. do_exit = true;
  57. break;
  58. }
  59. // Addition, comment for viewing data rates
  60. else
  61. {
  62. for (int k=0; k< BUFFER_LEN; k=k+4) {
  63. //buffer_value = (uint32_t*)buf;//(unsigned int)buf[k+3]+(unsigned int)buf[k+2]*256+(unsigned int)buf[k+1]*65536+(unsigned int)buf[k]*16777216;
  64. // if((check+1) != buffer_value)
  65. // { printf("Error at %u ie %llu %llu %llu %llu \n", check, dec2bin(buf[k]), dec2bin(buf[k+1]), dec2bin(buf[k+2]), dec2bin(buf[k+3]));
  66. // //for( int y =0; y< BUFFER_LEN ; y=y+4)printf("%llu %llu %llu %llu ", dec2bin(buf[y]), dec2bin(buf[y+1]), dec2bin(buf[y+2]), dec2bin(buf[y+3]));
  67. // }
  68. if((check + 1)!= buf[k+3] && (check!=255 or buf[k+3]!=0) )printf("Buffer Value : ie %c%c%c%c%c%c%c%c \n", hex_val[buf[k]/16], hex_val[buf[k]%16], hex_val[buf[k+1]/16], hex_val[buf[k+1]%16], hex_val[buf[k+2]/16], hex_val[buf[k+2]%16], hex_val[buf[k+3]/16], hex_val[buf[k+3]%16]);
  69. //
  70. //if(((buf[k+3] - buf[k+2]) != 68) && ((buf[k+2] - buf[k+3]) != 1880))
  71. check = buf[k+3];
  72. }
  73. }
  74. //Addition
  75. rx_count += count;
  76. }
  77. }
  78. printf("Read stopped\r\n");
  79. }
  80.  
  81. static void show_help(const char *bin)
  82. {
  83. printf("Usage: %s <out channel count> <in channel count> [mode]\r\n", bin);
  84. printf(" channel count: [0, 1] for 245 mode, [0-4] for 600 mode\r\n");
  85. printf(" mode: 0 = FT245 mode (default), 1 = FT600 mode\r\n");
  86. }
  87.  
  88. static void get_queue_status(HANDLE handle)
  89. {
  90. for (uint8_t channel = 0; channel < out_ch_cnt; channel++) {
  91. DWORD dwBufferred;
  92.  
  93. if (FT_OK != FT_GetUnsentBuffer(handle, channel,
  94. NULL, &dwBufferred)) {
  95. printf("Failed to get unsent buffer size\r\n");
  96. continue;
  97. }
  98. unique_ptr<uint8_t[]> p(new uint8_t[dwBufferred]);
  99.  
  100. printf("CH%d OUT unsent buffer size in queue:%u\r\n",
  101. channel, dwBufferred);
  102. if (FT_OK != FT_GetUnsentBuffer(handle, channel,
  103. p.get(), &dwBufferred)) {
  104. printf("Failed to read unsent buffer size\r\n");
  105. continue;
  106. }
  107. }
  108.  
  109. for (uint8_t channel = 0; channel < in_ch_cnt; channel++) {
  110. DWORD dwBufferred;
  111.  
  112. if (FT_OK != FT_GetReadQueueStatus(handle, channel, &dwBufferred))
  113. continue;
  114. printf("CH%d IN unread buffer size in queue:%u\r\n",
  115. channel, dwBufferred);
  116. }
  117. }
  118.  
  119. static bool validate_arguments(int argc, char *argv[])
  120. {
  121. if (argc != 3 && argc != 4)
  122. return false;
  123.  
  124. if (argc == 4) {
  125. int val = atoi(argv[3]);
  126. if (val != 0 && val != 1)
  127. return false;
  128. fifo_600mode = (bool)val;
  129. }
  130.  
  131. out_ch_cnt = atoi(argv[1]);
  132. in_ch_cnt = atoi(argv[2]);
  133.  
  134. if ((in_ch_cnt == 0 && out_ch_cnt == 0) ||
  135. in_ch_cnt > 4 || out_ch_cnt > 4) {
  136. show_help(argv[0]);
  137. return false;
  138. }
  139. return true;
  140. }
  141.  
  142. int main(int argc, char *argv[])
  143. {
  144.  
  145. get_version();
  146.  
  147. if (!validate_arguments(argc, argv)) {
  148. show_help(argv[0]);
  149. return 1;
  150. }
  151.  
  152. if (!get_device_lists(500))
  153. return 1;
  154.  
  155. if (!set_channel_config(fifo_600mode, CONFIGURATION_FIFO_CLK_100))
  156. return 1;
  157.  
  158. /* Must be called before FT_Create is called */
  159. turn_off_thread_safe();
  160.  
  161. FT_HANDLE handle = NULL;
  162.  
  163. FT_Create(0, FT_OPEN_BY_INDEX, &handle);
  164. if (!handle) {
  165. printf("Failed to create device\r\n");
  166. return -1;
  167. }
  168. if (out_ch_cnt)
  169. write_thread = thread(write_test, handle);
  170. if (in_ch_cnt)
  171. read_thread = thread(read_test, handle);
  172. measure_thread = thread(show_throughput, handle);
  173. register_signals();
  174.  
  175. if (write_thread.joinable())
  176. write_thread.join();
  177. if (read_thread.joinable())
  178. read_thread.join();
  179. if (measure_thread.joinable())
  180. measure_thread.join();
  181. get_queue_status(handle);
  182. return 0;
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement