Martin Simpson / SWSPI_HD

Fork of SWSPI by Dave Van Wagner

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SWSPI_HD.h Source File

SWSPI_HD.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 /* Based on above SWSPI credit as above
00024  * This is now SWSPI_HD class type to provide single line data input output (dio)
00025  * M. R. Simpson 19/06/2018
00026  */
00027 
00028 #ifndef SWSPI_HD_H
00029 #define SWSPI_HD_H
00030 
00031 /** A software implemented SPI that can use any digital pins
00032  *
00033  * Useful when don't want to share a single SPI hardware among attached devices
00034  * or when pinout doesn't match exactly to the target's SPI pins
00035  *
00036  * @code
00037  * #include "mbed.h"
00038  * #include "SWSPI_HD.h"
00039  * 
00040  * SWSPI_HD spi(D8, D7, D6); // dio, sclk (n)cs (Using Arduino form factor Board (NUCLEO-32F401RE)
00041  * 
00042  * int main() 
00043  * {
00044  *     spi.format(8, 0);
00045  *     spi.frequency(10000000);
00046  *     cs.write(0);
00047  *     spi.write(0x9f);
00048  *     int jedecid = (spi.write(0) << 16) | (spi.write(0) << 8) | spi.write(0);
00049  *     cs.write(1);
00050  * }
00051  * @endcode
00052  */
00053 class SWSPI_HD
00054 {
00055 private:
00056     DigitalInOut* dio;
00057     DigitalOut* sclk;
00058     DigitalOut* cs;
00059     int port;
00060     int bits;
00061     int mode;
00062     int polarity; // idle clock value
00063     int phase; // 0=sample on leading (first) clock edge, 1=trailing (second)
00064     int freq;
00065     
00066 public:
00067     /** Create SWSPI_HD object
00068      *
00069      *  @param dio
00070      *  @param sclk_pin
00071      */
00072     SWSPI_HD(PinName dio_pin, PinName sclk_pin, PinName cs_pin);
00073     
00074     /** Destructor */
00075     ~SWSPI_HD();
00076     
00077     /** Specify SPI format
00078      *
00079      *  @param bits  8 or 16 are typical values
00080      *  @param mode  0, 1, 2, or 3 phase (bit1) and idle clock (bit0)
00081      */
00082     void format(int bits, int mode = 0);
00083     
00084     /** Specify SPI clock frequency
00085      *
00086      *  @param hz  frequency (optional, defaults to 10000000)
00087      */
00088     void frequency(int hz = 10000000);
00089     
00090     /** Write data and read result
00091      *
00092      *  @param value  data to write (see format for bit size)
00093      *  returns value read from device
00094      */
00095     int write(int value);
00096 };
00097 
00098 #endif // SWSPI_HD_H