dev01 Brautlecht / mbed-STM32F030F4

Dependents:   STM32F031_Blink_Aug17

Fork of mbed-STM32F030F4 by Nothing Special

Committer:
mega64
Date:
Sat Oct 18 02:40:17 2014 +0000
Revision:
0:38ccae254a29
only for STM32F030F4

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mega64 0:38ccae254a29 1 /* mbed Microcontroller Library
mega64 0:38ccae254a29 2 * Copyright (c) 2006-2013 ARM Limited
mega64 0:38ccae254a29 3 *
mega64 0:38ccae254a29 4 * Licensed under the Apache License, Version 2.0 (the "License");
mega64 0:38ccae254a29 5 * you may not use this file except in compliance with the License.
mega64 0:38ccae254a29 6 * You may obtain a copy of the License at
mega64 0:38ccae254a29 7 *
mega64 0:38ccae254a29 8 * http://www.apache.org/licenses/LICENSE-2.0
mega64 0:38ccae254a29 9 *
mega64 0:38ccae254a29 10 * Unless required by applicable law or agreed to in writing, software
mega64 0:38ccae254a29 11 * distributed under the License is distributed on an "AS IS" BASIS,
mega64 0:38ccae254a29 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mega64 0:38ccae254a29 13 * See the License for the specific language governing permissions and
mega64 0:38ccae254a29 14 * limitations under the License.
mega64 0:38ccae254a29 15 */
mega64 0:38ccae254a29 16 #ifndef MBED_I2C_H
mega64 0:38ccae254a29 17 #define MBED_I2C_H
mega64 0:38ccae254a29 18
mega64 0:38ccae254a29 19 #include "platform.h"
mega64 0:38ccae254a29 20
mega64 0:38ccae254a29 21 #if DEVICE_I2C
mega64 0:38ccae254a29 22
mega64 0:38ccae254a29 23 #include "i2c_api.h"
mega64 0:38ccae254a29 24
mega64 0:38ccae254a29 25 namespace mbed {
mega64 0:38ccae254a29 26
mega64 0:38ccae254a29 27 /** An I2C Master, used for communicating with I2C slave devices
mega64 0:38ccae254a29 28 *
mega64 0:38ccae254a29 29 * Example:
mega64 0:38ccae254a29 30 * @code
mega64 0:38ccae254a29 31 * // Read from I2C slave at address 0x62
mega64 0:38ccae254a29 32 *
mega64 0:38ccae254a29 33 * #include "mbed.h"
mega64 0:38ccae254a29 34 *
mega64 0:38ccae254a29 35 * I2C i2c(p28, p27);
mega64 0:38ccae254a29 36 *
mega64 0:38ccae254a29 37 * int main() {
mega64 0:38ccae254a29 38 * int address = 0x62;
mega64 0:38ccae254a29 39 * char data[2];
mega64 0:38ccae254a29 40 * i2c.read(address, data, 2);
mega64 0:38ccae254a29 41 * }
mega64 0:38ccae254a29 42 * @endcode
mega64 0:38ccae254a29 43 */
mega64 0:38ccae254a29 44 class I2C {
mega64 0:38ccae254a29 45
mega64 0:38ccae254a29 46 public:
mega64 0:38ccae254a29 47 enum RxStatus {
mega64 0:38ccae254a29 48 NoData,
mega64 0:38ccae254a29 49 MasterGeneralCall,
mega64 0:38ccae254a29 50 MasterWrite,
mega64 0:38ccae254a29 51 MasterRead
mega64 0:38ccae254a29 52 };
mega64 0:38ccae254a29 53
mega64 0:38ccae254a29 54 enum Acknowledge {
mega64 0:38ccae254a29 55 NoACK = 0,
mega64 0:38ccae254a29 56 ACK = 1
mega64 0:38ccae254a29 57 };
mega64 0:38ccae254a29 58
mega64 0:38ccae254a29 59 /** Create an I2C Master interface, connected to the specified pins
mega64 0:38ccae254a29 60 *
mega64 0:38ccae254a29 61 * @param sda I2C data line pin
mega64 0:38ccae254a29 62 * @param scl I2C clock line pin
mega64 0:38ccae254a29 63 */
mega64 0:38ccae254a29 64 I2C(PinName sda, PinName scl);
mega64 0:38ccae254a29 65
mega64 0:38ccae254a29 66 /** Set the frequency of the I2C interface
mega64 0:38ccae254a29 67 *
mega64 0:38ccae254a29 68 * @param hz The bus frequency in hertz
mega64 0:38ccae254a29 69 */
mega64 0:38ccae254a29 70 void frequency(int hz);
mega64 0:38ccae254a29 71
mega64 0:38ccae254a29 72 /** Read from an I2C slave
mega64 0:38ccae254a29 73 *
mega64 0:38ccae254a29 74 * Performs a complete read transaction. The bottom bit of
mega64 0:38ccae254a29 75 * the address is forced to 1 to indicate a read.
mega64 0:38ccae254a29 76 *
mega64 0:38ccae254a29 77 * @param address 8-bit I2C slave address [ addr | 1 ]
mega64 0:38ccae254a29 78 * @param data Pointer to the byte-array to read data in to
mega64 0:38ccae254a29 79 * @param length Number of bytes to read
mega64 0:38ccae254a29 80 * @param repeated Repeated start, true - don't send stop at end
mega64 0:38ccae254a29 81 *
mega64 0:38ccae254a29 82 * @returns
mega64 0:38ccae254a29 83 * 0 on success (ack),
mega64 0:38ccae254a29 84 * non-0 on failure (nack)
mega64 0:38ccae254a29 85 */
mega64 0:38ccae254a29 86 int read(int address, char *data, int length, bool repeated = false);
mega64 0:38ccae254a29 87
mega64 0:38ccae254a29 88 /** Read a single byte from the I2C bus
mega64 0:38ccae254a29 89 *
mega64 0:38ccae254a29 90 * @param ack indicates if the byte is to be acknowledged (1 = acknowledge)
mega64 0:38ccae254a29 91 *
mega64 0:38ccae254a29 92 * @returns
mega64 0:38ccae254a29 93 * the byte read
mega64 0:38ccae254a29 94 */
mega64 0:38ccae254a29 95 int read(int ack);
mega64 0:38ccae254a29 96
mega64 0:38ccae254a29 97 /** Write to an I2C slave
mega64 0:38ccae254a29 98 *
mega64 0:38ccae254a29 99 * Performs a complete write transaction. The bottom bit of
mega64 0:38ccae254a29 100 * the address is forced to 0 to indicate a write.
mega64 0:38ccae254a29 101 *
mega64 0:38ccae254a29 102 * @param address 8-bit I2C slave address [ addr | 0 ]
mega64 0:38ccae254a29 103 * @param data Pointer to the byte-array data to send
mega64 0:38ccae254a29 104 * @param length Number of bytes to send
mega64 0:38ccae254a29 105 * @param repeated Repeated start, true - do not send stop at end
mega64 0:38ccae254a29 106 *
mega64 0:38ccae254a29 107 * @returns
mega64 0:38ccae254a29 108 * 0 on success (ack),
mega64 0:38ccae254a29 109 * non-0 on failure (nack)
mega64 0:38ccae254a29 110 */
mega64 0:38ccae254a29 111 int write(int address, const char *data, int length, bool repeated = false);
mega64 0:38ccae254a29 112
mega64 0:38ccae254a29 113 /** Write single byte out on the I2C bus
mega64 0:38ccae254a29 114 *
mega64 0:38ccae254a29 115 * @param data data to write out on bus
mega64 0:38ccae254a29 116 *
mega64 0:38ccae254a29 117 * @returns
mega64 0:38ccae254a29 118 * '1' if an ACK was received,
mega64 0:38ccae254a29 119 * '0' otherwise
mega64 0:38ccae254a29 120 */
mega64 0:38ccae254a29 121 int write(int data);
mega64 0:38ccae254a29 122
mega64 0:38ccae254a29 123 /** Creates a start condition on the I2C bus
mega64 0:38ccae254a29 124 */
mega64 0:38ccae254a29 125
mega64 0:38ccae254a29 126 void start(void);
mega64 0:38ccae254a29 127
mega64 0:38ccae254a29 128 /** Creates a stop condition on the I2C bus
mega64 0:38ccae254a29 129 */
mega64 0:38ccae254a29 130 void stop(void);
mega64 0:38ccae254a29 131
mega64 0:38ccae254a29 132 protected:
mega64 0:38ccae254a29 133 void aquire();
mega64 0:38ccae254a29 134
mega64 0:38ccae254a29 135 i2c_t _i2c;
mega64 0:38ccae254a29 136 static I2C *_owner;
mega64 0:38ccae254a29 137 int _hz;
mega64 0:38ccae254a29 138 };
mega64 0:38ccae254a29 139
mega64 0:38ccae254a29 140 } // namespace mbed
mega64 0:38ccae254a29 141
mega64 0:38ccae254a29 142 #endif
mega64 0:38ccae254a29 143
mega64 0:38ccae254a29 144 #endif