SPI Half-Duplex
SPIHalfDuplex.cpp@0:e9b41fc7d351, 2012-12-06 (annotated)
- Committer:
- mbed_unsupported
- Date:
- Thu Dec 06 12:47:45 2012 +0000
- Revision:
- 0:e9b41fc7d351
SPI Half-Duplex
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_unsupported | 0:e9b41fc7d351 | 1 | /* mbed Microcontroller Library |
mbed_unsupported | 0:e9b41fc7d351 | 2 | * Copyright (c) 2006-2012 ARM Limited |
mbed_unsupported | 0:e9b41fc7d351 | 3 | * |
mbed_unsupported | 0:e9b41fc7d351 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
mbed_unsupported | 0:e9b41fc7d351 | 5 | * of this software and associated documentation files (the "Software"), to deal |
mbed_unsupported | 0:e9b41fc7d351 | 6 | * in the Software without restriction, including without limitation the rights |
mbed_unsupported | 0:e9b41fc7d351 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
mbed_unsupported | 0:e9b41fc7d351 | 8 | * copies of the Software, and to permit persons to whom the Software is |
mbed_unsupported | 0:e9b41fc7d351 | 9 | * furnished to do so, subject to the following conditions: |
mbed_unsupported | 0:e9b41fc7d351 | 10 | * |
mbed_unsupported | 0:e9b41fc7d351 | 11 | * The above copyright notice and this permission notice shall be included in |
mbed_unsupported | 0:e9b41fc7d351 | 12 | * all copies or substantial portions of the Software. |
mbed_unsupported | 0:e9b41fc7d351 | 13 | * |
mbed_unsupported | 0:e9b41fc7d351 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
mbed_unsupported | 0:e9b41fc7d351 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
mbed_unsupported | 0:e9b41fc7d351 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
mbed_unsupported | 0:e9b41fc7d351 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
mbed_unsupported | 0:e9b41fc7d351 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
mbed_unsupported | 0:e9b41fc7d351 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
mbed_unsupported | 0:e9b41fc7d351 | 20 | * SOFTWARE. |
mbed_unsupported | 0:e9b41fc7d351 | 21 | * |
mbed_unsupported | 0:e9b41fc7d351 | 22 | * NOTE: This is an unsupported legacy untested library. |
mbed_unsupported | 0:e9b41fc7d351 | 23 | */ |
mbed_unsupported | 0:e9b41fc7d351 | 24 | #include "SPIHalfDuplex.h" |
mbed_unsupported | 0:e9b41fc7d351 | 25 | |
mbed_unsupported | 0:e9b41fc7d351 | 26 | #if DEVICE_SPI |
mbed_unsupported | 0:e9b41fc7d351 | 27 | |
mbed_unsupported | 0:e9b41fc7d351 | 28 | #include "spi_api.h" |
mbed_unsupported | 0:e9b41fc7d351 | 29 | #include "PinNames.h" |
mbed_unsupported | 0:e9b41fc7d351 | 30 | #include "pinmap.h" |
mbed_unsupported | 0:e9b41fc7d351 | 31 | |
mbed_unsupported | 0:e9b41fc7d351 | 32 | #define GPIO_MODE 0 |
mbed_unsupported | 0:e9b41fc7d351 | 33 | #define SPI_MODE 2 |
mbed_unsupported | 0:e9b41fc7d351 | 34 | |
mbed_unsupported | 0:e9b41fc7d351 | 35 | namespace mbed { |
mbed_unsupported | 0:e9b41fc7d351 | 36 | |
mbed_unsupported | 0:e9b41fc7d351 | 37 | SPIHalfDuplex::SPIHalfDuplex(PinName mosi, PinName miso, PinName sclk, |
mbed_unsupported | 0:e9b41fc7d351 | 38 | const char *name) : SPI(mosi, miso, sclk, name) { |
mbed_unsupported | 0:e9b41fc7d351 | 39 | _mosi = mosi; |
mbed_unsupported | 0:e9b41fc7d351 | 40 | _miso = miso; |
mbed_unsupported | 0:e9b41fc7d351 | 41 | _sbits = _bits; |
mbed_unsupported | 0:e9b41fc7d351 | 42 | } |
mbed_unsupported | 0:e9b41fc7d351 | 43 | |
mbed_unsupported | 0:e9b41fc7d351 | 44 | void SPIHalfDuplex::slave_format(int sbits) { |
mbed_unsupported | 0:e9b41fc7d351 | 45 | _sbits = sbits; |
mbed_unsupported | 0:e9b41fc7d351 | 46 | } |
mbed_unsupported | 0:e9b41fc7d351 | 47 | |
mbed_unsupported | 0:e9b41fc7d351 | 48 | int SPIHalfDuplex::write(int value) { |
mbed_unsupported | 0:e9b41fc7d351 | 49 | int t_bits = _bits; |
mbed_unsupported | 0:e9b41fc7d351 | 50 | pin_function(_mosi, SPI_MODE); |
mbed_unsupported | 0:e9b41fc7d351 | 51 | int ret_val = SPI::write(value); |
mbed_unsupported | 0:e9b41fc7d351 | 52 | if(ret_val != value) { |
mbed_unsupported | 0:e9b41fc7d351 | 53 | return -1; |
mbed_unsupported | 0:e9b41fc7d351 | 54 | } |
mbed_unsupported | 0:e9b41fc7d351 | 55 | format(_sbits, _mode); |
mbed_unsupported | 0:e9b41fc7d351 | 56 | pin_function(_mosi, GPIO_MODE); |
mbed_unsupported | 0:e9b41fc7d351 | 57 | ret_val = SPI::write(0x55); |
mbed_unsupported | 0:e9b41fc7d351 | 58 | format(t_bits, _mode); |
mbed_unsupported | 0:e9b41fc7d351 | 59 | pin_function(_mosi, SPI_MODE); |
mbed_unsupported | 0:e9b41fc7d351 | 60 | return ret_val; |
mbed_unsupported | 0:e9b41fc7d351 | 61 | } |
mbed_unsupported | 0:e9b41fc7d351 | 62 | |
mbed_unsupported | 0:e9b41fc7d351 | 63 | } // end namespace mbed |
mbed_unsupported | 0:e9b41fc7d351 | 64 | |
mbed_unsupported | 0:e9b41fc7d351 | 65 | #endif |