4545
Fork of mbed-dev by
drivers/SPISlave.h@149:156823d33999, 2016-10-28 (annotated)
- Committer:
- <>
- Date:
- Fri Oct 28 11:17:30 2016 +0100
- Revision:
- 149:156823d33999
This updates the lib to the mbed lib v128
NOTE: This release includes a restructuring of the file and directory locations and thus some
include paths in your code may need updating accordingly.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 149:156823d33999 | 1 | /* mbed Microcontroller Library |
<> | 149:156823d33999 | 2 | * Copyright (c) 2006-2013 ARM Limited |
<> | 149:156823d33999 | 3 | * |
<> | 149:156823d33999 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
<> | 149:156823d33999 | 5 | * you may not use this file except in compliance with the License. |
<> | 149:156823d33999 | 6 | * You may obtain a copy of the License at |
<> | 149:156823d33999 | 7 | * |
<> | 149:156823d33999 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
<> | 149:156823d33999 | 9 | * |
<> | 149:156823d33999 | 10 | * Unless required by applicable law or agreed to in writing, software |
<> | 149:156823d33999 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
<> | 149:156823d33999 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
<> | 149:156823d33999 | 13 | * See the License for the specific language governing permissions and |
<> | 149:156823d33999 | 14 | * limitations under the License. |
<> | 149:156823d33999 | 15 | */ |
<> | 149:156823d33999 | 16 | #ifndef MBED_SPISLAVE_H |
<> | 149:156823d33999 | 17 | #define MBED_SPISLAVE_H |
<> | 149:156823d33999 | 18 | |
<> | 149:156823d33999 | 19 | #include "platform/platform.h" |
<> | 149:156823d33999 | 20 | |
<> | 149:156823d33999 | 21 | #if DEVICE_SPISLAVE |
<> | 149:156823d33999 | 22 | |
<> | 149:156823d33999 | 23 | #include "hal/spi_api.h" |
<> | 149:156823d33999 | 24 | |
<> | 149:156823d33999 | 25 | namespace mbed { |
<> | 149:156823d33999 | 26 | /** \addtogroup drivers */ |
<> | 149:156823d33999 | 27 | /** @{*/ |
<> | 149:156823d33999 | 28 | |
<> | 149:156823d33999 | 29 | /** A SPI slave, used for communicating with a SPI Master device |
<> | 149:156823d33999 | 30 | * |
<> | 149:156823d33999 | 31 | * The default format is set to 8-bits, mode 0, and a clock frequency of 1MHz |
<> | 149:156823d33999 | 32 | * |
<> | 149:156823d33999 | 33 | * @Note Synchronization level: Not protected |
<> | 149:156823d33999 | 34 | * |
<> | 149:156823d33999 | 35 | * Example: |
<> | 149:156823d33999 | 36 | * @code |
<> | 149:156823d33999 | 37 | * // Reply to a SPI master as slave |
<> | 149:156823d33999 | 38 | * |
<> | 149:156823d33999 | 39 | * #include "mbed.h" |
<> | 149:156823d33999 | 40 | * |
<> | 149:156823d33999 | 41 | * SPISlave device(p5, p6, p7, p8); // mosi, miso, sclk, ssel |
<> | 149:156823d33999 | 42 | * |
<> | 149:156823d33999 | 43 | * int main() { |
<> | 149:156823d33999 | 44 | * device.reply(0x00); // Prime SPI with first reply |
<> | 149:156823d33999 | 45 | * while(1) { |
<> | 149:156823d33999 | 46 | * if(device.receive()) { |
<> | 149:156823d33999 | 47 | * int v = device.read(); // Read byte from master |
<> | 149:156823d33999 | 48 | * v = (v + 1) % 0x100; // Add one to it, modulo 256 |
<> | 149:156823d33999 | 49 | * device.reply(v); // Make this the next reply |
<> | 149:156823d33999 | 50 | * } |
<> | 149:156823d33999 | 51 | * } |
<> | 149:156823d33999 | 52 | * } |
<> | 149:156823d33999 | 53 | * @endcode |
<> | 149:156823d33999 | 54 | */ |
<> | 149:156823d33999 | 55 | class SPISlave { |
<> | 149:156823d33999 | 56 | |
<> | 149:156823d33999 | 57 | public: |
<> | 149:156823d33999 | 58 | |
<> | 149:156823d33999 | 59 | /** Create a SPI slave connected to the specified pins |
<> | 149:156823d33999 | 60 | * |
<> | 149:156823d33999 | 61 | * mosi or miso can be specfied as NC if not used |
<> | 149:156823d33999 | 62 | * |
<> | 149:156823d33999 | 63 | * @param mosi SPI Master Out, Slave In pin |
<> | 149:156823d33999 | 64 | * @param miso SPI Master In, Slave Out pin |
<> | 149:156823d33999 | 65 | * @param sclk SPI Clock pin |
<> | 149:156823d33999 | 66 | * @param ssel SPI chip select pin |
<> | 149:156823d33999 | 67 | */ |
<> | 149:156823d33999 | 68 | SPISlave(PinName mosi, PinName miso, PinName sclk, PinName ssel); |
<> | 149:156823d33999 | 69 | |
<> | 149:156823d33999 | 70 | /** Configure the data transmission format |
<> | 149:156823d33999 | 71 | * |
<> | 149:156823d33999 | 72 | * @param bits Number of bits per SPI frame (4 - 16) |
<> | 149:156823d33999 | 73 | * @param mode Clock polarity and phase mode (0 - 3) |
<> | 149:156823d33999 | 74 | * |
<> | 149:156823d33999 | 75 | * @code |
<> | 149:156823d33999 | 76 | * mode | POL PHA |
<> | 149:156823d33999 | 77 | * -----+-------- |
<> | 149:156823d33999 | 78 | * 0 | 0 0 |
<> | 149:156823d33999 | 79 | * 1 | 0 1 |
<> | 149:156823d33999 | 80 | * 2 | 1 0 |
<> | 149:156823d33999 | 81 | * 3 | 1 1 |
<> | 149:156823d33999 | 82 | * @endcode |
<> | 149:156823d33999 | 83 | */ |
<> | 149:156823d33999 | 84 | void format(int bits, int mode = 0); |
<> | 149:156823d33999 | 85 | |
<> | 149:156823d33999 | 86 | /** Set the spi bus clock frequency |
<> | 149:156823d33999 | 87 | * |
<> | 149:156823d33999 | 88 | * @param hz SCLK frequency in hz (default = 1MHz) |
<> | 149:156823d33999 | 89 | */ |
<> | 149:156823d33999 | 90 | void frequency(int hz = 1000000); |
<> | 149:156823d33999 | 91 | |
<> | 149:156823d33999 | 92 | /** Polls the SPI to see if data has been received |
<> | 149:156823d33999 | 93 | * |
<> | 149:156823d33999 | 94 | * @returns |
<> | 149:156823d33999 | 95 | * 0 if no data, |
<> | 149:156823d33999 | 96 | * 1 otherwise |
<> | 149:156823d33999 | 97 | */ |
<> | 149:156823d33999 | 98 | int receive(void); |
<> | 149:156823d33999 | 99 | |
<> | 149:156823d33999 | 100 | /** Retrieve data from receive buffer as slave |
<> | 149:156823d33999 | 101 | * |
<> | 149:156823d33999 | 102 | * @returns |
<> | 149:156823d33999 | 103 | * the data in the receive buffer |
<> | 149:156823d33999 | 104 | */ |
<> | 149:156823d33999 | 105 | int read(void); |
<> | 149:156823d33999 | 106 | |
<> | 149:156823d33999 | 107 | /** Fill the transmission buffer with the value to be written out |
<> | 149:156823d33999 | 108 | * as slave on the next received message from the master. |
<> | 149:156823d33999 | 109 | * |
<> | 149:156823d33999 | 110 | * @param value the data to be transmitted next |
<> | 149:156823d33999 | 111 | */ |
<> | 149:156823d33999 | 112 | void reply(int value); |
<> | 149:156823d33999 | 113 | |
<> | 149:156823d33999 | 114 | protected: |
<> | 149:156823d33999 | 115 | spi_t _spi; |
<> | 149:156823d33999 | 116 | |
<> | 149:156823d33999 | 117 | int _bits; |
<> | 149:156823d33999 | 118 | int _mode; |
<> | 149:156823d33999 | 119 | int _hz; |
<> | 149:156823d33999 | 120 | }; |
<> | 149:156823d33999 | 121 | |
<> | 149:156823d33999 | 122 | } // namespace mbed |
<> | 149:156823d33999 | 123 | |
<> | 149:156823d33999 | 124 | #endif |
<> | 149:156823d33999 | 125 | |
<> | 149:156823d33999 | 126 | #endif |
<> | 149:156823d33999 | 127 | |
<> | 149:156823d33999 | 128 | /** @}*/ |