QP/C++
qs.cpp
Go to the documentation of this file.
00001 
00002 // Product: QS/C++
00003 // Last Updated for Version: 4.4.00
00004 // Date of the Last Update:  Mar 28, 2012
00005 //
00006 //                    Q u a n t u m     L e a P s
00007 //                    ---------------------------
00008 //                    innovating embedded systems
00009 //
00010 // Copyright (C) 2002-2012 Quantum Leaps, LLC. All rights reserved.
00011 //
00012 // This program is open source software: you can redistribute it and/or
00013 // modify it under the terms of the GNU General Public License as published
00014 // by the Free Software Foundation, either version 2 of the License, or
00015 // (at your option) any later version.
00016 //
00017 // Alternatively, this program may be distributed and modified under the
00018 // terms of Quantum Leaps commercial licenses, which expressly supersede
00019 // the GNU General Public License and are specifically designed for
00020 // licensees interested in retaining the proprietary status of their code.
00021 //
00022 // This program is distributed in the hope that it will be useful,
00023 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00024 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00025 // GNU General Public License for more details.
00026 //
00027 // You should have received a copy of the GNU General Public License
00028 // along with this program. If not, see <http://www.gnu.org/licenses/>.
00029 //
00030 // Contact information:
00031 // Quantum Leaps Web sites: http://www.quantum-leaps.com
00032 //                          http://www.state-machine.com
00033 // e-mail:                  info@quantum-leaps.com
00035 #include "qs_pkg.h"
00036 
00041 
00042 QP_BEGIN_
00043 
00044 //............................................................................
00045 uint8_t QS::glbFilter_[32];                                // global QS filter
00046 
00047 //............................................................................
00048 uint8_t *QS_ring_;                  // pointer to the start of the ring buffer
00049 QSCtr QS_end_;                         // offset of the end of the ring buffer
00050 QSCtr QS_head_;                  // offset to where next byte will be inserted
00051 QSCtr QS_tail_;                 // offset of where next byte will be extracted
00052 QSCtr QS_used_;                // number of bytes currently in the ring buffer
00053 uint8_t QS_seq_;                                 // the record sequence number
00054 uint8_t QS_chksum_;                      // the checksum of the current record
00055 uint8_t QS_full_;                       // the ring buffer is temporarily full
00056 
00057 //............................................................................
00058 char_t const Q_ROM * Q_ROM_VAR QS::getVersion(void) {
00059     uint8_t const u8_zero = static_cast<uint8_t>('0');
00060     static char_t const Q_ROM Q_ROM_VAR version[] = {
00061         static_cast<char_t>(((QP_VERSION >> 12) & 0xFU) + u8_zero),
00062         static_cast<char_t>('.'),
00063         static_cast<char_t>(((QP_VERSION >>  8) & 0xFU) + u8_zero),
00064         static_cast<char_t>('.'),
00065         static_cast<char_t>(((QP_VERSION >>  4) & 0xFU) + u8_zero),
00066         static_cast<char_t>((QP_VERSION         & 0xFU) + u8_zero),
00067         static_cast<char_t>('\0')
00068     };
00069     return version;
00070 }
00071 //............................................................................
00072 void QS::initBuf(uint8_t sto[], uint32_t const stoSize) {
00073     QS_ring_ = &sto[0];
00074     QS_end_  = static_cast<QSCtr>(stoSize);
00075 }
00076 //............................................................................
00077 void QS::filterOn(uint8_t const rec) {
00078     if (rec == QS_ALL_RECORDS) {
00079         uint8_t i;
00080         for (i = static_cast<uint8_t>(0);
00081              i < static_cast<uint8_t>(sizeof(glbFilter_));
00082              ++i)
00083         {
00084             glbFilter_[i] = static_cast<uint8_t>(0xFF);
00085         }
00086     }
00087     else {
00088         glbFilter_[rec >> 3] |=
00089             static_cast<uint8_t>(1U << (rec & static_cast<uint8_t>(0x07)));
00090     }
00091 }
00092 //............................................................................
00093 void QS::filterOff(uint8_t const rec) {
00094     if (rec == QS_ALL_RECORDS) {
00095         uint8_t i;
00096         for (i = static_cast<uint8_t>(0);
00097             i < static_cast<uint8_t>(sizeof(glbFilter_));
00098             ++i)
00099         {
00100             glbFilter_[i] = static_cast<uint8_t>(0);
00101         }
00102     }
00103     else {
00104         glbFilter_[rec >> 3] &= static_cast<uint8_t>(
00105            ~static_cast<uint8_t>((1U << (rec & static_cast<uint8_t>(0x07)))));
00106     }
00107 }
00108 //............................................................................
00109 void QS::begin(uint8_t const rec) {
00110     QS_chksum_ = static_cast<uint8_t>(0);                // clear the checksum
00111     ++QS_seq_;                         // always increment the sequence number
00112     QS_INSERT_ESC_BYTE(QS_seq_)                   // store the sequence number
00113     QS_INSERT_ESC_BYTE(rec)                             // store the record ID
00114 }
00115 //............................................................................
00116 void QS::end(void) {
00117     QS_INSERT_CHKSUM_BYTE()
00118     QS_INSERT_BYTE(QS_FRAME)
00119     if (QS_used_ > QS_end_) {                    // overrun over the old data?
00120         QS_tail_ = QS_head_;                 // shift the tail to the old data
00121         QS_used_ = QS_end_;                        // the whole buffer is used
00122     }
00123 }
00124 //............................................................................
00125 void QS::u8(uint8_t const format, uint8_t const d) {
00126     QS_INSERT_ESC_BYTE(format)
00127     QS_INSERT_ESC_BYTE(d)
00128 }
00129 //............................................................................
00130 void QS::u16(uint8_t const format, uint16_t d) {
00131     QS_INSERT_ESC_BYTE(format)
00132     QS_INSERT_ESC_BYTE(static_cast<uint8_t>(d))
00133     d >>= 8;
00134     QS_INSERT_ESC_BYTE(static_cast<uint8_t>(d))
00135 }
00136 //............................................................................
00137 void QS::u32(uint8_t const format, uint32_t d) {
00138     QS_INSERT_ESC_BYTE(format)
00139     QS_INSERT_ESC_BYTE(static_cast<uint8_t>(d))
00140     d >>= 8;
00141     QS_INSERT_ESC_BYTE(static_cast<uint8_t>(d))
00142     d >>= 8;
00143     QS_INSERT_ESC_BYTE(static_cast<uint8_t>(d))
00144     d >>= 8;
00145     QS_INSERT_ESC_BYTE(static_cast<uint8_t>(d))
00146 }
00147 
00148 QP_END_