mbed library sources. Supersedes mbed-src.

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

Committer:
AnnaBridge
Date:
Wed Feb 20 22:31:08 2019 +0000
Revision:
189:f392fc9709a3
Parent:
188:bcfe06ba3d64
mbed library release version 165

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
AnnaBridge 189:f392fc9709a3 3 * SPDX-License-Identifier: Apache-2.0
<> 149:156823d33999 4 *
<> 149:156823d33999 5 * Licensed under the Apache License, Version 2.0 (the "License");
<> 149:156823d33999 6 * you may not use this file except in compliance with the License.
<> 149:156823d33999 7 * You may obtain a copy of the License at
<> 149:156823d33999 8 *
<> 149:156823d33999 9 * http://www.apache.org/licenses/LICENSE-2.0
<> 149:156823d33999 10 *
<> 149:156823d33999 11 * Unless required by applicable law or agreed to in writing, software
<> 149:156823d33999 12 * distributed under the License is distributed on an "AS IS" BASIS,
<> 149:156823d33999 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<> 149:156823d33999 14 * See the License for the specific language governing permissions and
<> 149:156823d33999 15 * limitations under the License.
<> 149:156823d33999 16 */
<> 149:156823d33999 17 #ifndef MBED_SPISLAVE_H
<> 149:156823d33999 18 #define MBED_SPISLAVE_H
<> 149:156823d33999 19
<> 149:156823d33999 20 #include "platform/platform.h"
AnnaBridge 168:9672193075cf 21 #include "platform/NonCopyable.h"
<> 149:156823d33999 22
AnnaBridge 189:f392fc9709a3 23 #if DEVICE_SPISLAVE || defined(DOXYGEN_ONLY)
<> 149:156823d33999 24
<> 149:156823d33999 25 #include "hal/spi_api.h"
<> 149:156823d33999 26
<> 149:156823d33999 27 namespace mbed {
<> 149:156823d33999 28 /** \addtogroup drivers */
<> 149:156823d33999 29
AnnaBridge 188:bcfe06ba3d64 30 /** A SPI slave, used for communicating with a SPI master device.
<> 149:156823d33999 31 *
AnnaBridge 188:bcfe06ba3d64 32 * The default format is set to 8 bits, mode 0 and a clock frequency of 1MHz.
<> 149:156823d33999 33 *
AnnaBridge 167:e84263d55307 34 * @note Synchronization level: Not protected
<> 149:156823d33999 35 *
AnnaBridge 188:bcfe06ba3d64 36 * Example of how to reply to a SPI master as slave:
<> 149:156823d33999 37 * @code
<> 149:156823d33999 38 *
<> 149:156823d33999 39 * #include "mbed.h"
<> 149:156823d33999 40 *
AnnaBridge 188:bcfe06ba3d64 41 * SPISlave device(SPI_MOSI, SPI_MISO, SPI_SCLK, SPI_CS);
<> 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
AnnaBridge 188:bcfe06ba3d64 60 /** Create a SPI slave connected to the specified pins.
<> 149:156823d33999 61 *
AnnaBridge 188:bcfe06ba3d64 62 * @note Either mosi or miso can be specified as NC if not used.
<> 149:156823d33999 63 *
AnnaBridge 188:bcfe06ba3d64 64 * @param mosi SPI Master Out, Slave In pin.
AnnaBridge 188:bcfe06ba3d64 65 * @param miso SPI Master In, Slave Out pin.
AnnaBridge 188:bcfe06ba3d64 66 * @param sclk SPI Clock pin.
AnnaBridge 188:bcfe06ba3d64 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
AnnaBridge 188:bcfe06ba3d64 71 /** Configure the data transmission format.
<> 149:156823d33999 72 *
AnnaBridge 188:bcfe06ba3d64 73 * @param bits Number of bits per SPI frame (4 - 16).
AnnaBridge 188:bcfe06ba3d64 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
AnnaBridge 188:bcfe06ba3d64 87 /** Set the SPI bus clock frequency.
<> 149:156823d33999 88 *
AnnaBridge 188:bcfe06ba3d64 89 * @param hz Clock frequency in hz (default = 1MHz).
<> 149:156823d33999 90 */
<> 149:156823d33999 91 void frequency(int hz = 1000000);
<> 149:156823d33999 92
AnnaBridge 188:bcfe06ba3d64 93 /** Polls the SPI to see if data has been received.
<> 149:156823d33999 94 *
AnnaBridge 188:bcfe06ba3d64 95 * @return Presence of received data.
AnnaBridge 188:bcfe06ba3d64 96 * @retval 0 No data waiting.
AnnaBridge 188:bcfe06ba3d64 97 * @retval 1 Data waiting.
<> 149:156823d33999 98 */
<> 149:156823d33999 99 int receive(void);
<> 149:156823d33999 100
AnnaBridge 188:bcfe06ba3d64 101 /** Retrieve data from receive buffer as slave.
<> 149:156823d33999 102 *
AnnaBridge 188:bcfe06ba3d64 103 * @return The data in the receive buffer.
<> 149:156823d33999 104 */
<> 149:156823d33999 105 int read(void);
<> 149:156823d33999 106
<> 149:156823d33999 107 /** Fill the transmission buffer with the value to be written out
<> 149:156823d33999 108 * as slave on the next received message from the master.
<> 149:156823d33999 109 *
AnnaBridge 188:bcfe06ba3d64 110 * @param value The data to be transmitted next.
<> 149:156823d33999 111 */
<> 149:156823d33999 112 void reply(int value);
<> 149:156823d33999 113
AnnaBridge 188:bcfe06ba3d64 114 #if !defined(DOXYGEN_ONLY)
AnnaBridge 188:bcfe06ba3d64 115
<> 149:156823d33999 116 protected:
AnnaBridge 188:bcfe06ba3d64 117 /* Internal SPI object identifying the resources */
<> 149:156823d33999 118 spi_t _spi;
<> 149:156823d33999 119
AnnaBridge 188:bcfe06ba3d64 120 /* How many bits in an SPI frame */
<> 149:156823d33999 121 int _bits;
AnnaBridge 188:bcfe06ba3d64 122 /* Clock phase and polarity */
<> 149:156823d33999 123 int _mode;
AnnaBridge 188:bcfe06ba3d64 124 /* Clock frequency */
<> 149:156823d33999 125 int _hz;
AnnaBridge 188:bcfe06ba3d64 126
AnnaBridge 188:bcfe06ba3d64 127 #endif //!defined(DOXYGEN_ONLY)
<> 149:156823d33999 128 };
<> 149:156823d33999 129
<> 149:156823d33999 130 } // namespace mbed
<> 149:156823d33999 131
<> 149:156823d33999 132 #endif
<> 149:156823d33999 133
<> 149:156823d33999 134 #endif