Yihui Xiong / Mbed 2 deprecated Seeed_nRF51822_Motion

Dependencies:   mbed

Committer:
yihui
Date:
Mon Jan 11 02:32:24 2016 +0000
Revision:
0:638edba3adf6
initial

Who changed what in which revision?

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