Quick and dirty port of scmRTOS demo to mbed 1768. scmRTOS is a small RTOS written using C++. Offers (static) processes, critical sections, mutexes, messages, channels.

Dependencies:   mbed

Committer:
igorsk
Date:
Thu Sep 09 21:19:01 2010 +0000
Revision:
0:a405220cf420

        

Who changed what in which revision?

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