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.

180 lines
5.8KB

  1. // Copyright 2013 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 DAC.
  28. #include "streams/drivers/adc.h"
  29. #include <stm32f10x_conf.h>
  30. #include <string.h>
  31. namespace streams {
  32. /* static */
  33. uint8_t Adc::pots_sequence_[kNumPots] = {
  34. ADC_Channel_2,
  35. ADC_Channel_3,
  36. ADC_Channel_1,
  37. ADC_Channel_0
  38. };
  39. /* static */
  40. Adc* Adc::instance_;
  41. void Adc::Init(bool single_channel, CvProcessingCallback callback) {
  42. DMA_InitTypeDef dma_init;
  43. ADC_InitTypeDef adc_init;
  44. GPIO_InitTypeDef gpio_init;
  45. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  46. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  47. RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
  48. RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE);
  49. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
  50. RCC_ADCCLKConfig(RCC_PCLK2_Div6); // 12 MHz DAC clock
  51. gpio_init.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  52. gpio_init.GPIO_Speed = GPIO_Speed_10MHz;
  53. gpio_init.GPIO_Mode = GPIO_Mode_AIN;
  54. GPIO_Init(GPIOA, &gpio_init);
  55. gpio_init.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
  56. gpio_init.GPIO_Speed = GPIO_Speed_10MHz;
  57. gpio_init.GPIO_Mode = GPIO_Mode_AIN;
  58. GPIO_Init(GPIOB, &gpio_init);
  59. // ADC 1 will be used in DMA mode to scan the CV inputs.
  60. // Use DMA to automatically copy ADC data register to values_ buffer.
  61. DMA_DeInit(DMA1_Channel1);
  62. dma_init.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;
  63. dma_init.DMA_MemoryBaseAddr = (uint32_t)&cvs_[0];
  64. dma_init.DMA_DIR = DMA_DIR_PeripheralSRC;
  65. dma_init.DMA_BufferSize = single_channel ? 1 : kNumCVs;
  66. dma_init.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  67. dma_init.DMA_MemoryInc = DMA_MemoryInc_Enable;
  68. dma_init.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  69. dma_init.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  70. dma_init.DMA_Mode = DMA_Mode_Circular;
  71. dma_init.DMA_Priority = DMA_Priority_High;
  72. dma_init.DMA_M2M = DMA_M2M_Disable;
  73. DMA_Init(DMA1_Channel1, &dma_init);
  74. DMA_Cmd(DMA1_Channel1, ENABLE);
  75. ADC_DeInit(ADC1);
  76. adc_init.ADC_Mode = ADC_Mode_Independent;
  77. adc_init.ADC_ScanConvMode = ENABLE;
  78. adc_init.ADC_ContinuousConvMode = ENABLE;
  79. adc_init.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  80. adc_init.ADC_DataAlign = ADC_DataAlign_Left;
  81. adc_init.ADC_NbrOfChannel = single_channel ? 1 : kNumCVs;
  82. ADC_Init(ADC1, &adc_init);
  83. if (single_channel) {
  84. ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_71Cycles5);
  85. } else {
  86. // Sample rate: 31.09 kHz
  87. // 72000 / 6 / (12.5 * 6 + 71.5 * 2 + 55.5 * 2 + 28.5 * 2)
  88. ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_71Cycles5);
  89. ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 2, ADC_SampleTime_55Cycles5);
  90. ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 3, ADC_SampleTime_28Cycles5);
  91. ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 4, ADC_SampleTime_71Cycles5);
  92. ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 5, ADC_SampleTime_55Cycles5);
  93. ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 6, ADC_SampleTime_28Cycles5);
  94. }
  95. if (callback) {
  96. DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE);
  97. }
  98. ADC_DeInit(ADC2);
  99. adc_init.ADC_Mode = ADC_Mode_Independent;
  100. adc_init.ADC_ScanConvMode = DISABLE;
  101. adc_init.ADC_ContinuousConvMode = DISABLE;
  102. adc_init.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  103. adc_init.ADC_DataAlign = ADC_DataAlign_Left;
  104. adc_init.ADC_NbrOfChannel = 1;
  105. ADC_Init(ADC2, &adc_init);
  106. ADC_Cmd(ADC1, ENABLE);
  107. ADC_ResetCalibration(ADC1);
  108. while (ADC_GetResetCalibrationStatus(ADC1));
  109. ADC_StartCalibration(ADC1);
  110. while (ADC_GetCalibrationStatus(ADC1));
  111. ADC_Cmd(ADC2, ENABLE);
  112. ADC_ResetCalibration(ADC2);
  113. while (ADC_GetResetCalibrationStatus(ADC2));
  114. ADC_StartCalibration(ADC2);
  115. while (ADC_GetCalibrationStatus(ADC2));
  116. pot_index_ = kNumPots - 1;
  117. last_read_pot_ = 0;
  118. instance_ = this;
  119. callback_ = callback;
  120. ScanPots();
  121. }
  122. void Adc::Start() {
  123. ADC_DMACmd(ADC1, ENABLE);
  124. ADC_SoftwareStartConvCmd(ADC1, ENABLE);
  125. }
  126. void Adc::DeInit() {
  127. ADC_SoftwareStartConvCmd(ADC1, DISABLE);
  128. ADC_DMACmd(ADC1, DISABLE);
  129. ADC_Cmd(ADC1, DISABLE);
  130. }
  131. void Adc::ScanPots() {
  132. pots_[pot_index_] = ADC2->DR;
  133. last_read_pot_ = pot_index_;
  134. ++pot_index_;
  135. if (pot_index_ == kNumPots) {
  136. pot_index_ = 0;
  137. }
  138. ADC_RegularChannelConfig(
  139. ADC2,
  140. pots_sequence_[pot_index_],
  141. 1,
  142. ADC_SampleTime_239Cycles5);
  143. ADC_SoftwareStartConvCmd(ADC2, ENABLE);
  144. }
  145. extern "C" {
  146. void DMA1_Channel1_IRQHandler(void) {
  147. if (DMA_GetITStatus(DMA1_IT_TC1) == SET) {
  148. DMA_ClearITPendingBit(DMA1_IT_GL1);
  149. Adc::GetInstance()->Callback();
  150. }
  151. }
  152. }
  153. } // namespace streams