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.

106 lines
4.1KB

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