mbed library sources
Fork of mbed-src by
api/SPI.h@307:d33324c3b5a2, 2014-09-03 (annotated)
- Committer:
- mbed_official
- Date:
- Wed Sep 03 13:30:08 2014 +0100
- Revision:
- 307:d33324c3b5a2
- Parent:
- 212:34d62c0b2af6
Synchronized with git revision 7aedd02f25c846ee1f22665ecc167585a8ad56bb
Full URL: https://github.com/mbedmicro/mbed/commit/7aedd02f25c846ee1f22665ecc167585a8ad56bb/
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 13:0645d8841f51 | 1 | /* mbed Microcontroller Library |
bogdanm | 13:0645d8841f51 | 2 | * Copyright (c) 2006-2013 ARM Limited |
bogdanm | 13:0645d8841f51 | 3 | * |
bogdanm | 13:0645d8841f51 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
bogdanm | 13:0645d8841f51 | 5 | * you may not use this file except in compliance with the License. |
bogdanm | 13:0645d8841f51 | 6 | * You may obtain a copy of the License at |
bogdanm | 13:0645d8841f51 | 7 | * |
bogdanm | 13:0645d8841f51 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
bogdanm | 13:0645d8841f51 | 9 | * |
bogdanm | 13:0645d8841f51 | 10 | * Unless required by applicable law or agreed to in writing, software |
bogdanm | 13:0645d8841f51 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
bogdanm | 13:0645d8841f51 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
bogdanm | 13:0645d8841f51 | 13 | * See the License for the specific language governing permissions and |
bogdanm | 13:0645d8841f51 | 14 | * limitations under the License. |
bogdanm | 13:0645d8841f51 | 15 | */ |
bogdanm | 13:0645d8841f51 | 16 | #ifndef MBED_SPI_H |
bogdanm | 13:0645d8841f51 | 17 | #define MBED_SPI_H |
bogdanm | 13:0645d8841f51 | 18 | |
bogdanm | 13:0645d8841f51 | 19 | #include "platform.h" |
bogdanm | 13:0645d8841f51 | 20 | |
bogdanm | 13:0645d8841f51 | 21 | #if DEVICE_SPI |
bogdanm | 13:0645d8841f51 | 22 | |
bogdanm | 13:0645d8841f51 | 23 | #include "spi_api.h" |
bogdanm | 13:0645d8841f51 | 24 | |
bogdanm | 13:0645d8841f51 | 25 | namespace mbed { |
bogdanm | 13:0645d8841f51 | 26 | |
bogdanm | 13:0645d8841f51 | 27 | /** A SPI Master, used for communicating with SPI slave devices |
bogdanm | 13:0645d8841f51 | 28 | * |
bogdanm | 13:0645d8841f51 | 29 | * The default format is set to 8-bits, mode 0, and a clock frequency of 1MHz |
bogdanm | 13:0645d8841f51 | 30 | * |
bogdanm | 13:0645d8841f51 | 31 | * Most SPI devices will also require Chip Select and Reset signals. These |
bogdanm | 13:0645d8841f51 | 32 | * can be controlled using <DigitalOut> pins |
bogdanm | 13:0645d8841f51 | 33 | * |
bogdanm | 13:0645d8841f51 | 34 | * Example: |
bogdanm | 13:0645d8841f51 | 35 | * @code |
bogdanm | 13:0645d8841f51 | 36 | * // Send a byte to a SPI slave, and record the response |
bogdanm | 13:0645d8841f51 | 37 | * |
bogdanm | 13:0645d8841f51 | 38 | * #include "mbed.h" |
bogdanm | 13:0645d8841f51 | 39 | * |
bogdanm | 13:0645d8841f51 | 40 | * SPI device(p5, p6, p7); // mosi, miso, sclk |
bogdanm | 13:0645d8841f51 | 41 | * |
bogdanm | 13:0645d8841f51 | 42 | * int main() { |
bogdanm | 13:0645d8841f51 | 43 | * int response = device.write(0xFF); |
bogdanm | 13:0645d8841f51 | 44 | * } |
bogdanm | 13:0645d8841f51 | 45 | * @endcode |
bogdanm | 13:0645d8841f51 | 46 | */ |
bogdanm | 13:0645d8841f51 | 47 | class SPI { |
bogdanm | 13:0645d8841f51 | 48 | |
bogdanm | 13:0645d8841f51 | 49 | public: |
bogdanm | 13:0645d8841f51 | 50 | |
bogdanm | 13:0645d8841f51 | 51 | /** Create a SPI master connected to the specified pins |
bogdanm | 13:0645d8841f51 | 52 | * |
bogdanm | 13:0645d8841f51 | 53 | * Pin Options: |
bogdanm | 13:0645d8841f51 | 54 | * (5, 6, 7) or (11, 12, 13) |
bogdanm | 13:0645d8841f51 | 55 | * |
bogdanm | 13:0645d8841f51 | 56 | * mosi or miso can be specfied as NC if not used |
bogdanm | 13:0645d8841f51 | 57 | * |
bogdanm | 13:0645d8841f51 | 58 | * @param mosi SPI Master Out, Slave In pin |
bogdanm | 13:0645d8841f51 | 59 | * @param miso SPI Master In, Slave Out pin |
bogdanm | 13:0645d8841f51 | 60 | * @param sclk SPI Clock pin |
bogdanm | 13:0645d8841f51 | 61 | */ |
mbed_official | 50:b08ceb75017d | 62 | SPI(PinName mosi, PinName miso, PinName sclk, PinName _unused=NC); |
bogdanm | 13:0645d8841f51 | 63 | |
bogdanm | 13:0645d8841f51 | 64 | /** Configure the data transmission format |
bogdanm | 13:0645d8841f51 | 65 | * |
bogdanm | 13:0645d8841f51 | 66 | * @param bits Number of bits per SPI frame (4 - 16) |
bogdanm | 13:0645d8841f51 | 67 | * @param mode Clock polarity and phase mode (0 - 3) |
bogdanm | 13:0645d8841f51 | 68 | * |
bogdanm | 13:0645d8841f51 | 69 | * @code |
bogdanm | 13:0645d8841f51 | 70 | * mode | POL PHA |
bogdanm | 13:0645d8841f51 | 71 | * -----+-------- |
bogdanm | 13:0645d8841f51 | 72 | * 0 | 0 0 |
bogdanm | 13:0645d8841f51 | 73 | * 1 | 0 1 |
bogdanm | 13:0645d8841f51 | 74 | * 2 | 1 0 |
bogdanm | 13:0645d8841f51 | 75 | * 3 | 1 1 |
bogdanm | 13:0645d8841f51 | 76 | * @endcode |
bogdanm | 13:0645d8841f51 | 77 | */ |
bogdanm | 13:0645d8841f51 | 78 | void format(int bits, int mode = 0); |
bogdanm | 13:0645d8841f51 | 79 | |
bogdanm | 13:0645d8841f51 | 80 | /** Set the spi bus clock frequency |
bogdanm | 13:0645d8841f51 | 81 | * |
bogdanm | 13:0645d8841f51 | 82 | * @param hz SCLK frequency in hz (default = 1MHz) |
bogdanm | 13:0645d8841f51 | 83 | */ |
bogdanm | 13:0645d8841f51 | 84 | void frequency(int hz = 1000000); |
bogdanm | 13:0645d8841f51 | 85 | |
bogdanm | 13:0645d8841f51 | 86 | /** Write to the SPI Slave and return the response |
bogdanm | 13:0645d8841f51 | 87 | * |
bogdanm | 13:0645d8841f51 | 88 | * @param value Data to be sent to the SPI slave |
bogdanm | 13:0645d8841f51 | 89 | * |
bogdanm | 13:0645d8841f51 | 90 | * @returns |
bogdanm | 13:0645d8841f51 | 91 | * Response from the SPI slave |
bogdanm | 13:0645d8841f51 | 92 | */ |
bogdanm | 13:0645d8841f51 | 93 | virtual int write(int value); |
bogdanm | 13:0645d8841f51 | 94 | |
mbed_official | 212:34d62c0b2af6 | 95 | public: |
mbed_official | 212:34d62c0b2af6 | 96 | virtual ~SPI() { |
mbed_official | 212:34d62c0b2af6 | 97 | } |
mbed_official | 212:34d62c0b2af6 | 98 | |
bogdanm | 13:0645d8841f51 | 99 | protected: |
bogdanm | 13:0645d8841f51 | 100 | spi_t _spi; |
bogdanm | 13:0645d8841f51 | 101 | |
bogdanm | 13:0645d8841f51 | 102 | void aquire(void); |
bogdanm | 13:0645d8841f51 | 103 | static SPI *_owner; |
bogdanm | 13:0645d8841f51 | 104 | int _bits; |
bogdanm | 13:0645d8841f51 | 105 | int _mode; |
bogdanm | 13:0645d8841f51 | 106 | int _hz; |
bogdanm | 13:0645d8841f51 | 107 | }; |
bogdanm | 13:0645d8841f51 | 108 | |
bogdanm | 13:0645d8841f51 | 109 | } // namespace mbed |
bogdanm | 13:0645d8841f51 | 110 | |
bogdanm | 13:0645d8841f51 | 111 | #endif |
bogdanm | 13:0645d8841f51 | 112 | |
bogdanm | 13:0645d8841f51 | 113 | #endif |