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:
166:3a9487d57a5c
project opencv 3.1 on GR PEACH board, no use SD card.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
thedo 166:3a9487d57a5c 1 /* Copyright (c) 2010-2012 mbed.org, MIT License
thedo 166:3a9487d57a5c 2 *
thedo 166:3a9487d57a5c 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
thedo 166:3a9487d57a5c 4 * and associated documentation files (the "Software"), to deal in the Software without
thedo 166:3a9487d57a5c 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
thedo 166:3a9487d57a5c 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
thedo 166:3a9487d57a5c 7 * Software is furnished to do so, subject to the following conditions:
thedo 166:3a9487d57a5c 8 *
thedo 166:3a9487d57a5c 9 * The above copyright notice and this permission notice shall be included in all copies or
thedo 166:3a9487d57a5c 10 * substantial portions of the Software.
thedo 166:3a9487d57a5c 11 *
thedo 166:3a9487d57a5c 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
thedo 166:3a9487d57a5c 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
thedo 166:3a9487d57a5c 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
thedo 166:3a9487d57a5c 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
thedo 166:3a9487d57a5c 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
thedo 166:3a9487d57a5c 17 */
thedo 166:3a9487d57a5c 18
thedo 166:3a9487d57a5c 19 #include "USBHostConf.h"
thedo 166:3a9487d57a5c 20
thedo 166:3a9487d57a5c 21 #ifdef USBHOST_3GMODULE
thedo 166:3a9487d57a5c 22
thedo 166:3a9487d57a5c 23 #define __DEBUG__ 0
thedo 166:3a9487d57a5c 24 #ifndef __MODULE__
thedo 166:3a9487d57a5c 25 #define __MODULE__ "WANDongleSerialPort.cpp"
thedo 166:3a9487d57a5c 26 #endif
thedo 166:3a9487d57a5c 27
thedo 166:3a9487d57a5c 28 #include "dbg.h"
thedo 166:3a9487d57a5c 29 #include <stdint.h>
thedo 166:3a9487d57a5c 30 #include "rtos.h"
thedo 166:3a9487d57a5c 31
thedo 166:3a9487d57a5c 32 #include "WANDongleSerialPort.h"
thedo 166:3a9487d57a5c 33
thedo 166:3a9487d57a5c 34 WANDongleSerialPort::WANDongleSerialPort() : cb_tx_en(false), cb_rx_en(false), listener(NULL)
thedo 166:3a9487d57a5c 35 {
thedo 166:3a9487d57a5c 36 reset();
thedo 166:3a9487d57a5c 37 }
thedo 166:3a9487d57a5c 38
thedo 166:3a9487d57a5c 39 void WANDongleSerialPort::init(USBHost* pHost)
thedo 166:3a9487d57a5c 40 {
thedo 166:3a9487d57a5c 41 host = pHost;
thedo 166:3a9487d57a5c 42 }
thedo 166:3a9487d57a5c 43
thedo 166:3a9487d57a5c 44 void WANDongleSerialPort::reset()
thedo 166:3a9487d57a5c 45 {
thedo 166:3a9487d57a5c 46 tx_mtx.lock();
thedo 166:3a9487d57a5c 47 rx_mtx.lock();
thedo 166:3a9487d57a5c 48
thedo 166:3a9487d57a5c 49 bulk_in = NULL;
thedo 166:3a9487d57a5c 50 bulk_out = NULL;
thedo 166:3a9487d57a5c 51
thedo 166:3a9487d57a5c 52 buf_out_len = 0;
thedo 166:3a9487d57a5c 53 max_out_size = 0;
thedo 166:3a9487d57a5c 54 lock_tx = false;
thedo 166:3a9487d57a5c 55 cb_tx_pending = false;
thedo 166:3a9487d57a5c 56
thedo 166:3a9487d57a5c 57 buf_in_len = 0;
thedo 166:3a9487d57a5c 58 buf_in_read_pos = 0;
thedo 166:3a9487d57a5c 59 lock_rx = false;
thedo 166:3a9487d57a5c 60 cb_rx_pending = false;
thedo 166:3a9487d57a5c 61
thedo 166:3a9487d57a5c 62 tx_mtx.unlock();
thedo 166:3a9487d57a5c 63 rx_mtx.unlock();
thedo 166:3a9487d57a5c 64 }
thedo 166:3a9487d57a5c 65
thedo 166:3a9487d57a5c 66 int WANDongleSerialPort::readPacket()
thedo 166:3a9487d57a5c 67 {
thedo 166:3a9487d57a5c 68 USB_DBG("Read packet on %p", this);
thedo 166:3a9487d57a5c 69 rx_mtx.lock();
thedo 166:3a9487d57a5c 70 if(lock_rx)
thedo 166:3a9487d57a5c 71 {
thedo 166:3a9487d57a5c 72 USB_ERR("Fail");
thedo 166:3a9487d57a5c 73 rx_mtx.unlock();
thedo 166:3a9487d57a5c 74 return -1;
thedo 166:3a9487d57a5c 75 }
thedo 166:3a9487d57a5c 76
thedo 166:3a9487d57a5c 77 if( bulk_in == NULL )
thedo 166:3a9487d57a5c 78 {
thedo 166:3a9487d57a5c 79 USB_WARN("Port is disconnected");
thedo 166:3a9487d57a5c 80 rx_mtx.unlock();
thedo 166:3a9487d57a5c 81 return -1;
thedo 166:3a9487d57a5c 82 }
thedo 166:3a9487d57a5c 83
thedo 166:3a9487d57a5c 84 lock_rx = true; //Receiving
thedo 166:3a9487d57a5c 85 rx_mtx.unlock();
thedo 166:3a9487d57a5c 86 // USB_DBG("readPacket");
thedo 166:3a9487d57a5c 87 //lock_rx.lock();
thedo 166:3a9487d57a5c 88 USB_TYPE res = host->bulkRead(dev, (USBEndpoint *)bulk_in, buf_in, ((USBEndpoint *)bulk_in)->getSize(), false); //Queue transfer
thedo 166:3a9487d57a5c 89 if(res != USB_TYPE_PROCESSING)
thedo 166:3a9487d57a5c 90 {
thedo 166:3a9487d57a5c 91 //lock_rx.unlock();
thedo 166:3a9487d57a5c 92 USB_ERR("host->bulkRead() returned %d", res);
thedo 166:3a9487d57a5c 93 Thread::wait(100);
thedo 166:3a9487d57a5c 94 return -1;
thedo 166:3a9487d57a5c 95 }
thedo 166:3a9487d57a5c 96 return 0;
thedo 166:3a9487d57a5c 97 }
thedo 166:3a9487d57a5c 98
thedo 166:3a9487d57a5c 99 int WANDongleSerialPort::writePacket()
thedo 166:3a9487d57a5c 100 {
thedo 166:3a9487d57a5c 101 tx_mtx.lock();
thedo 166:3a9487d57a5c 102 if(lock_tx)
thedo 166:3a9487d57a5c 103 {
thedo 166:3a9487d57a5c 104 USB_ERR("Fail");
thedo 166:3a9487d57a5c 105 tx_mtx.unlock();
thedo 166:3a9487d57a5c 106 return -1;
thedo 166:3a9487d57a5c 107 }
thedo 166:3a9487d57a5c 108
thedo 166:3a9487d57a5c 109 if( bulk_out == NULL )
thedo 166:3a9487d57a5c 110 {
thedo 166:3a9487d57a5c 111 USB_WARN("Port is disconnected");
thedo 166:3a9487d57a5c 112 tx_mtx.unlock();
thedo 166:3a9487d57a5c 113 return -1;
thedo 166:3a9487d57a5c 114 }
thedo 166:3a9487d57a5c 115
thedo 166:3a9487d57a5c 116 lock_tx = true; //Transmitting
thedo 166:3a9487d57a5c 117 tx_mtx.unlock();
thedo 166:3a9487d57a5c 118 // USB_DBG("writePacket");
thedo 166:3a9487d57a5c 119
thedo 166:3a9487d57a5c 120 //lock_tx.lock();
thedo 166:3a9487d57a5c 121 USB_TYPE res = host->bulkWrite(dev, (USBEndpoint *)bulk_out, buf_out, buf_out_len, false); //Queue transfer
thedo 166:3a9487d57a5c 122 if(res != USB_TYPE_PROCESSING)
thedo 166:3a9487d57a5c 123 {
thedo 166:3a9487d57a5c 124 //lock_tx.unlock();
thedo 166:3a9487d57a5c 125 USB_ERR("host->bulkWrite() returned %d", res);
thedo 166:3a9487d57a5c 126 Thread::wait(100);
thedo 166:3a9487d57a5c 127 return -1;
thedo 166:3a9487d57a5c 128 }
thedo 166:3a9487d57a5c 129 return 0;
thedo 166:3a9487d57a5c 130 }
thedo 166:3a9487d57a5c 131
thedo 166:3a9487d57a5c 132 int WANDongleSerialPort::putc(int c)
thedo 166:3a9487d57a5c 133 {
thedo 166:3a9487d57a5c 134 tx_mtx.lock();
thedo 166:3a9487d57a5c 135 if(!lock_tx)
thedo 166:3a9487d57a5c 136 {
thedo 166:3a9487d57a5c 137 if(buf_out_len < max_out_size)
thedo 166:3a9487d57a5c 138 {
thedo 166:3a9487d57a5c 139 buf_out[buf_out_len] = (uint8_t)c;
thedo 166:3a9487d57a5c 140 buf_out_len++;
thedo 166:3a9487d57a5c 141 }
thedo 166:3a9487d57a5c 142 }
thedo 166:3a9487d57a5c 143 else
thedo 166:3a9487d57a5c 144 {
thedo 166:3a9487d57a5c 145 USB_ERR("CAN'T WRITE!");
thedo 166:3a9487d57a5c 146 }
thedo 166:3a9487d57a5c 147 tx_mtx.unlock();
thedo 166:3a9487d57a5c 148 return c;
thedo 166:3a9487d57a5c 149 }
thedo 166:3a9487d57a5c 150
thedo 166:3a9487d57a5c 151 int WANDongleSerialPort::getc()
thedo 166:3a9487d57a5c 152 {
thedo 166:3a9487d57a5c 153 rx_mtx.lock();
thedo 166:3a9487d57a5c 154 int c = 0;
thedo 166:3a9487d57a5c 155 if(!lock_rx)
thedo 166:3a9487d57a5c 156 {
thedo 166:3a9487d57a5c 157 if(buf_in_read_pos < buf_in_len)
thedo 166:3a9487d57a5c 158 {
thedo 166:3a9487d57a5c 159 c = (int)buf_in[buf_in_read_pos];
thedo 166:3a9487d57a5c 160 buf_in_read_pos++;
thedo 166:3a9487d57a5c 161 }
thedo 166:3a9487d57a5c 162 }
thedo 166:3a9487d57a5c 163 else
thedo 166:3a9487d57a5c 164 {
thedo 166:3a9487d57a5c 165 USB_ERR("CAN'T READ!");
thedo 166:3a9487d57a5c 166 }
thedo 166:3a9487d57a5c 167 rx_mtx.unlock();
thedo 166:3a9487d57a5c 168 return c;
thedo 166:3a9487d57a5c 169 }
thedo 166:3a9487d57a5c 170
thedo 166:3a9487d57a5c 171 int WANDongleSerialPort::readable()
thedo 166:3a9487d57a5c 172 {
thedo 166:3a9487d57a5c 173 rx_mtx.lock();
thedo 166:3a9487d57a5c 174 if (lock_rx)
thedo 166:3a9487d57a5c 175 {
thedo 166:3a9487d57a5c 176 rx_mtx.unlock();
thedo 166:3a9487d57a5c 177 return 0;
thedo 166:3a9487d57a5c 178 }
thedo 166:3a9487d57a5c 179
thedo 166:3a9487d57a5c 180 /* if( !lock_rx.trylock() )
thedo 166:3a9487d57a5c 181 {
thedo 166:3a9487d57a5c 182 return 0;
thedo 166:3a9487d57a5c 183 }*/
thedo 166:3a9487d57a5c 184 int res = buf_in_len - buf_in_read_pos;
thedo 166:3a9487d57a5c 185 //lock_rx.unlock();
thedo 166:3a9487d57a5c 186 rx_mtx.unlock();
thedo 166:3a9487d57a5c 187 return res;
thedo 166:3a9487d57a5c 188 }
thedo 166:3a9487d57a5c 189
thedo 166:3a9487d57a5c 190 int WANDongleSerialPort::writeable()
thedo 166:3a9487d57a5c 191 {
thedo 166:3a9487d57a5c 192 tx_mtx.lock();
thedo 166:3a9487d57a5c 193 if (lock_tx)
thedo 166:3a9487d57a5c 194 {
thedo 166:3a9487d57a5c 195 tx_mtx.unlock();
thedo 166:3a9487d57a5c 196 return 0;
thedo 166:3a9487d57a5c 197 }
thedo 166:3a9487d57a5c 198
thedo 166:3a9487d57a5c 199 /*if( !lock_tx.trylock() )
thedo 166:3a9487d57a5c 200 {
thedo 166:3a9487d57a5c 201 return 0;
thedo 166:3a9487d57a5c 202 }*/
thedo 166:3a9487d57a5c 203 int res = max_out_size - buf_out_len;
thedo 166:3a9487d57a5c 204 tx_mtx.unlock();
thedo 166:3a9487d57a5c 205 //lock_tx.unlock();
thedo 166:3a9487d57a5c 206 return res;
thedo 166:3a9487d57a5c 207 }
thedo 166:3a9487d57a5c 208
thedo 166:3a9487d57a5c 209 void WANDongleSerialPort::attach(IUSBHostSerialListener* pListener)
thedo 166:3a9487d57a5c 210 {
thedo 166:3a9487d57a5c 211 if(pListener == NULL)
thedo 166:3a9487d57a5c 212 {
thedo 166:3a9487d57a5c 213 setupIrq(false, RxIrq);
thedo 166:3a9487d57a5c 214 setupIrq(false, TxIrq);
thedo 166:3a9487d57a5c 215 }
thedo 166:3a9487d57a5c 216 listener = pListener;
thedo 166:3a9487d57a5c 217 if(pListener != NULL)
thedo 166:3a9487d57a5c 218 {
thedo 166:3a9487d57a5c 219 setupIrq(true, RxIrq);
thedo 166:3a9487d57a5c 220 setupIrq(true, TxIrq);
thedo 166:3a9487d57a5c 221 }
thedo 166:3a9487d57a5c 222 }
thedo 166:3a9487d57a5c 223
thedo 166:3a9487d57a5c 224 void WANDongleSerialPort::setupIrq(bool en, IrqType irq /*= RxIrq*/)
thedo 166:3a9487d57a5c 225 {
thedo 166:3a9487d57a5c 226 switch(irq)
thedo 166:3a9487d57a5c 227 {
thedo 166:3a9487d57a5c 228 case RxIrq:
thedo 166:3a9487d57a5c 229 rx_mtx.lock();
thedo 166:3a9487d57a5c 230 cb_rx_en = en;
thedo 166:3a9487d57a5c 231 if(en && cb_rx_pending)
thedo 166:3a9487d57a5c 232 {
thedo 166:3a9487d57a5c 233 cb_rx_pending = false;
thedo 166:3a9487d57a5c 234 rx_mtx.unlock();
thedo 166:3a9487d57a5c 235 listener->readable(); //Process the interrupt that was raised
thedo 166:3a9487d57a5c 236 }
thedo 166:3a9487d57a5c 237 else
thedo 166:3a9487d57a5c 238 {
thedo 166:3a9487d57a5c 239 rx_mtx.unlock();
thedo 166:3a9487d57a5c 240 }
thedo 166:3a9487d57a5c 241 break;
thedo 166:3a9487d57a5c 242 case TxIrq:
thedo 166:3a9487d57a5c 243 tx_mtx.lock();
thedo 166:3a9487d57a5c 244 cb_tx_en = en;
thedo 166:3a9487d57a5c 245 if(en && cb_tx_pending)
thedo 166:3a9487d57a5c 246 {
thedo 166:3a9487d57a5c 247 cb_tx_pending = false;
thedo 166:3a9487d57a5c 248 tx_mtx.unlock();
thedo 166:3a9487d57a5c 249 listener->writeable(); //Process the interrupt that was raised
thedo 166:3a9487d57a5c 250 }
thedo 166:3a9487d57a5c 251 else
thedo 166:3a9487d57a5c 252 {
thedo 166:3a9487d57a5c 253 tx_mtx.unlock();
thedo 166:3a9487d57a5c 254 }
thedo 166:3a9487d57a5c 255 break;
thedo 166:3a9487d57a5c 256 }
thedo 166:3a9487d57a5c 257 }
thedo 166:3a9487d57a5c 258
thedo 166:3a9487d57a5c 259
thedo 166:3a9487d57a5c 260 void WANDongleSerialPort::connect( USBDeviceConnected* pDev, USBEndpoint* pInEp, USBEndpoint* pOutEp )
thedo 166:3a9487d57a5c 261 {
thedo 166:3a9487d57a5c 262 dev = pDev;
thedo 166:3a9487d57a5c 263 bulk_in = pInEp;
thedo 166:3a9487d57a5c 264 bulk_out = pOutEp;
thedo 166:3a9487d57a5c 265 max_out_size = bulk_out->getSize();
thedo 166:3a9487d57a5c 266 if( max_out_size > WANDONGLE_MAX_OUTEP_SIZE )
thedo 166:3a9487d57a5c 267 {
thedo 166:3a9487d57a5c 268 max_out_size = WANDONGLE_MAX_OUTEP_SIZE;
thedo 166:3a9487d57a5c 269 }
thedo 166:3a9487d57a5c 270 bulk_in->attach(this, &WANDongleSerialPort::rxHandler);
thedo 166:3a9487d57a5c 271 bulk_out->attach(this, &WANDongleSerialPort::txHandler);
thedo 166:3a9487d57a5c 272 readPacket(); //Start receiving data
thedo 166:3a9487d57a5c 273 }
thedo 166:3a9487d57a5c 274
thedo 166:3a9487d57a5c 275 void WANDongleSerialPort::disconnect( )
thedo 166:3a9487d57a5c 276 {
thedo 166:3a9487d57a5c 277 reset();
thedo 166:3a9487d57a5c 278 }
thedo 166:3a9487d57a5c 279
thedo 166:3a9487d57a5c 280 //Private methods
thedo 166:3a9487d57a5c 281
thedo 166:3a9487d57a5c 282
thedo 166:3a9487d57a5c 283 void WANDongleSerialPort::rxHandler()
thedo 166:3a9487d57a5c 284 {
thedo 166:3a9487d57a5c 285 if (((USBEndpoint *) bulk_in)->getState() == USB_TYPE_IDLE) //Success
thedo 166:3a9487d57a5c 286 {
thedo 166:3a9487d57a5c 287 buf_in_read_pos = 0;
thedo 166:3a9487d57a5c 288 buf_in_len = ((USBEndpoint *) bulk_in)->getLengthTransferred(); //Update length
thedo 166:3a9487d57a5c 289 //lock_rx.unlock();
thedo 166:3a9487d57a5c 290 rx_mtx.lock();
thedo 166:3a9487d57a5c 291 lock_rx = false; //Transmission complete
thedo 166:3a9487d57a5c 292 if(cb_rx_en)
thedo 166:3a9487d57a5c 293 {
thedo 166:3a9487d57a5c 294 rx_mtx.unlock();
thedo 166:3a9487d57a5c 295 listener->readable(); //Call handler from the IRQ context
thedo 166:3a9487d57a5c 296 //readPacket() should be called by the handler subsequently once the buffer has been emptied
thedo 166:3a9487d57a5c 297 }
thedo 166:3a9487d57a5c 298 else
thedo 166:3a9487d57a5c 299 {
thedo 166:3a9487d57a5c 300 cb_rx_pending = true; //Queue the callback
thedo 166:3a9487d57a5c 301 rx_mtx.unlock();
thedo 166:3a9487d57a5c 302 }
thedo 166:3a9487d57a5c 303
thedo 166:3a9487d57a5c 304 }
thedo 166:3a9487d57a5c 305 else //Error, try reading again
thedo 166:3a9487d57a5c 306 {
thedo 166:3a9487d57a5c 307 //lock_rx.unlock();
thedo 166:3a9487d57a5c 308 USB_DBG("Trying again");
thedo 166:3a9487d57a5c 309 readPacket();
thedo 166:3a9487d57a5c 310 }
thedo 166:3a9487d57a5c 311 }
thedo 166:3a9487d57a5c 312
thedo 166:3a9487d57a5c 313 void WANDongleSerialPort::txHandler()
thedo 166:3a9487d57a5c 314 {
thedo 166:3a9487d57a5c 315 if (((USBEndpoint *) bulk_out)->getState() == USB_TYPE_IDLE) //Success
thedo 166:3a9487d57a5c 316 {
thedo 166:3a9487d57a5c 317 tx_mtx.lock();
thedo 166:3a9487d57a5c 318 buf_out_len = 0; //Reset length
thedo 166:3a9487d57a5c 319 lock_tx = false; //Transmission complete
thedo 166:3a9487d57a5c 320 //lock_tx.unlock();
thedo 166:3a9487d57a5c 321 if(cb_tx_en)
thedo 166:3a9487d57a5c 322 {
thedo 166:3a9487d57a5c 323 tx_mtx.unlock();
thedo 166:3a9487d57a5c 324 listener->writeable(); //Call handler from the IRQ context
thedo 166:3a9487d57a5c 325 //writePacket() should be called by the handler subsequently once the buffer has been filled
thedo 166:3a9487d57a5c 326 }
thedo 166:3a9487d57a5c 327 else
thedo 166:3a9487d57a5c 328 {
thedo 166:3a9487d57a5c 329 cb_tx_pending = true; //Queue the callback
thedo 166:3a9487d57a5c 330 tx_mtx.unlock();
thedo 166:3a9487d57a5c 331 }
thedo 166:3a9487d57a5c 332 }
thedo 166:3a9487d57a5c 333 else //Error, try reading again
thedo 166:3a9487d57a5c 334 {
thedo 166:3a9487d57a5c 335 //lock_tx.unlock();
thedo 166:3a9487d57a5c 336 writePacket();
thedo 166:3a9487d57a5c 337 }
thedo 166:3a9487d57a5c 338 }
thedo 166:3a9487d57a5c 339
thedo 166:3a9487d57a5c 340 #endif /* USBHOST_3GMODULE */