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