Siegurd

Untitled

Aug 18th, 2023
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <alsa/asoundlib.h>
  4. #include <iostream>
  5.  
  6. #define GET_BIT(X,N) ( ( (X) >> (N) ) & 1 )
  7.  
  8. void ad7771_data_to_voltage(double ref, uint32_t *raw_code, double *voltage) {
  9. if((*raw_code <= 0x7FFFFF) && (*raw_code >= 0x00)) {
  10. *voltage = (double)(*raw_code * (ref/16777216.0));
  11. }
  12. else {
  13. *voltage = (double)((*raw_code * (ref/16777216.0))-ref);
  14. }
  15. }
  16.  
  17. timespec diff(timespec start, timespec end){
  18. timespec temp;
  19. if ((end.tv_nsec-start.tv_nsec)<0) {
  20. temp.tv_sec = end.tv_sec-start.tv_sec-1;
  21. temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
  22. } else {
  23. temp.tv_sec = end.tv_sec-start.tv_sec;
  24. temp.tv_nsec = end.tv_nsec-start.tv_nsec;
  25. }
  26. return temp;
  27. }
  28.  
  29. int main (int argc, char *argv[]) {
  30. double ref = 2.5;
  31. uint32_t raw_code = 0;
  32. double voltage = -1;
  33.  
  34. int i;
  35. int err;
  36.  
  37. snd_pcm_t *capture_handle;
  38. snd_pcm_hw_params_t *hw_params;
  39. //const int datarate=128000;
  40. if ((err = snd_pcm_open (&capture_handle, "hw", SND_PCM_STREAM_CAPTURE, 0)) < 0) {
  41. fprintf (stderr, "cannot open audio device %s (%s)\n", argv[1], snd_strerror (err));
  42. exit (1);
  43. }
  44.  
  45. if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
  46. fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n", snd_strerror (err));
  47. exit (1);
  48. }
  49. fprintf (stderr,"1 \n");
  50. if ((err = snd_pcm_hw_params_any (capture_handle, hw_params)) < 0) {
  51. fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n", snd_strerror (err));
  52. exit (1);
  53. }
  54. fprintf (stderr,"2 \n");
  55. if ((err = snd_pcm_hw_params_set_access (capture_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
  56. fprintf (stderr, "cannot set access type (%s)\n", snd_strerror (err));
  57. exit (1);
  58. }
  59. fprintf (stderr,"3 \n");
  60. if ((err = snd_pcm_hw_params_set_format (capture_handle, hw_params, SND_PCM_FORMAT_S32)) < 0) {
  61. fprintf (stderr, "cannot set sample format (%s)\n", snd_strerror (err));
  62. exit (1);
  63. }
  64. fprintf (stderr,"4 \n");
  65. unsigned int rate = 192000;
  66. if ((err = snd_pcm_hw_params_set_rate_near (capture_handle, hw_params, &rate, 0)) < 0) {
  67. fprintf (stderr, "cannot set sample rate (%s)\n", snd_strerror (err));
  68. exit (1);
  69. }
  70. fprintf (stderr,"5 \n"); fprintf (stderr,"Rate: %d\n",rate);
  71. if ((err = snd_pcm_hw_params_set_channels (capture_handle, hw_params, 8)) < 0) {
  72. fprintf (stderr, "cannot set channel count (%s)\n", snd_strerror (err));
  73. exit (1);
  74. }
  75. fprintf (stderr,"6 \n");
  76. if ((err = snd_pcm_hw_params (capture_handle, hw_params)) < 0) {
  77. fprintf (stderr, "cannot set parameters (%s)\n", snd_strerror (err));
  78. exit (1);
  79. }
  80. fprintf (stderr,"7 \n");
  81. snd_pcm_hw_params_free (hw_params);
  82. fprintf (stderr,"8 \n");
  83. if ((err = snd_pcm_prepare (capture_handle)) < 0) {
  84. fprintf (stderr, "cannot prepare audio interface for use (%s)\n", snd_strerror (err));
  85. exit (1);
  86. }
  87. fprintf (stderr,"9 \n");
  88.  
  89. timespec time1_, time2_;
  90.  
  91. uint32_t ad7771_butch_size=32*32;
  92. uint8_t buf[32*1024];
  93. uint32_t counbter=0;
  94.  
  95. for (int i1 = 0; i1 < 10; ++i1) {
  96.  
  97. clock_gettime(CLOCK_REALTIME, &time1_);
  98. std::cout<< "loop ns: " << diff(time2_,time1_).tv_nsec << std::endl;
  99. clock_gettime(CLOCK_REALTIME, &time2_);
  100.  
  101. if ((err = snd_pcm_readi (capture_handle, &buf, ad7771_butch_size)) != ad7771_butch_size) {
  102. fprintf (stderr, "read from audio interface failed (%s)\n", snd_strerror (err));
  103. exit (1);
  104. }
  105. //counbter+=ad7771_butch_size;
  106. }
  107.  
  108. fprintf (stderr,"10 \n");
  109. snd_pcm_close (capture_handle);
  110. exit (0);
  111. }
  112.  
  113. root@BeagleBone:/home/debian# g++ -oalsatut1 alsatut1.cpp -lasound && ./alsatut1
  114. 1
  115. 2
  116. 3
  117. 4
  118. 5
  119. Rate: 192000
  120. 6
  121. 7
  122. 8
  123. 9
  124. loop ns: 2113288995
  125. loop ns: 9338154
  126. loop ns: 6528263
  127. loop ns: 7033311
  128. loop ns: 6846199
  129. loop ns: 7037977
  130. loop ns: 6838533
  131. loop ns: 7022313
  132. loop ns: 6833200
  133. loop ns: 6833283
  134. 10
  135. root@BeagleBone:/home/debian#
  136.  
Advertisement
Add Comment
Please, Sign In to add comment