analogout_device.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2015, STMicroelectronics
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include "mbed_assert.h"
  29. #include "analogout_api.h"
  30. #if DEVICE_ANALOGOUT
  31. #include "cmsis.h"
  32. #include "pinmap.h"
  33. #include "mbed_error.h"
  34. #include "PeripheralPins.h"
  35. // These variables are used for the "free" function
  36. static int channel1_used = 0;
  37. static int channel2_used = 0;
  38. void analogout_init(dac_t *obj, PinName pin)
  39. {
  40. DAC_ChannelConfTypeDef sConfig = {0};
  41. // Get the peripheral name from the pin and assign it to the object
  42. obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC);
  43. MBED_ASSERT(obj->dac != (DACName)NC);
  44. // Get the pin function and assign the used channel to the object
  45. uint32_t function = pinmap_function(pin, PinMap_DAC);
  46. MBED_ASSERT(function != (uint32_t)NC);
  47. switch (STM_PIN_CHANNEL(function)) {
  48. case 1:
  49. obj->channel = DAC_CHANNEL_1;
  50. break;
  51. #if defined(DAC_CHANNEL_2)
  52. case 2:
  53. obj->channel = DAC_CHANNEL_2;
  54. break;
  55. #endif
  56. default:
  57. error("Unknown DAC channel");
  58. break;
  59. }
  60. // Configure GPIO
  61. pinmap_pinout(pin, PinMap_DAC);
  62. // Save the pin for future use
  63. obj->pin = pin;
  64. // Enable DAC clock
  65. __HAL_RCC_DAC1_CLK_ENABLE();
  66. // Configure DAC
  67. obj->handle.Instance = DAC;
  68. obj->handle.State = HAL_DAC_STATE_RESET;
  69. if (HAL_DAC_Init(&obj->handle) != HAL_OK) {
  70. error("HAL_DAC_Init failed");
  71. }
  72. sConfig.DAC_SampleAndHold = DAC_SAMPLEANDHOLD_DISABLE;
  73. sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
  74. sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
  75. sConfig.DAC_ConnectOnChipPeripheral = DAC_CHIPCONNECT_DISABLE;
  76. sConfig.DAC_UserTrimming = DAC_TRIMMING_FACTORY;
  77. if (obj->channel == DAC_CHANNEL_1) {
  78. channel1_used = 1;
  79. } else { // channel 1 per default
  80. channel2_used = 1;
  81. }
  82. if (HAL_DAC_ConfigChannel(&obj->handle, &sConfig, obj->channel) != HAL_OK) {
  83. error("Cannot configure DAC channel\n");
  84. }
  85. analogout_write_u16(obj, 0);
  86. }
  87. void analogout_free(dac_t *obj)
  88. {
  89. // Reset DAC and disable clock
  90. if (obj->channel == DAC_CHANNEL_1) {
  91. channel1_used = 0;
  92. }
  93. #if defined(DAC_CHANNEL_2)
  94. if (obj->channel == DAC_CHANNEL_2) {
  95. channel2_used = 0;
  96. }
  97. #endif
  98. if ((channel1_used == 0) && (channel2_used == 0)) {
  99. __HAL_RCC_DAC1_FORCE_RESET();
  100. __HAL_RCC_DAC1_RELEASE_RESET();
  101. __HAL_RCC_DAC1_CLK_DISABLE();
  102. }
  103. // Configure GPIO
  104. pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
  105. }
  106. #endif // DEVICE_ANALOGOUT