mbed library for NZ32-SC151

Committer:
modtronix-com
Date:
Fri Aug 19 15:46:42 2016 +1000
Revision:
17:639ed60ce759
Parent:
1:71204b8406f2
Added tag v1.1 for changeset 076cbe3e55be

Who changed what in which revision?

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