/*
 * $Id: efm8_spi.c 461 2019-06-18 02:34:13Z true $
 * begin 20190505 true
 *
 * SPI transfer functions
 */

#include "efm8_config.h"
#include "driver/efm8_spi.h"


uint8_t spi0_xfer_byte(uint8_t byte)
{
	while (!SPI0CN0_TXBMT);
	SPI0DAT = byte;
	while (!SPI0CN0_SPIF);
	SPI0CN0_SPIF = 0;

	return SPI0DAT;
}

void spi0_xfer(uint8_t *rbuf, uint8_t *wbuf, uint8_t len)
{
	uint8_t i;
	uint8_t r;

	SPI0_CS = 0;

	i = 0;
	while (i != len) {
		if (wbuf) {
			r = spi0_xfer_byte(wbuf[i]);
			if (rbuf) {
				rbuf[i] = r;
			}
		}
		i++;
	}

	SPI0_CS = 1;
}
