/*
 * ll_dmadrivenusartcore.h
 *
 *  Created on: Nov 4, 2019
 *      Author: abody
 */

#ifndef LL_USARTCORE_H_
#define LL_USARTCORE_H_
#include <platform/usart_ll.h>

#include "f4ll/ll_dmahelper.h"

namespace f4ll {

class LL_UsartCore
{
public:
	static inline void HandleUsartIrq(LL_UsartCore *_this) { _this->UsartIsr(); }
	static inline void HandleRxDmaIrq(LL_UsartCore *_this) { _this->RxDmaIsr(); }
	static inline void HandleTxDmaIrq(LL_UsartCore *_this) { _this->TxDmaIsr(); }

protected:
	LL_UsartCore(USART_TypeDef *usart, DMA_TypeDef *dma, uint32_t streamRx, uint32_t streamTx);

	virtual void ReceiverIdle() = 0;
	virtual void TransmissionComplete() = 0;

	virtual void RxDmaTransferComplete() = 0;
	virtual void RxDmaHalfTransfer() = 0;
	virtual void RxDmaError(LL_DmaHelper::DmaErrorType reason) = 0;

	virtual void TxDmaTransferComplete() = 0;
	virtual void TxDmaHalfTransfer() = 0;
	virtual void TxDmaError(LL_DmaHelper::DmaErrorType reason) = 0;

	void SetupTransmit(void const *buffer, uint16_t length);
	void SetupReceive(void *buffer, uint16_t length);

	void UsartIsr();
	void RxDmaIsr();
	void TxDmaIsr();

	USART_TypeDef *m_usart;
	LL_DmaHelper   m_rxDma;
	LL_DmaHelper   m_txDma;
};

} /* namespace f4ll */

#endif /* LL_USARTCORE_H_ */
