Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

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