/*
 * ll_dmahelper.h
 *
 *  Created on: Oct 25, 2019
 *      Author: abody
 */

#ifndef LL_DMAHELPER_H_
#define LL_DMAHELPER_H_

#include <inttypes.h>
#include <platform/dma_ll.h>

namespace f4ll {

class DmaHelper {
public:
	DmaHelper(DMA_TypeDef *dma, uint32_t stream);
	DmaHelper(DmaHelper const &base) = default;

	inline DMA_TypeDef* GetDma() const          { return m_dma; }
	inline uint32_t GetStream() const           { return m_stream; }
	inline volatile uint32_t* GetIsReg() const 	{ return m_isReg; }
	inline volatile uint32_t* GetIfcReg() const { return m_ifcReg; }
	inline uint32_t GetFeMask() const           { return m_FEMasks[m_stream]; }
	inline uint32_t GetDmeMask() const          { return m_DMEMasks[m_stream]; }
	inline uint32_t GetTeMask() const           { return m_TEMasks[m_stream]; }
	inline uint32_t GetHtMask() const           { return m_HTMasks[m_stream]; }
	inline uint32_t GetTcMask() const           { return m_TCMasks[m_stream]; }

	inline bool IsEnabledIt_HT()				{ return LL_DMA_IsEnabledIT_HT(m_dma, m_stream) != 0; }
	inline bool IsEnabledIt_TE()				{ return LL_DMA_IsEnabledIT_TE(m_dma, m_stream) != 0; }
	inline bool IsEnabledIt_TC()				{ return LL_DMA_IsEnabledIT_TC(m_dma, m_stream) != 0; }
	inline bool IsEnabledIt_DME()				{ return LL_DMA_IsEnabledIT_DME(m_dma, m_stream) != 0; }
	inline bool IsEnabledIt_FE()                { return LL_DMA_IsEnabledIT_FE(m_dma, m_stream) != 0; }

	enum class DmaErrorType {
		Transfer,
		DirectMode,
		Fifo
	};

private:
	DMA_TypeDef			*m_dma;
	uint32_t 			 m_stream;
	volatile uint32_t	*m_isReg;
	volatile uint32_t	*m_ifcReg;

	static const uint32_t m_FEMasks[8];
	static const uint32_t m_DMEMasks[8];
	static const uint32_t m_TEMasks[8];
	static const uint32_t m_HTMasks[8];
	static const uint32_t m_TCMasks[8];
};

} /* namespace f4ll */

#endif /* LL_DMAHELPER_H_ */
