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.
 
 
 
 

168 lines
2.9 KiB

  1. #include "config.h"
  2. #include "adc.h"
  3. #include "dac.h"
  4. #include "uart.h"
  5. #include "timer.h"
  6. #include <stdio.h>
  7. #include <math.h>
  8. #include "calibrate.h"
  9. #include "util.h"
  10. #include "led.h"
  11. #include "mode.h"
  12. #include "zoom.h"
  13. static uint16_t dac = DAC_MID;
  14. void sweep(void)
  15. {
  16. int32_t d;
  17. int16_t a;
  18. #define SWEEP ((DAC_HIGH - DAC_LOW) * (int32_t)2000 / 65535)
  19. /* sweep range */
  20. for (d = (int32_t)dac - SWEEP; d < (int32_t)dac + SWEEP; d++) {
  21. if (d < DAC_LOW) {
  22. uart1_put_dec(DAC_LOW);
  23. uart1_put_string(" 0\r\n");
  24. continue;
  25. }
  26. if (d > DAC_HIGH) {
  27. uart1_put_dec(DAC_HIGH);
  28. uart1_put_string(" 0\r\n");
  29. continue;
  30. }
  31. dac_write(d);
  32. msleep(1);
  33. a = adc_get();
  34. uart1_put_dec(d);
  35. uart1_put(' ');
  36. uart1_put_dec(a);
  37. uart1_crlf();
  38. }
  39. }
  40. void run_debug(void)
  41. {
  42. int16_t adc;
  43. int32_t v;
  44. char buf[4];
  45. uart1_init(115200);
  46. led_pattern(0b10101010);
  47. uart1_put_string("Zoom NILM Debug\r\n");
  48. while (1) {
  49. dac = dac & DAC_HIGH; // mask off invalid bits
  50. dac_write(dac);
  51. uart1_put_hex16(dac);
  52. uart1_put(' ');
  53. uart1_put_dec(dac_get_actual(dac));
  54. uart1_put(' ');
  55. uart1_put(' ');
  56. adc = adc_get();
  57. uart1_put_hex16(adc);
  58. uart1_put(' ');
  59. uart1_put_dec(adc);
  60. uart1_crlf();
  61. switch (uart1_get()) {
  62. // small step
  63. case '[':
  64. dac--;
  65. break;
  66. case ']':
  67. dac++;
  68. break;
  69. // medium step
  70. case '-':
  71. dac -= 16;
  72. break;
  73. case '+':
  74. case '=':
  75. dac += 16;
  76. break;
  77. // big step
  78. case ',':
  79. case '<':
  80. dac -= 1024;
  81. break;
  82. case '.':
  83. case '>':
  84. dac += 1024;
  85. break;
  86. // set DAC to midpoint
  87. case '0':
  88. dac = DAC_MID;
  89. break;
  90. // set DAC to specified hex value
  91. case 'v':
  92. case 'V':
  93. buf[0] = uart1_get();
  94. buf[1] = uart1_get();
  95. buf[2] = uart1_get();
  96. buf[3] = uart1_get();
  97. v = hex_to_u16(buf);
  98. if (v < 0)
  99. uart1_put_string("bad value\r\n");
  100. else
  101. dac = v;
  102. break;
  103. // maintain ADC input at zero
  104. case 'z':
  105. case 'Z':
  106. uart1_put_string("zeroing input...\r\n");
  107. while (!uart1_can_get())
  108. dac = seek(dac, 1024);
  109. uart1_get();
  110. break;
  111. // test seeking
  112. case '1':
  113. uart1_put_string("seek 512\r\n");
  114. dac = seek(dac, 512);
  115. break;
  116. case '2':
  117. uart1_put_string("seek 1536\r\n");
  118. dac = seek(dac, 1536);
  119. break;
  120. // run calibration
  121. case 'c':
  122. case 'C':
  123. uart1_put_string("calibrating...\r\n");
  124. dac = do_calibrate();
  125. uart1_put_string("new g_scale ");
  126. uart1_put_float(g_scale);
  127. uart1_crlf();
  128. break;
  129. // sweep DAC
  130. case 's':
  131. case 'S':
  132. uart1_put_string("sweep around ");
  133. uart1_put_dec(dac);
  134. uart1_crlf();
  135. sweep();
  136. break;
  137. // dump raw ADC value
  138. case 'r':
  139. case 'R':
  140. while(!uart1_can_get()) {
  141. uart1_put_hex16(adc_get_raw());
  142. uart1_crlf();
  143. }
  144. uart1_get();
  145. break;
  146. }
  147. }
  148. }