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.

117 lines
4.8KB

  1. // Copyright 2014 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.
  28. #include "clouds/drivers/adc.h"
  29. #include <stm32f4xx_conf.h>
  30. namespace clouds {
  31. void Adc::Init() {
  32. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
  33. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOC, ENABLE);
  34. RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
  35. DMA_InitTypeDef dma_init;
  36. ADC_CommonInitTypeDef adc_common_init;
  37. ADC_InitTypeDef adc_init;
  38. GPIO_InitTypeDef gpio_init;
  39. gpio_init.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
  40. gpio_init.GPIO_Pin |= GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  41. gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL;
  42. gpio_init.GPIO_Mode = GPIO_Mode_AN;
  43. GPIO_Init(GPIOA, &gpio_init);
  44. gpio_init.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
  45. gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL;
  46. gpio_init.GPIO_Mode = GPIO_Mode_AN;
  47. GPIO_Init(GPIOC, &gpio_init);
  48. // Use DMA to automatically copy ADC data register to values_ buffer.
  49. dma_init.DMA_Channel = DMA_Channel_0;
  50. dma_init.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;
  51. dma_init.DMA_Memory0BaseAddr = (uint32_t)&values_[0];
  52. dma_init.DMA_DIR = DMA_DIR_PeripheralToMemory;
  53. dma_init.DMA_BufferSize = ADC_CHANNEL_LAST;
  54. dma_init.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  55. dma_init.DMA_MemoryInc = DMA_MemoryInc_Enable;
  56. dma_init.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  57. dma_init.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  58. dma_init.DMA_Mode = DMA_Mode_Circular;
  59. dma_init.DMA_Priority = DMA_Priority_High;
  60. dma_init.DMA_FIFOMode = DMA_FIFOMode_Disable;
  61. dma_init.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
  62. dma_init.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  63. dma_init.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  64. DMA_Init(DMA2_Stream0, &dma_init);
  65. DMA_Cmd(DMA2_Stream0, ENABLE);
  66. adc_common_init.ADC_Mode = ADC_Mode_Independent;
  67. adc_common_init.ADC_Prescaler = ADC_Prescaler_Div8;
  68. adc_common_init.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
  69. adc_common_init.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_20Cycles;
  70. ADC_CommonInit(&adc_common_init);
  71. adc_init.ADC_Resolution = ADC_Resolution_12b;
  72. adc_init.ADC_ScanConvMode = ENABLE;
  73. adc_init.ADC_ContinuousConvMode = DISABLE;
  74. adc_init.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
  75. adc_init.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
  76. adc_init.ADC_DataAlign = ADC_DataAlign_Left;
  77. adc_init.ADC_NbrOfConversion = ADC_CHANNEL_LAST;
  78. ADC_Init(ADC1, &adc_init);
  79. // 168M / 2 / 8 / (10 x (480 + 20)) = 2.1kHz.
  80. ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 1, ADC_SampleTime_480Cycles);
  81. ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 2, ADC_SampleTime_480Cycles);
  82. ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 3, ADC_SampleTime_480Cycles);
  83. ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 4, ADC_SampleTime_480Cycles);
  84. ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 5, ADC_SampleTime_480Cycles);
  85. ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 6, ADC_SampleTime_480Cycles);
  86. ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 7, ADC_SampleTime_480Cycles);
  87. ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 8, ADC_SampleTime_480Cycles);
  88. ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 9, ADC_SampleTime_480Cycles);
  89. ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 10, ADC_SampleTime_480Cycles);
  90. ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
  91. ADC_Cmd(ADC1, ENABLE);
  92. ADC_DMACmd(ADC1, ENABLE);
  93. Convert();
  94. }
  95. void Adc::Convert() {
  96. ADC_SoftwareStartConv(ADC1);
  97. }
  98. void Adc::DeInit() {
  99. ADC_DeInit();
  100. }
  101. } // namespace clouds