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.

114 lines
4.6KB

  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 "warps/drivers/adc.h"
  29. #include <stm32f4xx_conf.h>
  30. namespace warps {
  31. void Adc::Init() {
  32. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
  33. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, 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. // Use DMA to automatically copy ADC data register to values_ buffer.
  45. dma_init.DMA_Channel = DMA_Channel_0;
  46. dma_init.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;
  47. dma_init.DMA_Memory0BaseAddr = (uint32_t)&values_[0];
  48. dma_init.DMA_DIR = DMA_DIR_PeripheralToMemory;
  49. dma_init.DMA_BufferSize = ADC_LAST;
  50. dma_init.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  51. dma_init.DMA_MemoryInc = DMA_MemoryInc_Enable;
  52. dma_init.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  53. dma_init.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  54. dma_init.DMA_Mode = DMA_Mode_Circular;
  55. dma_init.DMA_Priority = DMA_Priority_High;
  56. dma_init.DMA_FIFOMode = DMA_FIFOMode_Disable;
  57. dma_init.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
  58. dma_init.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  59. dma_init.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  60. DMA_Init(DMA2_Stream0, &dma_init);
  61. DMA_Cmd(DMA2_Stream0, ENABLE);
  62. adc_common_init.ADC_Mode = ADC_Mode_Independent;
  63. adc_common_init.ADC_Prescaler = ADC_Prescaler_Div8;
  64. adc_common_init.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
  65. adc_common_init.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_20Cycles;
  66. ADC_CommonInit(&adc_common_init);
  67. adc_init.ADC_Resolution = ADC_Resolution_12b;
  68. adc_init.ADC_ScanConvMode = ENABLE;
  69. adc_init.ADC_ContinuousConvMode = DISABLE;
  70. adc_init.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
  71. adc_init.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
  72. adc_init.ADC_DataAlign = ADC_DataAlign_Left;
  73. adc_init.ADC_NbrOfConversion = ADC_LAST;
  74. ADC_Init(ADC1, &adc_init);
  75. // 168M / 2 / 8 / (8 x (480 + 20)) = 2.6kHz.
  76. ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_480Cycles);
  77. ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 2, ADC_SampleTime_480Cycles);
  78. ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 3, ADC_SampleTime_480Cycles);
  79. ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 4, ADC_SampleTime_480Cycles);
  80. ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 5, ADC_SampleTime_480Cycles);
  81. ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 6, ADC_SampleTime_480Cycles);
  82. ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 7, ADC_SampleTime_480Cycles);
  83. ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 8, ADC_SampleTime_480Cycles);
  84. ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
  85. ADC_Cmd(ADC1, ENABLE);
  86. ADC_DMACmd(ADC1, ENABLE);
  87. Convert();
  88. }
  89. void Adc::DeInit() {
  90. DMA_Cmd(DMA2_Stream0, DISABLE);
  91. ADC_DMARequestAfterLastTransferCmd(ADC1, DISABLE);
  92. ADC_Cmd(ADC1, DISABLE);
  93. ADC_DMACmd(ADC1, DISABLE);
  94. ADC_DeInit();
  95. }
  96. void Adc::Convert() {
  97. ADC_SoftwareStartConv(ADC1);
  98. }
  99. } // namespace warps