Rtos API example

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  *
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:
00036  * @code
00037  * // Reply to a SPI master as slave
00038  *
00039  * #include "mbed.h"
00040  *
00041  * SPISlave device(p5, p6, p7, p8); // mosi, miso, sclk, ssel
00042  *
00043  * int main() {
00044  *     device.reply(0x00);              // Prime SPI with first reply
00045  *     while(1) {
00046  *         if(device.receive()) {
00047  *             int v = device.read();   // Read byte from master
00048  *             v = (v + 1) % 0x100;     // Add one to it, modulo 256
00049  *             device.reply(v);         // Make this the next reply
00050  *         }
00051  *     }
00052  * }
00053  * @endcode
00054  * @ingroup drivers
00055  */
00056 class SPISlave : private NonCopyable<SPISlave> {
00057 
00058 public:
00059 
00060     /** Create a SPI slave connected to the specified pins
00061      *
00062      *  mosi or miso can be specfied as NC if not used
00063      *
00064      *  @param mosi SPI Master Out, Slave In pin
00065      *  @param miso SPI Master In, Slave Out pin
00066      *  @param sclk SPI Clock pin
00067      *  @param ssel SPI chip select pin
00068      */
00069     SPISlave(PinName mosi, PinName miso, PinName sclk, PinName ssel);
00070 
00071     /** Configure the data transmission format
00072      *
00073      *  @param bits Number of bits per SPI frame (4 - 16)
00074      *  @param mode Clock polarity and phase mode (0 - 3)
00075      *
00076      * @code
00077      * mode | POL PHA
00078      * -----+--------
00079      *   0  |  0   0
00080      *   1  |  0   1
00081      *   2  |  1   0
00082      *   3  |  1   1
00083      * @endcode
00084      */
00085     void format(int bits, int mode = 0);
00086 
00087     /** Set the spi bus clock frequency
00088      *
00089      *  @param hz SCLK frequency in hz (default = 1MHz)
00090      */
00091     void frequency(int hz = 1000000);
00092 
00093     /** Polls the SPI to see if data has been received
00094      *
00095      *  @returns
00096      *    0 if no data,
00097      *    1 otherwise
00098      */
00099     int receive(void);
00100 
00101     /** Retrieve  data from receive buffer as slave
00102      *
00103      *  @returns
00104      *    the data in the receive buffer
00105      */
00106     int read(void);
00107 
00108     /** Fill the transmission buffer with the value to be written out
00109      *  as slave on the next received message from the master.
00110      *
00111      *  @param value the data to be transmitted next
00112      */
00113     void reply(int value);
00114 
00115 protected:
00116     spi_t _spi;
00117 
00118     int _bits;
00119     int _mode;
00120     int _hz;
00121 };
00122 
00123 } // namespace mbed
00124 
00125 #endif
00126 
00127 #endif