mbed library for slider v2

Dependents:   kl46z_slider_v2

Committer:
mturner5
Date:
Wed Sep 14 07:04:27 2016 +0000
Revision:
0:b7116bd48af6
Tried to use the timer.

Who changed what in which revision?

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