mbed SDK library sources
Fork of mbed-src by
Embed:
(wiki syntax)
Show/hide line numbers
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.h" 00020 00021 #if DEVICE_SPISLAVE 00022 00023 #include "spi_api.h" 00024 00025 namespace mbed { 00026 00027 /** A SPI slave, used for communicating with a SPI Master device 00028 * 00029 * The default format is set to 8-bits, mode 0, and a clock frequency of 1MHz 00030 * 00031 * Example: 00032 * @code 00033 * // Reply to a SPI master as slave 00034 * 00035 * #include "mbed.h" 00036 * 00037 * SPISlave device(p5, p6, p7, p8); // mosi, miso, sclk, ssel 00038 * 00039 * int main() { 00040 * device.reply(0x00); // Prime SPI with first reply 00041 * while(1) { 00042 * if(device.receive()) { 00043 * int v = device.read(); // Read byte from master 00044 * v = (v + 1) % 0x100; // Add one to it, modulo 256 00045 * device.reply(v); // Make this the next reply 00046 * } 00047 * } 00048 * } 00049 * @endcode 00050 */ 00051 class SPISlave { 00052 00053 public: 00054 00055 /** Create a SPI slave connected to the specified pins 00056 * 00057 * Pin Options: 00058 * (5, 6, 7i, 8) or (11, 12, 13, 14) 00059 * 00060 * mosi or miso can be specfied as NC if not used 00061 * 00062 * @param mosi SPI Master Out, Slave In pin 00063 * @param miso SPI Master In, Slave Out pin 00064 * @param sclk SPI Clock pin 00065 * @param ssel SPI chip select pin 00066 * @param name (optional) A string to identify the object 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 SCLK 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 * @returns 00095 * 0 if no data, 00096 * 1 otherwise 00097 */ 00098 int receive(void); 00099 00100 /** Retrieve data from receive buffer as slave 00101 * 00102 * @returns 00103 * the data in the receive buffer 00104 */ 00105 int read(void); 00106 00107 /** Fill the transmission buffer with the value to be written out 00108 * as slave on the next received message from the master. 00109 * 00110 * @param value the data to be transmitted next 00111 */ 00112 void reply(int value); 00113 00114 protected: 00115 spi_t _spi; 00116 00117 int _bits; 00118 int _mode; 00119 int _hz; 00120 }; 00121 00122 } // namespace mbed 00123 00124 #endif 00125 00126 #endif
Generated on Tue Jul 12 2022 18:13:44 by 1.7.2