Line 1... |
Line 1... |
/**
|
/*
|
* @file
|
* Copyright 2018 Sergey Khabarov, sergeykhbr@gmail.com
|
* @copyright Copyright 2016 GNSS Sensor Ltd. All right reserved.
|
*
|
* @author Sergey Khabarov - sergeykhbr@gmail.com
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
* @brief COM-port class implementation.
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
*/
|
*/
|
|
|
#include <iostream>
|
#include <iostream>
|
#include <stdio.h>
|
#include <stdio.h>
|
#include <string.h>
|
#include <string.h>
|
#include "api_types.h"
|
#include "api_types.h"
|
#include "api_core.h"
|
#include "api_core.h"
|
#include "coreservices/iserial.h"
|
#include "coreservices/iserial.h"
|
#include "coreservices/isignal.h"
|
|
#include "comport.h"
|
#include "comport.h"
|
|
|
|
|
namespace debugger {
|
namespace debugger {
|
|
|
Line 32... |
Line 40... |
registerAttribute("ComPortSpeed", &comPortSpeed_);
|
registerAttribute("ComPortSpeed", &comPortSpeed_);
|
|
|
isEnable_.make_boolean(true);
|
isEnable_.make_boolean(true);
|
uartSim_.make_string("");
|
uartSim_.make_string("");
|
logFile_.make_string("uart0.log");
|
logFile_.make_string("uart0.log");
|
|
logfile_ = NULL;
|
comPortName_.make_string("");
|
comPortName_.make_string("");
|
comPortSpeed_.make_int64(115200);
|
comPortSpeed_.make_int64(115200);
|
portListeners_.make_list(0);
|
portListeners_.make_list(0);
|
iuartSim_ = 0;
|
iuartSim_ = 0;
|
portOpened_ = false;
|
portOpened_ = false;
|
RISCV_mutex_init(&mutexListeners_);
|
RISCV_mutex_init(&mutexListeners_);
|
|
prtHandler_ = 0;
|
}
|
}
|
|
|
ComPortService::~ComPortService() {
|
ComPortService::~ComPortService() {
|
RISCV_mutex_destroy(&mutexListeners_);
|
RISCV_mutex_destroy(&mutexListeners_);
|
if (logfile_) {
|
if (logfile_) {
|
Line 80... |
Line 90... |
logFile_.to_string());
|
logFile_.to_string());
|
logfile_ = fopen(logFile_.to_string(), "w");
|
logfile_ = fopen(logFile_.to_string(), "w");
|
}
|
}
|
}
|
}
|
|
|
|
void ComPortService::predeleteService() {
|
|
if (isSimulation_ && iuartSim_) {
|
|
iuartSim_->unregisterRawListener(static_cast<IRawListener *>(this));
|
|
}
|
|
}
|
|
|
//#define READ_RAWDATA_FROM_FILE
|
//#define READ_RAWDATA_FROM_FILE
|
void ComPortService::busyLoop() {
|
void ComPortService::busyLoop() {
|
char tbuf[4096];
|
char tbuf[4096];
|
int tbuf_cnt;
|
int tbuf_cnt;
|
#ifdef READ_RAWDATA_FROM_FILE
|
#ifdef READ_RAWDATA_FROM_FILE
|
Line 91... |
Line 107... |
char logbuf[1024];
|
char logbuf[1024];
|
int logbuf_sz;
|
int logbuf_sz;
|
#endif
|
#endif
|
|
|
while (isEnabled()) {
|
while (isEnabled()) {
|
if (!isSimulation_ && !portOpened_) {
|
|
int err = openSerialPort(comPortName_.to_string(),
|
|
comPortSpeed_.to_int(), &hPort_);
|
|
if (err < 0) {
|
|
RISCV_error("Openning %s at %d . . .failed",
|
|
comPortName_.to_string(), comPortSpeed_.to_int());
|
|
RISCV_sleep_ms(1000);
|
|
#ifdef READ_RAWDATA_FROM_FILE
|
#ifdef READ_RAWDATA_FROM_FILE
|
logbuf_sz = fread(logbuf, 1, sizeof(logbuf), script);
|
logbuf_sz = fread(logbuf, 1, sizeof(logbuf), script);
|
if (logbuf_sz) {
|
if (logbuf_sz) {
|
for (unsigned i = 0; i < portListeners_.size(); i++) {
|
for (unsigned i = 0; i < portListeners_.size(); i++) {
|
IRawListener *ilstn = static_cast<IRawListener *>(
|
IRawListener *ilstn = static_cast<IRawListener *>(
|
portListeners_[i].to_iface());
|
portListeners_[i].to_iface());
|
ilstn->updateData(logbuf, logbuf_sz);
|
ilstn->updateData(logbuf, logbuf_sz);
|
}
|
}
|
|
} else {
|
|
fseek(script, 0, SEEK_SET);
|
}
|
}
|
#endif
|
#endif
|
|
if (!isSimulation_ && !portOpened_) {
|
|
AttributeType settings;
|
|
settings.make_list(4);
|
|
settings[0u].make_int64(comPortSpeed_.to_int());
|
|
int err = openPort(comPortName_.to_string(), settings);
|
|
if (err < 0) {
|
|
RISCV_error("Openning %s at %d . . .failed",
|
|
comPortName_.to_string(), comPortSpeed_.to_int());
|
|
RISCV_sleep_ms(1000);
|
continue;
|
continue;
|
} else {
|
} else {
|
portOpened_ = true;
|
portOpened_ = true;
|
}
|
}
|
}
|
}
|
Line 121... |
Line 141... |
tbuf[tbuf_cnt++] = txFifo_.get();
|
tbuf[tbuf_cnt++] = txFifo_.get();
|
}
|
}
|
if (tbuf_cnt) {
|
if (tbuf_cnt) {
|
if (isSimulation_ && iuartSim_) {
|
if (isSimulation_ && iuartSim_) {
|
iuartSim_->writeData(tbuf, tbuf_cnt);
|
iuartSim_->writeData(tbuf, tbuf_cnt);
|
} else if (!isSimulation_ && hPort_) {
|
} else if (!isSimulation_ && prtHandler_) {
|
writeSerialPort(&hPort_, tbuf, tbuf_cnt);
|
writeSerialPort(&prtHandler_, tbuf, tbuf_cnt);
|
}
|
}
|
}
|
}
|
|
|
// Receiveing...
|
// Receiveing...
|
if (!isSimulation_ && hPort_) {
|
if (!isSimulation_ && prtHandler_) {
|
tbuf_cnt = readSerialPort(&hPort_, tbuf, sizeof(tbuf) - tbuf_cnt);
|
tbuf_cnt = readSerialPort(&prtHandler_, tbuf,
|
|
sizeof(tbuf) - tbuf_cnt);
|
if (tbuf_cnt < 0) {
|
if (tbuf_cnt < 0) {
|
closeSerialPort(&hPort_);
|
closePort();
|
portOpened_ = false;
|
portOpened_ = false;
|
continue;
|
continue;
|
}
|
}
|
} else if (isSimulation_) {
|
} else if (isSimulation_) {
|
tbuf_cnt = 0;
|
tbuf_cnt = 0;
|