Skip to content

Commit

Permalink
drivers: add periph_common
Browse files Browse the repository at this point in the history
This module contains shared functions for periph implementations.
  • Loading branch information
kaspar030 committed Aug 14, 2015
1 parent 77ca7a5 commit 10a77b7
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
1 change: 1 addition & 0 deletions drivers/periph_common/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include $(RIOTBASE)/Makefile.base
91 changes: 91 additions & 0 deletions drivers/periph_common/spi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
*
* 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.
*/

/**
* @ingroup drivers
* @{
*
* @file
* @brief common SPI function fallback implementations
*
* @author Kaspar Schleiser <kaspar@schleiser.de>
*
* @}
*/
#include <stddef.h>

#include "board.h"
#include "cpu.h"
#include "periph/spi.h"
#include "periph_cpu.h"

#if SPI_NUMOF

#ifdef PERIPH_SPI_NEEDS_TRANSFER_BYTES
int spi_transfer_bytes(spi_t dev, char *out, char *in, unsigned int length)
{
int i, trans_ret, trans_bytes = 0;
char in_temp;

for (i = 0; i < length; i++) {
if (out != NULL) {
trans_ret = spi_transfer_byte(dev, out[i], &in_temp);
}
else {
trans_ret = spi_transfer_byte(dev, 0, &in_temp);
}
if (trans_ret < 0) {
return -1;
}
if (in != NULL) {
in[i] = in_temp;
}
trans_bytes++;
}

return trans_bytes++;
}
#endif

#ifdef PERIPH_SPI_NEEDS_TRANSFER_REG
int spi_transfer_reg(spi_t dev, uint8_t reg, char out, char *in)
{
int trans_ret;

trans_ret = spi_transfer_byte(dev, reg, in);
if (trans_ret < 0) {
return -1;
}
trans_ret = spi_transfer_byte(dev, out, in);
if (trans_ret < 0) {
return -1;
}

return 1;
}
#endif

#ifdef PERIPH_SPI_NEEDS_TRANSFER_REGS
int spi_transfer_regs(spi_t dev, uint8_t reg, char *out, char *in, unsigned int length)
{
int trans_ret;

trans_ret = spi_transfer_byte(dev, reg, in);
if (trans_ret < 0) {
return -1;
}
trans_ret = spi_transfer_bytes(dev, out, in, length);
if (trans_ret < 0) {
return -1;
}

return trans_ret;
}
#endif

#endif /* SPI_NUMOF */

0 comments on commit 10a77b7

Please sign in to comment.