/*
 * Spi_Util.c
 *
 *  Created on: 2020Äê10ÔÂ15ÈÕ
 *      Author: zhou_l4
 */
#include "osif.h"
#include "Abstraction/Include/OS_Interface.h"

static uint8_t spi_com_sequence_num;

void nvdSpiUtil_Init()
{
	spi_com_sequence_num = 0;
}

uint32_t nubSpiUtil_Generatekey()
{
	//JPCC_DEBUG(JPCC_LOG_LEVEL_INFO, "[ SPI ][%s:%d] OS_GetCurrentTime", __FUNCTION__, __LINE__);
	return OS_getCurrentTime();
}

uint8_t nubSpiUtil_GenerateCheckSum(uint8_t *buffer,uint32_t size)
{
	uint8_t sum = 0;
	uint32_t i = 0;
	for (i = 0; i < size; i++)
	{
		sum ^= buffer[i];
	}
	return sum;
}

uint8_t nubSpiUtil_GenerateSequenceNum()
{
	uint8_t num = spi_com_sequence_num;
	if(spi_com_sequence_num == 0xFF)
	{
		spi_com_sequence_num = 0x00;
	}
	else
	{
		spi_com_sequence_num++;
	}
	return num;
}

void nvdSpiUtil_SyncSequenceNum(uint8_t num)
{
	spi_com_sequence_num = num;
	return;
}

void nvdSpiUtil_LockInit(mutex_t * mutex)
{
	*mutex = OS_createLock();
}

void nvdSpiUtil_Lock(mutex_t * mutex)
{
	OS_lock(mutex);
}

void nvdSpiUtil_UnLock(mutex_t * mutex)
{
	OS_unlock(mutex);
}
