Official Sheffield ARMBand micro:bit program

Committer:
MrBedfordVan
Date:
Mon Oct 17 12:41:20 2016 +0000
Revision:
0:b9164b348919
Official Sheffield ARMBand Micro:bit program

Who changed what in which revision?

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