#include "canerrorbox.h"

CanErrorBox CanErrors;

__weak void CanErrorCallbackEWG()
{
	CanErrors.EWG.counter++;
}

__weak void CanErrorCallbackEPV()
{
	CanErrors.EPV.counter++;
}

__weak void CanErrorCallbackBOF()
{
	CanErrors.BOF.counter++;
}

__weak void CanErrorCallbackSTF()
{
	CanErrors.STF.counter++;
}

__weak void CanErrorCallbackFOR()
{
	CanErrors.FOR.counter++;
}

__weak void CanErrorCallbackACK()
{
	CanErrors.ACK.counter++;
}

__weak void CanErrorCallbackBR()
{
	CanErrors.BR.counter++;
}

__weak void CanErrorCallbackBD()
{
	CanErrors.BD.counter++;
}

__weak void CanErrorCallbackCRC()
{
	CanErrors.CRC_ERROR.counter++;
}

__weak void CanErrorCallbackRXFOV0()
{
	CanErrors.RXFOV0.counter++;
}

__weak void CanErrorCallbackRXFOV1()
{
	CanErrors.RXFOV1.counter++;
}

__weak void CanErrorCallbackTXALST0()
{
	CanErrors.TXALST0.counter++;
}

__weak void CanErrorCallbackTXTERR0()
{
	CanErrors.TXTERR0.counter++;
}

__weak void CanErrorCallbackTXALST1()
{
	CanErrors.TXALST1.counter++;
}

__weak void CanErrorCallbackTXTERR1()
{
	CanErrors.TXTERR1.counter++;
}

__weak void CanErrorCallbackTXALST2()
{
	CanErrors.TXALST2.counter++;
}

__weak void CanErrorCallbackTXTERR2()
{
	CanErrors.TXTERR2.counter++;
}

__weak void CanErrorCallbackTIMEOUT()
{
	CanErrors.TIMEOUT.counter++;
}

__weak void CanErrorCallbackNOTINITIALIZED()
{
	CanErrors.NOTINITIALIZED.counter++;
}

__weak void CanErrorCallbackNOTREADY()
{
	CanErrors.NOTREADY.counter++;
}

__weak void CanErrorCallbackNOTSTARTED()
{
	CanErrors.NOTSTARTED.counter++;
}

__weak void CanErrorCallbackPARAM()
{
	CanErrors.PARAM.counter++;
}

#ifdef CAN_ERROR_BOX_ENABLED
void HAL_CAN_ErrorCallback(CAN_HandleTypeDef *hcan)
{
	if(!hcan->ErrorCode)
	{
		// NO ERROR
		return;
	}
  if(hcan->ErrorCode & HAL_CAN_ERROR_EWG)
	{
    // Protocol Error Warning
		CanErrorCallbackEWG();
  }
  if(hcan->ErrorCode & HAL_CAN_ERROR_EPV)
	{
    // Error Passive
    CanErrorCallbackEPV();
  }
  if(hcan->ErrorCode & HAL_CAN_ERROR_BOF)
	{
    // Bus-off error
    CanErrorCallbackBOF();
  }
  if(hcan->ErrorCode & HAL_CAN_ERROR_STF)
	{
    // Stuff error
    CanErrorCallbackSTF();
  }
  if(hcan->ErrorCode & HAL_CAN_ERROR_FOR)
	{
    // Form error
    CanErrorCallbackFOR();
  }
  if(hcan->ErrorCode & HAL_CAN_ERROR_ACK)
	{
		// Acknowledgment error
		CanErrorCallbackACK();
  }
  if(hcan->ErrorCode & HAL_CAN_ERROR_BR)
	{
		// Bit recessive error
		CanErrorCallbackBR();
  }
  if(hcan->ErrorCode & HAL_CAN_ERROR_BD)
	{
		// Bit dominant error
		CanErrorCallbackBD();
  }
  if(hcan->ErrorCode & HAL_CAN_ERROR_CRC)	
	{
		// CRC error
		CanErrorCallbackCRC();
  }
  if(hcan->ErrorCode & HAL_CAN_ERROR_RX_FOV0)
	{
		// Rx FIFO0 overrun error
		CanErrorCallbackRXFOV0();
  }
  if(hcan->ErrorCode & HAL_CAN_ERROR_RX_FOV1)
	{
		// Rx FIFO1 overrun error
		CanErrorCallbackRXFOV1();
  }
  if(hcan->ErrorCode & HAL_CAN_ERROR_TX_ALST0)
	{
		// TxMailbox 0 transmit failure due to arbitration lost
		CanErrorCallbackTXALST0();
  }
  if(hcan->ErrorCode & HAL_CAN_ERROR_TX_TERR0)
	{
		// TxMailbox 0 transmit failure due to transmit error
		CanErrorCallbackTXTERR0();
  }
  if(hcan->ErrorCode & HAL_CAN_ERROR_TX_ALST1)
	{
		// TxMailbox 1 transmit failure due to arbitration lost
		CanErrorCallbackTXALST1();
  }
  if(hcan->ErrorCode & HAL_CAN_ERROR_TX_TERR1)
	{
		// TxMailbox 1 transmit failure due to transmit error
		CanErrorCallbackTXTERR1();
  }
  if(hcan->ErrorCode & HAL_CAN_ERROR_TX_ALST2)
	{
		// TxMailbox 2 transmit failure due to arbitration lost
		CanErrorCallbackTXALST2();
  }
  if(hcan->ErrorCode & HAL_CAN_ERROR_TX_TERR2)
	{
		// TxMailbox 2 transmit failure due to transmit error
		CanErrorCallbackTXTERR2();
  }
  if(hcan->ErrorCode & HAL_CAN_ERROR_TIMEOUT)
	{
		// Timeout error
		CanErrorCallbackTIMEOUT();
  }
  if(hcan->ErrorCode & HAL_CAN_ERROR_NOT_INITIALIZED)
	{
		// Peripheral not initialized
		CanErrorCallbackNOTINITIALIZED();
  }
  if(hcan->ErrorCode & HAL_CAN_ERROR_NOT_READY)
	{
		// Peripheral not ready
		CanErrorCallbackNOTREADY();
  }
  if(hcan->ErrorCode & HAL_CAN_ERROR_NOT_STARTED)
	{
		// Peripheral not started
		CanErrorCallbackNOTSTARTED();
  }
  if(hcan->ErrorCode & HAL_CAN_ERROR_PARAM)
	{
		// Parameter error
		CanErrorCallbackPARAM();
  }
}
#endif