PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)
Dependents: YATTT sd_map_test cPong SnowDemo ... more
PokittoLib
Library for programming Pokitto hardware
How to Use
- Import this library to online compiler (see button "import" on the right hand side
- DO NOT import mbed-src anymore, a better version is now included inside PokittoLib
- Change My_settings.h according to your project
- Start coding!
mbed-pokitto/hal/i2c_api.h@71:531419862202, 2019-12-25 (annotated)
- Committer:
- Pokitto
- Date:
- Wed Dec 25 23:59:52 2019 +0000
- Revision:
- 71:531419862202
- Parent:
- 5:ea7377f3d1af
Changed Mode2 C++ refresh code (graphical errors)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Pokitto | 5:ea7377f3d1af | 1 | /* mbed Microcontroller Library |
Pokitto | 5:ea7377f3d1af | 2 | * Copyright (c) 2006-2015 ARM Limited |
Pokitto | 5:ea7377f3d1af | 3 | * |
Pokitto | 5:ea7377f3d1af | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Pokitto | 5:ea7377f3d1af | 5 | * you may not use this file except in compliance with the License. |
Pokitto | 5:ea7377f3d1af | 6 | * You may obtain a copy of the License at |
Pokitto | 5:ea7377f3d1af | 7 | * |
Pokitto | 5:ea7377f3d1af | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Pokitto | 5:ea7377f3d1af | 9 | * |
Pokitto | 5:ea7377f3d1af | 10 | * Unless required by applicable law or agreed to in writing, software |
Pokitto | 5:ea7377f3d1af | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Pokitto | 5:ea7377f3d1af | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Pokitto | 5:ea7377f3d1af | 13 | * See the License for the specific language governing permissions and |
Pokitto | 5:ea7377f3d1af | 14 | * limitations under the License. |
Pokitto | 5:ea7377f3d1af | 15 | */ |
Pokitto | 5:ea7377f3d1af | 16 | #ifndef MBED_I2C_API_H |
Pokitto | 5:ea7377f3d1af | 17 | #define MBED_I2C_API_H |
Pokitto | 5:ea7377f3d1af | 18 | |
Pokitto | 5:ea7377f3d1af | 19 | #include "device.h" |
Pokitto | 5:ea7377f3d1af | 20 | #include "buffer.h" |
Pokitto | 5:ea7377f3d1af | 21 | |
Pokitto | 5:ea7377f3d1af | 22 | #if DEVICE_I2C |
Pokitto | 5:ea7377f3d1af | 23 | |
Pokitto | 5:ea7377f3d1af | 24 | /** |
Pokitto | 5:ea7377f3d1af | 25 | * @defgroup I2CEvents I2C Events Macros |
Pokitto | 5:ea7377f3d1af | 26 | * |
Pokitto | 5:ea7377f3d1af | 27 | * @{ |
Pokitto | 5:ea7377f3d1af | 28 | */ |
Pokitto | 5:ea7377f3d1af | 29 | #define I2C_EVENT_ERROR (1 << 1) |
Pokitto | 5:ea7377f3d1af | 30 | #define I2C_EVENT_ERROR_NO_SLAVE (1 << 2) |
Pokitto | 5:ea7377f3d1af | 31 | #define I2C_EVENT_TRANSFER_COMPLETE (1 << 3) |
Pokitto | 5:ea7377f3d1af | 32 | #define I2C_EVENT_TRANSFER_EARLY_NACK (1 << 4) |
Pokitto | 5:ea7377f3d1af | 33 | #define I2C_EVENT_ALL (I2C_EVENT_ERROR | I2C_EVENT_TRANSFER_COMPLETE | I2C_EVENT_ERROR_NO_SLAVE | I2C_EVENT_TRANSFER_EARLY_NACK) |
Pokitto | 5:ea7377f3d1af | 34 | |
Pokitto | 5:ea7377f3d1af | 35 | /**@}*/ |
Pokitto | 5:ea7377f3d1af | 36 | |
Pokitto | 5:ea7377f3d1af | 37 | #if DEVICE_I2C_ASYNCH |
Pokitto | 5:ea7377f3d1af | 38 | /** Asynch i2c hal structure |
Pokitto | 5:ea7377f3d1af | 39 | */ |
Pokitto | 5:ea7377f3d1af | 40 | typedef struct { |
Pokitto | 5:ea7377f3d1af | 41 | struct i2c_s i2c; /**< Target specific i2c structure */ |
Pokitto | 5:ea7377f3d1af | 42 | struct buffer_s tx_buff; /**< Tx buffer */ |
Pokitto | 5:ea7377f3d1af | 43 | struct buffer_s rx_buff; /**< Rx buffer */ |
Pokitto | 5:ea7377f3d1af | 44 | } i2c_t; |
Pokitto | 5:ea7377f3d1af | 45 | |
Pokitto | 5:ea7377f3d1af | 46 | #else |
Pokitto | 5:ea7377f3d1af | 47 | /** Non-asynch i2c hal structure |
Pokitto | 5:ea7377f3d1af | 48 | */ |
Pokitto | 5:ea7377f3d1af | 49 | typedef struct i2c_s i2c_t; |
Pokitto | 5:ea7377f3d1af | 50 | |
Pokitto | 5:ea7377f3d1af | 51 | #endif |
Pokitto | 5:ea7377f3d1af | 52 | |
Pokitto | 5:ea7377f3d1af | 53 | enum { |
Pokitto | 5:ea7377f3d1af | 54 | I2C_ERROR_NO_SLAVE = -1, |
Pokitto | 5:ea7377f3d1af | 55 | I2C_ERROR_BUS_BUSY = -2 |
Pokitto | 5:ea7377f3d1af | 56 | }; |
Pokitto | 5:ea7377f3d1af | 57 | |
Pokitto | 5:ea7377f3d1af | 58 | #ifdef __cplusplus |
Pokitto | 5:ea7377f3d1af | 59 | extern "C" { |
Pokitto | 5:ea7377f3d1af | 60 | #endif |
Pokitto | 5:ea7377f3d1af | 61 | |
Pokitto | 5:ea7377f3d1af | 62 | /** |
Pokitto | 5:ea7377f3d1af | 63 | * \defgroup GeneralI2C I2C Configuration Functions |
Pokitto | 5:ea7377f3d1af | 64 | * @{ |
Pokitto | 5:ea7377f3d1af | 65 | */ |
Pokitto | 5:ea7377f3d1af | 66 | |
Pokitto | 5:ea7377f3d1af | 67 | /** Initialize the I2C peripheral. It sets the default parameters for I2C |
Pokitto | 5:ea7377f3d1af | 68 | * peripheral, and configure its specifieds pins. |
Pokitto | 5:ea7377f3d1af | 69 | * @param obj The i2c object |
Pokitto | 5:ea7377f3d1af | 70 | * @param sda The sda pin |
Pokitto | 5:ea7377f3d1af | 71 | * @param scl The scl pin |
Pokitto | 5:ea7377f3d1af | 72 | */ |
Pokitto | 5:ea7377f3d1af | 73 | void i2c_init(i2c_t *obj, PinName sda, PinName scl); |
Pokitto | 5:ea7377f3d1af | 74 | |
Pokitto | 5:ea7377f3d1af | 75 | /** Configure the I2C frequency. |
Pokitto | 5:ea7377f3d1af | 76 | * @param obj The i2c object |
Pokitto | 5:ea7377f3d1af | 77 | * @param hz Frequency in Hz |
Pokitto | 5:ea7377f3d1af | 78 | */ |
Pokitto | 5:ea7377f3d1af | 79 | void i2c_frequency(i2c_t *obj, int hz); |
Pokitto | 5:ea7377f3d1af | 80 | |
Pokitto | 5:ea7377f3d1af | 81 | /** Send START command. |
Pokitto | 5:ea7377f3d1af | 82 | * @param obj The i2c object |
Pokitto | 5:ea7377f3d1af | 83 | */ |
Pokitto | 5:ea7377f3d1af | 84 | int i2c_start(i2c_t *obj); |
Pokitto | 5:ea7377f3d1af | 85 | |
Pokitto | 5:ea7377f3d1af | 86 | /** Send STOP command. |
Pokitto | 5:ea7377f3d1af | 87 | * @param obj The i2c object |
Pokitto | 5:ea7377f3d1af | 88 | */ |
Pokitto | 5:ea7377f3d1af | 89 | int i2c_stop(i2c_t *obj); |
Pokitto | 5:ea7377f3d1af | 90 | |
Pokitto | 5:ea7377f3d1af | 91 | /** Blocking reading data. |
Pokitto | 5:ea7377f3d1af | 92 | * @param obj The i2c object |
Pokitto | 5:ea7377f3d1af | 93 | * @param address 7-bit address (last bit is 1) |
Pokitto | 5:ea7377f3d1af | 94 | * @param data The buffer for receiving |
Pokitto | 5:ea7377f3d1af | 95 | * @param length Number of bytes to read |
Pokitto | 5:ea7377f3d1af | 96 | * @param stop Stop to be generated after the transfer is done |
Pokitto | 5:ea7377f3d1af | 97 | * @return Number of read bytes |
Pokitto | 5:ea7377f3d1af | 98 | */ |
Pokitto | 5:ea7377f3d1af | 99 | int i2c_read(i2c_t *obj, int address, char *data, int length, int stop); |
Pokitto | 5:ea7377f3d1af | 100 | |
Pokitto | 5:ea7377f3d1af | 101 | /** Blocking sending data. |
Pokitto | 5:ea7377f3d1af | 102 | * @param obj The i2c object |
Pokitto | 5:ea7377f3d1af | 103 | * @param address 7-bit address (last bit is 0) |
Pokitto | 5:ea7377f3d1af | 104 | * @param data The buffer for sending |
Pokitto | 5:ea7377f3d1af | 105 | * @param length Number of bytes to wrte |
Pokitto | 5:ea7377f3d1af | 106 | * @param stop Stop to be generated after the transfer is done |
Pokitto | 5:ea7377f3d1af | 107 | * @return Number of written bytes |
Pokitto | 5:ea7377f3d1af | 108 | */ |
Pokitto | 5:ea7377f3d1af | 109 | int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop); |
Pokitto | 5:ea7377f3d1af | 110 | |
Pokitto | 5:ea7377f3d1af | 111 | /** Reset I2C peripheral. TODO: The action here. Most of the implementation sends stop(). |
Pokitto | 5:ea7377f3d1af | 112 | * @param obj The i2c object |
Pokitto | 5:ea7377f3d1af | 113 | */ |
Pokitto | 5:ea7377f3d1af | 114 | void i2c_reset(i2c_t *obj); |
Pokitto | 5:ea7377f3d1af | 115 | |
Pokitto | 5:ea7377f3d1af | 116 | /** Read one byte. |
Pokitto | 5:ea7377f3d1af | 117 | * @param obj The i2c object |
Pokitto | 5:ea7377f3d1af | 118 | * @param last Acknoledge |
Pokitto | 5:ea7377f3d1af | 119 | * @return The read byte |
Pokitto | 5:ea7377f3d1af | 120 | */ |
Pokitto | 5:ea7377f3d1af | 121 | int i2c_byte_read(i2c_t *obj, int last); |
Pokitto | 5:ea7377f3d1af | 122 | |
Pokitto | 5:ea7377f3d1af | 123 | /** Write one byte. |
Pokitto | 5:ea7377f3d1af | 124 | * @param obj The i2c object |
Pokitto | 5:ea7377f3d1af | 125 | * @param data Byte to be written |
Pokitto | 5:ea7377f3d1af | 126 | * @return 1 if NAK was received, 0 if ACK was received, 2 for timeout. |
Pokitto | 5:ea7377f3d1af | 127 | */ |
Pokitto | 5:ea7377f3d1af | 128 | int i2c_byte_write(i2c_t *obj, int data); |
Pokitto | 5:ea7377f3d1af | 129 | |
Pokitto | 5:ea7377f3d1af | 130 | /**@}*/ |
Pokitto | 5:ea7377f3d1af | 131 | |
Pokitto | 5:ea7377f3d1af | 132 | #if DEVICE_I2CSLAVE |
Pokitto | 5:ea7377f3d1af | 133 | |
Pokitto | 5:ea7377f3d1af | 134 | /** |
Pokitto | 5:ea7377f3d1af | 135 | * \defgroup SynchI2C Synchronous I2C Hardware Abstraction Layer for slave |
Pokitto | 5:ea7377f3d1af | 136 | * @{ |
Pokitto | 5:ea7377f3d1af | 137 | */ |
Pokitto | 5:ea7377f3d1af | 138 | |
Pokitto | 5:ea7377f3d1af | 139 | /** Configure I2C as slave or master. |
Pokitto | 5:ea7377f3d1af | 140 | * @param obj The I2C object |
Pokitto | 5:ea7377f3d1af | 141 | * @return non-zero if a value is available |
Pokitto | 5:ea7377f3d1af | 142 | */ |
Pokitto | 5:ea7377f3d1af | 143 | void i2c_slave_mode(i2c_t *obj, int enable_slave); |
Pokitto | 5:ea7377f3d1af | 144 | |
Pokitto | 5:ea7377f3d1af | 145 | /** Check to see if the I2C slave has been addressed. |
Pokitto | 5:ea7377f3d1af | 146 | * @param obj The I2C object |
Pokitto | 5:ea7377f3d1af | 147 | * @return The status - 1 - read addresses, 2 - write to all slaves, |
Pokitto | 5:ea7377f3d1af | 148 | * 3 write addressed, 0 - the slave has not been addressed |
Pokitto | 5:ea7377f3d1af | 149 | */ |
Pokitto | 5:ea7377f3d1af | 150 | int i2c_slave_receive(i2c_t *obj); |
Pokitto | 5:ea7377f3d1af | 151 | |
Pokitto | 5:ea7377f3d1af | 152 | /** Configure I2C as slave or master. |
Pokitto | 5:ea7377f3d1af | 153 | * @param obj The I2C object |
Pokitto | 5:ea7377f3d1af | 154 | * @return non-zero if a value is available |
Pokitto | 5:ea7377f3d1af | 155 | */ |
Pokitto | 5:ea7377f3d1af | 156 | int i2c_slave_read(i2c_t *obj, char *data, int length); |
Pokitto | 5:ea7377f3d1af | 157 | |
Pokitto | 5:ea7377f3d1af | 158 | /** Configure I2C as slave or master. |
Pokitto | 5:ea7377f3d1af | 159 | * @param obj The I2C object |
Pokitto | 5:ea7377f3d1af | 160 | * @return non-zero if a value is available |
Pokitto | 5:ea7377f3d1af | 161 | */ |
Pokitto | 5:ea7377f3d1af | 162 | int i2c_slave_write(i2c_t *obj, const char *data, int length); |
Pokitto | 5:ea7377f3d1af | 163 | |
Pokitto | 5:ea7377f3d1af | 164 | /** Configure I2C address. |
Pokitto | 5:ea7377f3d1af | 165 | * @param obj The I2C object |
Pokitto | 5:ea7377f3d1af | 166 | * @param idx Currently not used |
Pokitto | 5:ea7377f3d1af | 167 | * @param address The address to be set |
Pokitto | 5:ea7377f3d1af | 168 | * @param mask Currently not used |
Pokitto | 5:ea7377f3d1af | 169 | */ |
Pokitto | 5:ea7377f3d1af | 170 | void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask); |
Pokitto | 5:ea7377f3d1af | 171 | |
Pokitto | 5:ea7377f3d1af | 172 | #endif |
Pokitto | 5:ea7377f3d1af | 173 | |
Pokitto | 5:ea7377f3d1af | 174 | /**@}*/ |
Pokitto | 5:ea7377f3d1af | 175 | |
Pokitto | 5:ea7377f3d1af | 176 | #if DEVICE_I2C_ASYNCH |
Pokitto | 5:ea7377f3d1af | 177 | |
Pokitto | 5:ea7377f3d1af | 178 | /** |
Pokitto | 5:ea7377f3d1af | 179 | * \defgroup AsynchI2C Asynchronous I2C Hardware Abstraction Layer |
Pokitto | 5:ea7377f3d1af | 180 | * @{ |
Pokitto | 5:ea7377f3d1af | 181 | */ |
Pokitto | 5:ea7377f3d1af | 182 | |
Pokitto | 5:ea7377f3d1af | 183 | /** Start i2c asynchronous transfer. |
Pokitto | 5:ea7377f3d1af | 184 | * @param obj The I2C object |
Pokitto | 5:ea7377f3d1af | 185 | * @param tx The buffer to send |
Pokitto | 5:ea7377f3d1af | 186 | * @param tx_length The number of words to transmit |
Pokitto | 5:ea7377f3d1af | 187 | * @param rx The buffer to receive |
Pokitto | 5:ea7377f3d1af | 188 | * @param rx_length The number of words to receive |
Pokitto | 5:ea7377f3d1af | 189 | * @param address The address to be set - 7bit or 9 bit |
Pokitto | 5:ea7377f3d1af | 190 | * @param stop If true, stop will be generated after the transfer is done |
Pokitto | 5:ea7377f3d1af | 191 | * @param handler The I2C IRQ handler to be set |
Pokitto | 5:ea7377f3d1af | 192 | * @param hint DMA hint usage |
Pokitto | 5:ea7377f3d1af | 193 | */ |
Pokitto | 5:ea7377f3d1af | 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:ea7377f3d1af | 195 | |
Pokitto | 5:ea7377f3d1af | 196 | /** The asynchronous IRQ handler |
Pokitto | 5:ea7377f3d1af | 197 | * @param obj The I2C object which holds the transfer information |
Pokitto | 5:ea7377f3d1af | 198 | * @return event flags if a transfer termination condition was met or 0 otherwise. |
Pokitto | 5:ea7377f3d1af | 199 | */ |
Pokitto | 5:ea7377f3d1af | 200 | uint32_t i2c_irq_handler_asynch(i2c_t *obj); |
Pokitto | 5:ea7377f3d1af | 201 | |
Pokitto | 5:ea7377f3d1af | 202 | /** Attempts to determine if I2C peripheral is already in use. |
Pokitto | 5:ea7377f3d1af | 203 | * @param obj The I2C object |
Pokitto | 5:ea7377f3d1af | 204 | * @return non-zero if the I2C module is active or zero if it is not |
Pokitto | 5:ea7377f3d1af | 205 | */ |
Pokitto | 5:ea7377f3d1af | 206 | uint8_t i2c_active(i2c_t *obj); |
Pokitto | 5:ea7377f3d1af | 207 | |
Pokitto | 5:ea7377f3d1af | 208 | /** Abort ongoing asynchronous transaction. |
Pokitto | 5:ea7377f3d1af | 209 | * @param obj The I2C object |
Pokitto | 5:ea7377f3d1af | 210 | */ |
Pokitto | 5:ea7377f3d1af | 211 | void i2c_abort_asynch(i2c_t *obj); |
Pokitto | 5:ea7377f3d1af | 212 | |
Pokitto | 5:ea7377f3d1af | 213 | #endif |
Pokitto | 5:ea7377f3d1af | 214 | |
Pokitto | 5:ea7377f3d1af | 215 | /**@}*/ |
Pokitto | 5:ea7377f3d1af | 216 | |
Pokitto | 5:ea7377f3d1af | 217 | #ifdef __cplusplus |
Pokitto | 5:ea7377f3d1af | 218 | } |
Pokitto | 5:ea7377f3d1af | 219 | #endif |
Pokitto | 5:ea7377f3d1af | 220 | |
Pokitto | 5:ea7377f3d1af | 221 | #endif |
Pokitto | 5:ea7377f3d1af | 222 | |
Pokitto | 5:ea7377f3d1af | 223 | #endif |