|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Zühlke Engineering (Austria) GmbH |
| 3 | + * |
| 4 | + * This file is part of the modm project. |
| 5 | + * |
| 6 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 7 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 9 | + */ |
| 10 | + |
| 11 | +#ifndef EXAMPLE_ADCDMA_HPP |
| 12 | +#define EXAMPLE_ADCDMA_HPP |
| 13 | + |
| 14 | +#include <modm/platform.hpp> |
| 15 | +#include <span> |
| 16 | + |
| 17 | +template<class Adc, class DmaChannel> |
| 18 | +class AdcDma |
| 19 | +{ |
| 20 | + struct Dma |
| 21 | + { |
| 22 | + using AdcChannel = |
| 23 | + typename DmaChannel::template RequestMapping<modm::platform::Peripheral::Adc1>::Channel; |
| 24 | + static constexpr modm::platform::DmaBase::Request AdcRequest = |
| 25 | + DmaChannel::template RequestMapping<modm::platform::Peripheral::Adc1>::Request; |
| 26 | + }; |
| 27 | + |
| 28 | +public: |
| 29 | + /** |
| 30 | + * \brief initialize both adc and dma |
| 31 | + * @tparam SystemClock |
| 32 | + */ |
| 33 | + template<class SystemClock> |
| 34 | + static void |
| 35 | + initialize(std::span<uint16_t> buffer, |
| 36 | + modm::platform::DmaBase::Priority priority = modm::platform::DmaBase::Priority::Low, |
| 37 | + modm::platform::DmaBase::CircularMode circularMode = |
| 38 | + modm::platform::DmaBase::CircularMode::Enabled, |
| 39 | + modm::platform::DmaBase::IrqHandler transferErrorCallback = nullptr, |
| 40 | + modm::platform::DmaBase::IrqHandler halfCompletedCallback = nullptr, |
| 41 | + modm::platform::DmaBase::IrqHandler completedCallback = nullptr) |
| 42 | + { |
| 43 | + Dma::AdcChannel::configure( |
| 44 | + modm::platform::DmaBase::DataTransferDirection::PeripheralToMemory, |
| 45 | + modm::platform::DmaBase::MemoryDataSize::HalfWord, |
| 46 | + modm::platform::DmaBase::PeripheralDataSize::HalfWord, |
| 47 | + modm::platform::DmaBase::MemoryIncrementMode::Increment, |
| 48 | + modm::platform::DmaBase::PeripheralIncrementMode::Fixed, priority, circularMode); |
| 49 | + Dma::AdcChannel::setPeripheralAddress(Adc::getDataRegisterAddress()); |
| 50 | + Dma::AdcChannel::setDataLength(buffer.size()); |
| 51 | + Dma::AdcChannel::setMemoryAddress(reinterpret_cast<uintptr_t>(buffer.data())); |
| 52 | + |
| 53 | + setTransferErrorCallback(transferErrorCallback); |
| 54 | + setHalfCompletedConversionCallback(halfCompletedCallback); |
| 55 | + setCompletedConversionCallback(completedCallback); |
| 56 | + |
| 57 | + Dma::AdcChannel::template setPeripheralRequest<Dma::AdcRequest>(); |
| 58 | + Adc::setDmaMode(Adc::DmaMode::Disabled); |
| 59 | + } |
| 60 | + |
| 61 | + static void |
| 62 | + startDma() |
| 63 | + { |
| 64 | + Adc::setDmaMode(Adc::DmaMode::Circular); |
| 65 | + DmaChannel::start(); |
| 66 | + } |
| 67 | + |
| 68 | + static void |
| 69 | + setTransferErrorCallback(modm::platform::DmaBase::IrqHandler transferErrorCallback) |
| 70 | + { |
| 71 | + if (transferErrorCallback == nullptr) { return; } |
| 72 | + Dma::AdcChannel::enableInterruptVector(); |
| 73 | + Dma::AdcChannel::enableInterrupt(modm::platform::DmaBase::InterruptEnable::TransferError); |
| 74 | + Dma::AdcChannel::setTransferErrorIrqHandler(transferErrorCallback); |
| 75 | + } |
| 76 | + |
| 77 | + static void |
| 78 | + setHalfCompletedConversionCallback(modm::platform::DmaBase::IrqHandler halfCompletedCallback) |
| 79 | + { |
| 80 | + if (halfCompletedCallback == nullptr) { return; } |
| 81 | + Dma::AdcChannel::enableInterruptVector(); |
| 82 | + Dma::AdcChannel::enableInterrupt(modm::platform::DmaBase::InterruptEnable::HalfTransfer); |
| 83 | + Dma::AdcChannel::setHalfTransferCompleteIrqHandler(halfCompletedCallback); |
| 84 | + } |
| 85 | + |
| 86 | + static void |
| 87 | + setCompletedConversionCallback(modm::platform::DmaBase::IrqHandler completedCallback) |
| 88 | + { |
| 89 | + if (completedCallback == nullptr) { return; } |
| 90 | + Dma::AdcChannel::enableInterruptVector(); |
| 91 | + Dma::AdcChannel::enableInterrupt( |
| 92 | + modm::platform::DmaBase::InterruptEnable::TransferComplete); |
| 93 | + Dma::AdcChannel::setTransferCompleteIrqHandler(completedCallback); |
| 94 | + } |
| 95 | +}; |
| 96 | + |
| 97 | +#endif // EXAMPLE_ADCDMA_HPP |
0 commit comments