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