.

Dependents:   RTC

Committer:
jhon309
Date:
Thu Aug 13 00:20:09 2015 +0000
Revision:
0:88e313c910d0
RTC Example

Who changed what in which revision?

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