/*------------------------------------------------------------------------------ -- 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; this->moveToThread(this); } void UARTPollingThread::run() { SocExplorerEngine::message(this->plugin,"Entering APB UART polling thread",3); while (!this->isInterruptionRequested()) { if(fifoDebugEnabled) { if(fifoDebugConfigured) { if(this->plugin->baseAddress()!=-1) { unsigned int status_reg,data; char ch; 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); } 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; char ch; uartMutex->lock(); if(uartOpened) { read =rs232read(this->uart,&ch,1); } uartMutex->unlock(); if(read==1) { emit this->sendChar(ch); } } msleep(100); } } void UARTPollingThread::sendChar(char c) { if(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() { if(uartOpened) { closeUart(); } this->uart = rs232open((char*)this->portName.toStdString().c_str()); if(this->uart!=badPortValue) { rs232setup(this->uart,8,this->uartSpeed,rs232parityNo,rs232OneStop); uartOpened = true; } return uartOpened; } void UARTPollingThread::closeUart() { uartMutex->lock(); rs232close(this->uart); uartOpened = false; uartMutex->unlock(); } void UARTPollingThread::setPortName(QString name) { this->portName = name; } void UARTPollingThread::setPortSpeedStr(QString speed) { this->uartSpeed = speed.toInt(); } void UARTPollingThread::setPortSpeed(int speed) { 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->fifoDebugEnabled = enable; } 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; } } }