Fork of mbed for KL05Z based smart sensor which has no crystal on the board, so MCG FEI mode is required.

Dependents:   smart-sensor-KL05Z-debug

Fork of mbed-src by Ermanno Brusadin

Committer:
r14793
Date:
Mon Jan 29 15:38:37 2018 +0000
Revision:
1:da408b460382
Parent:
0:0a673c671a56
changed KL05 MCG mode to FEI for smart sensor boards (no crystal)

Who changed what in which revision?

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