Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

170 Zeilen
4.0KB

  1. #include "imxrt.h"
  2. #include "core_pins.h"
  3. #include "debug/printf.h"
  4. static uint8_t calibrating;
  5. static uint8_t analog_config_bits = 10;
  6. static uint8_t analog_num_average = 4;
  7. const uint8_t pin_to_channel[] = { // pg 482
  8. 7, // 0/A0 AD_B1_02
  9. 8, // 1/A1 AD_B1_03
  10. 12, // 2/A2 AD_B1_07
  11. 11, // 3/A3 AD_B1_06
  12. 6, // 4/A4 AD_B1_01
  13. 5, // 5/A5 AD_B1_00
  14. 15, // 6/A6 AD_B1_10
  15. 0, // 7/A7 AD_B1_11
  16. 13, // 8/A8 AD_B1_08
  17. 14, // 9/A9 AD_B1_09
  18. 128, // 10
  19. 128, // 11
  20. 128, // 12
  21. 128, // 13
  22. 7, // 14/A0 AD_B1_02
  23. 8, // 15/A1 AD_B1_03
  24. 12, // 16/A2 AD_B1_07
  25. 11, // 17/A3 AD_B1_06
  26. 6, // 18/A4 AD_B1_01
  27. 5, // 19/A5 AD_B1_00
  28. 15, // 20/A6 AD_B1_10
  29. 0, // 21/A7 AD_B1_11
  30. 13, // 22/A8 AD_B1_08
  31. 14, // 23/A9 AD_B1_09
  32. 1, // 24/A10 AD_B0_12
  33. 2 // 25/A11 AD_B0_13
  34. // 26/A12 AD_B1_14 - only on ADC2
  35. // 27/A13 AD_B1_15 - only on ADC2
  36. };
  37. static void wait_for_cal(void)
  38. {
  39. printf("wait_for_cal\n");
  40. while (ADC1_GC & ADC_GC_CAL) ;
  41. // TODO: check CALF, but what do to about CAL failure?
  42. calibrating = 0;
  43. printf("cal complete\n");
  44. }
  45. int analogRead(uint8_t pin)
  46. {
  47. if (pin > sizeof(pin_to_channel)) return 0;
  48. if (calibrating) wait_for_cal();
  49. uint8_t ch = pin_to_channel[pin];
  50. if (ch > 15) return 0;
  51. ADC1_HC0 = ch;
  52. while (!(ADC1_HS & ADC_HS_COCO0)) ; // wait
  53. return ADC1_R0;
  54. }
  55. void analogReference(uint8_t type)
  56. {
  57. }
  58. void analogReadRes(unsigned int bits)
  59. {
  60. uint32_t tmp32, mode;
  61. if (bits == 8) {
  62. // 8 bit conversion (17 clocks) plus 8 clocks for input settling
  63. mode = ADC_CFG_MODE(0) | ADC_CFG_ADSTS(3);
  64. } else if (bits == 10) {
  65. // 10 bit conversion (17 clocks) plus 20 clocks for input settling
  66. mode = ADC_CFG_MODE(1) | ADC_CFG_ADSTS(2) | ADC_CFG_ADLSMP;
  67. } else {
  68. // 12 bit conversion (25 clocks) plus 24 clocks for input settling
  69. mode = ADC_CFG_MODE(2) | ADC_CFG_ADSTS(3) | ADC_CFG_ADLSMP;
  70. }
  71. tmp32 = (ADC1_CFG & (0xFFFFFC00));
  72. tmp32 |= (ADC1_CFG & (0x03)); // ADICLK
  73. tmp32 |= (ADC1_CFG & (0xE0)); // ADIV & ADLPC
  74. tmp32 |= mode;
  75. ADC1_CFG = tmp32;
  76. }
  77. void analogReadAveraging(unsigned int num)
  78. {
  79. uint32_t mode;
  80. //disable averaging
  81. ADC1_GC &= ~0x20;
  82. mode = ADC1_CFG & ~0xC000;
  83. if (num >= 32) {
  84. mode |= ADC_CFG_AVGS(3);
  85. } else if (num >= 16) {
  86. mode |= ADC_CFG_AVGS(2);
  87. } else if (num >= 8) {
  88. mode |= ADC_CFG_AVGS(1);
  89. } else if (num >= 4) {
  90. mode |= ADC_CFG_AVGS(0);
  91. } else {
  92. mode |= 0;
  93. }
  94. ADC1_CFG |= mode;
  95. if(num >= 4)
  96. ADC1_GC |= ADC_GC_AVGE;// turns on averaging
  97. }
  98. #define MAX_ADC_CLOCK 20000000
  99. __attribute__((section(".progmem")))
  100. void analog_init(void)
  101. {
  102. uint32_t mode, avg=0;
  103. printf("analogInit\n");
  104. CCM_CCGR1 |= CCM_CCGR1_ADC1(CCM_CCGR_ON);
  105. if (analog_config_bits == 8) {
  106. // 8 bit conversion (17 clocks) plus 8 clocks for input settling
  107. mode = ADC_CFG_MODE(0) | ADC_CFG_ADSTS(3);
  108. } else if (analog_config_bits == 10) {
  109. // 10 bit conversion (17 clocks) plus 20 clocks for input settling
  110. mode = ADC_CFG_MODE(1) | ADC_CFG_ADSTS(2) | ADC_CFG_ADLSMP;
  111. } else {
  112. // 12 bit conversion (25 clocks) plus 24 clocks for input settling
  113. mode = ADC_CFG_MODE(2) | ADC_CFG_ADSTS(3) | ADC_CFG_ADLSMP;
  114. }
  115. if (analog_num_average >= 4) {
  116. if (analog_num_average >= 32) {
  117. mode |= ADC_CFG_AVGS(3);
  118. } else if (analog_num_average >= 16) {
  119. mode |= ADC_CFG_AVGS(2);
  120. } else if (analog_num_average >= 8) {
  121. mode |= ADC_CFG_AVGS(1);
  122. }
  123. avg = ADC_GC_AVGE;
  124. }
  125. #if 1
  126. mode |= ADC_CFG_ADIV(1) | ADC_CFG_ADICLK(3); // async clock
  127. #else
  128. uint32_t clock = F_BUS;
  129. if (clock > MAX_ADC_CLOCK*8) {
  130. mode |= ADC_CFG_ADIV(3) | ADC_CFG_ADICLK(1); // use IPG/16
  131. } else if (clock > MAX_ADC_CLOCK*4) {
  132. mode |= ADC_CFG_ADIV(2) | ADC_CFG_ADICLK(1); // use IPG/8
  133. } else if (clock > MAX_ADC_CLOCK*2) {
  134. mode |= ADC_CFG_ADIV(1) | ADC_CFG_ADICLK(1); // use IPG/4
  135. } else if (clock > MAX_ADC_CLOCK) {
  136. mode |= ADC_CFG_ADIV(0) | ADC_CFG_ADICLK(1); // use IPG/2
  137. } else {
  138. mode |= ADC_CFG_ADIV(0) | ADC_CFG_ADICLK(0); // use IPG
  139. }
  140. #endif
  141. ADC1_CFG = mode | ADC_HC_AIEN | ADC_CFG_ADHSC;
  142. ADC1_GC = avg | ADC_GC_CAL; // begin cal
  143. calibrating = 1;
  144. }