mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Thu Nov 08 11:46:34 2018 +0000
Revision:
188:bcfe06ba3d64
Parent:
184:08ed48f1de7f
Child:
189:f392fc9709a3
mbed-dev library. Release version 164

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