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 SWSPI by
SWSPI.h
00001 /* SWSPI, Software SPI library 00002 * Copyright (c) 2012-2014, David R. Van Wagner, http://techwithdave.blogspot.com 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 00020 * THE SOFTWARE. 00021 */ 00022 00023 #ifndef SWSPI_H 00024 #define SWSPI_H 00025 00026 /** A software implemented SPI that can use any digital pins 00027 * 00028 * Useful when don't want to share a single SPI hardware among attached devices 00029 * or when pinout doesn't match exactly to the target's SPI pins 00030 * 00031 * @code 00032 * #include "mbed.h" 00033 * #include "SWSPI.h" 00034 * 00035 * SWSPI spi(p5, p6, p7); // mosi, miso, sclk 00036 * 00037 * int main() 00038 * { 00039 * DigitalOut cs(p8); 00040 * spi.format(8, 0); 00041 * spi.frequency(10000000); 00042 * cs.write(0); 00043 * spi.write(0x9f); 00044 * int jedecid = (spi.write(0) << 16) | (spi.write(0) << 8) | spi.write(0); 00045 * cs.write(1); 00046 * } 00047 * @endcode 00048 */ 00049 class SWSPI 00050 { 00051 private: 00052 DigitalOut mosi; 00053 DigitalIn* miso; 00054 DigitalOut sclk; 00055 int8_t bits; 00056 int8_t polarity; // idle clock value 00057 int8_t phase; // 0=sample on leading (first) clock edge, 1=trailing (second) 00058 unsigned delay_us; 00059 00060 public: 00061 /** Create SWSPI object 00062 * 00063 * @param mosi_pin 00064 * @param miso_pin can be NC 00065 * @param sclk_pin 00066 */ 00067 SWSPI(PinName mosi_pin, PinName miso_pin, PinName sclk_pin); 00068 00069 /** Destructor */ 00070 ~SWSPI(); 00071 00072 /** Specify SPI format 00073 * 00074 * @param bits 8 or 16 are typical values 00075 * @param mode 0, 1, 2, or 3 phase (bit1) and idle clock (bit0) 00076 */ 00077 void format(int bits, int mode = 0); 00078 00079 /** Specify SPI clock frequency 00080 * 00081 * @param hz frequency (optional, defaults to 10000000) 00082 */ 00083 void frequency(int hz = 10000000); 00084 00085 /** Write data and read result 00086 * 00087 * @param value data to write (see format for bit size) 00088 * returns value read from device 00089 */ 00090 int write(int value); 00091 }; 00092 00093 #endif // SWSPI_H
Generated on Sat Jul 16 2022 15:10:08 by
1.7.2
