Esempio n. 1
0
/**************************************************************************************************
 * @fn      HalSpiWriteRead
 *
 * @brief   Read a buffer from the SPI. Half duplex API.
 *
 * @param   port - SPI port.
 *          pBuf - Pointer to the buffer that will be read to.
 *          len - Number of bytes to read.
 *
 * @return  STATUS
 **************************************************************************************************/
int HalSpiRead(uint8 port, uint8 *pBuf, uint8 len)
{
	int ret;
	if (useFullDuplexAPI == TRUE)
	{
		ret = HalSpiWriteRead(port, pBuf, len);
	}
	else
	{
#ifdef __BIG_DEBUG__
		uint8 i;
#endif
		(void)port;

#ifdef __DEBUG_TIME__
		if ( (__DEBUG_TIME_ACTIVE == TRUE) &&  (__BIG_DEBUG_ACTIVE == TRUE) )
		{
			static struct timespec prevTime;
			time_printf_always_localized(" ----- READ SPI LOCK MUTEX ---------\n", NULL, NULL, &prevTime);
		}
#endif //(defined __DEBUG_TIME__)
		pthread_mutex_lock(&spiMutex1);

#ifdef __BIG_DEBUG__
		printf("SPI: Receive ...");
		for (i = 0 ; i < len; i++ ) printf(" 0x%.2x",rx[i]);
		printf("\n");
#endif

		ret = read(spiDevFd, pBuf, len);
		if (ret < 0 )
		{
			perror("can't read from SPI \n");
			npi_ipc_errno = NPI_LNX_ERROR_HAL_SPI_READ_FAILED;
			ret = NPI_LNX_FAILURE;
		}
		else if (ret == len)
		{
			ret = NPI_LNX_SUCCESS;
		}
		else
		{
			npi_ipc_errno = NPI_LNX_ERROR_HAL_SPI_READ_FAILED_INCORRECT_NUM_OF_BYTES;
			ret = NPI_LNX_FAILURE;
		}

#ifdef __DEBUG_TIME__
		if ( (__DEBUG_TIME_ACTIVE == TRUE) &&  (__BIG_DEBUG_ACTIVE == TRUE) )
		{
			static struct timespec prevTime;
			time_printf_always_localized(" ----- READ SPI DONE ---------------\n", NULL, NULL, &prevTime);
		}
#endif //(defined __DEBUG_TIME__)

		pthread_mutex_unlock(&spiMutex1);
	}
	return ret;
}
Esempio n. 2
0
/**************************************************************************************************
 * @fn      HalSpiWriteRead
 *
 * @brief   Read a buffer from the SPI. Half duplex API.
 *
 * @param   port - SPI port.
 *          pBuf - Pointer to the buffer that will be read to.
 *          len - Number of bytes to read.
 *
 * @return  STATUS
 **************************************************************************************************/
int HalSpiRead(uint8 port, uint8 *pBuf, uint8 len)
{
	int ret;
	if (useFullDuplexAPI == TRUE)
	{
		ret = HalSpiWriteRead(port, pBuf, len);
	}
	else
	{
#ifdef __BIG_DEBUG__
		uint8 i;
#endif
		(void)port;

#ifdef __DEBUG_TIME__
		gettimeofday(&curTime, NULL);
		long int diffPrev;
		int t = 0;
		if (curTime.tv_usec >= prevTime.tv_usec)
		{
			diffPrev = curTime.tv_usec - prevTime.tv_usec;
		}
		else
		{
			diffPrev = (curTime.tv_usec + 1000000) - prevTime.tv_usec;
			t = 1;
		}

		prevTime = curTime;
		int hours = ((curTime.tv_sec - startTime.tv_sec) - ((curTime.tv_sec - startTime.tv_sec) % 3600))/3600;
		int minutes = ((curTime.tv_sec - startTime.tv_sec) - ((curTime.tv_sec - startTime.tv_sec) % 60))/60;
		debug_printf("[%.3d:%.2d:%.2ld.%.6ld (+%ld.%6ld)] ----- READ SPI LOCK MUTEX ---------\n",
				hours,											// hours
				minutes,										// minutes
				(curTime.tv_sec - startTime.tv_sec) % 60,		// seconds
				curTime.tv_usec,
				curTime.tv_sec - prevTime.tv_sec - t,
				diffPrev);
#endif //(defined __DEBUG_TIME__)
		pthread_mutex_lock(&spiMutex1);

#ifdef __BIG_DEBUG__
		printf("SPI: Receive ...");
		for (i = 0 ; i < len; i++ ) printf(" 0x%.2x",rx[i]);
		printf("\n");
#endif

		ret = read(spiDevFd, pBuf, len);
		if (ret < 0 )
		{
			perror("can't read from SPI \n");
			npi_ipc_errno = NPI_LNX_ERROR_HAL_SPI_READ_FAILED;
			ret = NPI_LNX_FAILURE;
		}
		else if (ret == len)
		{
			ret = NPI_LNX_SUCCESS;
		}
		else
		{
			npi_ipc_errno = NPI_LNX_ERROR_HAL_SPI_READ_FAILED_INCORRECT_NUM_OF_BYTES;
			ret = NPI_LNX_FAILURE;
		}

#ifdef __DEBUG_TIME__
		gettimeofday(&curTime, NULL);
		t = 0;
		if (curTime.tv_usec >= prevTime.tv_usec)
		{
			diffPrev = curTime.tv_usec - prevTime.tv_usec;
		}
		else
		{
			diffPrev = (curTime.tv_usec + 1000000) - prevTime.tv_usec;
			t = 1;
		}

		prevTime = curTime;
		hours = ((curTime.tv_sec - startTime.tv_sec) - ((curTime.tv_sec - startTime.tv_sec) % 3600))/3600;
		minutes = ((curTime.tv_sec - startTime.tv_sec) - ((curTime.tv_sec - startTime.tv_sec) % 60))/60;
		debug_printf("[%.3d:%.2d:%.2ld.%.6ld (+%ld.%6ld)] ----- READ SPI DONE ---------------\n",
				hours,											// hours
				minutes,										// minutes
				(curTime.tv_sec - startTime.tv_sec) % 60,		// seconds
				curTime.tv_usec,
				curTime.tv_sec - prevTime.tv_sec - t,
				diffPrev);
#endif //(defined __DEBUG_TIME__)

		pthread_mutex_unlock(&spiMutex1);
	}
	return ret;
}