YAHAL
Yet Another Hardware Abstraction Library
Loading...
Searching...
No Matches
spi_interface.h
1// ---------------------------------------------
2// This file is part of
3// _ _ __ _ _ __ __
4// ( \/ ) /__\ ( )_( ) /__\ ( )
5// \ / /(__)\ ) _ ( /(__)\ )(__
6// (__)(__)(__)(_) (_)(__)(__)(____)
7//
8// Yet Another HW Abstraction Library
9// Copyright (C) Andreas Terstegge
10// BSD Licensed (see file LICENSE)
11//
12// ---------------------------------------------
13//
14// This file defines a generic and abstract C++
15// interface for a simple SPI interface.
16// Currently only 8-bit transfers are supported.
17
18#ifndef _SPI_INTERFACE_H_
19#define _SPI_INTERFACE_H_
20
21#include <stdint.h>
22#include <functional>
23using std::function;
24
26public:
27 // Perform a combines write/read operation.
28 // len bytes from txbuf are sent via SPI, and at
29 // the same time rxbuf is filled with len bytes.
30 // The number of bytes read/written is returned,
31 // otherwise -1 in case of errors
32 virtual int16_t spiTxRx(const uint8_t *txbuf, uint8_t *rxbuf, uint16_t len) = 0;
33
34 // Perform a write operation. len byte from txbuf are
35 // send via SPI, and the received bytes are discarded.
36 // The number of bytes written is returned, otherwise
37 // -1 in case of errors
38 virtual int16_t spiTx(const uint8_t *txbuf, uint16_t len) = 0;
39
40 // Perform a read operation. len bytes with value txbyte
41 // are sent, and the received values are stored in rxbuf.
42 // The number of bytes read is returned, otherwise -1 in
43 // case of errors
44 virtual int16_t spiRx(uint8_t txbyte, uint8_t *rxbuf, uint16_t len) = 0;
45
46 // Set the speed in Hz. Typical value is 1000000 (1 MHz)
47 virtual void setSpeed(uint32_t Hz) = 0;
48
49 // Enable/Disable automatic setting of the CS line.
50 virtual void generateCS(bool val) = 0;
51
52 // Control the CS line in case of generateCS(false).
53 virtual void setCS(bool val) = 0;
54
55 // In SPI client mode, attach a RX handler which
56 // is called for every received byte.
57 virtual void spiAttachRxIrq(function<void(uint8_t data)> f) = 0;
58
59protected:
60 virtual ~spi_interface() = default;
61};
62
63#endif // _SPI_INTERFACE_H_
64