I2C_EEPROM

Committer:
jhon309
Date:
Thu Aug 13 00:23:16 2015 +0000
Revision:
0:ac8863619623
I2C

Who changed what in which revision?

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