chokchai Hantanong
/
RtosTest
LED Fork
Fork of RtosTest by
scmRTOS/Common/usrlib.h@0:331db0b44b67, 2010-09-21 (annotated)
- Committer:
- mbed714
- Date:
- Tue Sep 21 19:48:05 2010 +0000
- Revision:
- 0:331db0b44b67
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed714 | 0:331db0b44b67 | 1 | //****************************************************************************** |
mbed714 | 0:331db0b44b67 | 2 | //* |
mbed714 | 0:331db0b44b67 | 3 | //* FULLNAME: Single-Chip Microcontroller Real-Time Operating System |
mbed714 | 0:331db0b44b67 | 4 | //* |
mbed714 | 0:331db0b44b67 | 5 | //* NICKNAME: scmRTOS |
mbed714 | 0:331db0b44b67 | 6 | //* |
mbed714 | 0:331db0b44b67 | 7 | //* PURPOSE: User Suport Library Header |
mbed714 | 0:331db0b44b67 | 8 | //* |
mbed714 | 0:331db0b44b67 | 9 | //* Version: 3.10 |
mbed714 | 0:331db0b44b67 | 10 | //* |
mbed714 | 0:331db0b44b67 | 11 | //* $Revision: 256 $ |
mbed714 | 0:331db0b44b67 | 12 | //* $Date:: 2010-01-22 #$ |
mbed714 | 0:331db0b44b67 | 13 | //* |
mbed714 | 0:331db0b44b67 | 14 | //* Copyright (c) 2003-2010, Harry E. Zhurov |
mbed714 | 0:331db0b44b67 | 15 | //* |
mbed714 | 0:331db0b44b67 | 16 | //* Permission is hereby granted, free of charge, to any person |
mbed714 | 0:331db0b44b67 | 17 | //* obtaining a copy of this software and associated documentation |
mbed714 | 0:331db0b44b67 | 18 | //* files (the "Software"), to deal in the Software without restriction, |
mbed714 | 0:331db0b44b67 | 19 | //* including without limitation the rights to use, copy, modify, merge, |
mbed714 | 0:331db0b44b67 | 20 | //* publish, distribute, sublicense, and/or sell copies of the Software, |
mbed714 | 0:331db0b44b67 | 21 | //* and to permit persons to whom the Software is furnished to do so, |
mbed714 | 0:331db0b44b67 | 22 | //* subject to the following conditions: |
mbed714 | 0:331db0b44b67 | 23 | //* |
mbed714 | 0:331db0b44b67 | 24 | //* The above copyright notice and this permission notice shall be included |
mbed714 | 0:331db0b44b67 | 25 | //* in all copies or substantial portions of the Software. |
mbed714 | 0:331db0b44b67 | 26 | //* |
mbed714 | 0:331db0b44b67 | 27 | //* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
mbed714 | 0:331db0b44b67 | 28 | //* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
mbed714 | 0:331db0b44b67 | 29 | //* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
mbed714 | 0:331db0b44b67 | 30 | //* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
mbed714 | 0:331db0b44b67 | 31 | //* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
mbed714 | 0:331db0b44b67 | 32 | //* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH |
mbed714 | 0:331db0b44b67 | 33 | //* THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
mbed714 | 0:331db0b44b67 | 34 | //* |
mbed714 | 0:331db0b44b67 | 35 | //* ================================================================= |
mbed714 | 0:331db0b44b67 | 36 | //* See http://scmrtos.sourceforge.net for documentation, latest |
mbed714 | 0:331db0b44b67 | 37 | //* information, license and contact details. |
mbed714 | 0:331db0b44b67 | 38 | //* ================================================================= |
mbed714 | 0:331db0b44b67 | 39 | //* |
mbed714 | 0:331db0b44b67 | 40 | //****************************************************************************** |
mbed714 | 0:331db0b44b67 | 41 | |
mbed714 | 0:331db0b44b67 | 42 | #ifndef USRLIB_H |
mbed714 | 0:331db0b44b67 | 43 | #define USRLIB_H |
mbed714 | 0:331db0b44b67 | 44 | |
mbed714 | 0:331db0b44b67 | 45 | #include <commdefs.h> |
mbed714 | 0:331db0b44b67 | 46 | |
mbed714 | 0:331db0b44b67 | 47 | //------------------------------------------------------------------------------ |
mbed714 | 0:331db0b44b67 | 48 | // |
mbed714 | 0:331db0b44b67 | 49 | // DESCRIPTON: user namespace for some useful types and functions |
mbed714 | 0:331db0b44b67 | 50 | // |
mbed714 | 0:331db0b44b67 | 51 | // |
mbed714 | 0:331db0b44b67 | 52 | namespace usr |
mbed714 | 0:331db0b44b67 | 53 | { |
mbed714 | 0:331db0b44b67 | 54 | //------------------------------------------------------------------------------ |
mbed714 | 0:331db0b44b67 | 55 | // |
mbed714 | 0:331db0b44b67 | 56 | /// The Circular Buffer |
mbed714 | 0:331db0b44b67 | 57 | // |
mbed714 | 0:331db0b44b67 | 58 | /// Byte-wide FIFO. |
mbed714 | 0:331db0b44b67 | 59 | // |
mbed714 | 0:331db0b44b67 | 60 | /// Allows to: |
mbed714 | 0:331db0b44b67 | 61 | /// add byte, |
mbed714 | 0:331db0b44b67 | 62 | /// get byte, |
mbed714 | 0:331db0b44b67 | 63 | /// write bytes from array, |
mbed714 | 0:331db0b44b67 | 64 | /// read bytes to array, |
mbed714 | 0:331db0b44b67 | 65 | /// and some other service actions. |
mbed714 | 0:331db0b44b67 | 66 | // |
mbed714 | 0:331db0b44b67 | 67 | class TCbuf |
mbed714 | 0:331db0b44b67 | 68 | { |
mbed714 | 0:331db0b44b67 | 69 | public: |
mbed714 | 0:331db0b44b67 | 70 | TCbuf(byte* const Address, const byte Size); |
mbed714 | 0:331db0b44b67 | 71 | bool write(const byte* data, const byte Count); |
mbed714 | 0:331db0b44b67 | 72 | void read(byte* const data, const byte Count); |
mbed714 | 0:331db0b44b67 | 73 | byte get_count() const { return count; } |
mbed714 | 0:331db0b44b67 | 74 | byte get_free_size() const { return size - count; } |
mbed714 | 0:331db0b44b67 | 75 | byte get_byte(const byte index) const; |
mbed714 | 0:331db0b44b67 | 76 | void clear() { count = 0; last = first; } |
mbed714 | 0:331db0b44b67 | 77 | bool put(const byte item); |
mbed714 | 0:331db0b44b67 | 78 | byte get(); |
mbed714 | 0:331db0b44b67 | 79 | |
mbed714 | 0:331db0b44b67 | 80 | private: |
mbed714 | 0:331db0b44b67 | 81 | //------------------------------------------------------------------------------ |
mbed714 | 0:331db0b44b67 | 82 | // |
mbed714 | 0:331db0b44b67 | 83 | // DESCRIPTON: For internal purposes |
mbed714 | 0:331db0b44b67 | 84 | // |
mbed714 | 0:331db0b44b67 | 85 | void push(const byte item); ///< Use this function with care - it doesn't perform free size check |
mbed714 | 0:331db0b44b67 | 86 | byte pop(); ///< Use this function with care - it doesn't perform count check |
mbed714 | 0:331db0b44b67 | 87 | //------------------------------------------------------------------------------ |
mbed714 | 0:331db0b44b67 | 88 | |
mbed714 | 0:331db0b44b67 | 89 | private: |
mbed714 | 0:331db0b44b67 | 90 | byte* buf; |
mbed714 | 0:331db0b44b67 | 91 | byte size; |
mbed714 | 0:331db0b44b67 | 92 | volatile byte count; |
mbed714 | 0:331db0b44b67 | 93 | byte first; |
mbed714 | 0:331db0b44b67 | 94 | byte last; |
mbed714 | 0:331db0b44b67 | 95 | }; |
mbed714 | 0:331db0b44b67 | 96 | //------------------------------------------------------------------------------ |
mbed714 | 0:331db0b44b67 | 97 | |
mbed714 | 0:331db0b44b67 | 98 | |
mbed714 | 0:331db0b44b67 | 99 | |
mbed714 | 0:331db0b44b67 | 100 | //----------------------------------------------------------------------- |
mbed714 | 0:331db0b44b67 | 101 | // |
mbed714 | 0:331db0b44b67 | 102 | /// The Ring Buffer Template |
mbed714 | 0:331db0b44b67 | 103 | /// |
mbed714 | 0:331db0b44b67 | 104 | /// Carries out FIFO functionality for |
mbed714 | 0:331db0b44b67 | 105 | /// arbitrary data types |
mbed714 | 0:331db0b44b67 | 106 | /// |
mbed714 | 0:331db0b44b67 | 107 | /// Allows to: |
mbed714 | 0:331db0b44b67 | 108 | /// add item to back (default), |
mbed714 | 0:331db0b44b67 | 109 | /// add item to front, |
mbed714 | 0:331db0b44b67 | 110 | /// get item at front (default), |
mbed714 | 0:331db0b44b67 | 111 | /// get item from back, |
mbed714 | 0:331db0b44b67 | 112 | /// write items from array, |
mbed714 | 0:331db0b44b67 | 113 | /// read items to array and some other actions |
mbed714 | 0:331db0b44b67 | 114 | // |
mbed714 | 0:331db0b44b67 | 115 | // |
mbed714 | 0:331db0b44b67 | 116 | // |
mbed714 | 0:331db0b44b67 | 117 | template<typename T, word Size, typename S = byte> |
mbed714 | 0:331db0b44b67 | 118 | class ring_buffer |
mbed714 | 0:331db0b44b67 | 119 | { |
mbed714 | 0:331db0b44b67 | 120 | public: |
mbed714 | 0:331db0b44b67 | 121 | ring_buffer() : Count(0), First(0), Last(0) { } |
mbed714 | 0:331db0b44b67 | 122 | |
mbed714 | 0:331db0b44b67 | 123 | //---------------------------------------------------------------- |
mbed714 | 0:331db0b44b67 | 124 | // |
mbed714 | 0:331db0b44b67 | 125 | // Data transfer functions |
mbed714 | 0:331db0b44b67 | 126 | // |
mbed714 | 0:331db0b44b67 | 127 | bool write(const T* data, const S cnt); |
mbed714 | 0:331db0b44b67 | 128 | void read(T* const data, const S cnt); |
mbed714 | 0:331db0b44b67 | 129 | |
mbed714 | 0:331db0b44b67 | 130 | bool push_back(const T item); |
mbed714 | 0:331db0b44b67 | 131 | bool push_front(const T item); |
mbed714 | 0:331db0b44b67 | 132 | |
mbed714 | 0:331db0b44b67 | 133 | T pop_front(); |
mbed714 | 0:331db0b44b67 | 134 | T pop_back(); |
mbed714 | 0:331db0b44b67 | 135 | |
mbed714 | 0:331db0b44b67 | 136 | bool push(const T item) { return push_back(item); } |
mbed714 | 0:331db0b44b67 | 137 | T pop() { return pop_front(); } |
mbed714 | 0:331db0b44b67 | 138 | |
mbed714 | 0:331db0b44b67 | 139 | //---------------------------------------------------------------- |
mbed714 | 0:331db0b44b67 | 140 | // |
mbed714 | 0:331db0b44b67 | 141 | // Service functions |
mbed714 | 0:331db0b44b67 | 142 | // |
mbed714 | 0:331db0b44b67 | 143 | S get_count() const { return Count; } |
mbed714 | 0:331db0b44b67 | 144 | S get_free_size() const { return Size - Count; } |
mbed714 | 0:331db0b44b67 | 145 | T& operator[](const S index); |
mbed714 | 0:331db0b44b67 | 146 | void flush() { Count = 0; Last = First; } |
mbed714 | 0:331db0b44b67 | 147 | |
mbed714 | 0:331db0b44b67 | 148 | private: |
mbed714 | 0:331db0b44b67 | 149 | //-------------------------------------------------------------- |
mbed714 | 0:331db0b44b67 | 150 | // DESCRIPTON: For internal purposes |
mbed714 | 0:331db0b44b67 | 151 | // Use this functions with care: it don't perform |
mbed714 | 0:331db0b44b67 | 152 | // free size and count check |
mbed714 | 0:331db0b44b67 | 153 | // |
mbed714 | 0:331db0b44b67 | 154 | void push_item(const T item); |
mbed714 | 0:331db0b44b67 | 155 | void push_item_front(const T item); |
mbed714 | 0:331db0b44b67 | 156 | T pop_item(); |
mbed714 | 0:331db0b44b67 | 157 | T pop_item_back(); |
mbed714 | 0:331db0b44b67 | 158 | //-------------------------------------------------------------- |
mbed714 | 0:331db0b44b67 | 159 | |
mbed714 | 0:331db0b44b67 | 160 | private: |
mbed714 | 0:331db0b44b67 | 161 | S Count; |
mbed714 | 0:331db0b44b67 | 162 | S First; |
mbed714 | 0:331db0b44b67 | 163 | S Last; |
mbed714 | 0:331db0b44b67 | 164 | T Buf[Size]; |
mbed714 | 0:331db0b44b67 | 165 | }; |
mbed714 | 0:331db0b44b67 | 166 | //------------------------------------------------------------------ |
mbed714 | 0:331db0b44b67 | 167 | } |
mbed714 | 0:331db0b44b67 | 168 | //--------------------------------------------------------------------------- |
mbed714 | 0:331db0b44b67 | 169 | |
mbed714 | 0:331db0b44b67 | 170 | |
mbed714 | 0:331db0b44b67 | 171 | //------------------------------------------------------------------------------ |
mbed714 | 0:331db0b44b67 | 172 | // |
mbed714 | 0:331db0b44b67 | 173 | // The ring buffer function-member definitions |
mbed714 | 0:331db0b44b67 | 174 | // |
mbed714 | 0:331db0b44b67 | 175 | // |
mbed714 | 0:331db0b44b67 | 176 | // |
mbed714 | 0:331db0b44b67 | 177 | template<typename T, word Size, typename S> |
mbed714 | 0:331db0b44b67 | 178 | bool usr::ring_buffer<T, Size, S>::write(const T* data, const S cnt) |
mbed714 | 0:331db0b44b67 | 179 | { |
mbed714 | 0:331db0b44b67 | 180 | if( cnt > (Size - Count) ) |
mbed714 | 0:331db0b44b67 | 181 | return false; |
mbed714 | 0:331db0b44b67 | 182 | |
mbed714 | 0:331db0b44b67 | 183 | for(S i = 0; i < cnt; i++) |
mbed714 | 0:331db0b44b67 | 184 | push_item(*(data++)); |
mbed714 | 0:331db0b44b67 | 185 | |
mbed714 | 0:331db0b44b67 | 186 | return true; |
mbed714 | 0:331db0b44b67 | 187 | } |
mbed714 | 0:331db0b44b67 | 188 | //------------------------------------------------------------------------------ |
mbed714 | 0:331db0b44b67 | 189 | template<typename T, word Size, typename S> |
mbed714 | 0:331db0b44b67 | 190 | void usr::ring_buffer<T, Size, S>::read(T* data, const S cnt) |
mbed714 | 0:331db0b44b67 | 191 | { |
mbed714 | 0:331db0b44b67 | 192 | S nItems = cnt <= Count ? cnt : Count; |
mbed714 | 0:331db0b44b67 | 193 | |
mbed714 | 0:331db0b44b67 | 194 | for(S i = 0; i < nItems; i++) |
mbed714 | 0:331db0b44b67 | 195 | data[i] = pop_item(); |
mbed714 | 0:331db0b44b67 | 196 | } |
mbed714 | 0:331db0b44b67 | 197 | //------------------------------------------------------------------------------ |
mbed714 | 0:331db0b44b67 | 198 | template<typename T, word Size, typename S> |
mbed714 | 0:331db0b44b67 | 199 | T& usr::ring_buffer<T, Size, S>::operator[](const S index) |
mbed714 | 0:331db0b44b67 | 200 | { |
mbed714 | 0:331db0b44b67 | 201 | S x = First + index; |
mbed714 | 0:331db0b44b67 | 202 | |
mbed714 | 0:331db0b44b67 | 203 | if(x < Size) |
mbed714 | 0:331db0b44b67 | 204 | return Buf[x]; |
mbed714 | 0:331db0b44b67 | 205 | else |
mbed714 | 0:331db0b44b67 | 206 | return Buf[x - Size]; |
mbed714 | 0:331db0b44b67 | 207 | } |
mbed714 | 0:331db0b44b67 | 208 | |
mbed714 | 0:331db0b44b67 | 209 | //------------------------------------------------------------------------------ |
mbed714 | 0:331db0b44b67 | 210 | template<typename T, word Size, typename S> |
mbed714 | 0:331db0b44b67 | 211 | bool usr::ring_buffer<T, Size, S>::push_back(const T item) |
mbed714 | 0:331db0b44b67 | 212 | { |
mbed714 | 0:331db0b44b67 | 213 | if(Count == Size) |
mbed714 | 0:331db0b44b67 | 214 | return false; |
mbed714 | 0:331db0b44b67 | 215 | |
mbed714 | 0:331db0b44b67 | 216 | push_item(item); |
mbed714 | 0:331db0b44b67 | 217 | return true; |
mbed714 | 0:331db0b44b67 | 218 | } |
mbed714 | 0:331db0b44b67 | 219 | //------------------------------------------------------------------------------ |
mbed714 | 0:331db0b44b67 | 220 | template<typename T, word Size, typename S> |
mbed714 | 0:331db0b44b67 | 221 | bool usr::ring_buffer<T, Size, S>::push_front(const T item) |
mbed714 | 0:331db0b44b67 | 222 | { |
mbed714 | 0:331db0b44b67 | 223 | if(Count == Size) |
mbed714 | 0:331db0b44b67 | 224 | return false; |
mbed714 | 0:331db0b44b67 | 225 | |
mbed714 | 0:331db0b44b67 | 226 | push_item_front(item); |
mbed714 | 0:331db0b44b67 | 227 | return true; |
mbed714 | 0:331db0b44b67 | 228 | } |
mbed714 | 0:331db0b44b67 | 229 | //------------------------------------------------------------------------------ |
mbed714 | 0:331db0b44b67 | 230 | template<typename T, word Size, typename S> |
mbed714 | 0:331db0b44b67 | 231 | T usr::ring_buffer<T, Size, S>::pop_front() |
mbed714 | 0:331db0b44b67 | 232 | { |
mbed714 | 0:331db0b44b67 | 233 | if(Count) |
mbed714 | 0:331db0b44b67 | 234 | return pop_item(); |
mbed714 | 0:331db0b44b67 | 235 | else |
mbed714 | 0:331db0b44b67 | 236 | return Buf[First]; |
mbed714 | 0:331db0b44b67 | 237 | } |
mbed714 | 0:331db0b44b67 | 238 | //------------------------------------------------------------------------------ |
mbed714 | 0:331db0b44b67 | 239 | template<typename T, word Size, typename S> |
mbed714 | 0:331db0b44b67 | 240 | T usr::ring_buffer<T, Size, S>::pop_back() |
mbed714 | 0:331db0b44b67 | 241 | { |
mbed714 | 0:331db0b44b67 | 242 | if(Count) |
mbed714 | 0:331db0b44b67 | 243 | return pop_item_back(); |
mbed714 | 0:331db0b44b67 | 244 | else |
mbed714 | 0:331db0b44b67 | 245 | return Buf[First]; |
mbed714 | 0:331db0b44b67 | 246 | } |
mbed714 | 0:331db0b44b67 | 247 | //------------------------------------------------------------------------------ |
mbed714 | 0:331db0b44b67 | 248 | template<typename T, word Size, typename S> |
mbed714 | 0:331db0b44b67 | 249 | void usr::ring_buffer<T, Size, S>::push_item(const T item) |
mbed714 | 0:331db0b44b67 | 250 | { |
mbed714 | 0:331db0b44b67 | 251 | Buf[Last] = item; |
mbed714 | 0:331db0b44b67 | 252 | Last++; |
mbed714 | 0:331db0b44b67 | 253 | Count++; |
mbed714 | 0:331db0b44b67 | 254 | |
mbed714 | 0:331db0b44b67 | 255 | if(Last == Size) |
mbed714 | 0:331db0b44b67 | 256 | Last = 0; |
mbed714 | 0:331db0b44b67 | 257 | } |
mbed714 | 0:331db0b44b67 | 258 | //------------------------------------------------------------------------------ |
mbed714 | 0:331db0b44b67 | 259 | template<typename T, word Size, typename S> |
mbed714 | 0:331db0b44b67 | 260 | void usr::ring_buffer<T, Size, S>::push_item_front(const T item) |
mbed714 | 0:331db0b44b67 | 261 | { |
mbed714 | 0:331db0b44b67 | 262 | if(First == 0) |
mbed714 | 0:331db0b44b67 | 263 | First = Size - 1; |
mbed714 | 0:331db0b44b67 | 264 | else |
mbed714 | 0:331db0b44b67 | 265 | --First; |
mbed714 | 0:331db0b44b67 | 266 | Buf[First] = item; |
mbed714 | 0:331db0b44b67 | 267 | Count++; |
mbed714 | 0:331db0b44b67 | 268 | } |
mbed714 | 0:331db0b44b67 | 269 | //------------------------------------------------------------------------------ |
mbed714 | 0:331db0b44b67 | 270 | template<typename T, word Size, typename S> |
mbed714 | 0:331db0b44b67 | 271 | T usr::ring_buffer<T, Size, S>::pop_item() |
mbed714 | 0:331db0b44b67 | 272 | { |
mbed714 | 0:331db0b44b67 | 273 | T item = Buf[First]; |
mbed714 | 0:331db0b44b67 | 274 | |
mbed714 | 0:331db0b44b67 | 275 | Count--; |
mbed714 | 0:331db0b44b67 | 276 | First++; |
mbed714 | 0:331db0b44b67 | 277 | if(First == Size) |
mbed714 | 0:331db0b44b67 | 278 | First = 0; |
mbed714 | 0:331db0b44b67 | 279 | |
mbed714 | 0:331db0b44b67 | 280 | return item; |
mbed714 | 0:331db0b44b67 | 281 | } |
mbed714 | 0:331db0b44b67 | 282 | //------------------------------------------------------------------------------ |
mbed714 | 0:331db0b44b67 | 283 | template<typename T, word Size, typename S> |
mbed714 | 0:331db0b44b67 | 284 | T usr::ring_buffer<T, Size, S>::pop_item_back() |
mbed714 | 0:331db0b44b67 | 285 | { |
mbed714 | 0:331db0b44b67 | 286 | |
mbed714 | 0:331db0b44b67 | 287 | if(Last == 0) |
mbed714 | 0:331db0b44b67 | 288 | Last = Size - 1; |
mbed714 | 0:331db0b44b67 | 289 | else |
mbed714 | 0:331db0b44b67 | 290 | --Last; |
mbed714 | 0:331db0b44b67 | 291 | |
mbed714 | 0:331db0b44b67 | 292 | Count--; |
mbed714 | 0:331db0b44b67 | 293 | return Buf[Last];; |
mbed714 | 0:331db0b44b67 | 294 | } |
mbed714 | 0:331db0b44b67 | 295 | //------------------------------------------------------------------------------ |
mbed714 | 0:331db0b44b67 | 296 | |
mbed714 | 0:331db0b44b67 | 297 | |
mbed714 | 0:331db0b44b67 | 298 | #endif // USRLIB_H |