Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: nRF51_Vdd TextLCD BME280
SPISlave.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #ifndef MBED_SPISLAVE_H 00017 #define MBED_SPISLAVE_H 00018 00019 #include "platform/platform.h" 00020 #include "platform/NonCopyable.h" 00021 00022 #if defined (DEVICE_SPISLAVE) || defined(DOXYGEN_ONLY) 00023 00024 #include "hal/spi_api.h" 00025 00026 namespace mbed { 00027 /** \addtogroup drivers */ 00028 00029 /** A SPI slave, used for communicating with a SPI master device. 00030 * 00031 * The default format is set to 8 bits, mode 0 and a clock frequency of 1MHz. 00032 * 00033 * @note Synchronization level: Not protected 00034 * 00035 * Example of how to reply to a SPI master as slave: 00036 * @code 00037 * 00038 * #include "mbed.h" 00039 * 00040 * SPISlave device(SPI_MOSI, SPI_MISO, SPI_SCLK, SPI_CS); 00041 * 00042 * int main() { 00043 * device.reply(0x00); // Prime SPI with first reply 00044 * while(1) { 00045 * if(device.receive()) { 00046 * int v = device.read(); // Read byte from master 00047 * v = (v + 1) % 0x100; // Add one to it, modulo 256 00048 * device.reply(v); // Make this the next reply 00049 * } 00050 * } 00051 * } 00052 * @endcode 00053 * @ingroup drivers 00054 */ 00055 class SPISlave : private NonCopyable<SPISlave> { 00056 00057 public: 00058 00059 /** Create a SPI slave connected to the specified pins. 00060 * 00061 * @note Either mosi or miso can be specified as NC if not used. 00062 * 00063 * @param mosi SPI Master Out, Slave In pin. 00064 * @param miso SPI Master In, Slave Out pin. 00065 * @param sclk SPI Clock pin. 00066 * @param ssel SPI Chip Select pin. 00067 */ 00068 SPISlave(PinName mosi, PinName miso, PinName sclk, PinName ssel); 00069 00070 /** Configure the data transmission format. 00071 * 00072 * @param bits Number of bits per SPI frame (4 - 16). 00073 * @param mode Clock polarity and phase mode (0 - 3). 00074 * 00075 * @code 00076 * mode | POL PHA 00077 * -----+-------- 00078 * 0 | 0 0 00079 * 1 | 0 1 00080 * 2 | 1 0 00081 * 3 | 1 1 00082 * @endcode 00083 */ 00084 void format(int bits, int mode = 0); 00085 00086 /** Set the SPI bus clock frequency. 00087 * 00088 * @param hz Clock frequency in hz (default = 1MHz). 00089 */ 00090 void frequency(int hz = 1000000); 00091 00092 /** Polls the SPI to see if data has been received. 00093 * 00094 * @return Presence of received data. 00095 * @retval 0 No data waiting. 00096 * @retval 1 Data waiting. 00097 */ 00098 int receive(void); 00099 00100 /** Retrieve data from receive buffer as slave. 00101 * 00102 * @return The data in the receive buffer. 00103 */ 00104 int read(void); 00105 00106 /** Fill the transmission buffer with the value to be written out 00107 * as slave on the next received message from the master. 00108 * 00109 * @param value The data to be transmitted next. 00110 */ 00111 void reply(int value); 00112 00113 #if !defined(DOXYGEN_ONLY) 00114 00115 protected: 00116 /* Internal SPI object identifying the resources */ 00117 spi_t _spi; 00118 00119 /* How many bits in an SPI frame */ 00120 int _bits; 00121 /* Clock phase and polarity */ 00122 int _mode; 00123 /* Clock frequency */ 00124 int _hz; 00125 00126 #endif //!defined(DOXYGEN_ONLY) 00127 }; 00128 00129 } // namespace mbed 00130 00131 #endif 00132 00133 #endif
Generated on Tue Jul 12 2022 15:15:59 by
