uartpollingthread.cpp
234 lines
| 7.5 KiB
| text/x-c
|
CppLexer
/ APBUARTPLUGIN / uartpollingthread.cpp
Jeandet Alexis
|
r27 | /*------------------------------------------------------------------------------ | ||
-- 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 <socexplorerengine.h> | ||||
UARTPollingThread::UARTPollingThread(socexplorerplugin *parent) : | ||||
QThread((QObject*)parent) | ||||
{ | ||||
this->plugin = parent; | ||||
r28 | uartMutex = new QMutex(); | |||
uartOpened = false; | ||||
fifoDebugConfigured = false; | ||||
r34 | p_fifoDebugEnabled = false; | |||
r28 | this->moveToThread(this); | |||
Jeandet Alexis
|
r27 | } | ||
Jeandet Alexis
|
r29 | UARTPollingThread::~UARTPollingThread() | ||
{ | ||||
this->requestInterruption(); | ||||
while(isRunning()); | ||||
} | ||||
Jeandet Alexis
|
r27 | void UARTPollingThread::run() | ||
{ | ||||
r28 | ||||
r32 | char ch[4097]; | |||
int timeout =10; | ||||
r28 | SocExplorerEngine::message(this->plugin,"Entering APB UART polling thread",3); | |||
Jeandet Alexis
|
r27 | while (!this->isInterruptionRequested()) | ||
{ | ||||
r34 | if(p_fifoDebugEnabled) | |||
Jeandet Alexis
|
r27 | { | ||
r28 | if(fifoDebugConfigured) | |||
Jeandet Alexis
|
r27 | { | ||
r28 | if(this->plugin->baseAddress()!=-1) | |||
{ | ||||
unsigned int status_reg,data; | ||||
char ch; | ||||
r49 | int cnt=0; | |||
r28 | QString printdata=""; | |||
Jeandet Alexis
|
r27 | plugin->parent->Read(&status_reg,1,this->plugin->baseAddress()+APB_UART_STATUS_REG); | ||
r49 | while ((status_reg & 4)==0) | |||
{ | ||||
r28 | 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); | ||||
r49 | cnt++; | |||
if(Q_UNLIKELY(cnt>100)) | ||||
{ | ||||
break; | ||||
} | ||||
r28 | } | |||
r49 | timeout = 100; | |||
r28 | if(printdata!="") | |||
emit apbUartTextReceived(printdata); | ||||
Jeandet Alexis
|
r27 | } | ||
r28 | else | |||
{ | ||||
this->plugin->setBaseAddress(SocExplorerEngine::self()->getEnumDeviceBaseAddress(this->plugin,this->plugin->VID(),this->plugin->PID(),0)); | ||||
} | ||||
Jeandet Alexis
|
r27 | } | ||
else | ||||
{ | ||||
r28 | configFifoDebug(true); | |||
Jeandet Alexis
|
r27 | } | ||
} | ||||
r28 | else | |||
Jeandet Alexis
|
r27 | { | ||
r32 | int read =0,avail=0; | |||
r28 | uartMutex->lock(); | |||
if(uartOpened) | ||||
{ | ||||
r32 | 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'; | ||||
} | ||||
r49 | else | |||
{ | ||||
timeout = 100; | ||||
} | ||||
r28 | } | |||
uartMutex->unlock(); | ||||
Jeandet Alexis
|
r29 | if(read>=1) | ||
r28 | { | |||
r32 | SocExplorerEngine::message(this->plugin,QString("Received %1 char(s) from APBUART").arg(read),3); | |||
emit this->apbUartTextReceived(QString(ch)); | ||||
r28 | } | |||
Jeandet Alexis
|
r27 | } | ||
r49 | msleep(timeout); | |||
Jeandet Alexis
|
r27 | } | ||
} | ||||
void UARTPollingThread::sendChar(char c) | ||||
{ | ||||
r34 | if(p_fifoDebugEnabled) | |||
r28 | { | |||
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(); | ||||
} | ||||
} | ||||
Jeandet Alexis
|
r27 | |||
r28 | bool UARTPollingThread::openUart() | |||
{ | ||||
Jeandet Alexis
|
r29 | uartMutex->lock(); | ||
r28 | if(uartOpened) | |||
{ | ||||
closeUart(); | ||||
} | ||||
Jeandet Alexis
|
r29 | SocExplorerEngine::message(this->plugin,"Opening UART "+this->portName,3); | ||
r28 | this->uart = rs232open((char*)this->portName.toStdString().c_str()); | |||
if(this->uart!=badPortValue) | ||||
{ | ||||
Jeandet Alexis
|
r29 | SocExplorerEngine::message(this->plugin,QString("Configuring UART, speed =%1").arg(this->uartSpeed),3); | ||
r28 | rs232setup(this->uart,8,this->uartSpeed,rs232parityNo,rs232OneStop); | |||
uartOpened = true; | ||||
} | ||||
Jeandet Alexis
|
r29 | uartMutex->unlock(); | ||
r28 | return uartOpened; | |||
Jeandet Alexis
|
r27 | } | ||
r28 | ||||
void UARTPollingThread::closeUart() | ||||
{ | ||||
uartMutex->lock(); | ||||
rs232close(this->uart); | ||||
uartOpened = false; | ||||
uartMutex->unlock(); | ||||
} | ||||
void UARTPollingThread::setPortName(QString name) | ||||
{ | ||||
Jeandet Alexis
|
r29 | SocExplorerEngine::message(this->plugin,"Changing UART port Name: "+name,3); | ||
r28 | this->portName = name; | |||
} | ||||
void UARTPollingThread::setPortSpeedStr(QString speed) | ||||
{ | ||||
Jeandet Alexis
|
r29 | SocExplorerEngine::message(this->plugin,"Changing UART speed: "+speed,3); | ||
r28 | this->uartSpeed = speed.toInt(); | |||
} | ||||
void UARTPollingThread::setPortSpeed(int speed) | ||||
{ | ||||
Jeandet Alexis
|
r29 | SocExplorerEngine::message(this->plugin,QString("Changing UART speed: %1").arg(speed),3); | ||
r28 | 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); | ||||
r34 | this->p_fifoDebugEnabled = enable; | |||
} | ||||
bool UARTPollingThread::fifoDebugEnabled() | ||||
{ | ||||
return p_fifoDebugEnabled; | ||||
r28 | } | |||
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; | ||||
} | ||||
} | ||||
} | ||||