00001 /* 00002 **************************************************************************** 00003 * 00004 * simulavr - A simulator for the Atmel AVR family of microcontrollers. 00005 * Copyright (C) 2001, 2002, 2003, 2004 Klaus Rudolph 00006 * 00007 * This program is free software; you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License as published by 00009 * the Free Software Foundation; either version 2 of the License, or 00010 * (at your option) any later version. 00011 * 00012 * This program is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with this program; if not, write to the Free Software 00019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 * 00021 **************************************************************************** 00022 * 00023 * $Id$ 00024 */ 00025 00026 #include "serialtx.h" 00027 #include "systemclock.h" 00028 #include "string2.h" 00029 #include "avrerror.h" 00030 00031 using namespace std; 00032 00033 SerialTxBuffered::SerialTxBuffered() 00034 { 00035 allPins["tx"] = &tx; 00036 Reset(); 00037 } 00038 00039 void SerialTxBuffered::Reset() 00040 { 00041 txState=TX_DISABLED; 00042 receiveInHex=false; 00043 baudrate=115200; 00044 maxBitCnt=8; 00045 tx='H'; 00046 } 00047 00048 Pin* SerialTxBuffered::GetPin(const char* name) 00049 { 00050 return allPins[name]; 00051 } 00052 00053 int SerialTxBuffered::Step( 00054 bool &trueHwStep, SystemClockOffset *timeToNextStepIn_ns) 00055 { 00056 switch (txState) { 00057 case TX_SEND_STARTBIT: 00058 data=*(inputBuffer.begin()); 00059 inputBuffer.erase(inputBuffer.begin()); 00060 tx='L'; 00061 bitCnt=0; 00062 //all we measures are in ns !; 00063 *timeToNextStepIn_ns=(SystemClockOffset)1e9/baudrate; 00064 txState=TX_SEND_DATABIT; 00065 break; 00066 00067 case TX_SEND_DATABIT: 00068 if (( data >> bitCnt ) & 0x01) { 00069 tx='H'; 00070 } else { 00071 tx='L'; 00072 } 00073 //all we measures are in ns !; 00074 *timeToNextStepIn_ns=(SystemClockOffset)1e9/baudrate; 00075 bitCnt++; 00076 if(bitCnt>=maxBitCnt) txState=TX_SEND_STOPBIT; 00077 break; 00078 00079 case TX_SEND_STOPBIT: 00080 tx='H'; 00081 txState=TX_STOPPING; 00082 //all we measures are in ns !; 00083 *timeToNextStepIn_ns=(SystemClockOffset)1e9/baudrate; 00084 break; 00085 00086 case TX_STOPPING: 00087 if (inputBuffer.size()>0) { //another char to send! 00088 txState=TX_SEND_STARTBIT; 00089 *timeToNextStepIn_ns=0;//(SystemClockOffset)1e9/baudrate; 00090 } else { 00091 txState=TX_DISABLED; 00092 *timeToNextStepIn_ns=-1; //last step, do not continue 00093 } 00094 00095 break; 00096 00097 default: 00098 avr_error("Illegal state in SerialTx"); 00099 } 00100 00101 00102 return 0; 00103 00104 } 00105 00106 void SerialTxBuffered::Send(unsigned char data) 00107 { 00108 inputBuffer.push_back(data); //write new char to input buffer 00109 00110 cerr << "TX: " << hex << data << " "; 00111 //if we not active, activate tx machine now 00112 if (txState==TX_DISABLED) { 00113 txState=TX_SEND_STARTBIT; 00114 SystemClock::Instance().Add(this); 00115 } 00116 } 00117 00118 void SerialTxBuffered::SetBaudRate(SystemClockOffset baud){ 00119 baudrate = baud; 00120 } 00121 00122 void SerialTxBuffered::SetHexInput(bool newValue){ 00123 receiveInHex = newValue; 00124 } 00125 00126 00127 00128 // =========================================================================== 00129 // =========================================================================== 00130 // =========================================================================== 00131 00132 00133 SerialTx::SerialTx(UserInterface *ui, const char *name, const char *baseWindow) 00134 { 00135 ostringstream os; 00136 os << "create SerialTx " << name << " " << baseWindow << endl; 00137 ui->Write(os.str()); 00138 ui->AddExternalType(name, this); 00139 Reset(); 00140 } 00141 00142 void SerialTx::SetNewValueFromUi(const string &s) { 00143 if ( receiveInHex ) { 00144 unsigned char value; 00145 bool rc; 00146 rc = StringToUnsignedChar( s.c_str(), &value, NULL, 16 ); 00147 if ( !rc ) { 00148 cerr << "SerialTx::SetNewValueFromUi:: bad conversion" << endl; 00149 } else { 00150 // cerr << "SerialTx::Send " << hex << (unsigned int) value << endl; 00151 Send(value); 00152 } 00153 } else { 00154 for(int i=0; i < s.length(); i++) 00155 Send(s[i]); 00156 } 00157 }