dhgdh

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by joey shelton

Committer:
nexpaq
Date:
Sat Sep 17 16:32:05 2016 +0000
Revision:
1:55a6170b404f
checking in for sharing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nexpaq 1:55a6170b404f 1 /* mbed Microcontroller Library
nexpaq 1:55a6170b404f 2 * Copyright (c) 2006-2013 ARM Limited
nexpaq 1:55a6170b404f 3 *
nexpaq 1:55a6170b404f 4 * Licensed under the Apache License, Version 2.0 (the "License");
nexpaq 1:55a6170b404f 5 * you may not use this file except in compliance with the License.
nexpaq 1:55a6170b404f 6 * You may obtain a copy of the License at
nexpaq 1:55a6170b404f 7 *
nexpaq 1:55a6170b404f 8 * http://www.apache.org/licenses/LICENSE-2.0
nexpaq 1:55a6170b404f 9 *
nexpaq 1:55a6170b404f 10 * Unless required by applicable law or agreed to in writing, software
nexpaq 1:55a6170b404f 11 * distributed under the License is distributed on an "AS IS" BASIS,
nexpaq 1:55a6170b404f 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
nexpaq 1:55a6170b404f 13 * See the License for the specific language governing permissions and
nexpaq 1:55a6170b404f 14 * limitations under the License.
nexpaq 1:55a6170b404f 15 */
nexpaq 1:55a6170b404f 16 #ifndef MBED_I2C_SLAVE_H
nexpaq 1:55a6170b404f 17 #define MBED_I2C_SLAVE_H
nexpaq 1:55a6170b404f 18
nexpaq 1:55a6170b404f 19 #include "platform.h"
nexpaq 1:55a6170b404f 20
nexpaq 1:55a6170b404f 21 #if DEVICE_I2CSLAVE
nexpaq 1:55a6170b404f 22
nexpaq 1:55a6170b404f 23 #include "i2c_api.h"
nexpaq 1:55a6170b404f 24
nexpaq 1:55a6170b404f 25 namespace mbed {
nexpaq 1:55a6170b404f 26
nexpaq 1:55a6170b404f 27 /** An I2C Slave, used for communicating with an I2C Master device
nexpaq 1:55a6170b404f 28 *
nexpaq 1:55a6170b404f 29 * @Note Synchronization level: Not protected
nexpaq 1:55a6170b404f 30 *
nexpaq 1:55a6170b404f 31 * Example:
nexpaq 1:55a6170b404f 32 * @code
nexpaq 1:55a6170b404f 33 * // Simple I2C responder
nexpaq 1:55a6170b404f 34 * #include <mbed.h>
nexpaq 1:55a6170b404f 35 *
nexpaq 1:55a6170b404f 36 * I2CSlave slave(p9, p10);
nexpaq 1:55a6170b404f 37 *
nexpaq 1:55a6170b404f 38 * int main() {
nexpaq 1:55a6170b404f 39 * char buf[10];
nexpaq 1:55a6170b404f 40 * char msg[] = "Slave!";
nexpaq 1:55a6170b404f 41 *
nexpaq 1:55a6170b404f 42 * slave.address(0xA0);
nexpaq 1:55a6170b404f 43 * while (1) {
nexpaq 1:55a6170b404f 44 * int i = slave.receive();
nexpaq 1:55a6170b404f 45 * switch (i) {
nexpaq 1:55a6170b404f 46 * case I2CSlave::ReadAddressed:
nexpaq 1:55a6170b404f 47 * slave.write(msg, strlen(msg) + 1); // Includes null char
nexpaq 1:55a6170b404f 48 * break;
nexpaq 1:55a6170b404f 49 * case I2CSlave::WriteGeneral:
nexpaq 1:55a6170b404f 50 * slave.read(buf, 10);
nexpaq 1:55a6170b404f 51 * printf("Read G: %s\n", buf);
nexpaq 1:55a6170b404f 52 * break;
nexpaq 1:55a6170b404f 53 * case I2CSlave::WriteAddressed:
nexpaq 1:55a6170b404f 54 * slave.read(buf, 10);
nexpaq 1:55a6170b404f 55 * printf("Read A: %s\n", buf);
nexpaq 1:55a6170b404f 56 * break;
nexpaq 1:55a6170b404f 57 * }
nexpaq 1:55a6170b404f 58 * for(int i = 0; i < 10; i++) buf[i] = 0; // Clear buffer
nexpaq 1:55a6170b404f 59 * }
nexpaq 1:55a6170b404f 60 * }
nexpaq 1:55a6170b404f 61 * @endcode
nexpaq 1:55a6170b404f 62 */
nexpaq 1:55a6170b404f 63 class I2CSlave {
nexpaq 1:55a6170b404f 64
nexpaq 1:55a6170b404f 65 public:
nexpaq 1:55a6170b404f 66 enum RxStatus {
nexpaq 1:55a6170b404f 67 NoData = 0,
nexpaq 1:55a6170b404f 68 ReadAddressed = 1,
nexpaq 1:55a6170b404f 69 WriteGeneral = 2,
nexpaq 1:55a6170b404f 70 WriteAddressed = 3
nexpaq 1:55a6170b404f 71 };
nexpaq 1:55a6170b404f 72
nexpaq 1:55a6170b404f 73 /** Create an I2C Slave interface, connected to the specified pins.
nexpaq 1:55a6170b404f 74 *
nexpaq 1:55a6170b404f 75 * @param sda I2C data line pin
nexpaq 1:55a6170b404f 76 * @param scl I2C clock line pin
nexpaq 1:55a6170b404f 77 */
nexpaq 1:55a6170b404f 78 I2CSlave(PinName sda, PinName scl);
nexpaq 1:55a6170b404f 79
nexpaq 1:55a6170b404f 80 /** Set the frequency of the I2C interface
nexpaq 1:55a6170b404f 81 *
nexpaq 1:55a6170b404f 82 * @param hz The bus frequency in hertz
nexpaq 1:55a6170b404f 83 */
nexpaq 1:55a6170b404f 84 void frequency(int hz);
nexpaq 1:55a6170b404f 85
nexpaq 1:55a6170b404f 86 /** Checks to see if this I2C Slave has been addressed.
nexpaq 1:55a6170b404f 87 *
nexpaq 1:55a6170b404f 88 * @returns
nexpaq 1:55a6170b404f 89 * A status indicating if the device has been addressed, and how
nexpaq 1:55a6170b404f 90 * - NoData - the slave has not been addressed
nexpaq 1:55a6170b404f 91 * - ReadAddressed - the master has requested a read from this slave
nexpaq 1:55a6170b404f 92 * - WriteAddressed - the master is writing to this slave
nexpaq 1:55a6170b404f 93 * - WriteGeneral - the master is writing to all slave
nexpaq 1:55a6170b404f 94 */
nexpaq 1:55a6170b404f 95 int receive(void);
nexpaq 1:55a6170b404f 96
nexpaq 1:55a6170b404f 97 /** Read from an I2C master.
nexpaq 1:55a6170b404f 98 *
nexpaq 1:55a6170b404f 99 * @param data pointer to the byte array to read data in to
nexpaq 1:55a6170b404f 100 * @param length maximum number of bytes to read
nexpaq 1:55a6170b404f 101 *
nexpaq 1:55a6170b404f 102 * @returns
nexpaq 1:55a6170b404f 103 * 0 on success,
nexpaq 1:55a6170b404f 104 * non-0 otherwise
nexpaq 1:55a6170b404f 105 */
nexpaq 1:55a6170b404f 106 int read(char *data, int length);
nexpaq 1:55a6170b404f 107
nexpaq 1:55a6170b404f 108 /** Read a single byte from an I2C master.
nexpaq 1:55a6170b404f 109 *
nexpaq 1:55a6170b404f 110 * @returns
nexpaq 1:55a6170b404f 111 * the byte read
nexpaq 1:55a6170b404f 112 */
nexpaq 1:55a6170b404f 113 int read(void);
nexpaq 1:55a6170b404f 114
nexpaq 1:55a6170b404f 115 /** Write to an I2C master.
nexpaq 1:55a6170b404f 116 *
nexpaq 1:55a6170b404f 117 * @param data pointer to the byte array to be transmitted
nexpaq 1:55a6170b404f 118 * @param length the number of bytes to transmite
nexpaq 1:55a6170b404f 119 *
nexpaq 1:55a6170b404f 120 * @returns
nexpaq 1:55a6170b404f 121 * 0 on success,
nexpaq 1:55a6170b404f 122 * non-0 otherwise
nexpaq 1:55a6170b404f 123 */
nexpaq 1:55a6170b404f 124 int write(const char *data, int length);
nexpaq 1:55a6170b404f 125
nexpaq 1:55a6170b404f 126 /** Write a single byte to an I2C master.
nexpaq 1:55a6170b404f 127 *
nexpaq 1:55a6170b404f 128 * @data the byte to write
nexpaq 1:55a6170b404f 129 *
nexpaq 1:55a6170b404f 130 * @returns
nexpaq 1:55a6170b404f 131 * '1' if an ACK was received,
nexpaq 1:55a6170b404f 132 * '0' otherwise
nexpaq 1:55a6170b404f 133 */
nexpaq 1:55a6170b404f 134 int write(int data);
nexpaq 1:55a6170b404f 135
nexpaq 1:55a6170b404f 136 /** Sets the I2C slave address.
nexpaq 1:55a6170b404f 137 *
nexpaq 1:55a6170b404f 138 * @param address The address to set for the slave (ignoring the least
nexpaq 1:55a6170b404f 139 * signifcant bit). If set to 0, the slave will only respond to the
nexpaq 1:55a6170b404f 140 * general call address.
nexpaq 1:55a6170b404f 141 */
nexpaq 1:55a6170b404f 142 void address(int address);
nexpaq 1:55a6170b404f 143
nexpaq 1:55a6170b404f 144 /** Reset the I2C slave back into the known ready receiving state.
nexpaq 1:55a6170b404f 145 */
nexpaq 1:55a6170b404f 146 void stop(void);
nexpaq 1:55a6170b404f 147
nexpaq 1:55a6170b404f 148 protected:
nexpaq 1:55a6170b404f 149 i2c_t _i2c;
nexpaq 1:55a6170b404f 150 };
nexpaq 1:55a6170b404f 151
nexpaq 1:55a6170b404f 152 } // namespace mbed
nexpaq 1:55a6170b404f 153
nexpaq 1:55a6170b404f 154 #endif
nexpaq 1:55a6170b404f 155
nexpaq 1:55a6170b404f 156 #endif