mbed-os

Fork of mbed-os by erkin yucel

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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