2015-08-13 15:20:49 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
|
2016-11-08 18:45:41 +01:00
|
|
|
* 2016 Freie Universität Berlin
|
2015-08-13 15:20:49 +02:00
|
|
|
*
|
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser General
|
|
|
|
* Public License v2.1. See the file LICENSE in the top level directory for more
|
|
|
|
* details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2018-05-23 17:55:11 +02:00
|
|
|
* @ingroup drivers_periph_spi
|
2015-08-13 15:20:49 +02:00
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @brief common SPI function fallback implementations
|
|
|
|
*
|
|
|
|
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
2016-11-08 18:45:41 +01:00
|
|
|
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
2015-08-13 15:20:49 +02:00
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
#include "board.h"
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "periph/spi.h"
|
|
|
|
|
2016-11-08 18:45:41 +01:00
|
|
|
#ifdef PERIPH_SPI_NEEDS_INIT_CS
|
|
|
|
int spi_init_cs(spi_t bus, spi_cs_t cs)
|
2015-08-13 15:20:49 +02:00
|
|
|
{
|
2016-11-08 18:45:41 +01:00
|
|
|
if (bus >= SPI_NUMOF) {
|
|
|
|
return SPI_NODEV;
|
|
|
|
}
|
2020-01-17 12:45:13 +01:00
|
|
|
if (gpio_is_equal(cs, SPI_CS_UNDEF) || !gpio_is_valid(cs)) {
|
2016-11-08 18:45:41 +01:00
|
|
|
return SPI_NOCS;
|
2015-08-13 15:20:49 +02:00
|
|
|
}
|
|
|
|
|
2016-11-08 18:45:41 +01:00
|
|
|
gpio_init((gpio_t)cs, GPIO_OUT);
|
|
|
|
gpio_set((gpio_t)cs);
|
|
|
|
|
|
|
|
return SPI_OK;
|
2015-08-13 15:20:49 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-09-28 20:04:45 +02:00
|
|
|
#ifdef PERIPH_SPI_NEEDS_TRANSFER_BYTE
|
2016-11-08 18:45:41 +01:00
|
|
|
uint8_t spi_transfer_byte(spi_t bus, spi_cs_t cs, bool cont, uint8_t out)
|
2016-09-28 20:04:45 +02:00
|
|
|
{
|
2016-11-08 18:45:41 +01:00
|
|
|
uint8_t in;
|
|
|
|
spi_transfer_bytes(bus, cs, cont, &out, &in, 1);
|
|
|
|
return in;
|
2016-09-28 20:04:45 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-08-13 15:20:49 +02:00
|
|
|
#ifdef PERIPH_SPI_NEEDS_TRANSFER_REG
|
2016-11-08 18:45:41 +01:00
|
|
|
uint8_t spi_transfer_reg(spi_t bus, spi_cs_t cs, uint8_t reg, uint8_t out)
|
2015-08-13 15:20:49 +02:00
|
|
|
{
|
2016-11-08 18:45:41 +01:00
|
|
|
spi_transfer_bytes(bus, cs, true, ®, NULL, 1);
|
|
|
|
return spi_transfer_byte(bus, cs, false, out);
|
2015-08-13 15:20:49 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef PERIPH_SPI_NEEDS_TRANSFER_REGS
|
2016-11-08 18:45:41 +01:00
|
|
|
void spi_transfer_regs(spi_t bus, spi_cs_t cs,
|
|
|
|
uint8_t reg, const void *out, void *in, size_t len)
|
2015-08-13 15:20:49 +02:00
|
|
|
{
|
2016-11-08 18:45:41 +01:00
|
|
|
spi_transfer_bytes(bus, cs, true, ®, NULL, 1);
|
|
|
|
spi_transfer_bytes(bus, cs, false, out, in, len);
|
2015-08-13 15:20:49 +02:00
|
|
|
}
|
|
|
|
#endif
|