25#include <kernel/scheduler/scheduler.h>
26#include <kernel/sync.h>
193 std::chrono::nanoseconds timeout = std::chrono::nanoseconds::zero());
206 std::chrono::nanoseconds timeout = std::chrono::nanoseconds::zero());
218 miosix::FastMutex mutex;
219 miosix::ConditionVariable cv;
220 std::map<DMADefs::DMAStreamId, DMAStream> streams;
375 *IFCR |= DMA_LIFCR_CHTIF0 << IFindex;
380 *IFCR |= DMA_LIFCR_CTCIF0 << IFindex;
385 *IFCR |= DMA_LIFCR_CTEIF0 << IFindex;
392 *IFCR |= DMA_LIFCR_CDMEIF0 << IFindex;
401 *IFCR |= (DMA_LIFCR_CHTIF0 | DMA_LIFCR_CTCIF0 | DMA_LIFCR_CTEIF0 |
402 DMA_LIFCR_CFEIF0 | DMA_LIFCR_CDMEIF0)
415 miosix::Thread* waitingThread =
nullptr;
419 bool halfTransferFlag =
false;
420 bool transferCompleteFlag =
false;
421 bool transferErrorFlag =
false;
422 bool fifoErrorFlag =
false;
423 bool directModeErrorFlag =
false;
425 std::function<void()> halfTransferCallback;
426 std::function<void()> transferCompleteCallback;
427 std::function<void()> errorCallback;
431 DMA_Stream_TypeDef* registers;
433 volatile uint32_t* ISR;
434 volatile uint32_t* IFCR;
437 inline bool waitForInterruptEventImpl(
438 bool isInterruptEnabled, std::function<
bool()> getEventStatus,
439 std::function<
void()> clearEventStatus,
bool& eventTriggered,
440 long long timeout_ns)
448 if (isInterruptEnabled)
463 waitingThread = miosix::Thread::getCurrentThread();
466 miosix::FastInterruptDisableLock dLock;
471 if (miosix::Thread::IRQenableIrqAndTimedWait(
472 dLock, timeout_ns + miosix::IRQgetTime()) ==
473 miosix::TimedWaitResult::Timeout)
481 waitingThread =
nullptr;
487 }
while (waitingThread);
493 miosix::Thread::IRQenableIrqAndWait(dLock);
494 }
while (waitingThread);
501 eventTriggered =
false;
508 const long long start = miosix::getTime();
513 }
while (!getEventStatus() &&
514 miosix::getTime() - start < timeout_ns);
516 result = getEventStatus();
520 while (!getEventStatus())
542 void invalidateCache();
564 if (pStream !=
nullptr)
580 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.
Driver for the VN100S IMU.
DMAErrors
Generic error codes that the DMA can generate.
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.