mbed

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Fri Feb 16 16:09:33 2018 +0000
Revision:
182:57724642e740
Parent:
168:9672193075cf
mbed-dev library. Release version 159.

Who changed what in which revision?

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