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.
SPI.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a copy 00005 * of this software and associated documentation files (the "Software"), to deal 00006 * in the Software without restriction, including without limitation the rights 00007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00008 * copies of the Software, and to permit persons to whom the Software is 00009 * furnished to do so, subject to the following conditions: 00010 * 00011 * The above copyright notice and this permission notice shall be included in 00012 * all copies or substantial portions of the Software. 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 00020 * SOFTWARE. 00021 */ 00022 #ifndef MBED_SPI_H 00023 #define MBED_SPI_H 00024 00025 #include "platform.h" 00026 00027 #if DEVICE_SPI 00028 00029 #include "spi_api.h" 00030 00031 namespace mbed { 00032 00033 /** A SPI Master, used for communicating with SPI slave devices 00034 * 00035 * The default format is set to 8-bits, mode 0, and a clock frequency of 1MHz 00036 * 00037 * Most SPI devices will also require Chip Select and Reset signals. These 00038 * can be controlled using <DigitalOut> pins 00039 * 00040 * Example: 00041 * @code 00042 * // Send a byte to a SPI slave, and record the response 00043 * 00044 * #include "mbed.h" 00045 * 00046 * SPI device(p5, p6, p7); // mosi, miso, sclk 00047 * 00048 * int main() { 00049 * int response = device.write(0xFF); 00050 * } 00051 * @endcode 00052 */ 00053 class SPI { 00054 00055 public: 00056 00057 /** Create a SPI master connected to the specified pins 00058 * 00059 * Pin Options: 00060 * (5, 6, 7) or (11, 12, 13) 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 */ 00068 SPI(PinName mosi, PinName miso, PinName sclk); 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 /** Write to the SPI Slave and return the response 00093 * 00094 * @param value Data to be sent to the SPI slave 00095 * 00096 * @returns 00097 * Response from the SPI slave 00098 */ 00099 virtual int write(int value); 00100 00101 protected: 00102 spi_t _spi; 00103 00104 void aquire(void); 00105 static SPI *_owner; 00106 int _bits; 00107 int _mode; 00108 int _hz; 00109 }; 00110 00111 } // namespace mbed 00112 00113 #endif 00114 00115 #endif 00116
Generated on Wed Jul 13 2022 02:59:12 by
