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.

91 lines
2.6KB

  1. // Copyright 2012 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. // See http://creativecommons.org/licenses/MIT/ for more information.
  24. //
  25. // -----------------------------------------------------------------------------
  26. //
  27. // Driver for ADC scanning.
  28. #ifndef BRAIDS_DRIVERS_ADC_H_
  29. #define BRAIDS_DRIVERS_ADC_H_
  30. #include <stm32f10x_conf.h>
  31. #include "stmlib/stmlib.h"
  32. namespace braids {
  33. const size_t kNumChannels = 4;
  34. class Adc {
  35. public:
  36. Adc() { }
  37. ~Adc() { }
  38. void Init(bool double_speed);
  39. // Inlined and optimized!
  40. inline bool PipelinedScan() {
  41. switch (acquisition_stage_) {
  42. case 0:
  43. rx_word_ |= SPI1->DR;
  44. channels_[active_channel_] = rx_word_;
  45. GPIOB->BSRR = GPIO_Pin_0;
  46. GPIOB->BRR = GPIO_Pin_0;
  47. SPI1->DR = 0x04 | 0x02;
  48. active_channel_ = (active_channel_ + 1) % kNumChannels;
  49. acquisition_stage_ = 1;
  50. break;
  51. case 1:
  52. SPI1->DR;
  53. SPI1->DR = active_channel_ << 6;
  54. acquisition_stage_ = 2;
  55. break;
  56. case 2:
  57. rx_word_ = (SPI1->DR & 0xf) << 8;
  58. SPI1->DR = 0x00; // Dummy trailing data.
  59. acquisition_stage_ = 0;
  60. break;
  61. }
  62. return (active_channel_ == 0 && acquisition_stage_ == 1);
  63. }
  64. bool PipelinedRead(uint8_t channel);
  65. uint16_t Read(uint8_t channel);
  66. uint16_t channel(uint8_t index) const { return channels_[index]; }
  67. private:
  68. uint16_t rx_word_;
  69. size_t active_channel_;
  70. size_t acquisition_stage_;
  71. uint16_t channels_[kNumChannels];
  72. DISALLOW_COPY_AND_ASSIGN(Adc);
  73. };
  74. } // namespace braids
  75. #endif // BRAIDS_DRIVERS_ADC_H_