PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)

Dependents:   Sensitive

Fork of PokittoLib by Jonne Valola

Committer:
spinal
Date:
Wed Oct 18 14:47:54 2017 +0000
Revision:
15:0bbe8f6fae32
Parent:
6:ea7377f3d1af
direct lcd stuff used by sensitive

Who changed what in which revision?

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