Opencv 3.1 project on GR-PEACH board

Fork of gr-peach-opencv-project by the do

Committer:
thedo
Date:
Tue Jul 04 06:23:13 2017 +0000
Revision:
170:54ff26da7eb6
Parent:
167:1657b442184c
project opencv 3.1 on GR PEACH board, no use SD card.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
thedo 167:1657b442184c 1 /* mbed Microcontroller Library
thedo 167:1657b442184c 2 * Copyright (c) 2006-2015 ARM Limited
thedo 167:1657b442184c 3 *
thedo 167:1657b442184c 4 * Licensed under the Apache License, Version 2.0 (the "License");
thedo 167:1657b442184c 5 * you may not use this file except in compliance with the License.
thedo 167:1657b442184c 6 * You may obtain a copy of the License at
thedo 167:1657b442184c 7 *
thedo 167:1657b442184c 8 * http://www.apache.org/licenses/LICENSE-2.0
thedo 167:1657b442184c 9 *
thedo 167:1657b442184c 10 * Unless required by applicable law or agreed to in writing, software
thedo 167:1657b442184c 11 * distributed under the License is distributed on an "AS IS" BASIS,
thedo 167:1657b442184c 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
thedo 167:1657b442184c 13 * See the License for the specific language governing permissions and
thedo 167:1657b442184c 14 * limitations under the License.
thedo 167:1657b442184c 15 */
thedo 167:1657b442184c 16 #include "drivers/I2C.h"
thedo 167:1657b442184c 17
thedo 167:1657b442184c 18 #if DEVICE_I2C
thedo 167:1657b442184c 19
thedo 167:1657b442184c 20 namespace mbed {
thedo 167:1657b442184c 21
thedo 167:1657b442184c 22 I2C *I2C::_owner = NULL;
thedo 167:1657b442184c 23 SingletonPtr<PlatformMutex> I2C::_mutex;
thedo 167:1657b442184c 24
thedo 167:1657b442184c 25 I2C::I2C(PinName sda, PinName scl) :
thedo 167:1657b442184c 26 #if DEVICE_I2C_ASYNCH
thedo 167:1657b442184c 27 _irq(this), _usage(DMA_USAGE_NEVER),
thedo 167:1657b442184c 28 #endif
thedo 167:1657b442184c 29 _i2c(), _hz(100000) {
thedo 167:1657b442184c 30 // No lock needed in the constructor
thedo 167:1657b442184c 31
thedo 167:1657b442184c 32 // The init function also set the frequency to 100000
thedo 167:1657b442184c 33 i2c_init(&_i2c, sda, scl);
thedo 167:1657b442184c 34
thedo 167:1657b442184c 35 // Used to avoid unnecessary frequency updates
thedo 167:1657b442184c 36 _owner = this;
thedo 167:1657b442184c 37 }
thedo 167:1657b442184c 38
thedo 167:1657b442184c 39 void I2C::frequency(int hz) {
thedo 167:1657b442184c 40 lock();
thedo 167:1657b442184c 41 _hz = hz;
thedo 167:1657b442184c 42
thedo 167:1657b442184c 43 // We want to update the frequency even if we are already the bus owners
thedo 167:1657b442184c 44 i2c_frequency(&_i2c, _hz);
thedo 167:1657b442184c 45
thedo 167:1657b442184c 46 // Updating the frequency of the bus we become the owners of it
thedo 167:1657b442184c 47 _owner = this;
thedo 167:1657b442184c 48 unlock();
thedo 167:1657b442184c 49 }
thedo 167:1657b442184c 50
thedo 167:1657b442184c 51 void I2C::aquire() {
thedo 167:1657b442184c 52 lock();
thedo 167:1657b442184c 53 if (_owner != this) {
thedo 167:1657b442184c 54 i2c_frequency(&_i2c, _hz);
thedo 167:1657b442184c 55 _owner = this;
thedo 167:1657b442184c 56 }
thedo 167:1657b442184c 57 unlock();
thedo 167:1657b442184c 58 }
thedo 167:1657b442184c 59
thedo 167:1657b442184c 60 // write - Master Transmitter Mode
thedo 167:1657b442184c 61 int I2C::write(int address, const char* data, int length, bool repeated) {
thedo 167:1657b442184c 62 lock();
thedo 167:1657b442184c 63 aquire();
thedo 167:1657b442184c 64
thedo 167:1657b442184c 65 int stop = (repeated) ? 0 : 1;
thedo 167:1657b442184c 66 int written = i2c_write(&_i2c, address, data, length, stop);
thedo 167:1657b442184c 67
thedo 167:1657b442184c 68 unlock();
thedo 167:1657b442184c 69 return length != written;
thedo 167:1657b442184c 70 }
thedo 167:1657b442184c 71
thedo 167:1657b442184c 72 int I2C::write(int data) {
thedo 167:1657b442184c 73 lock();
thedo 167:1657b442184c 74 int ret = i2c_byte_write(&_i2c, data);
thedo 167:1657b442184c 75 unlock();
thedo 167:1657b442184c 76 return ret;
thedo 167:1657b442184c 77 }
thedo 167:1657b442184c 78
thedo 167:1657b442184c 79 // read - Master Reciever Mode
thedo 167:1657b442184c 80 int I2C::read(int address, char* data, int length, bool repeated) {
thedo 167:1657b442184c 81 lock();
thedo 167:1657b442184c 82 aquire();
thedo 167:1657b442184c 83
thedo 167:1657b442184c 84 int stop = (repeated) ? 0 : 1;
thedo 167:1657b442184c 85 int read = i2c_read(&_i2c, address, data, length, stop);
thedo 167:1657b442184c 86
thedo 167:1657b442184c 87 unlock();
thedo 167:1657b442184c 88 return length != read;
thedo 167:1657b442184c 89 }
thedo 167:1657b442184c 90
thedo 167:1657b442184c 91 int I2C::read(int ack) {
thedo 167:1657b442184c 92 lock();
thedo 167:1657b442184c 93 int ret;
thedo 167:1657b442184c 94 if (ack) {
thedo 167:1657b442184c 95 ret = i2c_byte_read(&_i2c, 0);
thedo 167:1657b442184c 96 } else {
thedo 167:1657b442184c 97 ret = i2c_byte_read(&_i2c, 1);
thedo 167:1657b442184c 98 }
thedo 167:1657b442184c 99 unlock();
thedo 167:1657b442184c 100 return ret;
thedo 167:1657b442184c 101 }
thedo 167:1657b442184c 102
thedo 167:1657b442184c 103 void I2C::start(void) {
thedo 167:1657b442184c 104 lock();
thedo 167:1657b442184c 105 i2c_start(&_i2c);
thedo 167:1657b442184c 106 unlock();
thedo 167:1657b442184c 107 }
thedo 167:1657b442184c 108
thedo 167:1657b442184c 109 void I2C::stop(void) {
thedo 167:1657b442184c 110 lock();
thedo 167:1657b442184c 111 i2c_stop(&_i2c);
thedo 167:1657b442184c 112 unlock();
thedo 167:1657b442184c 113 }
thedo 167:1657b442184c 114
thedo 167:1657b442184c 115 void I2C::lock() {
thedo 167:1657b442184c 116 _mutex->lock();
thedo 167:1657b442184c 117 }
thedo 167:1657b442184c 118
thedo 167:1657b442184c 119 void I2C::unlock() {
thedo 167:1657b442184c 120 _mutex->unlock();
thedo 167:1657b442184c 121 }
thedo 167:1657b442184c 122
thedo 167:1657b442184c 123 #if DEVICE_I2C_ASYNCH
thedo 167:1657b442184c 124
thedo 167:1657b442184c 125 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)
thedo 167:1657b442184c 126 {
thedo 167:1657b442184c 127 lock();
thedo 167:1657b442184c 128 if (i2c_active(&_i2c)) {
thedo 167:1657b442184c 129 unlock();
thedo 167:1657b442184c 130 return -1; // transaction ongoing
thedo 167:1657b442184c 131 }
thedo 167:1657b442184c 132 aquire();
thedo 167:1657b442184c 133
thedo 167:1657b442184c 134 _callback = callback;
thedo 167:1657b442184c 135 int stop = (repeated) ? 0 : 1;
thedo 167:1657b442184c 136 _irq.callback(&I2C::irq_handler_asynch);
thedo 167:1657b442184c 137 i2c_transfer_asynch(&_i2c, (void *)tx_buffer, tx_length, (void *)rx_buffer, rx_length, address, stop, _irq.entry(), event, _usage);
thedo 167:1657b442184c 138 unlock();
thedo 167:1657b442184c 139 return 0;
thedo 167:1657b442184c 140 }
thedo 167:1657b442184c 141
thedo 167:1657b442184c 142 void I2C::abort_transfer(void)
thedo 167:1657b442184c 143 {
thedo 167:1657b442184c 144 lock();
thedo 167:1657b442184c 145 i2c_abort_asynch(&_i2c);
thedo 167:1657b442184c 146 unlock();
thedo 167:1657b442184c 147 }
thedo 167:1657b442184c 148
thedo 167:1657b442184c 149 void I2C::irq_handler_asynch(void)
thedo 167:1657b442184c 150 {
thedo 167:1657b442184c 151 int event = i2c_irq_handler_asynch(&_i2c);
thedo 167:1657b442184c 152 if (_callback && event) {
thedo 167:1657b442184c 153 _callback.call(event);
thedo 167:1657b442184c 154 }
thedo 167:1657b442184c 155
thedo 167:1657b442184c 156 }
thedo 167:1657b442184c 157
thedo 167:1657b442184c 158
thedo 167:1657b442184c 159 #endif
thedo 167:1657b442184c 160
thedo 167:1657b442184c 161 } // namespace mbed
thedo 167:1657b442184c 162
thedo 167:1657b442184c 163 #endif
thedo 167:1657b442184c 164