/*------------------------------------------------------------------------------ -- This file is a part of the SocExplorer Software -- Copyright (C) 2014, Plasma Physics Laboratory - CNRS -- -- This program is free software; you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by -- the Free Software Foundation; either version 2 of the License, or -- (at your option) any later version. -- -- This program is distributed in the hope that it will be useful, -- but WITHOUT ANY WARRANTY; without even the implied warranty of -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -- GNU General Public License for more details. -- -- You should have received a copy of the GNU General Public License -- along with this program; if not, write to the Free Software -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -------------------------------------------------------------------------------*/ /*-- Author : Alexis Jeandet -- Mail : alexis.jeandet@member.fsf.org ----------------------------------------------------------------------------*/ #include "uartpollingthread.h" #include UARTPollingThread::UARTPollingThread(socexplorerplugin *parent) : QThread((QObject*)parent) { this->plugin = parent; uartMutex = new QMutex(); uartOpened = false; fifoDebugConfigured = false; p_fifoDebugEnabled = false; this->moveToThread(this); } UARTPollingThread::~UARTPollingThread() { this->requestInterruption(); while(isRunning()); } void UARTPollingThread::run() { char ch[4097]; int timeout =10; SocExplorerEngine::message(this->plugin,"Entering APB UART polling thread",3); while (!this->isInterruptionRequested()) { if(p_fifoDebugEnabled) { if(fifoDebugConfigured) { if(this->plugin->baseAddress()!=-1) { unsigned int status_reg,data; char ch; int cnt=0; QString printdata=""; plugin->parent->Read(&status_reg,1,this->plugin->baseAddress()+APB_UART_STATUS_REG); while ((status_reg & 4)==0) { plugin->parent->Read(&data,1,this->plugin->baseAddress()+APB_UART_FIFO_DEBUG_REG); ch = (char)(0xff & data); printdata+=ch; plugin->parent->Read(&status_reg,1,this->plugin->baseAddress()+APB_UART_STATUS_REG); cnt++; if(Q_UNLIKELY(cnt>100)) { break; } } timeout = 100; if(printdata!="") emit apbUartTextReceived(printdata); } else { this->plugin->setBaseAddress(SocExplorerEngine::self()->getEnumDeviceBaseAddress(this->plugin,this->plugin->VID(),this->plugin->PID(),0)); } } else { configFifoDebug(true); } } else { int read =0,avail=0; uartMutex->lock(); if(uartOpened) { avail = rs232availablebytes(this->uart); SocExplorerEngine::message(this->plugin,QString("%1 available bytes on uart").arg(read),3); if(avail) { if(avail>=4096) { read = rs232read(this->uart,ch,4096); timeout = 0; } else { read = rs232read(this->uart,ch,avail); timeout = 10; } SocExplorerEngine::message(this->plugin,QString("Read %1 bytes on uart").arg(read),3); ch[read]='\0'; } else { timeout = 100; } } uartMutex->unlock(); if(read>=1) { SocExplorerEngine::message(this->plugin,QString("Received %1 char(s) from APBUART").arg(read),3); emit this->apbUartTextReceived(QString(ch)); } } msleep(timeout); } } void UARTPollingThread::sendChar(char c) { if(p_fifoDebugEnabled) { if(this->plugin->baseAddress()!=-1) { unsigned int i=0x0FF & c; plugin->parent->Write(&i,1,this->plugin->baseAddress()+APB_UART_FIFO_DEBUG_REG); } } else { uartMutex->lock(); rs232write(this->uart,&c,1); uartMutex->unlock(); } } bool UARTPollingThread::openUart() { uartMutex->lock(); if(uartOpened) { closeUart(); } SocExplorerEngine::message(this->plugin,"Opening UART "+this->portName,3); this->uart = rs232open((char*)this->portName.toStdString().c_str()); if(this->uart!=badPortValue) { SocExplorerEngine::message(this->plugin,QString("Configuring UART, speed =%1").arg(this->uartSpeed),3); rs232setup(this->uart,8,this->uartSpeed,rs232parityNo,rs232OneStop); uartOpened = true; } uartMutex->unlock(); return uartOpened; } void UARTPollingThread::closeUart() { uartMutex->lock(); rs232close(this->uart); uartOpened = false; uartMutex->unlock(); } void UARTPollingThread::setPortName(QString name) { SocExplorerEngine::message(this->plugin,"Changing UART port Name: "+name,3); this->portName = name; } void UARTPollingThread::setPortSpeedStr(QString speed) { SocExplorerEngine::message(this->plugin,"Changing UART speed: "+speed,3); this->uartSpeed = speed.toInt(); } void UARTPollingThread::setPortSpeed(int speed) { SocExplorerEngine::message(this->plugin,QString("Changing UART speed: %1").arg(speed),3); this->uartSpeed = speed; } void UARTPollingThread::setFifoDebugEable(bool enable) { if(enable) SocExplorerEngine::message(this->plugin,"Enabling APB UART FIFO debug mode",3); else SocExplorerEngine::message(this->plugin,"Disabling APB UART FIFO debug mode",3); if(uartOpened && enable) { closeUart(); } this->fifoDebugConfigured = false; configFifoDebug(enable); this->p_fifoDebugEnabled = enable; } bool UARTPollingThread::fifoDebugEnabled() { return p_fifoDebugEnabled; } void UARTPollingThread::configFifoDebug(bool enable) { SocExplorerEngine::message(this->plugin,"Configuring APB UART in FIFO debug mode",3); if(this->plugin->baseAddress()==-1) { this->plugin->setBaseAddress(SocExplorerEngine::self()->getEnumDeviceBaseAddress(this->plugin,this->plugin->VID(),this->plugin->PID(),0)); } if(this->plugin->baseAddress()!=-1) { if(enable) { unsigned int ctrl_reg= 0x843; this->plugin->parent->Write(&ctrl_reg,1,this->plugin->baseAddress()+APB_UART_CONTROL_REG); this->fifoDebugConfigured = true; } else { unsigned int ctrl_reg; /* Firts get Control reg value*/ this->plugin->parent->Read(&ctrl_reg,1,this->plugin->baseAddress()+APB_UART_CONTROL_REG); ctrl_reg = ctrl_reg & (~(1<<11)); this->plugin->parent->Write(&ctrl_reg,1,this->plugin->baseAddress()+APB_UART_CONTROL_REG); this->fifoDebugConfigured = true; } } }