Use MPU9250 with nRF51822

Dependencies:   eMPL_MPU

Fork of Seeed_Tiny_BLE_Flash by Darren Huang

Committer:
yihui
Date:
Thu Dec 10 08:00:18 2015 +0000
Revision:
5:9b240c1d5251
get 9dof data from mpu9250

Who changed what in which revision?

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