test

Dependents:   robotic_fish_6

Committer:
juansal12
Date:
Fri Dec 03 23:00:34 2021 +0000
Revision:
0:c792b17d9f78
uploaded sofi code ;

Who changed what in which revision?

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