Erste version der Software für der Prototyp

Committer:
borlanic
Date:
Thu Mar 29 07:02:09 2018 +0000
Revision:
0:380207fcb5c1
Encoder, IMU --> OK; Controller --> in bearbeitung

Who changed what in which revision?

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