Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

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