CAN Queue mechanism permitting creation and management of CAN messages through a queueing

Committer:
WiredHome
Date:
Sun Jul 15 15:15:20 2012 +0000
Revision:
0:7cf23260330d
[mbed] converted /A_CANAdapter/CANUtilities/CANQueue

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WiredHome 0:7cf23260330d 1 /// @file CANQueue.h
WiredHome 0:7cf23260330d 2 /// The CAN Queue mechanism permits the creation and management of
WiredHome 0:7cf23260330d 3 /// messages through a CAN queue.
WiredHome 0:7cf23260330d 4 ///
WiredHome 0:7cf23260330d 5 /// @todo disable on the relevant interrupts, rather than all when protecting
WiredHome 0:7cf23260330d 6 /// the critical section.
WiredHome 0:7cf23260330d 7 ///
WiredHome 0:7cf23260330d 8 /// @note Copyright &copr; 2011 by Smartware Computing, all rights reserved.
WiredHome 0:7cf23260330d 9 /// Individuals may use this application for evaluation or non-commercial
WiredHome 0:7cf23260330d 10 /// purposes. Within this restriction, changes may be made to this application
WiredHome 0:7cf23260330d 11 /// as long as this copyright notice is retained. The user shall make
WiredHome 0:7cf23260330d 12 /// clear that their work is a derived work, and not the original.
WiredHome 0:7cf23260330d 13 /// Users of this application and sources accept this application "as is" and
WiredHome 0:7cf23260330d 14 /// shall hold harmless Smartware Computing, for any undesired results while
WiredHome 0:7cf23260330d 15 /// using this application - whether real or imagined.
WiredHome 0:7cf23260330d 16 ///
WiredHome 0:7cf23260330d 17 /// @author David Smart, Smartware Computing
WiredHome 0:7cf23260330d 18 ///
WiredHome 0:7cf23260330d 19
WiredHome 0:7cf23260330d 20 // These are needed by this file
WiredHome 0:7cf23260330d 21 #include "CANQueue.h"
WiredHome 0:7cf23260330d 22 //#include "CANUtilities.h"
WiredHome 0:7cf23260330d 23
WiredHome 0:7cf23260330d 24
WiredHome 0:7cf23260330d 25 CANQueue::CANQueue(int Size)
WiredHome 0:7cf23260330d 26 {
WiredHome 0:7cf23260330d 27 queue = new CANmsg[Size];
WiredHome 0:7cf23260330d 28 queueSize = Size;
WiredHome 0:7cf23260330d 29 queueCount = 0;
WiredHome 0:7cf23260330d 30 queueMaxCount = 0;
WiredHome 0:7cf23260330d 31 enqueuePosition = 0;
WiredHome 0:7cf23260330d 32 dequeuePosition = 0;
WiredHome 0:7cf23260330d 33 }
WiredHome 0:7cf23260330d 34
WiredHome 0:7cf23260330d 35
WiredHome 0:7cf23260330d 36 CANQueue::~CANQueue()
WiredHome 0:7cf23260330d 37 {
WiredHome 0:7cf23260330d 38 delete[] queue;
WiredHome 0:7cf23260330d 39 }
WiredHome 0:7cf23260330d 40
WiredHome 0:7cf23260330d 41
WiredHome 0:7cf23260330d 42 bool CANQueue::Enqueue(CANmsg msg)
WiredHome 0:7cf23260330d 43 {
WiredHome 0:7cf23260330d 44 #if 1
WiredHome 0:7cf23260330d 45 return Enqueue(&msg);
WiredHome 0:7cf23260330d 46 #else
WiredHome 0:7cf23260330d 47 queue[enqueuePosition++] = msg;
WiredHome 0:7cf23260330d 48 enqueuePosition = enqueuePosition % queueSize;
WiredHome 0:7cf23260330d 49 queueCount++;
WiredHome 0:7cf23260330d 50 if (queueCount > queueMaxCount) // track the max
WiredHome 0:7cf23260330d 51 queueMaxCount = queueCount;
WiredHome 0:7cf23260330d 52 if (queueCount > queueSize) // just overwrote the oldest
WiredHome 0:7cf23260330d 53 {
WiredHome 0:7cf23260330d 54 dequeuePosition++;
WiredHome 0:7cf23260330d 55 dequeuePosition = dequeuePosition % queueSize;
WiredHome 0:7cf23260330d 56 queueCount = queueSize;
WiredHome 0:7cf23260330d 57 return false;
WiredHome 0:7cf23260330d 58 }
WiredHome 0:7cf23260330d 59 return true;
WiredHome 0:7cf23260330d 60 #endif
WiredHome 0:7cf23260330d 61 }
WiredHome 0:7cf23260330d 62
WiredHome 0:7cf23260330d 63
WiredHome 0:7cf23260330d 64 bool CANQueue::Enqueue(CANmsg *msg)
WiredHome 0:7cf23260330d 65 {
WiredHome 0:7cf23260330d 66 bool retval = true;
WiredHome 0:7cf23260330d 67
WiredHome 0:7cf23260330d 68 __disable_irq();
WiredHome 0:7cf23260330d 69 queue[enqueuePosition++] = *msg;
WiredHome 0:7cf23260330d 70 enqueuePosition = enqueuePosition % queueSize;
WiredHome 0:7cf23260330d 71 queueCount++;
WiredHome 0:7cf23260330d 72 if (queueCount > queueMaxCount) // track the max
WiredHome 0:7cf23260330d 73 queueMaxCount = queueCount;
WiredHome 0:7cf23260330d 74 if (queueCount > queueSize) // just overwrote the oldest
WiredHome 0:7cf23260330d 75 {
WiredHome 0:7cf23260330d 76 dequeuePosition++;
WiredHome 0:7cf23260330d 77 dequeuePosition = dequeuePosition % queueSize;
WiredHome 0:7cf23260330d 78 queueCount = queueSize;
WiredHome 0:7cf23260330d 79 retval = false;
WiredHome 0:7cf23260330d 80 }
WiredHome 0:7cf23260330d 81 __enable_irq();
WiredHome 0:7cf23260330d 82 return retval;
WiredHome 0:7cf23260330d 83 }
WiredHome 0:7cf23260330d 84
WiredHome 0:7cf23260330d 85
WiredHome 0:7cf23260330d 86
WiredHome 0:7cf23260330d 87 bool CANQueue::Dequeue(CANmsg *msg)
WiredHome 0:7cf23260330d 88 {
WiredHome 0:7cf23260330d 89 if (queueCount)
WiredHome 0:7cf23260330d 90 {
WiredHome 0:7cf23260330d 91 __disable_irq();
WiredHome 0:7cf23260330d 92 *msg = queue[dequeuePosition++];
WiredHome 0:7cf23260330d 93 dequeuePosition = dequeuePosition % queueSize;
WiredHome 0:7cf23260330d 94 queueCount--;
WiredHome 0:7cf23260330d 95 __enable_irq();
WiredHome 0:7cf23260330d 96 return true;
WiredHome 0:7cf23260330d 97 }
WiredHome 0:7cf23260330d 98 return false;
WiredHome 0:7cf23260330d 99 }