sw SPI interface specific for the SOLID Slow control beta!!

Dependents:   sscm SPItest sscm

Fork of SWSPI by Dave Van Wagner

Committer:
wbeaumont
Date:
Fri Oct 23 11:23:00 2015 +0000
Revision:
5:75730aba1ce7
Parent:
4:2a23c789bd1c
production version archiving

Who changed what in which revision?

UserRevisionLine numberNew contents of line
davervw 0:6a500a08c7fd 1 /* SWSPI, Software SPI library
davervw 0:6a500a08c7fd 2 * Copyright (c) 2012-2014, David R. Van Wagner, http://techwithdave.blogspot.com
davervw 0:6a500a08c7fd 3 *
davervw 0:6a500a08c7fd 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
davervw 0:6a500a08c7fd 5 * of this software and associated documentation files (the "Software"), to deal
davervw 0:6a500a08c7fd 6 * in the Software without restriction, including without limitation the rights
davervw 0:6a500a08c7fd 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
davervw 0:6a500a08c7fd 8 * copies of the Software, and to permit persons to whom the Software is
davervw 0:6a500a08c7fd 9 * furnished to do so, subject to the following conditions:
davervw 0:6a500a08c7fd 10 *
davervw 0:6a500a08c7fd 11 * The above copyright notice and this permission notice shall be included in
davervw 0:6a500a08c7fd 12 * all copies or substantial portions of the Software.
davervw 0:6a500a08c7fd 13 *
davervw 0:6a500a08c7fd 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
davervw 0:6a500a08c7fd 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
davervw 0:6a500a08c7fd 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
davervw 0:6a500a08c7fd 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
davervw 0:6a500a08c7fd 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
davervw 0:6a500a08c7fd 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
davervw 0:6a500a08c7fd 20 * THE SOFTWARE.
wbeaumont 5:75730aba1ce7 21 *
wbeaumont 5:75730aba1ce7 22 * Software adapted for the SM1 project of the SOLID colaboration
wbeaumont 5:75730aba1ce7 23 * (C) Wim Beaumont Univeristeit Antwerpen 2014 , 2015
davervw 0:6a500a08c7fd 24 */
davervw 0:6a500a08c7fd 25
davervw 0:6a500a08c7fd 26 #include <mbed.h>
davervw 0:6a500a08c7fd 27 #include "SWSPI.h"
davervw 0:6a500a08c7fd 28
wbeaumont 4:2a23c789bd1c 29 #define SWSPI_SRC_VER "1.22"
wbeaumont 3:9c5ae1507a81 30
wbeaumont 1:02327a96a5e2 31 SWSPI::SWSPI(DigitalOut *mosi_pin, DigitalIn *miso_pin, DigitalOut *sclk_pin)
wbeaumont 4:2a23c789bd1c 32 :getVersion( SWSPI_HDR_VER , SWSPI_SRC_VER , __TIME__, __DATE__)
davervw 0:6a500a08c7fd 33 {
wbeaumont 1:02327a96a5e2 34 mosi = mosi_pin;
wbeaumont 1:02327a96a5e2 35 miso = miso_pin;
wbeaumont 1:02327a96a5e2 36 sclk = sclk_pin;
davervw 0:6a500a08c7fd 37 format(8);
davervw 0:6a500a08c7fd 38 frequency();
davervw 0:6a500a08c7fd 39 }
davervw 0:6a500a08c7fd 40
davervw 0:6a500a08c7fd 41 SWSPI::~SWSPI()
davervw 0:6a500a08c7fd 42 {
wbeaumont 1:02327a96a5e2 43
davervw 0:6a500a08c7fd 44 }
davervw 0:6a500a08c7fd 45
davervw 0:6a500a08c7fd 46 void SWSPI::format(int bits, int mode)
davervw 0:6a500a08c7fd 47 {
davervw 0:6a500a08c7fd 48 this->bits = bits;
davervw 0:6a500a08c7fd 49 this->mode = mode;
wbeaumont 1:02327a96a5e2 50 polarity = !((mode >> 1) & 1);
davervw 0:6a500a08c7fd 51 phase = mode & 1;
davervw 0:6a500a08c7fd 52 sclk->write(polarity);
davervw 0:6a500a08c7fd 53 }
davervw 0:6a500a08c7fd 54
davervw 0:6a500a08c7fd 55 void SWSPI::frequency(int hz)
davervw 0:6a500a08c7fd 56 {
davervw 0:6a500a08c7fd 57 this->freq = hz;
davervw 0:6a500a08c7fd 58 }
davervw 0:6a500a08c7fd 59
wbeaumont 2:598d0028d5d2 60 unsigned int SWSPI::write(unsigned int value)
davervw 0:6a500a08c7fd 61 {
davervw 0:6a500a08c7fd 62 int read = 0;
wbeaumont 2:598d0028d5d2 63 //printf("SPI write %08X ",value);
wbeaumont 1:02327a96a5e2 64 //wait(1.0/freq/2);
davervw 0:6a500a08c7fd 65 for (int bit = bits-1; bit >= 0; --bit)
davervw 0:6a500a08c7fd 66 {
wbeaumont 2:598d0028d5d2 67 mosi->write(((value >> bit) & 0x01) == 0);
davervw 0:6a500a08c7fd 68
davervw 0:6a500a08c7fd 69 if (phase == 0)
davervw 0:6a500a08c7fd 70 {
wbeaumont 2:598d0028d5d2 71 if (!miso->read())
davervw 0:6a500a08c7fd 72 read |= (1 << bit);
davervw 0:6a500a08c7fd 73 }
davervw 0:6a500a08c7fd 74
davervw 0:6a500a08c7fd 75 sclk->write(!polarity);
davervw 0:6a500a08c7fd 76
wbeaumont 1:02327a96a5e2 77 //wait(1.0/freq/2);
davervw 0:6a500a08c7fd 78
davervw 0:6a500a08c7fd 79 if (phase == 1)
davervw 0:6a500a08c7fd 80 {
wbeaumont 2:598d0028d5d2 81 if (!miso->read())
davervw 0:6a500a08c7fd 82 read |= (1 << bit);
davervw 0:6a500a08c7fd 83 }
davervw 0:6a500a08c7fd 84
davervw 0:6a500a08c7fd 85 sclk->write(polarity);
davervw 0:6a500a08c7fd 86
wbeaumont 1:02327a96a5e2 87 //wait(1.0/freq/2);
davervw 0:6a500a08c7fd 88 }
wbeaumont 2:598d0028d5d2 89 //printf(" SPIR reads %08X \n\r" , read);
davervw 0:6a500a08c7fd 90 return read;
davervw 0:6a500a08c7fd 91 }
davervw 0:6a500a08c7fd 92
wbeaumont 3:9c5ae1507a81 93
wbeaumont 3:9c5ae1507a81 94 #include "sscm_comm.h"
wbeaumont 3:9c5ae1507a81 95 /*
wbeaumont 3:9c5ae1507a81 96
wbeaumont 3:9c5ae1507a81 97 unsigned short SWSPI::get_src_version_nr(){
wbeaumont 3:9c5ae1507a81 98 return sscm_comm::get_hex_version_nr(SWSPI_SRC_VER);
wbeaumont 3:9c5ae1507a81 99 }
wbeaumont 3:9c5ae1507a81 100
wbeaumont 3:9c5ae1507a81 101 // returns the version number of hdr of this module
wbeaumont 3:9c5ae1507a81 102 unsigned short SWSPI::get_hdr_version_nr(){
wbeaumont 3:9c5ae1507a81 103 return sscm_comm::get_hex_version_nr(SWSPI_SRC_VER);
wbeaumont 3:9c5ae1507a81 104
wbeaumont 3:9c5ae1507a81 105 }
wbeaumont 3:9c5ae1507a81 106 */