mbed library sources

Fork of mbed-src by mbed official

Committer:
bogdanm
Date:
Mon Aug 05 14:12:34 2013 +0300
Revision:
13:0645d8841f51
Parent:
10:3bc89ef62ce7
Child:
552:a1b9575155a3
Update mbed sources to revision 64

Who changed what in which revision?

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