This is a repository for all my programs or modified programs.

Committer:
mturner5
Date:
Sun Sep 11 23:48:09 2016 +0000
Revision:
0:72480818e4a9
Made delays for the LEDS and button presses

Who changed what in which revision?

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