Mistake on this page?
Report an issue in GitHub or email us
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 
27 namespace 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 and a clock frequency of 1MHz.
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  */
59 class SPISlave : private NonCopyable<SPISlave> {
60 
61 public:
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 static_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  /** Configure the data transmission format.
84  *
85  * @param bits Number of bits per SPI frame (4 - 16).
86  * @param mode Clock polarity and phase mode (0 - 3).
87  *
88  * @code
89  * mode | POL PHA
90  * -----+--------
91  * 0 | 0 0
92  * 1 | 0 1
93  * 2 | 1 0
94  * 3 | 1 1
95  * @endcode
96  */
97  void format(int bits, int mode = 0);
98 
99  /** Set the SPI bus clock frequency.
100  *
101  * @param hz Clock frequency in hz (default = 1MHz).
102  */
103  void frequency(int hz = 1000000);
104 
105  /** Polls the SPI to see if data has been received.
106  *
107  * @return Presence of received data.
108  * @retval 0 No data waiting.
109  * @retval 1 Data waiting.
110  */
111  int receive(void);
112 
113  /** Retrieve data from receive buffer as slave.
114  *
115  * @return The data in the receive buffer.
116  */
117  int read(void);
118 
119  /** Fill the transmission buffer with the value to be written out
120  * as slave on the next received message from the master.
121  *
122  * @param value The data to be transmitted next.
123  */
124  void reply(int value);
125 
126 #if !defined(DOXYGEN_ONLY)
127 
128 protected:
129  /* Internal SPI object identifying the resources */
130  spi_t _spi;
131 
132  /* How many bits in an SPI frame */
133  int _bits;
134  /* Clock phase and polarity */
135  int _mode;
136  /* Clock frequency */
137  int _hz;
138 
139 #endif //!defined(DOXYGEN_ONLY)
140 };
141 
142 /** @}*/
143 
144 } // namespace mbed
145 
146 #endif
147 
148 #endif
int receive(void)
Polls the SPI to see if data has been received.
void frequency(int hz=1000000)
Set the SPI bus clock frequency.
Prevents generation of copy constructor and copy assignment operator in derived classes.
Definition: NonCopyable.h:162
SPISlave(PinName mosi, PinName miso, PinName sclk, PinName ssel)
Create a SPI slave connected to the specified pins.
void format(int bits, int mode=0)
Configure the data transmission format.
void reply(int value)
Fill the transmission buffer with the value to be written out as slave on the next received message f...
int read(void)
Retrieve data from receive buffer as slave.
A SPI slave, used for communicating with a SPI master device.
Definition: SPISlave.h:59
Definition: ATHandler.h:46
Asynch SPI HAL structure.
Definition: spi_api.h:43
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.