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_SPI_H
mbotkinl 0:2cc6bb4d7fea 17 #define MBED_SPI_H
mbotkinl 0:2cc6bb4d7fea 18
mbotkinl 0:2cc6bb4d7fea 19 #include "platform.h"
mbotkinl 0:2cc6bb4d7fea 20
mbotkinl 0:2cc6bb4d7fea 21 #if DEVICE_SPI
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 Master, used for communicating with SPI slave devices
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 * Most SPI devices will also require Chip Select and Reset signals. These
mbotkinl 0:2cc6bb4d7fea 32 * can be controlled using <DigitalOut> pins
mbotkinl 0:2cc6bb4d7fea 33 *
mbotkinl 0:2cc6bb4d7fea 34 * Example:
mbotkinl 0:2cc6bb4d7fea 35 * @code
mbotkinl 0:2cc6bb4d7fea 36 * // Send a byte to a SPI slave, and record the response
mbotkinl 0:2cc6bb4d7fea 37 *
mbotkinl 0:2cc6bb4d7fea 38 * #include "mbed.h"
mbotkinl 0:2cc6bb4d7fea 39 *
mbotkinl 0:2cc6bb4d7fea 40 * SPI device(p5, p6, p7); // mosi, miso, sclk
mbotkinl 0:2cc6bb4d7fea 41 *
mbotkinl 0:2cc6bb4d7fea 42 * int main() {
mbotkinl 0:2cc6bb4d7fea 43 * int response = device.write(0xFF);
mbotkinl 0:2cc6bb4d7fea 44 * }
mbotkinl 0:2cc6bb4d7fea 45 * @endcode
mbotkinl 0:2cc6bb4d7fea 46 */
mbotkinl 0:2cc6bb4d7fea 47 class SPI {
mbotkinl 0:2cc6bb4d7fea 48
mbotkinl 0:2cc6bb4d7fea 49 public:
mbotkinl 0:2cc6bb4d7fea 50
mbotkinl 0:2cc6bb4d7fea 51 /** Create a SPI master connected to the specified pins
mbotkinl 0:2cc6bb4d7fea 52 *
mbotkinl 0:2cc6bb4d7fea 53 * Pin Options:
mbotkinl 0:2cc6bb4d7fea 54 * (5, 6, 7) or (11, 12, 13)
mbotkinl 0:2cc6bb4d7fea 55 *
mbotkinl 0:2cc6bb4d7fea 56 * mosi or miso can be specfied as NC if not used
mbotkinl 0:2cc6bb4d7fea 57 *
mbotkinl 0:2cc6bb4d7fea 58 * @param mosi SPI Master Out, Slave In pin
mbotkinl 0:2cc6bb4d7fea 59 * @param miso SPI Master In, Slave Out pin
mbotkinl 0:2cc6bb4d7fea 60 * @param sclk SPI Clock pin
mbotkinl 0:2cc6bb4d7fea 61 */
mbotkinl 0:2cc6bb4d7fea 62 SPI(PinName mosi, PinName miso, PinName sclk, PinName _unused=NC);
mbotkinl 0:2cc6bb4d7fea 63
mbotkinl 0:2cc6bb4d7fea 64 /** Configure the data transmission format
mbotkinl 0:2cc6bb4d7fea 65 *
mbotkinl 0:2cc6bb4d7fea 66 * @param bits Number of bits per SPI frame (4 - 16)
mbotkinl 0:2cc6bb4d7fea 67 * @param mode Clock polarity and phase mode (0 - 3)
mbotkinl 0:2cc6bb4d7fea 68 *
mbotkinl 0:2cc6bb4d7fea 69 * @code
mbotkinl 0:2cc6bb4d7fea 70 * mode | POL PHA
mbotkinl 0:2cc6bb4d7fea 71 * -----+--------
mbotkinl 0:2cc6bb4d7fea 72 * 0 | 0 0
mbotkinl 0:2cc6bb4d7fea 73 * 1 | 0 1
mbotkinl 0:2cc6bb4d7fea 74 * 2 | 1 0
mbotkinl 0:2cc6bb4d7fea 75 * 3 | 1 1
mbotkinl 0:2cc6bb4d7fea 76 * @endcode
mbotkinl 0:2cc6bb4d7fea 77 */
mbotkinl 0:2cc6bb4d7fea 78 void format(int bits, int mode = 0);
mbotkinl 0:2cc6bb4d7fea 79
mbotkinl 0:2cc6bb4d7fea 80 /** Set the spi bus clock frequency
mbotkinl 0:2cc6bb4d7fea 81 *
mbotkinl 0:2cc6bb4d7fea 82 * @param hz SCLK frequency in hz (default = 1MHz)
mbotkinl 0:2cc6bb4d7fea 83 */
mbotkinl 0:2cc6bb4d7fea 84 void frequency(int hz = 1000000);
mbotkinl 0:2cc6bb4d7fea 85
mbotkinl 0:2cc6bb4d7fea 86 /** Write to the SPI Slave and return the response
mbotkinl 0:2cc6bb4d7fea 87 *
mbotkinl 0:2cc6bb4d7fea 88 * @param value Data to be sent to the SPI slave
mbotkinl 0:2cc6bb4d7fea 89 *
mbotkinl 0:2cc6bb4d7fea 90 * @returns
mbotkinl 0:2cc6bb4d7fea 91 * Response from the SPI slave
mbotkinl 0:2cc6bb4d7fea 92 */
mbotkinl 0:2cc6bb4d7fea 93 virtual int write(int value);
mbotkinl 0:2cc6bb4d7fea 94
mbotkinl 0:2cc6bb4d7fea 95 public:
mbotkinl 0:2cc6bb4d7fea 96 virtual ~SPI() {
mbotkinl 0:2cc6bb4d7fea 97 }
mbotkinl 0:2cc6bb4d7fea 98
mbotkinl 0:2cc6bb4d7fea 99 protected:
mbotkinl 0:2cc6bb4d7fea 100 spi_t _spi;
mbotkinl 0:2cc6bb4d7fea 101
mbotkinl 0:2cc6bb4d7fea 102 void aquire(void);
mbotkinl 0:2cc6bb4d7fea 103 static SPI *_owner;
mbotkinl 0:2cc6bb4d7fea 104 int _bits;
mbotkinl 0:2cc6bb4d7fea 105 int _mode;
mbotkinl 0:2cc6bb4d7fea 106 int _hz;
mbotkinl 0:2cc6bb4d7fea 107 };
mbotkinl 0:2cc6bb4d7fea 108
mbotkinl 0:2cc6bb4d7fea 109 } // namespace mbed
mbotkinl 0:2cc6bb4d7fea 110
mbotkinl 0:2cc6bb4d7fea 111 #endif
mbotkinl 0:2cc6bb4d7fea 112
mbotkinl 0:2cc6bb4d7fea 113 #endif