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@1:17d758fe52f8, 2018-06-19 (annotated)
- Committer:
- martinsimpson
- Date:
- Tue Jun 19 09:55:18 2018 +0000
- Revision:
- 1:17d758fe52f8
- Child:
- 2:70039cc8ba02
Fork of SWSPI first commit
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| martinsimpson | 1:17d758fe52f8 | 1 | /* SWSPI, Software SPI library |
| martinsimpson | 1:17d758fe52f8 | 2 | * Copyright (c) 2012-2014, David R. Van Wagner, http://techwithdave.blogspot.com |
| martinsimpson | 1:17d758fe52f8 | 3 | * |
| martinsimpson | 1:17d758fe52f8 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| martinsimpson | 1:17d758fe52f8 | 5 | * of this software and associated documentation files (the "Software"), to deal |
| martinsimpson | 1:17d758fe52f8 | 6 | * in the Software without restriction, including without limitation the rights |
| martinsimpson | 1:17d758fe52f8 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| martinsimpson | 1:17d758fe52f8 | 8 | * copies of the Software, and to permit persons to whom the Software is |
| martinsimpson | 1:17d758fe52f8 | 9 | * furnished to do so, subject to the following conditions: |
| martinsimpson | 1:17d758fe52f8 | 10 | * |
| martinsimpson | 1:17d758fe52f8 | 11 | * The above copyright notice and this permission notice shall be included in |
| martinsimpson | 1:17d758fe52f8 | 12 | * all copies or substantial portions of the Software. |
| martinsimpson | 1:17d758fe52f8 | 13 | * |
| martinsimpson | 1:17d758fe52f8 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| martinsimpson | 1:17d758fe52f8 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| martinsimpson | 1:17d758fe52f8 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| martinsimpson | 1:17d758fe52f8 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| martinsimpson | 1:17d758fe52f8 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| martinsimpson | 1:17d758fe52f8 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| martinsimpson | 1:17d758fe52f8 | 20 | * THE SOFTWARE. |
| martinsimpson | 1:17d758fe52f8 | 21 | */ |
| martinsimpson | 1:17d758fe52f8 | 22 | |
| martinsimpson | 1:17d758fe52f8 | 23 | #include <mbed.h> |
| martinsimpson | 1:17d758fe52f8 | 24 | #include "SWSPI_HD.h" |
| martinsimpson | 1:17d758fe52f8 | 25 | |
| martinsimpson | 1:17d758fe52f8 | 26 | SWSPI_HD::SWSPI_HD(PinName dio_pin, PinName sclk_pin) |
| martinsimpson | 1:17d758fe52f8 | 27 | { |
| martinsimpson | 1:17d758fe52f8 | 28 | dio = new DigitalInOut(dio_pin); |
| martinsimpson | 1:17d758fe52f8 | 29 | sclk = new DigitalOut(sclk_pin); |
| martinsimpson | 1:17d758fe52f8 | 30 | format(8); |
| martinsimpson | 1:17d758fe52f8 | 31 | frequency(); |
| martinsimpson | 1:17d758fe52f8 | 32 | } |
| martinsimpson | 1:17d758fe52f8 | 33 | |
| martinsimpson | 1:17d758fe52f8 | 34 | SWSPI_HD::~SWSPI_HD() |
| martinsimpson | 1:17d758fe52f8 | 35 | { |
| martinsimpson | 1:17d758fe52f8 | 36 | delete dio; |
| martinsimpson | 1:17d758fe52f8 | 37 | delete sclk; |
| martinsimpson | 1:17d758fe52f8 | 38 | } |
| martinsimpson | 1:17d758fe52f8 | 39 | |
| martinsimpson | 1:17d758fe52f8 | 40 | void SWSPI_HD::format(int bits, int mode) |
| martinsimpson | 1:17d758fe52f8 | 41 | { |
| martinsimpson | 1:17d758fe52f8 | 42 | this->bits = bits; |
| martinsimpson | 1:17d758fe52f8 | 43 | this->mode = mode; |
| martinsimpson | 1:17d758fe52f8 | 44 | polarity = (mode >> 1) & 1; |
| martinsimpson | 1:17d758fe52f8 | 45 | phase = mode & 1; |
| martinsimpson | 1:17d758fe52f8 | 46 | sclk->write(polarity); |
| martinsimpson | 1:17d758fe52f8 | 47 | } |
| martinsimpson | 1:17d758fe52f8 | 48 | |
| martinsimpson | 1:17d758fe52f8 | 49 | void SWSPI_HD::frequency(int hz) |
| martinsimpson | 1:17d758fe52f8 | 50 | { |
| martinsimpson | 1:17d758fe52f8 | 51 | this->freq = hz; |
| martinsimpson | 1:17d758fe52f8 | 52 | } |
| martinsimpson | 1:17d758fe52f8 | 53 | |
| martinsimpson | 1:17d758fe52f8 | 54 | int SWSPI_HD::write(int value) |
| martinsimpson | 1:17d758fe52f8 | 55 | { |
| martinsimpson | 1:17d758fe52f8 | 56 | int read = 0; |
| martinsimpson | 1:17d758fe52f8 | 57 | for (int bit = bits-1; bit >= 0; --bit) |
| martinsimpson | 1:17d758fe52f8 | 58 | { |
| martinsimpson | 1:17d758fe52f8 | 59 | dio->output();// set as an OUTPUT |
| martinsimpson | 1:17d758fe52f8 | 60 | |
| martinsimpson | 1:17d758fe52f8 | 61 | dio->write(((value >> bit) & 0x01) != 0); |
| martinsimpson | 1:17d758fe52f8 | 62 | |
| martinsimpson | 1:17d758fe52f8 | 63 | dio->input();// set as an INPUT |
| martinsimpson | 1:17d758fe52f8 | 64 | |
| martinsimpson | 1:17d758fe52f8 | 65 | if (phase == 0) |
| martinsimpson | 1:17d758fe52f8 | 66 | { |
| martinsimpson | 1:17d758fe52f8 | 67 | |
| martinsimpson | 1:17d758fe52f8 | 68 | if (dio->read()) // was miso |
| martinsimpson | 1:17d758fe52f8 | 69 | read |= (1 << bit); |
| martinsimpson | 1:17d758fe52f8 | 70 | } |
| martinsimpson | 1:17d758fe52f8 | 71 | |
| martinsimpson | 1:17d758fe52f8 | 72 | sclk->write(!polarity); |
| martinsimpson | 1:17d758fe52f8 | 73 | |
| martinsimpson | 1:17d758fe52f8 | 74 | wait(1.0/freq/2); |
| martinsimpson | 1:17d758fe52f8 | 75 | |
| martinsimpson | 1:17d758fe52f8 | 76 | if (phase == 1) |
| martinsimpson | 1:17d758fe52f8 | 77 | { |
| martinsimpson | 1:17d758fe52f8 | 78 | if (dio->read()) // was miso |
| martinsimpson | 1:17d758fe52f8 | 79 | read |= (1 << bit); |
| martinsimpson | 1:17d758fe52f8 | 80 | } |
| martinsimpson | 1:17d758fe52f8 | 81 | |
| martinsimpson | 1:17d758fe52f8 | 82 | sclk->write(polarity); |
| martinsimpson | 1:17d758fe52f8 | 83 | |
| martinsimpson | 1:17d758fe52f8 | 84 | wait(1.0/freq/2); |
| martinsimpson | 1:17d758fe52f8 | 85 | } |
| martinsimpson | 1:17d758fe52f8 | 86 | |
| martinsimpson | 1:17d758fe52f8 | 87 | return read; |
| martinsimpson | 1:17d758fe52f8 | 88 | } |
| martinsimpson | 1:17d758fe52f8 | 89 |
