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  /** Configure the data transmission format.
75  *
76  * @param bits Number of bits per SPI frame (4 - 16).
77  * @param mode Clock polarity and phase mode (0 - 3).
78  *
79  * @code
80  * mode | POL PHA
81  * -----+--------
82  * 0 | 0 0
83  * 1 | 0 1
84  * 2 | 1 0
85  * 3 | 1 1
86  * @endcode
87  */
88  void format(int bits, int mode = 0);
89 
90  /** Set the SPI bus clock frequency.
91  *
92  * @param hz Clock frequency in hz (default = 1MHz).
93  */
94  void frequency(int hz = 1000000);
95 
96  /** Polls the SPI to see if data has been received.
97  *
98  * @return Presence of received data.
99  * @retval 0 No data waiting.
100  * @retval 1 Data waiting.
101  */
102  int receive(void);
103 
104  /** Retrieve data from receive buffer as slave.
105  *
106  * @return The data in the receive buffer.
107  */
108  int read(void);
109 
110  /** Fill the transmission buffer with the value to be written out
111  * as slave on the next received message from the master.
112  *
113  * @param value The data to be transmitted next.
114  */
115  void reply(int value);
116 
117 #if !defined(DOXYGEN_ONLY)
118 
119 protected:
120  /* Internal SPI object identifying the resources */
121  spi_t _spi;
122 
123  /* How many bits in an SPI frame */
124  int _bits;
125  /* Clock phase and polarity */
126  int _mode;
127  /* Clock frequency */
128  int _hz;
129 
130 #endif //!defined(DOXYGEN_ONLY)
131 };
132 
133 /** @}*/
134 
135 } // namespace mbed
136 
137 #endif
138 
139 #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:169
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
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.