[270] | 1 | /* mbed Microcontroller Library
|
---|
| 2 | * Copyright (c) 2006-2015 ARM Limited
|
---|
| 3 | *
|
---|
| 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
| 5 | * you may not use this file except in compliance with the License.
|
---|
| 6 | * You may obtain a copy of the License at
|
---|
| 7 | *
|
---|
| 8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
| 9 | *
|
---|
| 10 | * Unless required by applicable law or agreed to in writing, software
|
---|
| 11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
| 13 | * See the License for the specific language governing permissions and
|
---|
| 14 | * limitations under the License.
|
---|
| 15 | */
|
---|
| 16 | #ifndef MBED_I2C_API_H
|
---|
| 17 | #define MBED_I2C_API_H
|
---|
| 18 |
|
---|
| 19 | #include "device.h"
|
---|
| 20 | #include "buffer.h"
|
---|
| 21 |
|
---|
| 22 | #if DEVICE_I2C
|
---|
| 23 |
|
---|
| 24 | /**
|
---|
| 25 | * @defgroup I2CEvents I2C Events Macros
|
---|
| 26 | *
|
---|
| 27 | * @{
|
---|
| 28 | */
|
---|
| 29 | #define I2C_EVENT_ERROR (1 << 1)
|
---|
| 30 | #define I2C_EVENT_ERROR_NO_SLAVE (1 << 2)
|
---|
| 31 | #define I2C_EVENT_TRANSFER_COMPLETE (1 << 3)
|
---|
| 32 | #define I2C_EVENT_TRANSFER_EARLY_NACK (1 << 4)
|
---|
| 33 | #define I2C_EVENT_ALL (I2C_EVENT_ERROR | I2C_EVENT_TRANSFER_COMPLETE | I2C_EVENT_ERROR_NO_SLAVE | I2C_EVENT_TRANSFER_EARLY_NACK)
|
---|
| 34 |
|
---|
| 35 | /**@}*/
|
---|
| 36 |
|
---|
| 37 | #if DEVICE_I2C_ASYNCH
|
---|
| 38 | /** Asynch i2c hal structure
|
---|
| 39 | */
|
---|
| 40 | typedef struct {
|
---|
| 41 | struct i2c_s i2c; /**< Target specific i2c structure */
|
---|
| 42 | struct buffer_s tx_buff; /**< Tx buffer */
|
---|
| 43 | struct buffer_s rx_buff; /**< Rx buffer */
|
---|
| 44 | } i2c_t;
|
---|
| 45 |
|
---|
| 46 | #else
|
---|
| 47 | /** Non-asynch i2c hal structure
|
---|
| 48 | */
|
---|
| 49 | typedef struct i2c_s i2c_t;
|
---|
| 50 |
|
---|
| 51 | #endif
|
---|
| 52 |
|
---|
| 53 | enum {
|
---|
| 54 | I2C_ERROR_NO_SLAVE = -1,
|
---|
| 55 | I2C_ERROR_BUS_BUSY = -2
|
---|
| 56 | };
|
---|
| 57 |
|
---|
| 58 | #ifdef __cplusplus
|
---|
| 59 | extern "C" {
|
---|
| 60 | #endif
|
---|
| 61 |
|
---|
| 62 | /**
|
---|
| 63 | * \defgroup GeneralI2C I2C Configuration Functions
|
---|
| 64 | * @{
|
---|
| 65 | */
|
---|
| 66 |
|
---|
| 67 | /** Initialize the I2C peripheral. It sets the default parameters for I2C
|
---|
| 68 | * peripheral, and configure its specifieds pins.
|
---|
| 69 | * @param obj The i2c object
|
---|
| 70 | * @param sda The sda pin
|
---|
| 71 | * @param scl The scl pin
|
---|
| 72 | */
|
---|
| 73 | void i2c_init(i2c_t *obj, PinName sda, PinName scl);
|
---|
| 74 |
|
---|
| 75 | /** Configure the I2C frequency.
|
---|
| 76 | * @param obj The i2c object
|
---|
| 77 | * @param hz Frequency in Hz
|
---|
| 78 | */
|
---|
| 79 | void i2c_frequency(i2c_t *obj, int hz);
|
---|
| 80 |
|
---|
| 81 | /** Send START command.
|
---|
| 82 | * @param obj The i2c object
|
---|
| 83 | */
|
---|
| 84 | int i2c_start(i2c_t *obj);
|
---|
| 85 |
|
---|
| 86 | /** Send STOP command.
|
---|
| 87 | * @param obj The i2c object
|
---|
| 88 | */
|
---|
| 89 | int i2c_stop(i2c_t *obj);
|
---|
| 90 |
|
---|
| 91 | /** Blocking reading data.
|
---|
| 92 | * @param obj The i2c object
|
---|
| 93 | * @param address 7-bit address (last bit is 1)
|
---|
| 94 | * @param data The buffer for receiving
|
---|
| 95 | * @param length Number of bytes to read
|
---|
| 96 | * @param stop Stop to be generated after the transfer is done
|
---|
| 97 | * @return Number of read bytes
|
---|
| 98 | */
|
---|
| 99 | int i2c_read(i2c_t *obj, int address, char *data, int length, int stop);
|
---|
| 100 |
|
---|
| 101 | /** Blocking sending data.
|
---|
| 102 | * @param obj The i2c object
|
---|
| 103 | * @param address 7-bit address (last bit is 0)
|
---|
| 104 | * @param data The buffer for sending
|
---|
| 105 | * @param length Number of bytes to wrte
|
---|
| 106 | * @param stop Stop to be generated after the transfer is done
|
---|
| 107 | * @return Number of written bytes
|
---|
| 108 | */
|
---|
| 109 | int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop);
|
---|
| 110 |
|
---|
| 111 | /** Reset I2C peripheral. TODO: The action here. Most of the implementation sends stop().
|
---|
| 112 | * @param obj The i2c object
|
---|
| 113 | */
|
---|
| 114 | void i2c_reset(i2c_t *obj);
|
---|
| 115 |
|
---|
| 116 | /** Read one byte.
|
---|
| 117 | * @param obj The i2c object
|
---|
| 118 | * @param last Acknoledge
|
---|
| 119 | * @return The read byte
|
---|
| 120 | */
|
---|
| 121 | int i2c_byte_read(i2c_t *obj, int last);
|
---|
| 122 |
|
---|
| 123 | /** Write one byte.
|
---|
| 124 | * @param obj The i2c object
|
---|
| 125 | * @param data Byte to be written
|
---|
| 126 | * @return 1 if NAK was received, 0 if ACK was received, 2 for timeout.
|
---|
| 127 | */
|
---|
| 128 | int i2c_byte_write(i2c_t *obj, int data);
|
---|
| 129 |
|
---|
| 130 | /**@}*/
|
---|
| 131 |
|
---|
| 132 | #if DEVICE_I2CSLAVE
|
---|
| 133 |
|
---|
| 134 | /**
|
---|
| 135 | * \defgroup SynchI2C Synchronous I2C Hardware Abstraction Layer for slave
|
---|
| 136 | * @{
|
---|
| 137 | */
|
---|
| 138 |
|
---|
| 139 | /** Configure I2C as slave or master.
|
---|
| 140 | * @param obj The I2C object
|
---|
| 141 | * @return non-zero if a value is available
|
---|
| 142 | */
|
---|
| 143 | void i2c_slave_mode(i2c_t *obj, int enable_slave);
|
---|
| 144 |
|
---|
| 145 | /** Check to see if the I2C slave has been addressed.
|
---|
| 146 | * @param obj The I2C object
|
---|
| 147 | * @return The status - 1 - read addresses, 2 - write to all slaves,
|
---|
| 148 | * 3 write addressed, 0 - the slave has not been addressed
|
---|
| 149 | */
|
---|
| 150 | int i2c_slave_receive(i2c_t *obj);
|
---|
| 151 |
|
---|
| 152 | /** Configure I2C as slave or master.
|
---|
| 153 | * @param obj The I2C object
|
---|
| 154 | * @return non-zero if a value is available
|
---|
| 155 | */
|
---|
| 156 | int i2c_slave_read(i2c_t *obj, char *data, int length);
|
---|
| 157 |
|
---|
| 158 | /** Configure I2C as slave or master.
|
---|
| 159 | * @param obj The I2C object
|
---|
| 160 | * @return non-zero if a value is available
|
---|
| 161 | */
|
---|
| 162 | int i2c_slave_write(i2c_t *obj, const char *data, int length);
|
---|
| 163 |
|
---|
| 164 | /** Configure I2C address.
|
---|
| 165 | * @param obj The I2C object
|
---|
| 166 | * @param idx Currently not used
|
---|
| 167 | * @param address The address to be set
|
---|
| 168 | * @param mask Currently not used
|
---|
| 169 | */
|
---|
| 170 | void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask);
|
---|
| 171 |
|
---|
| 172 | #endif
|
---|
| 173 |
|
---|
| 174 | /**@}*/
|
---|
| 175 |
|
---|
| 176 | #if DEVICE_I2C_ASYNCH
|
---|
| 177 |
|
---|
| 178 | /**
|
---|
| 179 | * \defgroup AsynchI2C Asynchronous I2C Hardware Abstraction Layer
|
---|
| 180 | * @{
|
---|
| 181 | */
|
---|
| 182 |
|
---|
| 183 | /** Start i2c asynchronous transfer.
|
---|
| 184 | * @param obj The I2C object
|
---|
| 185 | * @param tx The buffer to send
|
---|
| 186 | * @param tx_length The number of words to transmit
|
---|
| 187 | * @param rx The buffer to receive
|
---|
| 188 | * @param rx_length The number of words to receive
|
---|
| 189 | * @param address The address to be set - 7bit or 9 bit
|
---|
| 190 | * @param stop If true, stop will be generated after the transfer is done
|
---|
| 191 | * @param handler The I2C IRQ handler to be set
|
---|
| 192 | * @param hint DMA hint usage
|
---|
| 193 | */
|
---|
| 194 | void i2c_transfer_asynch(i2c_t *obj, const void *tx, size_t tx_length, void *rx, size_t rx_length, uint32_t address, uint32_t stop, uint32_t handler, uint32_t event, DMAUsage hint);
|
---|
| 195 |
|
---|
| 196 | /** The asynchronous IRQ handler
|
---|
| 197 | * @param obj The I2C object which holds the transfer information
|
---|
| 198 | * @return event flags if a transfer termination condition was met or 0 otherwise.
|
---|
| 199 | */
|
---|
| 200 | uint32_t i2c_irq_handler_asynch(i2c_t *obj);
|
---|
| 201 |
|
---|
| 202 | /** Attempts to determine if I2C peripheral is already in use.
|
---|
| 203 | * @param obj The I2C object
|
---|
| 204 | * @return non-zero if the I2C module is active or zero if it is not
|
---|
| 205 | */
|
---|
| 206 | uint8_t i2c_active(i2c_t *obj);
|
---|
| 207 |
|
---|
| 208 | /** Abort ongoing asynchronous transaction.
|
---|
| 209 | * @param obj The I2C object
|
---|
| 210 | */
|
---|
| 211 | void i2c_abort_asynch(i2c_t *obj);
|
---|
| 212 |
|
---|
| 213 | #endif
|
---|
| 214 |
|
---|
| 215 | /**@}*/
|
---|
| 216 |
|
---|
| 217 | #ifdef __cplusplus
|
---|
| 218 | }
|
---|
| 219 | #endif
|
---|
| 220 |
|
---|
| 221 | #endif
|
---|
| 222 |
|
---|
| 223 | #endif
|
---|