00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "CANPieController.hpp"
00031 #include <rtt/Logger.hpp>
00032 #include <rtt/os/fosi.h>
00033
00034 namespace RTT
00035 {namespace CAN
00036 {
00037 CANPieController::CANPieController( double period, bool interrupt ) :
00038 PeriodicActivity( RTT::OS::HighestPriority, period ),
00039 CANPieStatus( CpErr_INIT_MISSING ),
00040 CANPieChannel(0), process_in_int(interrupt),
00041 total_recv(0), total_trns(0), failed_recv(0), failed_trns(0), generic_trns(0)
00042 {
00043 }
00044
00045 CANPieController::~CANPieController()
00046 {
00047 CpUserAppDeInit(0);
00048 }
00049
00050 bool CANPieController::initialize()
00051 {
00052 return (CANPieStatus == CpErr_OK);
00053 }
00054
00055 void CANPieController::step()
00056 {
00057 while ( readFromBuffer(CANmsg) )
00058 {
00059 CANmsg.origin = this;
00060 bus->write(&CANmsg);
00061 }
00062 }
00063
00064 void CANPieController::finalize() {
00065 Logger::In in("CANPieController");
00066 log(Info) << "CANPie Controller stopped. Last run statistics :"<<endlog();
00067 Logger::log() << " Total Received : "<<total_recv<< ". Failed to Receive (FIFO empty) : "<< failed_recv<<Logger::nl;
00068 Logger::log() << " Total Transmitted : "<<total_trns<< ". Failed to Transmit (FIFO full) : "<< failed_trns<<
00069 ". Generic Transmit Errors: "<<generic_trns<<endlog();
00070 total_recv = failed_recv = total_trns = failed_trns = generic_trns = 0;
00071 }
00072
00073 void CANPieController::addBus( unsigned int chan, CANBusInterface* _bus)
00074 {
00075 CANPieChannel = chan;
00076 bus = _bus;
00077 controller[CANPieChannel] = this;
00078 CANPieStatus = CpUserAppInit(CANPieChannel, 32, 64, 10);
00079 log(Info) << "CANPieController : Added bus with status :"<<CANPieStatus<<endlog();
00080 CpUserIntFunctions( CANPieChannel, ReceiveIntHandler, 0 , 0);
00081 bus->setController( this );
00082 }
00083
00084 void CANPieController::process(const CANMessage* msg)
00085 {
00086 writeToBuffer(*msg);
00087 }
00088
00089 unsigned int CANPieController::nodeId() const
00090 {
00091 return 0;
00092 }
00093
00094 unsigned int CANPieController::ReceiveIntHandler(unsigned int channel, CpStruct_CAN* msg)
00095 {
00096 return controller[int(channel)]->receive(msg);
00097
00098 }
00099
00100 bool CANPieController::readFromBuffer( CANMessage& msg)
00101 {
00102 int status = CpErr_FIFO_EMPTY;
00103
00104 if ( (status = CpUserMsgRead(CANPieChannel, &msg) ) == CpErr_OK )
00105 {
00106 msg.origin = this;
00107 ++total_recv;
00108 }
00109 else
00110 ++failed_recv;
00111
00112 return (status == CpErr_OK);
00113 }
00114
00115 bool CANPieController::writeToBuffer(const CANMessage& msg)
00116 {
00117 int status = CpUserMsgWrite(CANPieChannel, &msg);
00118 if (status == CpErr_OK)
00119 ++total_trns;
00120 else if (status == CpErr_FIFO_FULL)
00121 ++failed_trns;
00122 else
00123 ++generic_trns;
00124 return (status == CpErr_OK);
00125 }
00126
00127 int CANPieController::receive(CpStruct_CAN* msg)
00128 {
00129 if (process_in_int)
00130 {
00131 CANmsg = *msg;
00132 CANmsg.origin = this;
00133 ++total_recv;
00134
00135 bus->write(&CANmsg);
00136
00137 return CP_CALLBACK_PROCESSED;
00138 }
00139
00140 return CP_CALLBACK_PUSH_FIFO;
00141 }
00142
00143 CANPieController* CANPieController::controller[CP_CHANNEL_MAX];
00144 }}
00145
00146
00147
00148