25#include <kernel/scheduler/scheduler.h>
26#include <kernel/sync.h>
180 std::chrono::nanoseconds timeout = std::chrono::nanoseconds::zero());
193 std::chrono::nanoseconds timeout = std::chrono::nanoseconds::zero());
205 miosix::FastMutex mutex;
206 miosix::ConditionVariable cv;
207 std::map<DMADefs::DMAStreamId, DMAStream> streams;
362 *IFCR |= DMA_LIFCR_CHTIF0 << IFindex;
367 *IFCR |= DMA_LIFCR_CTCIF0 << IFindex;
372 *IFCR |= DMA_LIFCR_CTEIF0 << IFindex;
379 *IFCR |= DMA_LIFCR_CDMEIF0 << IFindex;
388 *IFCR |= (DMA_LIFCR_CHTIF0 | DMA_LIFCR_CTCIF0 | DMA_LIFCR_CTEIF0 |
389 DMA_LIFCR_CFEIF0 | DMA_LIFCR_CDMEIF0)
402 miosix::Thread* waitingThread =
nullptr;
406 bool halfTransferFlag =
false;
407 bool transferCompleteFlag =
false;
408 bool transferErrorFlag =
false;
409 bool fifoErrorFlag =
false;
410 bool directModeErrorFlag =
false;
412 std::function<void()> halfTransferCallback;
413 std::function<void()> transferCompleteCallback;
414 std::function<void()> errorCallback;
418 DMA_Stream_TypeDef* registers;
420 volatile uint32_t* ISR;
421 volatile uint32_t* IFCR;
424 inline bool waitForInterruptEventImpl(
425 bool isInterruptEnabled, std::function<
bool()> getEventStatus,
426 std::function<
void()> clearEventStatus,
bool& eventTriggered,
427 long long timeout_ns)
435 if (isInterruptEnabled)
450 waitingThread = miosix::Thread::getCurrentThread();
453 miosix::FastInterruptDisableLock dLock;
458 if (miosix::Thread::IRQenableIrqAndTimedWait(
459 dLock, timeout_ns + miosix::getTime()) ==
460 miosix::TimedWaitResult::Timeout)
468 waitingThread =
nullptr;
474 }
while (waitingThread);
480 miosix::Thread::IRQenableIrqAndWait(dLock);
481 }
while (waitingThread);
488 eventTriggered =
false;
495 const long long start = miosix::getTime();
500 }
while (!getEventStatus() &&
501 miosix::getTime() - start < timeout_ns);
503 result = getEventStatus();
507 while (!getEventStatus())
529 void invalidateCache();
551 if (pStream !=
nullptr)
567 inline
bool isValid() {
return pStream !=
nullptr; }
This class is responsible for streams acquisition, streams release and interrupt handling.
DMADriver & operator=(const DMADriver &)=delete
DMAStreamGuard acquireStreamForPeripheral(DMADefs::Peripherals peripheral, std::chrono::nanoseconds timeout=std::chrono::nanoseconds::zero())
Try to acquire a stream that is connected to the specified peripheral.
static DMADriver & instance()
DMAStreamGuard acquireStream(DMADefs::DMAStreamId id, DMADefs::Channel channel, std::chrono::nanoseconds timeout=std::chrono::nanoseconds::zero())
Try to acquire the specified stream and initialize it with the correct channel.
DMADriver(const DMADriver &)=delete
void IRQhandleInterrupt(DMADefs::DMAStreamId id)
void releaseStream(DMADefs::DMAStreamId id)
bool tryStream(DMADefs::DMAStreamId id)
Simple RAII class to handle DMA streams.
DMAStreamGuard(DMAStreamGuard &&) noexcept=default
DMAStreamGuard & operator=(const DMAStreamGuard &)=delete
DMAStreamGuard(const DMAStreamGuard &)=delete
DMAStreamGuard(DMAStream *ptr)
This class represents the actual DMA stream. It can be used to setup, start and stop DMA transactions...
bool getHalfTransferFlagStatus()
Returns the last read status of the half transfer flag.
bool timedWaitForTransferComplete(std::chrono::nanoseconds timeout_ns)
Wait for the transfer complete signal. The caller waits for the corresponding interrupt,...
void clearDirectModeErrorFlag()
void resetTransferCompleteCallback()
void disable()
Stop the DMA transaction (if running). This is equivalent to killing the transaction: DO NOT expect t...
void clearFifoErrorFlag()
void clearTransferErrorFlag()
void clearAllFlags()
Clear all the flags for the selected stream in the DMA ISR register (LISR or HISR depending on the se...
int getCurrentBufferNumber()
Returns the number of the buffer currently in use when in double buffer mode.
bool getTransferCompleteFlagStatus()
Returns the last read status of the transfer complete flag.
void setup(DMATransaction &transaction)
Setup the stream with the given configuration.
void resetErrorCallback()
bool timedWaitForHalfTransfer(std::chrono::nanoseconds timeout_ns)
Wait for the half transfer complete signal. The caller waits for the corresponding interrupt,...
DMAStream & operator=(const DMAStream &)=delete
void clearTransferCompleteFlag()
void setErrorCallback(std::function< void()> callback)
void resetHalfTransferCallback()
DMADefs::Channel getCurrentChannel()
DMAStream(const DMAStream &)=delete
void setTransferCompleteCallback(std::function< void()> callback)
bool setNumberOfDataItems(const uint16_t nBytes)
Set the number of bytes to be exchanged during a dma transaction. Useful in case you don't want to ch...
DMADefs::DMAStreamId getStreamId()
void enable()
Activate the stream. As soon as the stream is enabled, it serves any DMA request from/to the peripher...
bool getDirectModeErrorFlagStatus()
Returns the last read status of the direct mode (default mode) error flag.
void waitForTransferComplete()
Wait for the transfer complete signal. The caller waits for the corresponding interrupt,...
void readFlags()
Reads the current flags status.
void setHalfTransferCallback(std::function< void()> callback)
void waitForHalfTransfer()
Wait for the half transfer complete signal. The caller waits for the corresponding interrupt,...
void clearHalfTransferFlag()
bool getFifoErrorFlagStatus()
Returns the last read status of the fifo error flag.
void setChannel(const DMADefs::Channel channel)
Select the channel to be used by the stream during the transactions.
DMAStream(DMAStream &&) noexcept=default
bool getTransferErrorFlagStatus()
Returns the last read status of the transfer error flag.
Peripherals
All the peripherals connected to dma.
Channel
Channels selectable for each dma stream.
This file includes all the types the logdecoder script will decode.
This is the configuration struct for a DMA transaction.
bool enableFifoErrorInterrupt
bool enableHalfTransferInterrupt
uint16_t numberOfDataItems
bool enableTransferErrorInterrupt
volatile void * secondMemoryAddress
volatile void * dstAddress
volatile void * srcAddress
bool doubleBufferMode
Enables double buffer mode.
bool enableDirectModeErrorInterrupt
bool enableTransferCompleteInterrupt
bool circularMode
Enables circular buffer mode.