mbed

Dependents:   DHTSensor_Test K64F_eCompass_OneNET_JW

Committer:
mbotkinl
Date:
Wed Feb 25 20:22:22 2015 +0000
Revision:
0:2cc6bb4d7fea
Working code to read Temperature and Humidity readings

Who changed what in which revision?

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