#include <avrstuff/avlr.h>
#include <board.hpp>

extern "C" {
#include "sd_spi_platform_dependencies.h"
}

extern "C" void sd_spi_pin_mode(uint8_t pin, uint8_t mode)
{
	if (mode == OUTPUT)
		PORT_FOR(pin).DIRSET = BIT_FOR(pin);
	else
		PORT_FOR(pin).DIRCLR = BIT_FOR(pin);
}

extern "C" void sd_spi_digital_write(uint8_t pin, uint8_t state)
{
	if (state)
		PORT_FOR(pin).OUTSET = BIT_FOR(pin);
	else
		PORT_FOR(pin).OUTCLR = BIT_FOR(pin);
}

extern "C" uint32_t sd_spi_millis()
{
	// TODO - we don't have a millisecond counter until the timer module
	// is implemented
	static uint32_t m = 0;
	return m++;
}

extern "C" void sd_spi_begin()
{

}

extern "C" void sd_spi_begin_transaction(uint32_t transfer_speed_hz)
{

}

extern "C" void sd_spi_end_transaction()
{

}

extern "C" void sd_spi_send_byte(uint8_t b)
{
	spi_sdcard.transfer(b);
}

extern "C" uint8_t sd_spi_receive_byte()
{
	return spi_sdcard.transfer(0xFF);
}
