Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

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