// Write User Zone
//
// The Write Large User Zone function is used to write data to CryptoMemory devices that have
// greater than 256 bytes in each user zone (AT88SC6416C, and larger).
 
// CryptoMemory Library Include Files
#include "CM_LIB.h"
#include "CM_I2C.h"
#include "CM_I2C_L.h"
#include "CM_GPA.h"

// Write User Zone
char cm_WriteLargeZone(uint32_t uiCryptoAddr, uint8_t* pucBuffer, uint8_t ucCount)
{
    uint8_t ucReturn;
	
    ucCM_InsBuff[0] = 0xb0;
    ucCM_InsBuff[1] = (uint8_t)(uiCryptoAddr>>8);
    ucCM_InsBuff[2] = (uint8_t)uiCryptoAddr;
    ucCM_InsBuff[3] = ucCount;

    // Three bytes of the command must be included in the polynominals
    cm_GPAcmd3(ucCM_InsBuff);
    
    // Include the data in the polynominals and encrypt it required
    cm_GPAencrypt(ucCM_Encrypt, pucBuffer, ucCount); 

    ucReturn = cm_WriteCommand(ucCM_InsBuff, pucBuffer, ucCount);

	// when anti-tearing, the host should send ACK should >= 20ms after write command
	if (ucCM_AntiTearing) CM_LOW_LEVEL.WaitClock(100);  //10

	// Done
	return ucReturn;
}
