/****************************************************************************/
/* Copyright 2007 MBARI.                                                    */
/* Monterey Bay Aquarium Research Institute Proprietary Information.        */
/* All rights reserved.       																						  */
/*	 																																				*/
/* SPI.c - handles SPI communications.                                      */
/*																																				  */
/* Rev History:																															*/
/* Initial Creation 11/2007		B.Kieft																	    	*/
/*																																					*/
/*																																					*/
/****************************************************************************/


#include <stdlib.h>

// Purpose: Sends spi string
#SEPARATE
void SPI_strwrite(char *s)
{
	int count = 0;
  while( (*s != 0) && (count < MAX_SPI_CHARS) ) {
  spi_write(*s++);
  count++;
  }
}

// Purpose: Reads spi string with clocks
#SEPARATE
void SPI_strread(char *s)
{
	int spi_char = 0;
	char spi_buff_in[MAX_SPI_CHARS];
	
  while( (s[spi_char] = spi_read(0)) && (spi_char < MAX_SPI_CHARS) ) {
    spi_char++;	
    delay_us(500); 
  }
}

// Purpose: Reads spi string with no clocks
#SEPARATE
void SPI_slave_strread(char *s)
{
	int spi_char = 0;
	char spi_buff_in[MAX_SPI_CHARS];
	
	while(!spi_data_is_in());
  while( (s[spi_char] = spi_read()) && (spi_char < MAX_SPI_CHARS) ) {
    spi_char++;	
    delay_us(50); 
  }
}


//void SPI_send16(int16 data)
//{
//SPI_WRITE((int8)(data >> 8)); 
//SPI_WRITE((int8)(data & 0xFF)); 
//}
//
//int16 SPI_read16(int16 data)
//{
//	int8 received_data[2]; // 0 is MSB and 1 is LSB  
//  
//  received_data[0] = spi_read(0); // First byte
//  received_data[1] = spi_read(0); // Second byte
// 
//  return make16(received_data[0],received_data[1]); // All together now...
//}
