Fork of the official mbed C/C SDK provides the software platform and libraries to build your applications for RenBED.

Dependents:   1-RenBuggyTimed RenBED_RGB RenBED_RGB_PWM RenBED_RGB

Fork of mbed by mbed official

Committer:
elijahorr
Date:
Thu Apr 14 07:28:54 2016 +0000
Revision:
121:672067c3ada4
Parent:
102:da0ca467f8b5
.

Who changed what in which revision?

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