1232

Committer:
ganlikun
Date:
Mon Oct 24 15:19:39 2022 +0000
Revision:
0:06036f8bee2d
11

Who changed what in which revision?

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