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.
Fork of mbed-src by
SPI.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_SPI_H 00017 #define MBED_SPI_H 00018 00019 #include "platform.h" 00020 00021 #if DEVICE_SPI 00022 00023 #include "spi_api.h" 00024 00025 namespace mbed { 00026 00027 /** A SPI Master, used for communicating with SPI slave devices 00028 * 00029 * The default format is set to 8-bits, mode 0, and a clock frequency of 1MHz 00030 * 00031 * Most SPI devices will also require Chip Select and Reset signals. These 00032 * can be controlled using <DigitalOut> pins 00033 * 00034 * Example: 00035 * @code 00036 * // Send a byte to a SPI slave, and record the response 00037 * 00038 * #include "mbed.h" 00039 * 00040 * SPI device(p5, p6, p7); // mosi, miso, sclk 00041 * 00042 * int main() { 00043 * int response = device.write(0xFF); 00044 * } 00045 * @endcode 00046 */ 00047 class SPI { 00048 00049 public: 00050 00051 /** Create a SPI master connected to the specified pins 00052 * 00053 * Pin Options: 00054 * (5, 6, 7) or (11, 12, 13) 00055 * 00056 * mosi or miso can be specfied as NC if not used 00057 * 00058 * @param mosi SPI Master Out, Slave In pin 00059 * @param miso SPI Master In, Slave Out pin 00060 * @param sclk SPI Clock pin 00061 */ 00062 SPI(PinName mosi, PinName miso, PinName sclk, PinName _unused=NC); 00063 00064 /** Configure the data transmission format 00065 * 00066 * @param bits Number of bits per SPI frame (4 - 16) 00067 * @param mode Clock polarity and phase mode (0 - 3) 00068 * 00069 * @code 00070 * mode | POL PHA 00071 * -----+-------- 00072 * 0 | 0 0 00073 * 1 | 0 1 00074 * 2 | 1 0 00075 * 3 | 1 1 00076 * @endcode 00077 */ 00078 void format(int bits, int mode = 0); 00079 00080 /** Set the spi bus clock frequency 00081 * 00082 * @param hz SCLK frequency in hz (default = 1MHz) 00083 */ 00084 void frequency(int hz = 1000000); 00085 00086 /** Write to the SPI Slave and return the response 00087 * 00088 * @param value Data to be sent to the SPI slave 00089 * 00090 * @returns 00091 * Response from the SPI slave 00092 */ 00093 virtual int write(int value); 00094 00095 protected: 00096 spi_t _spi; 00097 00098 void aquire(void); 00099 static SPI *_owner; 00100 int _bits; 00101 int _mode; 00102 int _hz; 00103 }; 00104 00105 } // namespace mbed 00106 00107 #endif 00108 00109 #endif
Generated on Tue Jul 12 2022 21:21:33 by
