Mbed OS Reference
Loading...
Searching...
No Matches
SPISlave.h
1/* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17#ifndef MBED_SPISLAVE_H
18#define MBED_SPISLAVE_H
19
20#include "platform/platform.h"
21#include "platform/NonCopyable.h"
22
23#if DEVICE_SPISLAVE || defined(DOXYGEN_ONLY)
24
25#include "hal/spi_api.h"
26
27namespace mbed {
28/**
29 * \defgroup drivers_SPISlave SPISlave class
30 * \ingroup drivers-public-api-spi
31 * @{
32 */
33
34/** A SPI slave, used for communicating with a SPI master device.
35 *
36 * The default format is set to 8 bits, mode 0.
37 *
38 * @note Synchronization level: Not protected
39 *
40 * Example of how to reply to a SPI master as slave:
41 * @code
42 *
43 * #include "mbed.h"
44 *
45 * SPISlave device(SPI_MOSI, SPI_MISO, SPI_SCLK, SPI_CS);
46 *
47 * int main() {
48 * device.reply(0x00); // Prime SPI with first reply
49 * while(1) {
50 * if(device.receive()) {
51 * int v = device.read(); // Read byte from master
52 * v = (v + 1) % 0x100; // Add one to it, modulo 256
53 * device.reply(v); // Make this the next reply
54 * }
55 * }
56 * }
57 * @endcode
58 */
59class SPISlave : private NonCopyable<SPISlave> {
60
61public:
62
63 /** Create a SPI slave connected to the specified pins.
64 *
65 * @note Either mosi or miso can be specified as NC if not used.
66 *
67 * @param mosi SPI Master Out, Slave In pin.
68 * @param miso SPI Master In, Slave Out pin.
69 * @param sclk SPI Clock pin.
70 * @param ssel SPI Chip Select pin.
71 */
72 SPISlave(PinName mosi, PinName miso, PinName sclk, PinName ssel);
73
74 /** Create a SPI slave connected to the specified pins.
75 *
76 * @note Either mosi or miso can be specified as NC if not used.
77 *
78 * @param pinmap reference to structure which holds static pinmap.
79 */
80 SPISlave(const spi_pinmap_t &pinmap);
81 SPISlave(const spi_pinmap_t &&) = delete; // prevent passing of temporary objects
82
83 /**
84 * @brief Destructor. Frees the SPI peripheral so it can be used elsewhere.
85 */
87
88 /** Configure the data transmission format.
89 *
90 * @param bits Number of bits per SPI frame (4 - 16).
91 * @param mode Clock polarity and phase mode (0 - 3).
92 *
93 * @code
94 * mode | POL PHA
95 * -----+--------
96 * 0 | 0 0
97 * 1 | 0 1
98 * 2 | 1 0
99 * 3 | 1 1
100 * @endcode
101 */
102 void format(int bits, int mode = 0);
103
104 /** Polls the SPI to see if data has been received.
105 *
106 * @return Presence of received data.
107 * @retval 0 No data waiting.
108 * @retval 1 Data waiting.
109 */
110 int receive(void);
111
112 /** Retrieve data from receive buffer as slave.
113 *
114 * @return The data in the receive buffer.
115 */
116 int read(void);
117
118 /** Fill the transmission buffer with the value to be written out
119 * as slave on the next received message from the master.
120 *
121 * @param value The data to be transmitted next.
122 */
123 void reply(int value);
124
125#if !defined(DOXYGEN_ONLY)
126
127protected:
128 /* Internal SPI object identifying the resources */
129 spi_t _spi;
130
131 /* How many bits in an SPI frame */
132 int _bits;
133 /* Clock phase and polarity */
134 int _mode;
135
136#endif //!defined(DOXYGEN_ONLY)
137};
138
139/** @}*/
140
141} // namespace mbed
142
143#endif
144
145#endif
Prevents generation of copy constructor and copy assignment operator in derived classes.
Definition: NonCopyable.h:162
A SPI slave, used for communicating with a SPI master device.
Definition: SPISlave.h:59
int receive(void)
Polls the SPI to see if data has been received.
~SPISlave()
Destructor.
void format(int bits, int mode=0)
Configure the data transmission format.
SPISlave(PinName mosi, PinName miso, PinName sclk, PinName ssel)
Create a SPI slave connected to the specified pins.
void reply(int value)
Fill the transmission buffer with the value to be written out as slave on the next received message f...
SPISlave(const spi_pinmap_t &pinmap)
Create a SPI slave connected to the specified pins.
int read(void)
Retrieve data from receive buffer as slave.
Asynch SPI HAL structure.
Definition: spi_api.h:69