main.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include "RegMap.h"
  2. #include "vna.h"
  3. #include <cmath>
  4. #include <fcntl.h>
  5. #include <iostream>
  6. #include <sys/mman.h>
  7. #define BITM_0_7 0x000000FF
  8. #define BITM_8_15 0x0000FF00
  9. #define BITM_16_23 0x00FF0000
  10. #define BITM_24_31 0xFF000000
  11. #define BITP_MANTISSA 0
  12. #define BITP_EXPONENT 23
  13. #define BITP_SIGN 31
  14. #define BITM_MANTISSA ((1 << 23) - 1) << BITP_MANTISSA
  15. #define BITM_EXPONENT ((1 << 8) - 1) << BITP_EXPONENT
  16. #define BITM_SIGN (1 << BITP_SIGN)
  17. void *bar0;
  18. void vna::cfgRegWrite() {
  19. // Write to the configuration register
  20. uint32_t *ptr = (uint32_t *) (bar0) + CFGREG_ADDR;
  21. *ptr = ENUM_CFGREG_MEASSTART_START;
  22. }
  23. void vna::cfgRegRead() {
  24. // Read the configuration register
  25. uint32_t *readValue;
  26. uint32_t reg = 0;
  27. while (!(reg & ENUM_CFGREG_MEASSTOP_STOP)) {
  28. readValue = (uint32_t *) (bar0) + CFGREG_ADDR;
  29. reg = lsbToMsb(*readValue);
  30. if (reg & ENUM_CFGREG_MEASSTOP_STOP) {
  31. std::cout << "End measurement bit is set" << std::endl;
  32. } else {
  33. std::cout << "End measurement bit is not set" << std::endl;
  34. }
  35. }
  36. }
  37. void vna::adcDataRead() {
  38. // Read the adc data
  39. for (int i = 0; i < 8; i++) {
  40. uint32_t *data = (uint32_t *) (bar0) + 0x02;
  41. adcData[i] = lsbToMsb(*data);
  42. (void) printf("adcData[%d] = %0x\n", i, adcData[i]);
  43. }
  44. // Convert the read adc data to fp32 1.8.23 form
  45. for (int i = 0; i < 8; i++) {
  46. int sign = (adcData[i] & BITM_SIGN) ? -1 : 1;
  47. int exponent = ((adcData[i] & BITM_EXPONENT) >> 23);
  48. float mantissa = (adcData[i] & BITM_MANTISSA) / (float) pow(2, 23);
  49. float adcFloat = sign * (mantissa + 1) * pow(2, (exponent - 127));
  50. (void) printf("adcDataFloat[%d] = %.12f\n", i, adcFloat);
  51. }
  52. }
  53. uint32_t vna::lsbToMsb(uint32_t reg) {
  54. auto result = static_cast<uint32_t>((reg & BITM_24_31) >> 24);
  55. result |= static_cast<uint32_t>((reg & BITM_16_23) >> 8);
  56. result |= static_cast<uint32_t>((reg & BITM_8_15) << 8);
  57. result |= static_cast<uint32_t>((reg & BITM_0_7) << 24);
  58. return result;
  59. }
  60. void vna::sendSettings() {
  61. /*Send the PGMODE reg*/
  62. uint32_t *data = (uint32_t *) (bar0) + PGMODE0_ADDR;
  63. *data = 0x49209U;
  64. /*Send the MEASCTRL reg*/
  65. data = (uint32_t *) (bar0) + MEAS_CTRL_ADDR;
  66. *data = 0x3f6000U;
  67. /*Send the ADCCTRL reg*/
  68. data = (uint32_t *) (bar0) + ADC_CTRL_ADDR;
  69. *data = 0x2U;
  70. /*Send the CORR_COEF_H reg*/
  71. data = (uint32_t *) (bar0) + CORR_COEF_H_ADDR;
  72. *data = 0x3fU;
  73. /*Send the PGMODE1 reg*/
  74. data = (uint32_t *) (bar0) + PGMODE1_ADDR;
  75. *data = 0x1000U;
  76. /*Send the MUXCTRL2 reg*/
  77. data = (uint32_t *) (bar0) + MUX_CTRL2_ADDR;
  78. *data = 0x1a3U;
  79. /*Send the MUXCTRL3 reg*/
  80. data = (uint32_t *) (bar0) + MUX_CTRL3_ADDR;
  81. *data = 0x13c2eU;
  82. /*Send the MUXCTRL4 reg*/
  83. data = (uint32_t *) (bar0) + MUX_CTRL4_ADDR;
  84. *data = 0x8421U;
  85. /*Send the PG7P1W reg*/
  86. data = (uint32_t *) (bar0) + PG7P1W_ADDR;
  87. *data = 0x1U;
  88. /*Send the PG1P1W reg*/
  89. data = (uint32_t *) (bar0) + PG1P1W_ADDR;
  90. *data = 0x1U;
  91. /*Send the PG2P1W reg*/
  92. data = (uint32_t *) (bar0) + PG2P1W_ADDR;
  93. *data = 0x1U;
  94. /*Send the PG3P1W reg*/
  95. data = (uint32_t *) (bar0) + PG3P1W_ADDR;
  96. *data = 0x1U;
  97. /*Send the PG4P1W reg*/
  98. data = (uint32_t *) (bar0) + PG4P1W_ADDR;
  99. *data = 0x1U;
  100. /*Send the PG5P1W reg*/
  101. data = (uint32_t *) (bar0) + PG5P1W_ADDR;
  102. *data = 0x1U;
  103. /*Send the PG6P1W reg*/
  104. data = (uint32_t *) (bar0) + PG6P1W_ADDR;
  105. *data = 0x1U;
  106. /*Send the MEASNUM reg*/
  107. data = (uint32_t *) (bar0) + MEASNUM_23_0_ADDR;
  108. *data = 0x1U;
  109. }
  110. int main() {
  111. const char *filename = "/dev/MyDmaModule";
  112. int pciFd;// File descriptor
  113. // Open the device file
  114. pciFd = open(filename, O_RDWR | O_SYNC);
  115. if (pciFd < 0) {
  116. std::cout << "Error: Cannot open device file" << std::endl;
  117. return -1;
  118. }
  119. bar0 = mmap(nullptr, 0x1000U, PROT_READ | PROT_WRITE, MAP_SHARED, pciFd, 0);
  120. if (bar0 == MAP_FAILED) {
  121. std::cout << "Error: Cannot map the device file" << std::endl;
  122. (void) close(pciFd);
  123. return -1;
  124. }
  125. // Create an object of the vna class
  126. vna tmsg;
  127. /* Send the settings */
  128. tmsg.sendSettings();
  129. // Call the cfgRegWrite method
  130. tmsg.cfgRegWrite();
  131. // Call the cfgRegRead method
  132. vna::cfgRegRead();
  133. // Call the adcDataRead method
  134. tmsg.adcDataRead();
  135. // Close the device file
  136. (void) close(pciFd);
  137. // Unmap the device file
  138. (void) munmap(bar0, 0x1000U);
  139. return 0;
  140. }