/*
 * interrupt.h
 *
 *  Created on: Aug 29, 2019
 *      Author: abody
 */

#ifndef CRC_HANDLER_H_
#define CRC_HANDLER_H_

#include <inttypes.h>

#ifdef HAVE_CONFIG
#include "config.h"
#endif	//	HAVE_CONFIG

#include <dma_helper.h>

#ifndef CRCTASKCOUNT
#define CRCTASKCOUNT 2
#endif

typedef struct {
	DMAINFO	dmaInfo;
	volatile uint8_t activeSlot;
	struct crctask_t {
		void *address;
		uint16_t wordCount;
		void (*callback)(void*, uint32_t, uint8_t);
		void *callbackParam;
	} volatile crcTasks[CRCTASKCOUNT];
} CRCSTATUS;

void InitCrcStatus(CRCSTATUS *status, DMA_TypeDef *dma, uint32_t stream);
static inline uint8_t GetActiveSlot(CRCSTATUS *status) {
	return status->activeSlot;
}
static inline uint8_t IsSlotQueued(CRCSTATUS *status, uint8_t slot) {
	return status->crcTasks[slot].address != NULL;
}
static inline uint8_t IsSlotActive(CRCSTATUS *status, uint8_t slot) {
	return status->crcTasks[slot].callback != NULL || status->crcTasks[slot].callbackParam != NULL;
}
uint8_t EnqueueCrcTask(CRCSTATUS *crcStatus, uint8_t slot, uint8_t *address, uint16_t len,
		void (*callback)(void*, uint32_t, uint8_t), void* callbackParam);
void WaitCrcResults(CRCSTATUS *status, uint8_t slot);
uint32_t ComputeCrc(CRCSTATUS *status, uint8_t slot, uint8_t *address, uint16_t len);
void ComputeCrcAsync(CRCSTATUS *status, uint8_t slot,
		uint8_t *address, uint16_t len,
		void (*callback)(void*, uint32_t, uint8_t), void* callbackParam);
void HandleCrcDmaIrq(CRCSTATUS *status);

#endif /* CRC_HANDLER_H_ */
