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.

99 lines
3.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 "frames/drivers/adc.h"
  29. #include <stm32f10x_conf.h>
  30. namespace frames {
  31. void Adc::Init(bool single_channel) {
  32. DMA_InitTypeDef dma_init;
  33. ADC_InitTypeDef adc_init;
  34. GPIO_InitTypeDef gpio_init;
  35. gpio_init.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | \
  36. GPIO_Pin_4 | GPIO_Pin_5;
  37. gpio_init.GPIO_Speed = GPIO_Speed_10MHz;
  38. gpio_init.GPIO_Mode = GPIO_Mode_AIN;
  39. GPIO_Init(GPIOA, &gpio_init);
  40. // Use DMA to automatically copy DAC data register to values_ buffer.
  41. DMA_DeInit(DMA1_Channel1);
  42. dma_init.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;
  43. dma_init.DMA_MemoryBaseAddr = (uint32_t)&values_[0];
  44. dma_init.DMA_DIR = DMA_DIR_PeripheralSRC;
  45. dma_init.DMA_BufferSize = kNumAdcChannels;
  46. dma_init.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  47. dma_init.DMA_MemoryInc = DMA_MemoryInc_Enable;
  48. dma_init.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  49. dma_init.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  50. dma_init.DMA_Mode = DMA_Mode_Circular;
  51. dma_init.DMA_Priority = DMA_Priority_High;
  52. dma_init.DMA_M2M = DMA_M2M_Disable;
  53. DMA_Init(DMA1_Channel1, &dma_init);
  54. DMA_Cmd(DMA1_Channel1, ENABLE);
  55. ADC_DeInit(ADC1);
  56. adc_init.ADC_Mode = ADC_Mode_Independent;
  57. adc_init.ADC_ScanConvMode = ENABLE;
  58. adc_init.ADC_ContinuousConvMode = ENABLE;
  59. adc_init.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  60. adc_init.ADC_DataAlign = ADC_DataAlign_Left;
  61. adc_init.ADC_NbrOfChannel = kNumAdcChannels;
  62. ADC_Init(ADC1, &adc_init);
  63. if (single_channel) {
  64. ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 1, ADC_SampleTime_71Cycles5);
  65. } else {
  66. ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_239Cycles5);
  67. ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_239Cycles5);
  68. ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_239Cycles5);
  69. ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 4, ADC_SampleTime_239Cycles5);
  70. ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 5, ADC_SampleTime_239Cycles5);
  71. ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 6, ADC_SampleTime_239Cycles5);
  72. }
  73. ADC_Cmd(ADC1, ENABLE);
  74. ADC_ResetCalibration(ADC1);
  75. while (ADC_GetResetCalibrationStatus(ADC1));
  76. ADC_StartCalibration(ADC1);
  77. while (ADC_GetCalibrationStatus(ADC1));
  78. ADC_DMACmd(ADC1, ENABLE);
  79. ADC_SoftwareStartConvCmd(ADC1, ENABLE);
  80. }
  81. void Adc::DeInit() {
  82. ADC_SoftwareStartConvCmd(ADC1, DISABLE);
  83. ADC_DMACmd(ADC1, DISABLE);
  84. ADC_Cmd(ADC1, DISABLE);
  85. }
  86. } // namespace frames