mbed official / mbed

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

Committer:
Kojto
Date:
Wed Apr 13 12:19:19 2016 +0100
Revision:
118:082adc85693f
Parent:
102:da0ca467f8b5
Child:
120:7c328cabac7e
Release 118 of the mbed library

Changes:
- RTC fixes for STM32 F0,L15, F7, F10
- LPC821 - fix PwmOut SCT bug
- KSDK - us ticker recursion bug fix

Who changed what in which revision?

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