test

Dependencies:   mbed Watchdog

Dependents:   STM32-MC_node

Committer:
ommpy
Date:
Mon Jul 06 17:18:59 2020 +0530
Revision:
0:d383e2dee0f7
first commit

Who changed what in which revision?

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