#include <avr/io.h>

/* Pin Definitions: */
#define SPI_DDR       DDRB
#define SPI_SCK_PIN   PB5
#define SPI_MOSI_PIN  PB3
#define SPI_MISO_PIN  PB4


void Initialize_SPI(void)
{
	/* Set MOSI and SCK output, all others input */
	SPI_DDR = (1<<SPI_MOSI_PIN)|(1<<SPI_SCK_PIN);
	/* Enable SPI, Master, set clock rate fck/16 */
	SPCR = (1<<SPE)|(1<<MSTR);
}

void SPI_Start_Transmission(volatile uint8_t *port, uint8_t pinNumber)
{
	*port &= ~(1<<pinNumber);
}

uint8_t SPI_Transfer_Byte(uint8_t byteToSend)
{
	SPDR = byteToSend;
	while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
	return SPDR;
}

void SPI_Stop_Transmission(volatile uint8_t *port, uint8_t pinNumber)
{
	*port |= (1<<pinNumber);
}
