Dataloger

Committer:
jhon309
Date:
Thu Aug 20 00:37:14 2015 +0000
Revision:
0:666850d06c9f
ok

Who changed what in which revision?

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