Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SPISlave.h Source File

SPISlave.h

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