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.

83 lines
2.3KB

  1. // Copyright 2012 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (olivier@mutable-instruments.net)
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. //
  16. // -----------------------------------------------------------------------------
  17. //
  18. // Staged SPI communication with the ADC. Prevents the 128 cycles lock.
  19. #ifndef EDGES_ADC_ACQUISITION_H_
  20. #define EDGES_ADC_ACQUISITION_H_
  21. #include "avrlibx/avrlibx.h"
  22. #include "edges/hardware_config.h"
  23. namespace edges {
  24. class ADCAcquisition {
  25. public:
  26. ADCAcquisition() { }
  27. ~ADCAcquisition() { }
  28. static void Init() {
  29. adc_.Init();
  30. acquisition_stage_ = 0;
  31. active_channel_ = 0;
  32. }
  33. static inline uint8_t Cycle() {
  34. int8_t result = -1;
  35. switch (acquisition_stage_) {
  36. case 0:
  37. rx_.bytes[0] = adc_.ImmediateRead();
  38. channels_[active_channel_] = rx_.value;
  39. adc_.End();
  40. adc_.Begin();
  41. result = active_channel_;
  42. active_channel_ = (active_channel_ + 1) & (kNumAdcChannels - 1);
  43. adc_.Overwrite(0x04 | 0x02 | (active_channel_ >> 2));
  44. acquisition_stage_ = 1;
  45. break;
  46. case 1:
  47. adc_.Overwrite(active_channel_ << 6);
  48. acquisition_stage_ = 2;
  49. break;
  50. case 2:
  51. rx_.bytes[1] = adc_.ImmediateRead() & 0xf;
  52. adc_.Overwrite(0x00); // Dummy write to get next byte.
  53. acquisition_stage_ = 0;
  54. break;
  55. }
  56. return result;
  57. }
  58. static uint16_t channel(uint8_t index) {
  59. return channels_[index];
  60. }
  61. private:
  62. static ADCInterface adc_;
  63. static Word rx_;
  64. static uint8_t active_channel_;
  65. static uint8_t acquisition_stage_;
  66. static uint16_t channels_[kNumAdcChannels];
  67. };
  68. extern ADCAcquisition adc_acquisition;
  69. } // namespace edges
  70. #endif // EDGES_ADC_ACQUISITION_H_