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/common/I2C.cpp@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 | #include "I2C.h" |
Pokitto | 5:ea7377f3d1af | 17 | |
Pokitto | 5:ea7377f3d1af | 18 | #if DEVICE_I2C |
Pokitto | 5:ea7377f3d1af | 19 | |
Pokitto | 5:ea7377f3d1af | 20 | namespace mbed { |
Pokitto | 5:ea7377f3d1af | 21 | |
Pokitto | 5:ea7377f3d1af | 22 | I2C *I2C::_owner = NULL; |
Pokitto | 5:ea7377f3d1af | 23 | |
Pokitto | 5:ea7377f3d1af | 24 | I2C::I2C(PinName sda, PinName scl) : |
Pokitto | 5:ea7377f3d1af | 25 | #if DEVICE_I2C_ASYNCH |
Pokitto | 5:ea7377f3d1af | 26 | _irq(this), _usage(DMA_USAGE_NEVER), |
Pokitto | 5:ea7377f3d1af | 27 | #endif |
Pokitto | 5:ea7377f3d1af | 28 | _i2c(), _hz(100000) { |
Pokitto | 5:ea7377f3d1af | 29 | // The init function also set the frequency to 100000 |
Pokitto | 5:ea7377f3d1af | 30 | i2c_init(&_i2c, sda, scl); |
Pokitto | 5:ea7377f3d1af | 31 | |
Pokitto | 5:ea7377f3d1af | 32 | // Used to avoid unnecessary frequency updates |
Pokitto | 5:ea7377f3d1af | 33 | _owner = this; |
Pokitto | 5:ea7377f3d1af | 34 | } |
Pokitto | 5:ea7377f3d1af | 35 | |
Pokitto | 5:ea7377f3d1af | 36 | void I2C::frequency(int hz) { |
Pokitto | 5:ea7377f3d1af | 37 | _hz = hz; |
Pokitto | 5:ea7377f3d1af | 38 | |
Pokitto | 5:ea7377f3d1af | 39 | // We want to update the frequency even if we are already the bus owners |
Pokitto | 5:ea7377f3d1af | 40 | i2c_frequency(&_i2c, _hz); |
Pokitto | 5:ea7377f3d1af | 41 | |
Pokitto | 5:ea7377f3d1af | 42 | // Updating the frequency of the bus we become the owners of it |
Pokitto | 5:ea7377f3d1af | 43 | _owner = this; |
Pokitto | 5:ea7377f3d1af | 44 | } |
Pokitto | 5:ea7377f3d1af | 45 | |
Pokitto | 5:ea7377f3d1af | 46 | void I2C::aquire() { |
Pokitto | 5:ea7377f3d1af | 47 | if (_owner != this) { |
Pokitto | 5:ea7377f3d1af | 48 | i2c_frequency(&_i2c, _hz); |
Pokitto | 5:ea7377f3d1af | 49 | _owner = this; |
Pokitto | 5:ea7377f3d1af | 50 | } |
Pokitto | 5:ea7377f3d1af | 51 | } |
Pokitto | 5:ea7377f3d1af | 52 | |
Pokitto | 5:ea7377f3d1af | 53 | // write - Master Transmitter Mode |
Pokitto | 5:ea7377f3d1af | 54 | int I2C::write(int address, const char* data, int length, bool repeated) { |
Pokitto | 5:ea7377f3d1af | 55 | aquire(); |
Pokitto | 5:ea7377f3d1af | 56 | |
Pokitto | 5:ea7377f3d1af | 57 | int stop = (repeated) ? 0 : 1; |
Pokitto | 5:ea7377f3d1af | 58 | int written = i2c_write(&_i2c, address, data, length, stop); |
Pokitto | 5:ea7377f3d1af | 59 | |
Pokitto | 5:ea7377f3d1af | 60 | return length != written; |
Pokitto | 5:ea7377f3d1af | 61 | } |
Pokitto | 5:ea7377f3d1af | 62 | |
Pokitto | 5:ea7377f3d1af | 63 | int I2C::write(int data) { |
Pokitto | 5:ea7377f3d1af | 64 | return i2c_byte_write(&_i2c, data); |
Pokitto | 5:ea7377f3d1af | 65 | } |
Pokitto | 5:ea7377f3d1af | 66 | |
Pokitto | 5:ea7377f3d1af | 67 | // read - Master Reciever Mode |
Pokitto | 5:ea7377f3d1af | 68 | int I2C::read(int address, char* data, int length, bool repeated) { |
Pokitto | 5:ea7377f3d1af | 69 | aquire(); |
Pokitto | 5:ea7377f3d1af | 70 | |
Pokitto | 5:ea7377f3d1af | 71 | int stop = (repeated) ? 0 : 1; |
Pokitto | 5:ea7377f3d1af | 72 | int read = i2c_read(&_i2c, address, data, length, stop); |
Pokitto | 5:ea7377f3d1af | 73 | |
Pokitto | 5:ea7377f3d1af | 74 | return length != read; |
Pokitto | 5:ea7377f3d1af | 75 | } |
Pokitto | 5:ea7377f3d1af | 76 | |
Pokitto | 5:ea7377f3d1af | 77 | int I2C::read(int ack) { |
Pokitto | 5:ea7377f3d1af | 78 | if (ack) { |
Pokitto | 5:ea7377f3d1af | 79 | return i2c_byte_read(&_i2c, 0); |
Pokitto | 5:ea7377f3d1af | 80 | } else { |
Pokitto | 5:ea7377f3d1af | 81 | return i2c_byte_read(&_i2c, 1); |
Pokitto | 5:ea7377f3d1af | 82 | } |
Pokitto | 5:ea7377f3d1af | 83 | } |
Pokitto | 5:ea7377f3d1af | 84 | |
Pokitto | 5:ea7377f3d1af | 85 | void I2C::start(void) { |
Pokitto | 5:ea7377f3d1af | 86 | i2c_start(&_i2c); |
Pokitto | 5:ea7377f3d1af | 87 | } |
Pokitto | 5:ea7377f3d1af | 88 | |
Pokitto | 5:ea7377f3d1af | 89 | void I2C::stop(void) { |
Pokitto | 5:ea7377f3d1af | 90 | i2c_stop(&_i2c); |
Pokitto | 5:ea7377f3d1af | 91 | } |
Pokitto | 5:ea7377f3d1af | 92 | |
Pokitto | 5:ea7377f3d1af | 93 | #if DEVICE_I2C_ASYNCH |
Pokitto | 5:ea7377f3d1af | 94 | |
Pokitto | 5:ea7377f3d1af | 95 | int I2C::transfer(int address, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, const event_callback_t& callback, int event, bool repeated) |
Pokitto | 5:ea7377f3d1af | 96 | { |
Pokitto | 5:ea7377f3d1af | 97 | if (i2c_active(&_i2c)) { |
Pokitto | 5:ea7377f3d1af | 98 | return -1; // transaction ongoing |
Pokitto | 5:ea7377f3d1af | 99 | } |
Pokitto | 5:ea7377f3d1af | 100 | aquire(); |
Pokitto | 5:ea7377f3d1af | 101 | |
Pokitto | 5:ea7377f3d1af | 102 | _callback = callback; |
Pokitto | 5:ea7377f3d1af | 103 | int stop = (repeated) ? 0 : 1; |
Pokitto | 5:ea7377f3d1af | 104 | _irq.callback(&I2C::irq_handler_asynch); |
Pokitto | 5:ea7377f3d1af | 105 | i2c_transfer_asynch(&_i2c, (void *)tx_buffer, tx_length, (void *)rx_buffer, rx_length, address, stop, _irq.entry(), event, _usage); |
Pokitto | 5:ea7377f3d1af | 106 | return 0; |
Pokitto | 5:ea7377f3d1af | 107 | } |
Pokitto | 5:ea7377f3d1af | 108 | |
Pokitto | 5:ea7377f3d1af | 109 | void I2C::abort_transfer(void) |
Pokitto | 5:ea7377f3d1af | 110 | { |
Pokitto | 5:ea7377f3d1af | 111 | i2c_abort_asynch(&_i2c); |
Pokitto | 5:ea7377f3d1af | 112 | } |
Pokitto | 5:ea7377f3d1af | 113 | |
Pokitto | 5:ea7377f3d1af | 114 | void I2C::irq_handler_asynch(void) |
Pokitto | 5:ea7377f3d1af | 115 | { |
Pokitto | 5:ea7377f3d1af | 116 | int event = i2c_irq_handler_asynch(&_i2c); |
Pokitto | 5:ea7377f3d1af | 117 | if (_callback && event) { |
Pokitto | 5:ea7377f3d1af | 118 | _callback.call(event); |
Pokitto | 5:ea7377f3d1af | 119 | } |
Pokitto | 5:ea7377f3d1af | 120 | |
Pokitto | 5:ea7377f3d1af | 121 | } |
Pokitto | 5:ea7377f3d1af | 122 | |
Pokitto | 5:ea7377f3d1af | 123 | |
Pokitto | 5:ea7377f3d1af | 124 | #endif |
Pokitto | 5:ea7377f3d1af | 125 | |
Pokitto | 5:ea7377f3d1af | 126 | } // namespace mbed |
Pokitto | 5:ea7377f3d1af | 127 | |
Pokitto | 5:ea7377f3d1af | 128 | #endif |