The official mbed C/C SDK provides the software platform and libraries to build your applications.

Fork of mbed by mbed official

Committer:
ldyz
Date:
Fri Jul 05 13:16:13 2013 +0000
Revision:
64:75c1708b266b
Parent:
59:0883845fe643
test

Who changed what in which revision?

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