mbed library sources
Fork of mbed-src by
api/SPISlave.h@552:a1b9575155a3, 2015-05-27 (annotated)
- Committer:
- mbed_official
- Date:
- Wed May 27 13:30:08 2015 +0100
- Revision:
- 552:a1b9575155a3
- Parent:
- 13:0645d8841f51
Synchronized with git revision a74a8f14fd8c4bf3dc09980c4bf9498ebcf4c207
Full URL: https://github.com/mbedmicro/mbed/commit/a74a8f14fd8c4bf3dc09980c4bf9498ebcf4c207/
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_SPISLAVE_H |
bogdanm | 13:0645d8841f51 | 17 | #define MBED_SPISLAVE_H |
bogdanm | 13:0645d8841f51 | 18 | |
bogdanm | 13:0645d8841f51 | 19 | #include "platform.h" |
bogdanm | 13:0645d8841f51 | 20 | |
bogdanm | 13:0645d8841f51 | 21 | #if DEVICE_SPISLAVE |
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 slave, used for communicating with a SPI Master device |
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 | * Example: |
bogdanm | 13:0645d8841f51 | 32 | * @code |
bogdanm | 13:0645d8841f51 | 33 | * // Reply to a SPI master as slave |
bogdanm | 13:0645d8841f51 | 34 | * |
bogdanm | 13:0645d8841f51 | 35 | * #include "mbed.h" |
bogdanm | 13:0645d8841f51 | 36 | * |
bogdanm | 13:0645d8841f51 | 37 | * SPISlave device(p5, p6, p7, p8); // mosi, miso, sclk, ssel |
bogdanm | 13:0645d8841f51 | 38 | * |
bogdanm | 13:0645d8841f51 | 39 | * int main() { |
bogdanm | 13:0645d8841f51 | 40 | * device.reply(0x00); // Prime SPI with first reply |
bogdanm | 13:0645d8841f51 | 41 | * while(1) { |
bogdanm | 13:0645d8841f51 | 42 | * if(device.receive()) { |
bogdanm | 13:0645d8841f51 | 43 | * int v = device.read(); // Read byte from master |
bogdanm | 13:0645d8841f51 | 44 | * v = (v + 1) % 0x100; // Add one to it, modulo 256 |
bogdanm | 13:0645d8841f51 | 45 | * device.reply(v); // Make this the next reply |
bogdanm | 13:0645d8841f51 | 46 | * } |
bogdanm | 13:0645d8841f51 | 47 | * } |
bogdanm | 13:0645d8841f51 | 48 | * } |
bogdanm | 13:0645d8841f51 | 49 | * @endcode |
bogdanm | 13:0645d8841f51 | 50 | */ |
bogdanm | 13:0645d8841f51 | 51 | class SPISlave { |
bogdanm | 13:0645d8841f51 | 52 | |
bogdanm | 13:0645d8841f51 | 53 | public: |
bogdanm | 13:0645d8841f51 | 54 | |
bogdanm | 13:0645d8841f51 | 55 | /** Create a SPI slave connected to the specified pins |
bogdanm | 13:0645d8841f51 | 56 | * |
bogdanm | 13:0645d8841f51 | 57 | * mosi or miso can be specfied as NC if not used |
bogdanm | 13:0645d8841f51 | 58 | * |
bogdanm | 13:0645d8841f51 | 59 | * @param mosi SPI Master Out, Slave In pin |
bogdanm | 13:0645d8841f51 | 60 | * @param miso SPI Master In, Slave Out pin |
bogdanm | 13:0645d8841f51 | 61 | * @param sclk SPI Clock pin |
bogdanm | 13:0645d8841f51 | 62 | * @param ssel SPI chip select pin |
bogdanm | 13:0645d8841f51 | 63 | */ |
bogdanm | 13:0645d8841f51 | 64 | SPISlave(PinName mosi, PinName miso, PinName sclk, PinName ssel); |
bogdanm | 13:0645d8841f51 | 65 | |
bogdanm | 13:0645d8841f51 | 66 | /** Configure the data transmission format |
bogdanm | 13:0645d8841f51 | 67 | * |
bogdanm | 13:0645d8841f51 | 68 | * @param bits Number of bits per SPI frame (4 - 16) |
bogdanm | 13:0645d8841f51 | 69 | * @param mode Clock polarity and phase mode (0 - 3) |
bogdanm | 13:0645d8841f51 | 70 | * |
bogdanm | 13:0645d8841f51 | 71 | * @code |
bogdanm | 13:0645d8841f51 | 72 | * mode | POL PHA |
bogdanm | 13:0645d8841f51 | 73 | * -----+-------- |
bogdanm | 13:0645d8841f51 | 74 | * 0 | 0 0 |
bogdanm | 13:0645d8841f51 | 75 | * 1 | 0 1 |
bogdanm | 13:0645d8841f51 | 76 | * 2 | 1 0 |
bogdanm | 13:0645d8841f51 | 77 | * 3 | 1 1 |
bogdanm | 13:0645d8841f51 | 78 | * @endcode |
bogdanm | 13:0645d8841f51 | 79 | */ |
bogdanm | 13:0645d8841f51 | 80 | void format(int bits, int mode = 0); |
bogdanm | 13:0645d8841f51 | 81 | |
bogdanm | 13:0645d8841f51 | 82 | /** Set the spi bus clock frequency |
bogdanm | 13:0645d8841f51 | 83 | * |
bogdanm | 13:0645d8841f51 | 84 | * @param hz SCLK frequency in hz (default = 1MHz) |
bogdanm | 13:0645d8841f51 | 85 | */ |
bogdanm | 13:0645d8841f51 | 86 | void frequency(int hz = 1000000); |
bogdanm | 13:0645d8841f51 | 87 | |
bogdanm | 13:0645d8841f51 | 88 | /** Polls the SPI to see if data has been received |
bogdanm | 13:0645d8841f51 | 89 | * |
bogdanm | 13:0645d8841f51 | 90 | * @returns |
bogdanm | 13:0645d8841f51 | 91 | * 0 if no data, |
bogdanm | 13:0645d8841f51 | 92 | * 1 otherwise |
bogdanm | 13:0645d8841f51 | 93 | */ |
bogdanm | 13:0645d8841f51 | 94 | int receive(void); |
bogdanm | 13:0645d8841f51 | 95 | |
bogdanm | 13:0645d8841f51 | 96 | /** Retrieve data from receive buffer as slave |
bogdanm | 13:0645d8841f51 | 97 | * |
bogdanm | 13:0645d8841f51 | 98 | * @returns |
bogdanm | 13:0645d8841f51 | 99 | * the data in the receive buffer |
bogdanm | 13:0645d8841f51 | 100 | */ |
bogdanm | 13:0645d8841f51 | 101 | int read(void); |
bogdanm | 13:0645d8841f51 | 102 | |
bogdanm | 13:0645d8841f51 | 103 | /** Fill the transmission buffer with the value to be written out |
bogdanm | 13:0645d8841f51 | 104 | * as slave on the next received message from the master. |
bogdanm | 13:0645d8841f51 | 105 | * |
bogdanm | 13:0645d8841f51 | 106 | * @param value the data to be transmitted next |
bogdanm | 13:0645d8841f51 | 107 | */ |
bogdanm | 13:0645d8841f51 | 108 | void reply(int value); |
bogdanm | 13:0645d8841f51 | 109 | |
bogdanm | 13:0645d8841f51 | 110 | protected: |
bogdanm | 13:0645d8841f51 | 111 | spi_t _spi; |
bogdanm | 13:0645d8841f51 | 112 | |
bogdanm | 13:0645d8841f51 | 113 | int _bits; |
bogdanm | 13:0645d8841f51 | 114 | int _mode; |
bogdanm | 13:0645d8841f51 | 115 | int _hz; |
bogdanm | 13:0645d8841f51 | 116 | }; |
bogdanm | 13:0645d8841f51 | 117 | |
bogdanm | 13:0645d8841f51 | 118 | } // namespace mbed |
bogdanm | 13:0645d8841f51 | 119 | |
bogdanm | 13:0645d8841f51 | 120 | #endif |
bogdanm | 13:0645d8841f51 | 121 | |
bogdanm | 13:0645d8841f51 | 122 | #endif |