-
Notifications
You must be signed in to change notification settings - Fork 17
/
canfestival.h
122 lines (94 loc) · 3.39 KB
/
canfestival.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
This file is part of CanFestival, a library implementing CanOpen Stack.
Copyright (C): Edouard TISSERANT and Francis DUPIN
AVR Port: Andreas GLAUSER and Peter CHRISTEN
See COPYING file for copyrights details.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef __CAN_CANFESTIVAL__
#define __CAN_CANFESTIVAL__
#include "applicfg.h"
#include "data.h"
#include "CO_timer.h"
#include "objacces.h"
#include "digitalWriteFast.h"
#include "CO_ErrorState.h"
// --------- to be called by user app ---------
template<const int redLEDPin, const int greenLEDPin> class CO {
public:
CO() :
m(Message_Initializer) {
}
;
void CO_Cycle();
void CO_Init();
private:
static void errorStateBlink(uint8_t dummy);
Message m;
};
template<int redLEDPin, int greenLEDPin> void CO<redLEDPin, greenLEDPin>::CO_Init() {
if(redLEDPin>0) pinModeFast(redLEDPin, OUTPUT);
if(greenLEDPin>0) pinModeFast(greenLEDPin, OUTPUT);
if(redLEDPin>0) digitalWriteFast(redLEDPin, 1);
if(greenLEDPin>0) digitalWriteFast(greenLEDPin, 1);
initCAN();
setState(Initialisation);
SetAlarm(0, &errorStateBlink, 62, 62);
if(redLEDPin>0) digitalWriteFast(redLEDPin, 0);
if(greenLEDPin>0) digitalWriteFast(greenLEDPin, 0);
}
template<int redLEDPin, int greenLEDPin> void CO<redLEDPin, greenLEDPin>::CO_Cycle() {
TimeDispatch();
if (CAN.checkReceive()) {
CAN.readMsgBuf(&(m.len), m.data);
m.cob_id = CAN.getCanId();
m.rtr = 0; // m_nRtr;
if (isRxNoError())
flashGreen();
canDispatch(&m); // process it
}
uint8_t ef = CAN.errorFlag();
if (ef & MCP_CAN::EFlg_TxWar)
setTxErrorState(tx_warning);
if (ef & MCP_CAN::EFlg_TxBusOff) {
setTxErrorState(tx_bus_off);
} else if (ef & MCP_CAN::EFlg_TxEP)
setTxErrorState(tx_passive);
else if (!isTxNoError()) {
if (CAN.checkTransmit())
resetTxErrorState();
}
updateTxErrorState();
if (ef && MCP_CAN::EFlg_RxWar)
setRxErrorState(rx_warning);
if (ef & (MCP_CAN::EFlg_Rx1Ovr | MCP_CAN::EFlg_Rx0Ovr))
setRxErrorState(rx_overflow);
else if (ef && MCP_CAN::EFlg_RxEP)
setRxErrorState(rx_passive);
else if (greePatternStarted())
resetRxErrorState();
updateRxErrorState();
}
template<int redLEDPin, int greenLEDPin> void CO<redLEDPin, greenLEDPin>::errorStateBlink(uint8_t dummy) {
if(nextRedBlinkState()) {
if(redLEDPin>0) digitalWriteFast(redLEDPin, 1);
} else {
if(redLEDPin>0) digitalWriteFast(redLEDPin, 0);
}
if(nextGreenBlinkState()) {
if(greenLEDPin>0) digitalWriteFast(greenLEDPin, 1);
} else {
if(greenLEDPin>0) digitalWriteFast(greenLEDPin, 0);
}
}
#endif