/*
 * 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 dma_helper
{
public:
    dma_helper(DMA_TypeDef *dma, uint32_t stream);
    dma_helper(dma_helper const &base) = default;

    inline DMA_TypeDef *get_dma() const { return m_dma; }
    inline uint32_t get_stream() const { return m_stream; }
    inline volatile uint32_t *get_is_reg() const { return m_is_reg; }
    inline volatile uint32_t *get_ifc_reg() const { return m_ifc_reg; }
    inline uint32_t get_fe_mask() const { return m_fe_masks[m_stream]; }
    inline uint32_t get_dme_mask() const { return m_dme_masks[m_stream]; }
    inline uint32_t get_te_mask() const { return m_te_masks[m_stream]; }
    inline uint32_t get_ht_mask() const { return m_ht_masks[m_stream]; }
    inline uint32_t get_tc_mask() const { return m_tc_masks[m_stream]; }

    inline bool is_enabled_it_ht() { return LL_DMA_IsEnabledIT_HT(m_dma, m_stream) != 0; }
    inline bool is_enabled_it_te() { return LL_DMA_IsEnabledIT_TE(m_dma, m_stream) != 0; }
    inline bool is_enabled_it_tc() { return LL_DMA_IsEnabledIT_TC(m_dma, m_stream) != 0; }
    inline bool is_enabled_it_dme() { return LL_DMA_IsEnabledIT_DME(m_dma, m_stream) != 0; }
    inline bool is_enabled_it_fe() { return LL_DMA_IsEnabledIT_FE(m_dma, m_stream) != 0; }

    enum class dma_error_type { transfer, direct_mode, fifo };

private:
    DMA_TypeDef *m_dma;
    uint32_t m_stream;
    volatile uint32_t *m_is_reg;
    volatile uint32_t *m_ifc_reg;

    static const uint32_t m_fe_masks[8];
    static const uint32_t m_dme_masks[8];
    static const uint32_t m_te_masks[8];
    static const uint32_t m_ht_masks[8];
    static const uint32_t m_tc_masks[8];
};

} /* namespace f4ll */

#endif /* LL_DMAHELPER_H_ */
