mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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