]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/api/SPISlave.h
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / api / SPISlave.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2013 ARM Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef MBED_SPISLAVE_H
17 #define MBED_SPISLAVE_H
18
19 #include "platform.h"
20
21 #if DEVICE_SPISLAVE
22
23 #include "spi_api.h"
24
25 namespace mbed {
26
27 /** A SPI slave, used for communicating with a SPI Master device
28  *
29  * The default format is set to 8-bits, mode 0, and a clock frequency of 1MHz
30  *
31  * Example:
32  * @code
33  * // Reply to a SPI master as slave
34  *
35  * #include "mbed.h"
36  *
37  * SPISlave device(p5, p6, p7, p8); // mosi, miso, sclk, ssel
38  *
39  * int main() {
40  *     device.reply(0x00);              // Prime SPI with first reply
41  *     while(1) {
42  *         if(device.receive()) {
43  *             int v = device.read();   // Read byte from master
44  *             v = (v + 1) % 0x100;     // Add one to it, modulo 256
45  *             device.reply(v);         // Make this the next reply
46  *         }
47  *     }
48  * }
49  * @endcode
50  */
51 class SPISlave {
52
53 public:
54
55     /** Create a SPI slave connected to the specified pins
56      *
57      * Pin Options:
58      *  (5, 6, 7i, 8) or (11, 12, 13, 14)
59      *
60      *  mosi or miso can be specfied as NC if not used
61      *
62      *  @param mosi SPI Master Out, Slave In pin
63      *  @param miso SPI Master In, Slave Out pin
64      *  @param sclk SPI Clock pin
65      *  @param ssel SPI chip select pin
66      *  @param name (optional) A string to identify the object
67      */
68     SPISlave(PinName mosi, PinName miso, PinName sclk, PinName ssel);
69
70     /** Configure the data transmission format
71      *
72      *  @param bits Number of bits per SPI frame (4 - 16)
73      *  @param mode Clock polarity and phase mode (0 - 3)
74      *
75      * @code
76      * mode | POL PHA
77      * -----+--------
78      *   0  |  0   0
79      *   1  |  0   1
80      *   2  |  1   0
81      *   3  |  1   1
82      * @endcode
83      */
84     void format(int bits, int mode = 0);
85
86     /** Set the spi bus clock frequency
87      *
88      *  @param hz SCLK frequency in hz (default = 1MHz)
89      */
90     void frequency(int hz = 1000000);
91
92     /** Polls the SPI to see if data has been received
93      *
94      *  @returns
95      *    0 if no data,
96      *    1 otherwise
97      */
98     int receive(void);
99
100     /** Retrieve  data from receive buffer as slave
101      *
102      *  @returns
103      *    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 protected:
115     spi_t _spi;
116
117     int _bits;
118     int _mode;
119     int _hz;
120 };
121
122 } // namespace mbed
123
124 #endif
125
126 #endif