]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/api/SPI.h
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / api / SPI.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_SPI_H
17 #define MBED_SPI_H
18
19 #include "platform.h"
20
21 #if DEVICE_SPI
22
23 #include "spi_api.h"
24
25 namespace mbed {
26
27 /** A SPI Master, used for communicating with SPI slave devices
28  *
29  * The default format is set to 8-bits, mode 0, and a clock frequency of 1MHz
30  *
31  * Most SPI devices will also require Chip Select and Reset signals. These
32  * can be controlled using <DigitalOut> pins
33  *
34  * Example:
35  * @code
36  * // Send a byte to a SPI slave, and record the response
37  *
38  * #include "mbed.h"
39  *
40  * SPI device(p5, p6, p7); // mosi, miso, sclk
41  *
42  * int main() {
43  *     int response = device.write(0xFF);
44  * }
45  * @endcode
46  */
47 class SPI {
48
49 public:
50
51     /** Create a SPI master connected to the specified pins
52      *
53      * Pin Options:
54      *  (5, 6, 7) or (11, 12, 13)
55      *
56      *  mosi or miso can be specfied as NC if not used
57      *
58      *  @param mosi SPI Master Out, Slave In pin
59      *  @param miso SPI Master In, Slave Out pin
60      *  @param sclk SPI Clock pin
61      */
62     SPI(PinName mosi, PinName miso, PinName sclk, PinName _unused=NC);
63
64     /** Configure the data transmission format
65      *
66      *  @param bits Number of bits per SPI frame (4 - 16)
67      *  @param mode Clock polarity and phase mode (0 - 3)
68      *
69      * @code
70      * mode | POL PHA
71      * -----+--------
72      *   0  |  0   0
73      *   1  |  0   1
74      *   2  |  1   0
75      *   3  |  1   1
76      * @endcode
77      */
78     void format(int bits, int mode = 0);
79
80     /** Set the spi bus clock frequency
81      *
82      *  @param hz SCLK frequency in hz (default = 1MHz)
83      */
84     void frequency(int hz = 1000000);
85
86     /** Write to the SPI Slave and return the response
87      *
88      *  @param value Data to be sent to the SPI slave
89      *
90      *  @returns
91      *    Response from the SPI slave
92     */
93     virtual int write(int value);
94
95 public:
96     virtual ~SPI() {
97     }
98
99 protected:
100     spi_t _spi;
101
102     void aquire(void);
103     static SPI *_owner;
104     int _bits;
105     int _mode;
106     int _hz;
107 };
108
109 } // namespace mbed
110
111 #endif
112
113 #endif