PokittoLib with changes to lcd refresh etc.

Dependents:   Pokittris

Fork of Pokitto by Pokitto Community Team

This is a fork by user @Spinal, and is used in Pokittris for testing. Do not import this to your own program.

Committer:
spinal
Date:
Sun Oct 15 18:03:02 2017 +0000
Revision:
11:02ad9c807a21
Parent:
5:7e5c566b1760
fixed 4color refreshRegion code

Who changed what in which revision?

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