Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
marcozecchini 0:9fca2b23d0ba 1 /* mbed Microcontroller Library
marcozecchini 0:9fca2b23d0ba 2 * Copyright (c) 2006-2015 ARM Limited
marcozecchini 0:9fca2b23d0ba 3 *
marcozecchini 0:9fca2b23d0ba 4 * Licensed under the Apache License, Version 2.0 (the "License");
marcozecchini 0:9fca2b23d0ba 5 * you may not use this file except in compliance with the License.
marcozecchini 0:9fca2b23d0ba 6 * You may obtain a copy of the License at
marcozecchini 0:9fca2b23d0ba 7 *
marcozecchini 0:9fca2b23d0ba 8 * http://www.apache.org/licenses/LICENSE-2.0
marcozecchini 0:9fca2b23d0ba 9 *
marcozecchini 0:9fca2b23d0ba 10 * Unless required by applicable law or agreed to in writing, software
marcozecchini 0:9fca2b23d0ba 11 * distributed under the License is distributed on an "AS IS" BASIS,
marcozecchini 0:9fca2b23d0ba 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
marcozecchini 0:9fca2b23d0ba 13 * See the License for the specific language governing permissions and
marcozecchini 0:9fca2b23d0ba 14 * limitations under the License.
marcozecchini 0:9fca2b23d0ba 15 */
marcozecchini 0:9fca2b23d0ba 16 #ifndef MBED_I2C_H
marcozecchini 0:9fca2b23d0ba 17 #define MBED_I2C_H
marcozecchini 0:9fca2b23d0ba 18
marcozecchini 0:9fca2b23d0ba 19 #include "platform/platform.h"
marcozecchini 0:9fca2b23d0ba 20
marcozecchini 0:9fca2b23d0ba 21 #if defined (DEVICE_I2C) || defined(DOXYGEN_ONLY)
marcozecchini 0:9fca2b23d0ba 22
marcozecchini 0:9fca2b23d0ba 23 #include "hal/i2c_api.h"
marcozecchini 0:9fca2b23d0ba 24 #include "platform/SingletonPtr.h"
marcozecchini 0:9fca2b23d0ba 25 #include "platform/PlatformMutex.h"
marcozecchini 0:9fca2b23d0ba 26 #include "platform/NonCopyable.h"
marcozecchini 0:9fca2b23d0ba 27
marcozecchini 0:9fca2b23d0ba 28 #if DEVICE_I2C_ASYNCH
marcozecchini 0:9fca2b23d0ba 29 #include "platform/CThunk.h"
marcozecchini 0:9fca2b23d0ba 30 #include "hal/dma_api.h"
marcozecchini 0:9fca2b23d0ba 31 #include "platform/FunctionPointer.h"
marcozecchini 0:9fca2b23d0ba 32 #endif
marcozecchini 0:9fca2b23d0ba 33
marcozecchini 0:9fca2b23d0ba 34 namespace mbed {
marcozecchini 0:9fca2b23d0ba 35 /** \addtogroup drivers */
marcozecchini 0:9fca2b23d0ba 36
marcozecchini 0:9fca2b23d0ba 37 /** An I2C Master, used for communicating with I2C slave devices
marcozecchini 0:9fca2b23d0ba 38 *
marcozecchini 0:9fca2b23d0ba 39 * @note Synchronization level: Thread safe
marcozecchini 0:9fca2b23d0ba 40 *
marcozecchini 0:9fca2b23d0ba 41 * Example:
marcozecchini 0:9fca2b23d0ba 42 * @code
marcozecchini 0:9fca2b23d0ba 43 * // Read from I2C slave at address 0x62
marcozecchini 0:9fca2b23d0ba 44 *
marcozecchini 0:9fca2b23d0ba 45 * #include "mbed.h"
marcozecchini 0:9fca2b23d0ba 46 *
marcozecchini 0:9fca2b23d0ba 47 * I2C i2c(p28, p27);
marcozecchini 0:9fca2b23d0ba 48 *
marcozecchini 0:9fca2b23d0ba 49 * int main() {
marcozecchini 0:9fca2b23d0ba 50 * int address = 0x62;
marcozecchini 0:9fca2b23d0ba 51 * char data[2];
marcozecchini 0:9fca2b23d0ba 52 * i2c.read(address, data, 2);
marcozecchini 0:9fca2b23d0ba 53 * }
marcozecchini 0:9fca2b23d0ba 54 * @endcode
marcozecchini 0:9fca2b23d0ba 55 * @ingroup drivers
marcozecchini 0:9fca2b23d0ba 56 */
marcozecchini 0:9fca2b23d0ba 57 class I2C : private NonCopyable<I2C> {
marcozecchini 0:9fca2b23d0ba 58
marcozecchini 0:9fca2b23d0ba 59 public:
marcozecchini 0:9fca2b23d0ba 60 enum RxStatus {
marcozecchini 0:9fca2b23d0ba 61 NoData,
marcozecchini 0:9fca2b23d0ba 62 MasterGeneralCall,
marcozecchini 0:9fca2b23d0ba 63 MasterWrite,
marcozecchini 0:9fca2b23d0ba 64 MasterRead
marcozecchini 0:9fca2b23d0ba 65 };
marcozecchini 0:9fca2b23d0ba 66
marcozecchini 0:9fca2b23d0ba 67 enum Acknowledge {
marcozecchini 0:9fca2b23d0ba 68 NoACK = 0,
marcozecchini 0:9fca2b23d0ba 69 ACK = 1
marcozecchini 0:9fca2b23d0ba 70 };
marcozecchini 0:9fca2b23d0ba 71
marcozecchini 0:9fca2b23d0ba 72 /** Create an I2C Master interface, connected to the specified pins
marcozecchini 0:9fca2b23d0ba 73 *
marcozecchini 0:9fca2b23d0ba 74 * @param sda I2C data line pin
marcozecchini 0:9fca2b23d0ba 75 * @param scl I2C clock line pin
marcozecchini 0:9fca2b23d0ba 76 */
marcozecchini 0:9fca2b23d0ba 77 I2C(PinName sda, PinName scl);
marcozecchini 0:9fca2b23d0ba 78
marcozecchini 0:9fca2b23d0ba 79 /** Set the frequency of the I2C interface
marcozecchini 0:9fca2b23d0ba 80 *
marcozecchini 0:9fca2b23d0ba 81 * @param hz The bus frequency in hertz
marcozecchini 0:9fca2b23d0ba 82 */
marcozecchini 0:9fca2b23d0ba 83 void frequency(int hz);
marcozecchini 0:9fca2b23d0ba 84
marcozecchini 0:9fca2b23d0ba 85 /** Read from an I2C slave
marcozecchini 0:9fca2b23d0ba 86 *
marcozecchini 0:9fca2b23d0ba 87 * Performs a complete read transaction. The bottom bit of
marcozecchini 0:9fca2b23d0ba 88 * the address is forced to 1 to indicate a read.
marcozecchini 0:9fca2b23d0ba 89 *
marcozecchini 0:9fca2b23d0ba 90 * @param address 8-bit I2C slave address [ addr | 1 ]
marcozecchini 0:9fca2b23d0ba 91 * @param data Pointer to the byte-array to read data in to
marcozecchini 0:9fca2b23d0ba 92 * @param length Number of bytes to read
marcozecchini 0:9fca2b23d0ba 93 * @param repeated Repeated start, true - don't send stop at end
marcozecchini 0:9fca2b23d0ba 94 *
marcozecchini 0:9fca2b23d0ba 95 * @returns
marcozecchini 0:9fca2b23d0ba 96 * 0 on success (ack),
marcozecchini 0:9fca2b23d0ba 97 * non-0 on failure (nack)
marcozecchini 0:9fca2b23d0ba 98 */
marcozecchini 0:9fca2b23d0ba 99 int read(int address, char *data, int length, bool repeated = false);
marcozecchini 0:9fca2b23d0ba 100
marcozecchini 0:9fca2b23d0ba 101 /** Read a single byte from the I2C bus
marcozecchini 0:9fca2b23d0ba 102 *
marcozecchini 0:9fca2b23d0ba 103 * @param ack indicates if the byte is to be acknowledged (1 = acknowledge)
marcozecchini 0:9fca2b23d0ba 104 *
marcozecchini 0:9fca2b23d0ba 105 * @returns
marcozecchini 0:9fca2b23d0ba 106 * the byte read
marcozecchini 0:9fca2b23d0ba 107 */
marcozecchini 0:9fca2b23d0ba 108 int read(int ack);
marcozecchini 0:9fca2b23d0ba 109
marcozecchini 0:9fca2b23d0ba 110 /** Write to an I2C slave
marcozecchini 0:9fca2b23d0ba 111 *
marcozecchini 0:9fca2b23d0ba 112 * Performs a complete write transaction. The bottom bit of
marcozecchini 0:9fca2b23d0ba 113 * the address is forced to 0 to indicate a write.
marcozecchini 0:9fca2b23d0ba 114 *
marcozecchini 0:9fca2b23d0ba 115 * @param address 8-bit I2C slave address [ addr | 0 ]
marcozecchini 0:9fca2b23d0ba 116 * @param data Pointer to the byte-array data to send
marcozecchini 0:9fca2b23d0ba 117 * @param length Number of bytes to send
marcozecchini 0:9fca2b23d0ba 118 * @param repeated Repeated start, true - do not send stop at end
marcozecchini 0:9fca2b23d0ba 119 *
marcozecchini 0:9fca2b23d0ba 120 * @returns
marcozecchini 0:9fca2b23d0ba 121 * 0 on success (ack),
marcozecchini 0:9fca2b23d0ba 122 * non-0 on failure (nack)
marcozecchini 0:9fca2b23d0ba 123 */
marcozecchini 0:9fca2b23d0ba 124 int write(int address, const char *data, int length, bool repeated = false);
marcozecchini 0:9fca2b23d0ba 125
marcozecchini 0:9fca2b23d0ba 126 /** Write single byte out on the I2C bus
marcozecchini 0:9fca2b23d0ba 127 *
marcozecchini 0:9fca2b23d0ba 128 * @param data data to write out on bus
marcozecchini 0:9fca2b23d0ba 129 *
marcozecchini 0:9fca2b23d0ba 130 * @returns
marcozecchini 0:9fca2b23d0ba 131 * '0' - NAK was received
marcozecchini 0:9fca2b23d0ba 132 * '1' - ACK was received,
marcozecchini 0:9fca2b23d0ba 133 * '2' - timeout
marcozecchini 0:9fca2b23d0ba 134 */
marcozecchini 0:9fca2b23d0ba 135 int write(int data);
marcozecchini 0:9fca2b23d0ba 136
marcozecchini 0:9fca2b23d0ba 137 /** Creates a start condition on the I2C bus
marcozecchini 0:9fca2b23d0ba 138 */
marcozecchini 0:9fca2b23d0ba 139
marcozecchini 0:9fca2b23d0ba 140 void start(void);
marcozecchini 0:9fca2b23d0ba 141
marcozecchini 0:9fca2b23d0ba 142 /** Creates a stop condition on the I2C bus
marcozecchini 0:9fca2b23d0ba 143 */
marcozecchini 0:9fca2b23d0ba 144 void stop(void);
marcozecchini 0:9fca2b23d0ba 145
marcozecchini 0:9fca2b23d0ba 146 /** Acquire exclusive access to this I2C bus
marcozecchini 0:9fca2b23d0ba 147 */
marcozecchini 0:9fca2b23d0ba 148 virtual void lock(void);
marcozecchini 0:9fca2b23d0ba 149
marcozecchini 0:9fca2b23d0ba 150 /** Release exclusive access to this I2C bus
marcozecchini 0:9fca2b23d0ba 151 */
marcozecchini 0:9fca2b23d0ba 152 virtual void unlock(void);
marcozecchini 0:9fca2b23d0ba 153
marcozecchini 0:9fca2b23d0ba 154 virtual ~I2C() {
marcozecchini 0:9fca2b23d0ba 155 // Do nothing
marcozecchini 0:9fca2b23d0ba 156 }
marcozecchini 0:9fca2b23d0ba 157
marcozecchini 0:9fca2b23d0ba 158 #if DEVICE_I2C_ASYNCH
marcozecchini 0:9fca2b23d0ba 159
marcozecchini 0:9fca2b23d0ba 160 /** Start non-blocking I2C transfer.
marcozecchini 0:9fca2b23d0ba 161 *
marcozecchini 0:9fca2b23d0ba 162 * This function locks the deep sleep until any event has occured
marcozecchini 0:9fca2b23d0ba 163 *
marcozecchini 0:9fca2b23d0ba 164 * @param address 8/10 bit I2c slave address
marcozecchini 0:9fca2b23d0ba 165 * @param tx_buffer The TX buffer with data to be transfered
marcozecchini 0:9fca2b23d0ba 166 * @param tx_length The length of TX buffer in bytes
marcozecchini 0:9fca2b23d0ba 167 * @param rx_buffer The RX buffer which is used for received data
marcozecchini 0:9fca2b23d0ba 168 * @param rx_length The length of RX buffer in bytes
marcozecchini 0:9fca2b23d0ba 169 * @param event The logical OR of events to modify
marcozecchini 0:9fca2b23d0ba 170 * @param callback The event callback function
marcozecchini 0:9fca2b23d0ba 171 * @param repeated Repeated start, true - do not send stop at end
marcozecchini 0:9fca2b23d0ba 172 * @return Zero if the transfer has started, or -1 if I2C peripheral is busy
marcozecchini 0:9fca2b23d0ba 173 */
marcozecchini 0:9fca2b23d0ba 174 int transfer(int address, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, const event_callback_t& callback, int event = I2C_EVENT_TRANSFER_COMPLETE, bool repeated = false);
marcozecchini 0:9fca2b23d0ba 175
marcozecchini 0:9fca2b23d0ba 176 /** Abort the on-going I2C transfer
marcozecchini 0:9fca2b23d0ba 177 */
marcozecchini 0:9fca2b23d0ba 178 void abort_transfer();
marcozecchini 0:9fca2b23d0ba 179 protected:
marcozecchini 0:9fca2b23d0ba 180 void irq_handler_asynch(void);
marcozecchini 0:9fca2b23d0ba 181 event_callback_t _callback;
marcozecchini 0:9fca2b23d0ba 182 CThunk<I2C> _irq;
marcozecchini 0:9fca2b23d0ba 183 DMAUsage _usage;
marcozecchini 0:9fca2b23d0ba 184 #endif
marcozecchini 0:9fca2b23d0ba 185
marcozecchini 0:9fca2b23d0ba 186 protected:
marcozecchini 0:9fca2b23d0ba 187 void aquire();
marcozecchini 0:9fca2b23d0ba 188
marcozecchini 0:9fca2b23d0ba 189 i2c_t _i2c;
marcozecchini 0:9fca2b23d0ba 190 static I2C *_owner;
marcozecchini 0:9fca2b23d0ba 191 int _hz;
marcozecchini 0:9fca2b23d0ba 192 static SingletonPtr<PlatformMutex> _mutex;
marcozecchini 0:9fca2b23d0ba 193 };
marcozecchini 0:9fca2b23d0ba 194
marcozecchini 0:9fca2b23d0ba 195 } // namespace mbed
marcozecchini 0:9fca2b23d0ba 196
marcozecchini 0:9fca2b23d0ba 197 #endif
marcozecchini 0:9fca2b23d0ba 198
marcozecchini 0:9fca2b23d0ba 199 #endif