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_HD.cpp
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 #include <mbed.h> 00024 #include "SWSPI_HD.h" 00025 00026 SWSPI_HD::SWSPI_HD(PinName dio_pin, PinName sclk_pin, PinName cs_pin) 00027 { 00028 dio = new DigitalInOut(dio_pin); 00029 sclk = new DigitalOut(sclk_pin); 00030 cs = new DigitalOut(cs_pin,true); 00031 format(8); 00032 frequency(); 00033 } 00034 00035 SWSPI_HD::~SWSPI_HD() 00036 { 00037 delete dio; 00038 delete sclk; 00039 delete cs; 00040 } 00041 00042 void SWSPI_HD::format(int bits, int mode) 00043 { 00044 this->bits = bits; 00045 this->mode = mode; 00046 polarity = (mode >> 1) & 1; 00047 phase = mode & 1; 00048 sclk->write(polarity); 00049 } 00050 00051 void SWSPI_HD::frequency(int hz) 00052 { 00053 this->freq = hz; 00054 } 00055 00056 int SWSPI_HD::write(int value) 00057 { 00058 int read = 0; 00059 //ensure clock is in right phase to start and disable slave device 00060 cs->write(true); 00061 sclk->write(!polarity); 00062 00063 //Write Out 00064 dio->output();// set as an OUTPUT 00065 cs->write(false);//select slave device (active low) 00066 for (int bit = bits-1; bit >= 0; --bit) 00067 { 00068 dio->write(((value >> bit) & 0x01) != 0); 00069 wait(1.0/freq/2); 00070 sclk->write(!polarity); 00071 wait(1.0/freq/2); 00072 sclk->write(polarity); 00073 } 00074 00075 //Read In 00076 dio->input();// set as an INPUT 00077 00078 for (int bit = bits-1; bit >= 0; --bit) 00079 { 00080 if (phase == 0) 00081 { 00082 if (dio->read()) // was miso 00083 read |= (1 << bit); 00084 } 00085 00086 sclk->write(!polarity); 00087 00088 wait(1.0/freq/2); 00089 00090 if (phase == 1) 00091 { 00092 if (dio->read()) // was miso 00093 read |= (1 << bit); 00094 } 00095 00096 sclk->write(polarity); 00097 00098 wait(1.0/freq/2); 00099 } 00100 cs->write(true); 00101 return read; 00102 } 00103
Generated on Wed Jul 13 2022 16:17:17 by
1.7.2
