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_API_H
Pokitto 5:7e5c566b1760 17 #define MBED_I2C_API_H
Pokitto 5:7e5c566b1760 18
Pokitto 5:7e5c566b1760 19 #include "device.h"
Pokitto 5:7e5c566b1760 20 #include "buffer.h"
Pokitto 5:7e5c566b1760 21
Pokitto 5:7e5c566b1760 22 #if DEVICE_I2C
Pokitto 5:7e5c566b1760 23
Pokitto 5:7e5c566b1760 24 /**
Pokitto 5:7e5c566b1760 25 * @defgroup I2CEvents I2C Events Macros
Pokitto 5:7e5c566b1760 26 *
Pokitto 5:7e5c566b1760 27 * @{
Pokitto 5:7e5c566b1760 28 */
Pokitto 5:7e5c566b1760 29 #define I2C_EVENT_ERROR (1 << 1)
Pokitto 5:7e5c566b1760 30 #define I2C_EVENT_ERROR_NO_SLAVE (1 << 2)
Pokitto 5:7e5c566b1760 31 #define I2C_EVENT_TRANSFER_COMPLETE (1 << 3)
Pokitto 5:7e5c566b1760 32 #define I2C_EVENT_TRANSFER_EARLY_NACK (1 << 4)
Pokitto 5:7e5c566b1760 33 #define I2C_EVENT_ALL (I2C_EVENT_ERROR | I2C_EVENT_TRANSFER_COMPLETE | I2C_EVENT_ERROR_NO_SLAVE | I2C_EVENT_TRANSFER_EARLY_NACK)
Pokitto 5:7e5c566b1760 34
Pokitto 5:7e5c566b1760 35 /**@}*/
Pokitto 5:7e5c566b1760 36
Pokitto 5:7e5c566b1760 37 #if DEVICE_I2C_ASYNCH
Pokitto 5:7e5c566b1760 38 /** Asynch i2c hal structure
Pokitto 5:7e5c566b1760 39 */
Pokitto 5:7e5c566b1760 40 typedef struct {
Pokitto 5:7e5c566b1760 41 struct i2c_s i2c; /**< Target specific i2c structure */
Pokitto 5:7e5c566b1760 42 struct buffer_s tx_buff; /**< Tx buffer */
Pokitto 5:7e5c566b1760 43 struct buffer_s rx_buff; /**< Rx buffer */
Pokitto 5:7e5c566b1760 44 } i2c_t;
Pokitto 5:7e5c566b1760 45
Pokitto 5:7e5c566b1760 46 #else
Pokitto 5:7e5c566b1760 47 /** Non-asynch i2c hal structure
Pokitto 5:7e5c566b1760 48 */
Pokitto 5:7e5c566b1760 49 typedef struct i2c_s i2c_t;
Pokitto 5:7e5c566b1760 50
Pokitto 5:7e5c566b1760 51 #endif
Pokitto 5:7e5c566b1760 52
Pokitto 5:7e5c566b1760 53 enum {
Pokitto 5:7e5c566b1760 54 I2C_ERROR_NO_SLAVE = -1,
Pokitto 5:7e5c566b1760 55 I2C_ERROR_BUS_BUSY = -2
Pokitto 5:7e5c566b1760 56 };
Pokitto 5:7e5c566b1760 57
Pokitto 5:7e5c566b1760 58 #ifdef __cplusplus
Pokitto 5:7e5c566b1760 59 extern "C" {
Pokitto 5:7e5c566b1760 60 #endif
Pokitto 5:7e5c566b1760 61
Pokitto 5:7e5c566b1760 62 /**
Pokitto 5:7e5c566b1760 63 * \defgroup GeneralI2C I2C Configuration Functions
Pokitto 5:7e5c566b1760 64 * @{
Pokitto 5:7e5c566b1760 65 */
Pokitto 5:7e5c566b1760 66
Pokitto 5:7e5c566b1760 67 /** Initialize the I2C peripheral. It sets the default parameters for I2C
Pokitto 5:7e5c566b1760 68 * peripheral, and configure its specifieds pins.
Pokitto 5:7e5c566b1760 69 * @param obj The i2c object
Pokitto 5:7e5c566b1760 70 * @param sda The sda pin
Pokitto 5:7e5c566b1760 71 * @param scl The scl pin
Pokitto 5:7e5c566b1760 72 */
Pokitto 5:7e5c566b1760 73 void i2c_init(i2c_t *obj, PinName sda, PinName scl);
Pokitto 5:7e5c566b1760 74
Pokitto 5:7e5c566b1760 75 /** Configure the I2C frequency.
Pokitto 5:7e5c566b1760 76 * @param obj The i2c object
Pokitto 5:7e5c566b1760 77 * @param hz Frequency in Hz
Pokitto 5:7e5c566b1760 78 */
Pokitto 5:7e5c566b1760 79 void i2c_frequency(i2c_t *obj, int hz);
Pokitto 5:7e5c566b1760 80
Pokitto 5:7e5c566b1760 81 /** Send START command.
Pokitto 5:7e5c566b1760 82 * @param obj The i2c object
Pokitto 5:7e5c566b1760 83 */
Pokitto 5:7e5c566b1760 84 int i2c_start(i2c_t *obj);
Pokitto 5:7e5c566b1760 85
Pokitto 5:7e5c566b1760 86 /** Send STOP command.
Pokitto 5:7e5c566b1760 87 * @param obj The i2c object
Pokitto 5:7e5c566b1760 88 */
Pokitto 5:7e5c566b1760 89 int i2c_stop(i2c_t *obj);
Pokitto 5:7e5c566b1760 90
Pokitto 5:7e5c566b1760 91 /** Blocking reading data.
Pokitto 5:7e5c566b1760 92 * @param obj The i2c object
Pokitto 5:7e5c566b1760 93 * @param address 7-bit address (last bit is 1)
Pokitto 5:7e5c566b1760 94 * @param data The buffer for receiving
Pokitto 5:7e5c566b1760 95 * @param length Number of bytes to read
Pokitto 5:7e5c566b1760 96 * @param stop Stop to be generated after the transfer is done
Pokitto 5:7e5c566b1760 97 * @return Number of read bytes
Pokitto 5:7e5c566b1760 98 */
Pokitto 5:7e5c566b1760 99 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop);
Pokitto 5:7e5c566b1760 100
Pokitto 5:7e5c566b1760 101 /** Blocking sending data.
Pokitto 5:7e5c566b1760 102 * @param obj The i2c object
Pokitto 5:7e5c566b1760 103 * @param address 7-bit address (last bit is 0)
Pokitto 5:7e5c566b1760 104 * @param data The buffer for sending
Pokitto 5:7e5c566b1760 105 * @param length Number of bytes to wrte
Pokitto 5:7e5c566b1760 106 * @param stop Stop to be generated after the transfer is done
Pokitto 5:7e5c566b1760 107 * @return Number of written bytes
Pokitto 5:7e5c566b1760 108 */
Pokitto 5:7e5c566b1760 109 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop);
Pokitto 5:7e5c566b1760 110
Pokitto 5:7e5c566b1760 111 /** Reset I2C peripheral. TODO: The action here. Most of the implementation sends stop().
Pokitto 5:7e5c566b1760 112 * @param obj The i2c object
Pokitto 5:7e5c566b1760 113 */
Pokitto 5:7e5c566b1760 114 void i2c_reset(i2c_t *obj);
Pokitto 5:7e5c566b1760 115
Pokitto 5:7e5c566b1760 116 /** Read one byte.
Pokitto 5:7e5c566b1760 117 * @param obj The i2c object
Pokitto 5:7e5c566b1760 118 * @param last Acknoledge
Pokitto 5:7e5c566b1760 119 * @return The read byte
Pokitto 5:7e5c566b1760 120 */
Pokitto 5:7e5c566b1760 121 int i2c_byte_read(i2c_t *obj, int last);
Pokitto 5:7e5c566b1760 122
Pokitto 5:7e5c566b1760 123 /** Write one byte.
Pokitto 5:7e5c566b1760 124 * @param obj The i2c object
Pokitto 5:7e5c566b1760 125 * @param data Byte to be written
Pokitto 5:7e5c566b1760 126 * @return 1 if NAK was received, 0 if ACK was received, 2 for timeout.
Pokitto 5:7e5c566b1760 127 */
Pokitto 5:7e5c566b1760 128 int i2c_byte_write(i2c_t *obj, int data);
Pokitto 5:7e5c566b1760 129
Pokitto 5:7e5c566b1760 130 /**@}*/
Pokitto 5:7e5c566b1760 131
Pokitto 5:7e5c566b1760 132 #if DEVICE_I2CSLAVE
Pokitto 5:7e5c566b1760 133
Pokitto 5:7e5c566b1760 134 /**
Pokitto 5:7e5c566b1760 135 * \defgroup SynchI2C Synchronous I2C Hardware Abstraction Layer for slave
Pokitto 5:7e5c566b1760 136 * @{
Pokitto 5:7e5c566b1760 137 */
Pokitto 5:7e5c566b1760 138
Pokitto 5:7e5c566b1760 139 /** Configure I2C as slave or master.
Pokitto 5:7e5c566b1760 140 * @param obj The I2C object
Pokitto 5:7e5c566b1760 141 * @return non-zero if a value is available
Pokitto 5:7e5c566b1760 142 */
Pokitto 5:7e5c566b1760 143 void i2c_slave_mode(i2c_t *obj, int enable_slave);
Pokitto 5:7e5c566b1760 144
Pokitto 5:7e5c566b1760 145 /** Check to see if the I2C slave has been addressed.
Pokitto 5:7e5c566b1760 146 * @param obj The I2C object
Pokitto 5:7e5c566b1760 147 * @return The status - 1 - read addresses, 2 - write to all slaves,
Pokitto 5:7e5c566b1760 148 * 3 write addressed, 0 - the slave has not been addressed
Pokitto 5:7e5c566b1760 149 */
Pokitto 5:7e5c566b1760 150 int i2c_slave_receive(i2c_t *obj);
Pokitto 5:7e5c566b1760 151
Pokitto 5:7e5c566b1760 152 /** Configure I2C as slave or master.
Pokitto 5:7e5c566b1760 153 * @param obj The I2C object
Pokitto 5:7e5c566b1760 154 * @return non-zero if a value is available
Pokitto 5:7e5c566b1760 155 */
Pokitto 5:7e5c566b1760 156 int i2c_slave_read(i2c_t *obj, char *data, int length);
Pokitto 5:7e5c566b1760 157
Pokitto 5:7e5c566b1760 158 /** Configure I2C as slave or master.
Pokitto 5:7e5c566b1760 159 * @param obj The I2C object
Pokitto 5:7e5c566b1760 160 * @return non-zero if a value is available
Pokitto 5:7e5c566b1760 161 */
Pokitto 5:7e5c566b1760 162 int i2c_slave_write(i2c_t *obj, const char *data, int length);
Pokitto 5:7e5c566b1760 163
Pokitto 5:7e5c566b1760 164 /** Configure I2C address.
Pokitto 5:7e5c566b1760 165 * @param obj The I2C object
Pokitto 5:7e5c566b1760 166 * @param idx Currently not used
Pokitto 5:7e5c566b1760 167 * @param address The address to be set
Pokitto 5:7e5c566b1760 168 * @param mask Currently not used
Pokitto 5:7e5c566b1760 169 */
Pokitto 5:7e5c566b1760 170 void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask);
Pokitto 5:7e5c566b1760 171
Pokitto 5:7e5c566b1760 172 #endif
Pokitto 5:7e5c566b1760 173
Pokitto 5:7e5c566b1760 174 /**@}*/
Pokitto 5:7e5c566b1760 175
Pokitto 5:7e5c566b1760 176 #if DEVICE_I2C_ASYNCH
Pokitto 5:7e5c566b1760 177
Pokitto 5:7e5c566b1760 178 /**
Pokitto 5:7e5c566b1760 179 * \defgroup AsynchI2C Asynchronous I2C Hardware Abstraction Layer
Pokitto 5:7e5c566b1760 180 * @{
Pokitto 5:7e5c566b1760 181 */
Pokitto 5:7e5c566b1760 182
Pokitto 5:7e5c566b1760 183 /** Start i2c asynchronous transfer.
Pokitto 5:7e5c566b1760 184 * @param obj The I2C object
Pokitto 5:7e5c566b1760 185 * @param tx The buffer to send
Pokitto 5:7e5c566b1760 186 * @param tx_length The number of words to transmit
Pokitto 5:7e5c566b1760 187 * @param rx The buffer to receive
Pokitto 5:7e5c566b1760 188 * @param rx_length The number of words to receive
Pokitto 5:7e5c566b1760 189 * @param address The address to be set - 7bit or 9 bit
Pokitto 5:7e5c566b1760 190 * @param stop If true, stop will be generated after the transfer is done
Pokitto 5:7e5c566b1760 191 * @param handler The I2C IRQ handler to be set
Pokitto 5:7e5c566b1760 192 * @param hint DMA hint usage
Pokitto 5:7e5c566b1760 193 */
Pokitto 5:7e5c566b1760 194 void i2c_transfer_asynch(i2c_t *obj, const void *tx, size_t tx_length, void *rx, size_t rx_length, uint32_t address, uint32_t stop, uint32_t handler, uint32_t event, DMAUsage hint);
Pokitto 5:7e5c566b1760 195
Pokitto 5:7e5c566b1760 196 /** The asynchronous IRQ handler
Pokitto 5:7e5c566b1760 197 * @param obj The I2C object which holds the transfer information
Pokitto 5:7e5c566b1760 198 * @return event flags if a transfer termination condition was met or 0 otherwise.
Pokitto 5:7e5c566b1760 199 */
Pokitto 5:7e5c566b1760 200 uint32_t i2c_irq_handler_asynch(i2c_t *obj);
Pokitto 5:7e5c566b1760 201
Pokitto 5:7e5c566b1760 202 /** Attempts to determine if I2C peripheral is already in use.
Pokitto 5:7e5c566b1760 203 * @param obj The I2C object
Pokitto 5:7e5c566b1760 204 * @return non-zero if the I2C module is active or zero if it is not
Pokitto 5:7e5c566b1760 205 */
Pokitto 5:7e5c566b1760 206 uint8_t i2c_active(i2c_t *obj);
Pokitto 5:7e5c566b1760 207
Pokitto 5:7e5c566b1760 208 /** Abort ongoing asynchronous transaction.
Pokitto 5:7e5c566b1760 209 * @param obj The I2C object
Pokitto 5:7e5c566b1760 210 */
Pokitto 5:7e5c566b1760 211 void i2c_abort_asynch(i2c_t *obj);
Pokitto 5:7e5c566b1760 212
Pokitto 5:7e5c566b1760 213 #endif
Pokitto 5:7e5c566b1760 214
Pokitto 5:7e5c566b1760 215 /**@}*/
Pokitto 5:7e5c566b1760 216
Pokitto 5:7e5c566b1760 217 #ifdef __cplusplus
Pokitto 5:7e5c566b1760 218 }
Pokitto 5:7e5c566b1760 219 #endif
Pokitto 5:7e5c566b1760 220
Pokitto 5:7e5c566b1760 221 #endif
Pokitto 5:7e5c566b1760 222
Pokitto 5:7e5c566b1760 223 #endif