wimbeaumont Project / SWSPI

Dependents:   sscm SPItest sscm

Fork of SWSPI by Dave Van Wagner

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SWSPI.cpp Source File

SWSPI.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  * Software adapted for the SM1 project of the SOLID colaboration 
00023  * (C) Wim Beaumont Univeristeit Antwerpen  2014 , 2015
00024  */
00025 
00026 #include <mbed.h>
00027 #include "SWSPI.h"
00028 
00029 #define SWSPI_SRC_VER  "1.22" 
00030 
00031 SWSPI::SWSPI(DigitalOut *mosi_pin, DigitalIn *miso_pin, DigitalOut *sclk_pin)
00032  :getVersion( SWSPI_HDR_VER , SWSPI_SRC_VER , __TIME__, __DATE__)
00033 {
00034     mosi = mosi_pin;
00035     miso = miso_pin;
00036     sclk = sclk_pin;
00037     format(8);
00038     frequency();
00039 }
00040 
00041 SWSPI::~SWSPI()
00042 {
00043     
00044 }
00045 
00046 void SWSPI::format(int bits, int mode)
00047 {
00048     this->bits = bits;
00049     this->mode = mode;
00050     polarity = !((mode >> 1) & 1);
00051     phase = mode & 1;
00052     sclk->write(polarity);
00053 }
00054 
00055 void SWSPI::frequency(int hz)
00056 {
00057     this->freq = hz;
00058 }
00059 
00060 unsigned int  SWSPI::write(unsigned int  value)
00061 {
00062     int read = 0;
00063     //printf("SPI write %08X ",value);
00064     //wait(1.0/freq/2);
00065     for (int bit = bits-1; bit >= 0; --bit)
00066     {
00067         mosi->write(((value >> bit) & 0x01) == 0);
00068 
00069         if (phase == 0)
00070         {
00071             if (!miso->read())
00072                 read |= (1 << bit);
00073         }
00074 
00075         sclk->write(!polarity);
00076 
00077         //wait(1.0/freq/2);
00078 
00079         if (phase == 1)
00080         {
00081             if (!miso->read())
00082                 read |= (1 << bit);
00083         }
00084 
00085         sclk->write(polarity);
00086 
00087         //wait(1.0/freq/2);
00088     }
00089     //printf("  SPIR reads %08X \n\r" , read);
00090     return read;
00091 }
00092 
00093 
00094 #include "sscm_comm.h" 
00095 /*
00096  
00097 unsigned short SWSPI::get_src_version_nr(){
00098        return sscm_comm::get_hex_version_nr(SWSPI_SRC_VER);
00099 }    
00100 
00101 // returns the version number of hdr of this  module 
00102 unsigned short  SWSPI::get_hdr_version_nr(){
00103       return sscm_comm::get_hex_version_nr(SWSPI_SRC_VER);
00104     
00105 }
00106 */