/*
 * llmemcpydma.cpp
 *
 *  Created on: Nov 4, 2019
 *      Author: abody
 */

#include <f4ll/memcpy_dma.h>

namespace f4ll {

memcpy_dma::memcpy_dma(DMA_TypeDef *dma, uint32_t stream)
    : dma_helper(dma, stream)
{
    LL_DMA_EnableIT_TC(dma, stream);
}

void *memcpy_dma::copy(void *dst, void const *src, uint16_t length)
{
    LL_DMA_SetM2MSrcAddress(get_dma(), get_stream(), (uint32_t)src);
    LL_DMA_SetM2MDstAddress(get_dma(), get_stream(), (uint32_t)dst);
    LL_DMA_SetDataLength(get_dma(), get_stream(), (length + 3) / 4);
    m_busy = 1;
    LL_DMA_EnableStream(get_dma(), get_stream());
    while (m_busy)
        ;
    return dst;
}

void memcpy_dma::dma_transfer_completed()
{
    if (*get_is_reg() & get_tc_mask()) { // DMA transfer complete
        *get_ifc_reg() = get_tc_mask();
        LL_DMA_DisableStream(get_dma(), get_stream());
        m_busy = 0;
    }
}
} /* namespace f4ll */
