CDY version that shares functionality with Counter

Dependencies:   SDFileSystem_HelloWorld mbed FATFileSystem

Committer:
Charles David Young
Date:
Mon Nov 05 09:52:17 2018 -0700
Revision:
3:c547dba5d39b
Parent:
0:aa13e1c335cd
debug

Who changed what in which revision?

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