/*
 * Send data to SDI pin - for SSI0 ();
 * Description: send data on one of the channel of POT. Channel value vary from 0 to 3. Data will be 8-bit value
 *
 */
void sendto_Dpot(uint8_t channel,uint8_t data)
{

	if (channel <= 3)
	{
		GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_3,0); // set CS pin low before sending bit
		spi_senddata((channel%4),data);
		GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_3,8); // set CS pin high before sending bit
	}

	else if (channel >=4 && channel <= 7)
	{
		// To Do -- select pin on which CS of second IC is connect

		GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_7,0); // set CS pin low before sending bit
		spi_senddata((channel%4),data);
		GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_7,128); // set CS pin high before sending bit
	}

	else if (channel >=8 && channel <= 11)
	{
		// To Do -- select pin on which CS of third IC is connect

		//GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_3,0); // set CS pin low before sending bit
		spi_senddata((channel%4),data);
		//GPIOPinWrite(GPIO_PORTA_BASE,GPIO_PIN_3,8); // set CS pin high before sending bit
	}
}
uint8_t WizFi250SpiDrv::sendCommand(const char *send_command, uint8_t cr_lf, uint8_t debug_print )
{
	uint8_t		nResult = RET_OK;
	uint32_t	temp_length_spi_received = 0;
	char crlf_string[3];
	char temp_buff[50];

	// clear spi rx buffer
	memset(m_spi_rx_buffer, 0, MAX_SPI_BUFSIZE);
	m_spi_rx_length = 0;

	// Send Command
	spi_senddata((uint8_t*) send_command, strlen(send_command), m_spi_rx_buffer, &m_spi_rx_length);
	m_spi_rx_length = m_spi_rx_length + temp_length_spi_received;

	// Send CR, LF
	if ( cr_lf == 1 )			strcpy(crlf_string, "\r");
	else if ( cr_lf == 2 )		strcpy(crlf_string, "\n");
	else if ( cr_lf == 3 )		strcpy(crlf_string, "\r\n");
	if ( cr_lf == 1 || cr_lf == 2 || cr_lf == 3 )
	{
		spi_senddata((uint8_t*)crlf_string, strlen(crlf_string), (m_spi_rx_buffer+m_spi_rx_length), &temp_length_spi_received);
		m_spi_rx_length = m_spi_rx_length + temp_length_spi_received;
	}

	if ( debug_print <= 4 )
	{
		strcpy_P( temp_buff, (char*)pgm_read_word(&debug_str_table[DBG_SPI_SEND]) );
		DBG_LN(temp_buff);

		DBG_LN(send_command);

		strcpy_P( temp_buff, (char*)pgm_read_word(&debug_str_table[DBG_SPI_END]) );
		DBG_LN(temp_buff);
	}

	return nResult;
}
uint8_t WizFi250SpiDrv::waitResponse(uint32_t check_delay, uint32_t check_count, char*str_find1, char* str_find2, uint8_t debug_print )
{
	uint32_t	i;
	uint32_t	temp_length_spi_received = 0;
	uint8_t		is_found1=0, is_found2=0;
	char		temp_buff[50];

	if ( str_find1 == 0 ) is_found1 = 1;
	if ( str_find2 == 0 ) is_found2 = 1;


	for (i=0; i<check_count; i++)
	{
		delay(check_delay);
		spi_senddata(0, 0, (m_spi_rx_buffer+m_spi_rx_length), &temp_length_spi_received);
		m_spi_rx_length = m_spi_rx_length + temp_length_spi_received;

		if ( is_found1 == 0 )
		{
			if ( strstr((char*)m_spi_rx_buffer, str_find1) )	is_found1 = 1;
		}
		if ( is_found2 == 0 )
		{
			if ( strstr((char*)m_spi_rx_buffer, str_find2) )	is_found2 = 1;
		}

		if ( is_found1 && is_found2 )		break;
	}


	if ( !(is_found1 && is_found2) )
	{
		if(debug_print <= 4)
		{
			strcpy_P( temp_buff, (char*)pgm_read_word(&debug_str_table[DBG_START_MSG]) );
			DBG(temp_buff);

			strcpy_P( temp_buff, (char*)pgm_read_word(&debug_str_table[DBG_ERROR_NOT_FOUND_SUCCESS_STRING]) );
			DBG_LN(temp_buff);
		}
		return SPI_TIMEOUT;
	}

	return SPI_SUCCESS;
}