Backup 1

Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

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