mbed-os for GR-LYCHEE

Dependents:   mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more

Committer:
dkato
Date:
Fri Feb 02 05:42:23 2018 +0000
Revision:
0:f782d9c66c49
mbed-os for GR-LYCHEE

Who changed what in which revision?

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