,,

Fork of Application by Daniel Sygut

Committer:
Zaitsev
Date:
Thu Feb 15 14:29:23 2018 +0000
Revision:
15:2a20c3d2616e
Parent:
10:41552d038a69
j

Who changed what in which revision?

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