inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

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