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 /** \addtogroup drivers */
29 
30 /** A SPI slave, used for communicating with a SPI master device.
31  *
32  * The default format is set to 8 bits, mode 0 and a clock frequency of 1MHz.
33  *
34  * @note Synchronization level: Not protected
35  *
36  * Example of how to reply to a SPI master as slave:
37  * @code
38  *
39  * #include "mbed.h"
40  *
41  * SPISlave device(SPI_MOSI, SPI_MISO, SPI_SCLK, SPI_CS);
42  *
43  * int main() {
44  * device.reply(0x00); // Prime SPI with first reply
45  * while(1) {
46  * if(device.receive()) {
47  * int v = device.read(); // Read byte from master
48  * v = (v + 1) % 0x100; // Add one to it, modulo 256
49  * device.reply(v); // Make this the next reply
50  * }
51  * }
52  * }
53  * @endcode
54  * @ingroup drivers
55  */
56 class SPISlave : private NonCopyable<SPISlave> {
57 
58 public:
59 
60  /** Create a SPI slave connected to the specified pins.
61  *
62  * @note Either mosi or miso can be specified as NC if not used.
63  *
64  * @param mosi SPI Master Out, Slave In pin.
65  * @param miso SPI Master In, Slave Out pin.
66  * @param sclk SPI Clock pin.
67  * @param ssel SPI Chip Select pin.
68  */
69  SPISlave(PinName mosi, PinName miso, PinName sclk, PinName ssel);
70 
71  /** Configure the data transmission format.
72  *
73  * @param bits Number of bits per SPI frame (4 - 16).
74  * @param mode Clock polarity and phase mode (0 - 3).
75  *
76  * @code
77  * mode | POL PHA
78  * -----+--------
79  * 0 | 0 0
80  * 1 | 0 1
81  * 2 | 1 0
82  * 3 | 1 1
83  * @endcode
84  */
85  void format(int bits, int mode = 0);
86 
87  /** Set the SPI bus clock frequency.
88  *
89  * @param hz Clock frequency in hz (default = 1MHz).
90  */
91  void frequency(int hz = 1000000);
92 
93  /** Polls the SPI to see if data has been received.
94  *
95  * @return Presence of received data.
96  * @retval 0 No data waiting.
97  * @retval 1 Data waiting.
98  */
99  int receive(void);
100 
101  /** Retrieve data from receive buffer as slave.
102  *
103  * @return The data in the receive buffer.
104  */
105  int read(void);
106 
107  /** Fill the transmission buffer with the value to be written out
108  * as slave on the next received message from the master.
109  *
110  * @param value The data to be transmitted next.
111  */
112  void reply(int value);
113 
114 #if !defined(DOXYGEN_ONLY)
115 
116 protected:
117  /* Internal SPI object identifying the resources */
118  spi_t _spi;
119 
120  /* How many bits in an SPI frame */
121  uint8_t _bits;
122  /* Clock phase and polarity */
123  spi_mode_t _mode;
124  /* Clock frequency */
125  uint32_t _hz;
126 
127  volatile uint32_t _buffer;
128  uint32_t _dummy;
129  volatile bool _is_pending;
130  volatile bool _has_received;
131 
132  static void irq_handler(spi_t *obj, void *ctx, spi_async_event_t *event);
133 #endif //!defined(DOXYGEN_ONLY)
134 };
135 
136 } // namespace mbed
137 
138 #endif
139 
140 #endif
int receive(void)
Polls the SPI to see if data has been received.
Event data reported to interrupt&#39;s callback.
Definition: spi_api.h:184
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:168
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.
enum _spi_mode_t spi_mode_t
SPI modes.
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:56
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.