KISS Data Aquisition and Control System
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

807 lines
26 KiB

  1. // Copyright Doug Lewis Sept 2017
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <prussdrv.h>
  5. #include <pruss_intc_mapping.h>
  6. #include <stdint.h>
  7. #include <unistd.h>
  8. #include <string.h>
  9. #include <errno.h>
  10. #include <signal.h>
  11. #include <fcntl.h>
  12. #include <ctype.h>
  13. #include <termios.h>
  14. #include <sys/types.h>
  15. #include <sys/mman.h>
  16. #include <sys/socket.h>
  17. #include <netinet/in.h>
  18. #include <netdb.h>
  19. #include <arpa/inet.h>
  20. #include </home/erps/daq2/pruSrc/pruadc.h>
  21. unsigned int adc_pru_data0[ADC_PRU_DATA_SIZE];
  22. unsigned int adc_pru_data1[ADC_PRU_DATA_SIZE];
  23. typedef struct adc_conf_t {
  24. unsigned int cycles_per_clock;
  25. unsigned int down_sample;
  26. unsigned int control_string;
  27. unsigned int num_blocks; // File size in number of 128k blocks to record
  28. unsigned int block_size;
  29. unsigned int mode;
  30. unsigned int addr;
  31. unsigned int size;
  32. } adc_conf;
  33. unsigned int readFileValue(char filename[]){
  34. FILE* fp;
  35. unsigned int value = 0;
  36. fp = fopen(filename, "rt");
  37. fscanf(fp, "%x", &value);
  38. fclose(fp);
  39. return value;
  40. }
  41. unsigned int adc_conf_set_address(adc_conf *conf) {
  42. //conf->addr = ADC_PRU_DATA_SIZE + readFileValue(MMAP1_LOC "addr");
  43. conf->addr = readFileValue(MMAP1_LOC "addr");
  44. }
  45. unsigned int adc_conf_get_address(adc_conf *conf) {
  46. return (conf->addr);
  47. }
  48. unsigned int adc_conf_set_size(adc_conf *conf) {
  49. conf->size = readFileValue(MMAP1_LOC "size");
  50. }
  51. unsigned int adc_conf_get_size(adc_conf *conf) {
  52. return(conf->size);
  53. }
  54. int adc_conf_set_cycles_per_clock (adc_conf *conf, unsigned int cycles) {
  55. conf->cycles_per_clock = cycles;
  56. }
  57. unsigned int adc_conf_get_cycles_per_clock (adc_conf *conf) {
  58. return (conf->cycles_per_clock) ;
  59. }
  60. int adc_conf_set_mode(adc_conf *conf, unsigned int mode){
  61. conf->mode = mode;
  62. }
  63. int adc_conf_set_block_size(adc_conf *conf, unsigned int block_size){
  64. conf->block_size = block_size;
  65. }
  66. unsigned int adc_conf_get_mode (adc_conf *conf) {
  67. return (conf->mode) ;
  68. }
  69. int adc_conf_set_down_sample(adc_conf *conf, unsigned int down_sample){
  70. conf->down_sample = down_sample;
  71. }
  72. unsigned int adc_conf_get_down_sample(adc_conf *conf){
  73. return (conf->down_sample);
  74. }
  75. int adc_conf_set_control_string(adc_conf *conf, unsigned int control_string){
  76. conf->control_string = control_string;
  77. }
  78. unsigned int adc_conf_get_control_string(adc_conf *conf){
  79. return (conf->control_string);
  80. }
  81. int adc_conf_set_data_file_size(adc_conf *conf, unsigned int data_file_size){
  82. conf->num_blocks = data_file_size;
  83. }
  84. unsigned int adc_conf_get_data_file_size(adc_conf *conf){
  85. return (conf->num_blocks);
  86. }
  87. int default_daq_init(adc_conf *conf) {
  88. adc_conf_set_cycles_per_clock(conf, ADS_CLOCKS);
  89. adc_conf_set_mode(conf, STOP);
  90. adc_conf_set_down_sample(conf, ADS_SAMPLE_DIVISOR);
  91. adc_conf_set_control_string(conf, ADS_CONTROL_STRING);
  92. adc_conf_set_block_size(conf, BUFSIZE); // Default block size is 128k
  93. adc_conf_set_data_file_size(conf, DAQ_FILE_SIZE);
  94. adc_conf_set_address(conf);
  95. adc_conf_set_size(conf);
  96. }
  97. int adc_conf_settings(adc_conf *conf) {
  98. return (OK);
  99. }
  100. int main(int argc, char **argv)
  101. {
  102. char in_char = '0';
  103. struct adc_conf *conf;
  104. unsigned int *sample;
  105. void *addr;
  106. int read_fd;
  107. int i = 0;
  108. conf = malloc(sizeof (adc_conf));
  109. sample = malloc(sizeof(unsigned int) * 8);
  110. if(getuid()!=0){
  111. printf("You must run this program as root. Exiting.\n");
  112. exit(EXIT_FAILURE);
  113. }
  114. // Initialize structure used by prussdrv_pruintc_intc
  115. // PRUSS_INTC_INITDATA is found in pruss_intc_mapping.h
  116. tpruss_intc_initdata pruss_intc_initdata = PRUSS_INTC_INITDATA;
  117. // Read in the location and address of PRU shared memory. This value changes
  118. // each time a new block of memory is allocated.
  119. default_daq_init((adc_conf *) conf);
  120. // data for setting up the ADC on PRU0.
  121. adc_pru_data0[0] = adc_conf_get_cycles_per_clock ((adc_conf *)conf);
  122. adc_pru_data0[1] = adc_conf_get_mode ((adc_conf *)conf);
  123. adc_pru_data0[2] = adc_conf_get_control_string ((adc_conf *)conf);
  124. adc_pru_data0[3] = CHANGE; // First time through, set to reflect changed.
  125. adc_pru_data0[4] = adc_conf_get_address((adc_conf *)conf);
  126. adc_pru_data0[5] = adc_conf_get_size((adc_conf *)conf);
  127. adc_pru_data0[6] = adc_conf_get_down_sample ((adc_conf *)conf);
  128. adc_pru_data0[7] = adc_conf_get_data_file_size ((adc_conf *)conf);
  129. // data for setting up the ADC on PRU1.
  130. adc_pru_data1[0] = adc_conf_get_cycles_per_clock ((adc_conf *)conf);
  131. adc_pru_data1[1] = adc_conf_get_mode ((adc_conf *)conf);
  132. adc_pru_data1[2] = adc_conf_get_control_string ((adc_conf *)conf);
  133. adc_pru_data1[3] = CHANGE; // First time through, set to reflect changed.
  134. adc_pru_data1[4] = adc_conf_get_address((adc_conf *)conf);
  135. adc_pru_data1[5] = adc_conf_get_size((adc_conf *)conf);
  136. adc_pru_data1[6] = adc_conf_get_down_sample ((adc_conf *)conf);
  137. adc_pru_data1[7] = adc_conf_get_data_file_size ((adc_conf *)conf);
  138. for (i = 8; i < 32; i ++)
  139. adc_pru_data1[i] = 0;
  140. printf("PRU1 Clock Counter %d \n", adc_pru_data1[0]);
  141. printf("PRU1 ADC State %d \n", adc_pru_data1[1]);
  142. printf("ADC1 Control String %x \n", adc_pru_data1[2]);
  143. printf("ADC1 Control String Modified %d \n", adc_pru_data1[3]);
  144. printf("PRU Shared Memory Address Pool: 0x%x\n", adc_pru_data1[4]);
  145. printf("PRU Shared Memory Pool Size: 0x%x\n", adc_pru_data1[5]);
  146. printf("Sample Divisor: 0x%x\n", adc_pru_data1[6]);
  147. printf("Number of 128k blocks to record 0x%x\n", adc_pru_data1[7]);
  148. // Allocate and initialize memory
  149. prussdrv_init ();
  150. prussdrv_open (PRU_EVTOUT_0);
  151. // Write the address and size into PRU0 Data RAM0.
  152. prussdrv_pru_write_memory(PRUSS0_PRU1_DATARAM, 0, adc_pru_data1, 32);
  153. // Map PRU's interrupts
  154. prussdrv_pruintc_init(&pruss_intc_initdata);
  155. // Load and execute the PRU program on the PRU
  156. prussdrv_exec_program (PRU1_ADC_CONTROL_PRU, "./pruadc1.bin");
  157. printf("ADC Control PRU1 program running on PRU %x \n", PRU1_ADC_CONTROL_PRU);
  158. //prussdrv_exec_program (PRU0_ADC_CONTROL_PRU, "./pruadc0.bin");
  159. //printf("ADC Control PRU0 program running on PRU %x \n", PRU0_ADC_CONTROL_PRU);
  160. if((read_fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1){
  161. printf("Failed to open memory!");
  162. return (ERR);
  163. }
  164. while (in_char != 'q') {
  165. printf("Enter Command : \n");
  166. scanf( " %c", &in_char);
  167. switch (in_char) {
  168. // Write the file to a binary file. It cannot be opened to look at
  169. // and will require postprocessing.
  170. case 'b' :
  171. printf("Binary data collection to file \n");
  172. printf("Data collection started \n");
  173. adc_pru_data0[1] = RUN;
  174. adc_pru_data0[3] = CHANGE;
  175. prussdrv_pru_write_memory(PRUSS0_PRU1_DATARAM, 0, adc_pru_data1, ADC_PRU_DATA_SIZE);
  176. // prussdrv_pru_write_memory(PRUSS0_PRU0_DATARAM, 0, adc_pru_data0, ADC_PRU_DATA_SIZE);
  177. data_to_bin_file (conf);
  178. break;
  179. case 'c' :
  180. read_and_average_cal_data( );
  181. break;
  182. // Write the file in a formatted hex value, with space separation
  183. // between each of the channels in a sample so that the file is
  184. // human readable for quick looks.
  185. case 'f' :
  186. printf("Formatted data collection \n");
  187. printf("Data collection started \n");
  188. adc_pru_data1[1] = RUN;
  189. adc_pru_data1[3] = CHANGE;
  190. prussdrv_pru_write_memory(PRUSS0_PRU1_DATARAM, 0, adc_pru_data1, ADC_PRU_DATA_SIZE);
  191. //prussdrv_pru_write_memory(PRUSS0_PRU0_DATARAM, 0, adc_pru_data0, ADC_PRU_DATA_SIZE);
  192. write_file_formatted ((adc_conf *)conf, 1);
  193. //write_file_formatted ((adc_conf *)conf, 0);
  194. break;
  195. case 'h' :
  196. printf("Data collection halted \n");
  197. adc_pru_data1[1] = STOP;
  198. adc_pru_data1[3] = CHANGE;
  199. prussdrv_pru_write_memory(PRUSS0_PRU1_DATARAM, 0, adc_pru_data1, ADC_PRU_DATA_SIZE);
  200. prussdrv_pru_write_memory(PRUSS0_PRU0_DATARAM, 0, adc_pru_data0, ADC_PRU_DATA_SIZE);
  201. break;
  202. // Get a single sample, print it to the screen
  203. case 'o':
  204. printf("getting a single sample \n");
  205. adc_pru_data1[1] = RUN;
  206. adc_pru_data1[3] = CHANGE;
  207. adc_conf_set_block_size((adc_conf *)conf, 32);
  208. prussdrv_pru_write_memory(PRUSS0_PRU1_DATARAM, 0, adc_pru_data1, ADC_PRU_DATA_SIZE);
  209. sampleGet((adc_conf *)conf, 1, sample);
  210. break;
  211. // Write the file as binary. Indicate to PRU1 that this is a throughput test.
  212. // The data from PRU1 will contain a 24 bit counter which is incremented for
  213. // each sample.`
  214. case 't' :
  215. printf("Throughput Test Mode, Binary data collection to file \n");
  216. printf("Data generation started \n");
  217. adc_pru_data1[1] = TEST;
  218. adc_pru_data1[3] = CHANGE;
  219. addr = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, read_fd, adc_pru_data1[4] & ~MAP_MASK);
  220. bzero((void *) addr, (size_t) (PRU1_START_OFFSET + BUF_HDR_SIZE));
  221. prussdrv_pru_write_memory(PRUSS0_PRU1_DATARAM, 0, adc_pru_data1, ADC_PRU_DATA_SIZE);
  222. data_to_bin_file ((adc_conf *)conf);
  223. break;
  224. case 's' :
  225. printf("Start PRU Data collection, and return \n");
  226. adc_pru_data1[1] = TEST;
  227. adc_pru_data1[3] = CHANGE;
  228. prussdrv_pru_write_memory(PRUSS0_PRU1_DATARAM, 0, adc_pru_data1, ADC_PRU_DATA_SIZE);
  229. break;
  230. case 'p' :
  231. get_buff_stats((adc_conf *)conf);
  232. break;
  233. default :
  234. printf("Command not recognized %c \n", in_char);
  235. }
  236. }
  237. prussdrv_exit ();
  238. }
  239. // Write data to a file so that the hex info can be looked at raw.
  240. // Formatting makes it easy to look at hex values of each channel per sample.
  241. // Also, this operation takes a relatively small set of samples, as opposed to
  242. // a longer duration continuous recording.
  243. int write_file_formatted (adc_conf *conf, int pru_num)
  244. {
  245. int read_fd;
  246. FILE *write_fd;
  247. void *map_base, *virt_addr;
  248. unsigned long read_result;
  249. int result;
  250. unsigned int addr = conf->addr;
  251. unsigned int size = conf->size;
  252. if((read_fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1){
  253. printf("Failed to open memory!");
  254. return (ERR);
  255. }
  256. printf("Memory file open successfully \n");
  257. if (pru_num == 1) {
  258. if((write_fd = fopen("/home/erps/data/outfile1.dat", "w")) == NULL){
  259. printf("Failed to open data file \n");
  260. return (ERR);
  261. addr += PRU1_START_OFFSET;
  262. printf("waiting on interrupt \n");
  263. prussdrv_pru_wait_event (PRU_EVTOUT_0);
  264. }
  265. } else if (pru_num == 0) {
  266. if((write_fd = fopen("/home/erps/data/outfile0.dat", "w")) == NULL){
  267. printf("Failed to open data file \n");
  268. return (ERR);
  269. addr += PRU0_START_OFFSET;
  270. }
  271. } else {
  272. printf("Invalid PRU %d \n", pru_num);
  273. return(ERR);
  274. }
  275. printf("Data file open successfully \n");
  276. map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, read_fd, addr & ~MAP_MASK);
  277. if(map_base == (void *) -1) {
  278. printf("Failed to map base address");
  279. return (ERR);
  280. }
  281. printf("Reading data \n");
  282. int i = 0;
  283. int j = 0;
  284. for(i = 0; i < size; i+= (8 * BYTES_PER_SAMPLE)){
  285. for (j = 0; j < 8; j++) {
  286. virt_addr = map_base + (addr & MAP_MASK);
  287. read_result = *((uint32_t *) virt_addr);
  288. fprintf( write_fd, "0x%08x ", read_result);
  289. addr += BYTES_PER_SAMPLE;
  290. }
  291. fprintf(write_fd, "\n");
  292. }
  293. if ((result = fclose (write_fd)) != 0) {
  294. printf("Error closing data file %d \n", result);
  295. }
  296. return (OK);
  297. }
  298. int sampleGet (adc_conf *conf, int pru_num, uint32_t *sample)
  299. {
  300. int read_fd;
  301. FILE *write_fd;
  302. void *map_base, *virt_addr;
  303. unsigned long read_result;
  304. int result;
  305. unsigned int addr = conf->addr;
  306. if((read_fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1){
  307. printf("Failed to open memory!");
  308. return (ERR);
  309. }
  310. printf("Memory file open successfully \n");
  311. map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, read_fd, conf->addr & ~MAP_MASK);
  312. if(map_base == (void *) -1) {
  313. printf("Failed to map base address");
  314. return (ERR);
  315. }
  316. printf("Reading data \n");
  317. int i = 0;
  318. int j = 0;
  319. for(i = 0; i < conf->size; i+= (8 * BYTES_PER_SAMPLE)){
  320. for (j = 0; j < 8; j++) {
  321. virt_addr = map_base + (addr & MAP_MASK);
  322. //read_result = *((uint32_t *) virt_addr);
  323. *sample = *((uint32_t *) virt_addr);
  324. sample ++;
  325. printf( "0x%08x ", sample);
  326. addr += BYTES_PER_SAMPLE;
  327. }
  328. fprintf(write_fd, "\n");
  329. }
  330. return (OK);
  331. }
  332. int get_buff_stats(adc_conf *conf)
  333. {
  334. int i, buffs, buff_head_ptr;
  335. int read_fd;
  336. void *map_base, *virt_addr;
  337. unsigned long read_result;
  338. unsigned int addr = conf->addr;
  339. unsigned int pru1_wr_flag = addr;
  340. unsigned int pru1_buff_count = addr + 4;
  341. unsigned int pru1_head = addr + 8;
  342. unsigned int pru1_wrap_count = addr +12;
  343. unsigned int pru0_wr_flag = addr + 0x80;
  344. unsigned int pru0_buff_count = addr + 0x84;
  345. unsigned int pru0_head = addr + 0x88;
  346. unsigned int pru0_wrap_count = addr + 0x92;
  347. if((read_fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1){
  348. printf("Failed to open memory!");
  349. return (ERR);
  350. }
  351. printf("Memory file open successfully \n");
  352. map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, read_fd, addr & ~MAP_MASK);
  353. if(map_base == (void *) -1) {
  354. printf("Failed to map base address");
  355. return (ERR);
  356. }
  357. virt_addr = map_base + (pru1_wr_flag & MAP_MASK);
  358. read_result = *((uint32_t *) virt_addr);
  359. printf ("PRU1_WR_FLAG = %x ", read_result);
  360. virt_addr = map_base + (pru1_buff_count & MAP_MASK);
  361. read_result = *((uint32_t *) virt_addr);
  362. printf ("PRU1_BUFF_COUNT = %x ", read_result);
  363. addr += 4;
  364. virt_addr = map_base + (pru1_head & MAP_MASK);
  365. read_result = *((uint32_t *) virt_addr);
  366. printf ("PRU1_HEAD = %x ", read_result);
  367. virt_addr = map_base + (pru1_wrap_count & MAP_MASK);
  368. read_result = *((uint32_t *) virt_addr);
  369. printf ("PRU1_WRAP_COUNT = %x \n", read_result);
  370. virt_addr = map_base + (addr & MAP_MASK);
  371. read_result = *((uint32_t *) virt_addr);
  372. printf ("PRU0_WR_FLAG = %x ", read_result);
  373. virt_addr = map_base + (addr & MAP_MASK);
  374. read_result = *((uint32_t *) virt_addr);
  375. printf ("PRU0_BUFF_COUNT = %x ", read_result);
  376. virt_addr = map_base + (addr & MAP_MASK);
  377. read_result = *((uint32_t *) virt_addr);
  378. printf ("PRU0_HEAD = %x ", read_result);
  379. virt_addr = map_base + (addr & MAP_MASK);
  380. read_result = *((uint32_t *) virt_addr);
  381. printf ("PRU0_WRAP_COUNT = %x \n\n", read_result);
  382. return (OK);
  383. }
  384. int tail_increment(int tail, int num)
  385. {
  386. tail += num;
  387. if (tail == QUEUE_SIZE)
  388. tail = 0;
  389. return (tail);
  390. }
  391. int create_bind_accept_sock()
  392. {
  393. #define PORT 12345
  394. struct sockaddr_in addr;
  395. int addr_len = sizeof(addr);
  396. int new_sock_fd, server_fd;
  397. int backlog = 5;
  398. new_sock_fd = socket(AF_INET, SOCK_STREAM, 0);
  399. if(server_fd == ERR)
  400. {
  401. printf("Error opening socket\n");
  402. return -1;
  403. }
  404. addr.sin_port = htons(PORT);
  405. addr.sin_addr.s_addr = 0;
  406. addr.sin_addr.s_addr = INADDR_ANY;
  407. addr.sin_family = AF_INET;
  408. if(bind(server_fd, (struct sockaddr *)&addr,sizeof(struct sockaddr_in) ) == -1)
  409. {
  410. printf("Error binding socket\n");
  411. return (ERR);
  412. }
  413. printf("Socket Successfully bound to port %u\n", PORT);
  414. listen(server_fd, backlog);
  415. addr_len = sizeof(addr);
  416. new_sock_fd = accept(server_fd, (struct sockaddr *) &addr, (socklen_t*) &addr_len);
  417. if (new_sock_fd < 0)
  418. error("ERROR on accept");
  419. return(new_sock_fd);
  420. }
  421. int send_buffs_to_host (void *virt_addr, int num_buffs_to_write, int sock_fd) {
  422. int32_t bytesToSend = num_buffs_to_write * BUFSIZE;
  423. int32_t send_left = 0;
  424. uint8_t *addr = virt_addr;
  425. struct timeval timeout;
  426. timeout.tv_sec = 5;
  427. timeout.tv_usec = 0;
  428. while (bytesToSend > 0 || (setsockopt(sock_fd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) > 0))
  429. {
  430. if ((send_left = send(sock_fd, (uint8_t*)addr, bytesToSend, 0)) < 0)
  431. {
  432. printf("ERROR: Failed to send buffer contents across TCPIP.\n");
  433. return(ERR);
  434. }
  435. bytesToSend -= send_left;
  436. addr += send_left;
  437. }
  438. }
  439. process_buffers(void *virt_addr, int num_buffs_to_write, int sock_fd, FILE *write_fd, int8_t flash_record, int8_t network_send)
  440. {
  441. uint32_t wr_num;
  442. if (flash_record == ENABLED)
  443. wr_num = write_buffs_to_flash (virt_addr, num_buffs_to_write, write_fd);
  444. if (network_send == ENABLED)
  445. send_buffs_to_host (virt_addr, num_buffs_to_write, sock_fd);
  446. }
  447. int write_buffs_to_flash (void *virt_addr, int num_buffs_to_write, FILE * write_fd) {
  448. int wr_num = 0;
  449. wr_num = fwrite ( virt_addr, sizeof(uint8_t), num_buffs_to_write * BUFSIZE, write_fd);
  450. if (wr_num != num_buffs_to_write * BUFSIZE)
  451. {
  452. printf ("Error writing flash file %d \n", errno);
  453. strerror(errno);
  454. }
  455. return (wr_num);
  456. }
  457. int data_to_bin_file (adc_conf *conf)
  458. {
  459. int i, j, k;
  460. int buffs = 0;
  461. unsigned long read_result;
  462. int read_fd;
  463. FILE *write_fd;
  464. void *map_base, *virt_addr;
  465. void *pru1_wr_flag_addr;
  466. void *pru1_buff_count_addr;
  467. void *pru1_head_addr;
  468. void *pru1_wrap_count_addr;
  469. int wr_num = 0;
  470. int tail = 0;
  471. int num_buffs = 0;
  472. int num_buffs_to_write = 0;
  473. int last_count = 0;
  474. int num_buffs_processed = 0;
  475. volatile unsigned long pru1_wr_flag = 0;
  476. volatile unsigned long pru1_buff_count = 0;
  477. volatile unsigned long pru1_head = 0;
  478. volatile unsigned long pru1_wrap_count = 0;
  479. unsigned int addr = conf->addr;
  480. unsigned int pru1_conf_addr = conf->addr;
  481. int iteration = 0;
  482. int client_sock_fd = 0;
  483. int network_send = TRUE;
  484. int flash_record = TRUE;
  485. if((read_fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1){
  486. printf("Failed to open memory!");
  487. return (ERR);
  488. }
  489. printf("Memory file open successfully \n");
  490. write_fd = fopen("/home/erps/data/outfile.bin", "wb");
  491. if( write_fd == NULL){
  492. printf("Failed to open data file \n");
  493. return (ERR);
  494. }
  495. printf("Data file open successfully \n");
  496. client_sock_fd = create_bind_accept_sock();
  497. if (client_sock_fd != ERR)
  498. printf("Socket Successfully created \n");
  499. map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, read_fd, addr & ~MAP_MASK);
  500. if(map_base == (void *) -1) {
  501. printf("Failed to map base address");
  502. return (ERR);
  503. }
  504. else {
  505. printf("Base Address = %x \n", map_base);
  506. }
  507. #if DEBUG
  508. printf("Dumping header data \n");
  509. i = 0;
  510. j = 0;
  511. for(i = 0; i < (PRU1_START_OFFSET + 256); i+= (8 * BYTES_PER_SAMPLE)){
  512. for (j = 0; j < 8; j++) {
  513. virt_addr = map_base + (addr & MAP_MASK);
  514. read_result = *((uint32_t *) virt_addr);
  515. printf( "0x%08x ", read_result);
  516. addr += BYTES_PER_SAMPLE;
  517. }
  518. printf("\n");
  519. }
  520. #endif
  521. pru1_conf_addr = conf->addr;
  522. addr = conf->addr + PRU1_START_OFFSET;
  523. pru1_wr_flag_addr = map_base + ((pru1_conf_addr + PRU1_WR_FLAG) & MAP_MASK);
  524. pru1_wr_flag = *((uint32_t *) pru1_wr_flag_addr);
  525. pru1_buff_count_addr = map_base + ((pru1_conf_addr + PRU1_BUFF_COUNT) & MAP_MASK);
  526. pru1_buff_count = *((uint32_t *) pru1_buff_count_addr);
  527. pru1_head_addr = map_base + ((pru1_conf_addr + PRU1_HEAD) & MAP_MASK);
  528. pru1_head = *((uint32_t *) pru1_head_addr);
  529. pru1_wrap_count_addr = map_base + ((pru1_conf_addr + PRU1_WRAP_COUNT) & MAP_MASK);
  530. pru1_wrap_count = *((uint32_t *) pru1_wrap_count_addr);
  531. tail = 0;
  532. //tail = 4; // this is for test purposes only
  533. while (num_buffs_processed < DAQ_FILE_SIZE)
  534. {
  535. virt_addr = map_base + ((pru1_conf_addr + PRU1_WR_FLAG) & MAP_MASK);
  536. pru1_wr_flag = *((uint32_t *) pru1_wr_flag_addr);
  537. pru1_buff_count_addr = map_base + ((pru1_conf_addr + PRU1_BUFF_COUNT) & MAP_MASK);
  538. pru1_buff_count = *((uint32_t *) pru1_buff_count_addr);
  539. pru1_head_addr = map_base + ((pru1_conf_addr + PRU1_HEAD) & MAP_MASK);
  540. pru1_head = *((uint32_t *) pru1_head_addr);
  541. pru1_wrap_count_addr = map_base + ((pru1_conf_addr + PRU1_WRAP_COUNT) & MAP_MASK);
  542. pru1_wrap_count = *((uint32_t *) pru1_wrap_count_addr);
  543. if (pru1_wr_flag == 1)
  544. {
  545. num_buffs = pru1_buff_count - last_count;
  546. if (num_buffs > 0)
  547. {
  548. printf("h = %d, t = %d, num_buffs = %d ", pru1_head, tail, num_buffs);
  549. }
  550. if (num_buffs <= QUEUE_SIZE)
  551. {
  552. if (tail < pru1_head)
  553. {
  554. num_buffs_to_write = pru1_head - tail;
  555. virt_addr = map_base + (addr & MAP_MASK);
  556. process_buffers(virt_addr, num_buffs_to_write, client_sock_fd, write_fd, flash_record, network_send);
  557. num_buffs_processed += num_buffs_to_write;
  558. addr += num_buffs_to_write * BUFSIZE;
  559. tail = tail_increment(tail, num_buffs_to_write);
  560. pru1_wr_flag = 0;
  561. }
  562. else if (tail > pru1_head)
  563. {
  564. // Write the buffers from the tail to the end of the queue.
  565. virt_addr = map_base + (addr & MAP_MASK);
  566. num_buffs_to_write = QUEUE_SIZE - tail;
  567. process_buffers(virt_addr, num_buffs_to_write, client_sock_fd, write_fd, flash_record, network_send);
  568. addr += num_buffs_to_write * BUFSIZE;
  569. tail = tail_increment(tail, num_buffs_to_write);
  570. num_buffs_processed += num_buffs_to_write;
  571. // Write the buffers from the beginning of the queue (zeroth element) to the head
  572. virt_addr = map_base + (addr & MAP_MASK);
  573. num_buffs_to_write = pru1_head;
  574. process_buffers(virt_addr, num_buffs_to_write, client_sock_fd, write_fd, flash_record, network_send);
  575. addr += num_buffs_to_write * BUFSIZE;
  576. num_buffs_processed += num_buffs_to_write;
  577. tail = tail_increment(tail, num_buffs_to_write);
  578. }
  579. else if ((tail == pru1_head) && (num_buffs == QUEUE_SIZE))
  580. {
  581. // Write the buffers from the tail to the end of the queue.
  582. virt_addr = map_base + (addr & MAP_MASK);
  583. num_buffs_to_write += QUEUE_SIZE - tail;
  584. process_buffers(virt_addr, num_buffs_to_write, client_sock_fd, write_fd, flash_record, network_send);
  585. num_buffs_processed += num_buffs_to_write;
  586. addr += num_buffs_to_write * BUFSIZE;
  587. tail = tail_increment(tail, num_buffs_to_write);
  588. // Write the buffers from the beginning of the queue to the head
  589. virt_addr = map_base + (addr & MAP_MASK);
  590. num_buffs_to_write = pru1_head;
  591. process_buffers(virt_addr, num_buffs_to_write, client_sock_fd, write_fd, flash_record, network_send);
  592. addr += num_buffs_to_write * BUFSIZE;
  593. num_buffs_processed += num_buffs_to_write;
  594. tail = tail_increment(tail, num_buffs_to_write); // This may look funny, but we are incrementing tail by tail.
  595. }
  596. pru1_wr_flag = 0;
  597. if (num_buffs > 0)
  598. {
  599. printf("num_buffs_processed = %d \n", num_buffs_processed);
  600. }
  601. last_count = pru1_buff_count;
  602. usleep(1000);
  603. }
  604. else if (num_buffs > QUEUE_SIZE)
  605. {
  606. printf("Queue overflow %d \n", num_buffs);
  607. }
  608. }
  609. }
  610. printf("TOTAL num_buffs_processed = %d \n", num_buffs_processed);
  611. fclose ((FILE *)write_fd);
  612. return (OK);
  613. }
  614. read_and_average_cal_data( ) {
  615. FILE *read_fd;
  616. int i = 0;
  617. int j = 0;
  618. uint32_t s[8];
  619. uint32_t total[8];
  620. uint32_t average[8];
  621. read_fd = fopen("/home/erps/caldata/5Vcal_with2p2res.dat", "r");
  622. if( read_fd == NULL){
  623. printf("Failed to open data file \n");
  624. return (ERR);
  625. }
  626. else
  627. printf("Cal File Opened \n");
  628. for (i = 0; i < 8; i ++){
  629. fscanf(read_fd, "%x %x %x %x %x %x %x %x \n", &s[0], &s[1], &s[2], &s[3], &s[4], &s[5], &s[6], &s[7]);
  630. // printf("%x %x %x %x %x %x %x %x \n", s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7]);
  631. }
  632. for (i = 0; i < 8; i ++) {
  633. total[i] = 0;
  634. s[i] = 0;
  635. average[i] = 0;
  636. }
  637. for (i = 0; i < 100; i ++) {
  638. for (j = 0; j < 8; j++) {
  639. fscanf(read_fd, "%x", &s[j]);
  640. s[j] &= s[j] & 0x0FFFFFF;
  641. total[j] += s[j];
  642. }
  643. fscanf(read_fd, "\n");
  644. printf("%x %x %x %x %x %x %x %x \n",s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7]);
  645. printf("%x %x %x %x %x %x %x %x \n",total[0], total[1], total[2], total[3], total[4], total[5], total[6], total[7]);
  646. }
  647. for (i = 0; i < 8; i ++)
  648. {
  649. average[i] = total[i] / 100;
  650. }
  651. printf("%x %x %x %x %x %x %x %x \n",average[0], average[1], average[2], average[3], average[4], average[5], average[6], average[7]);
  652. fclose ((FILE *)read_fd);
  653. }