author | Laurent Bessard |
Thu, 06 Dec 2012 16:07:46 +0100 | |
changeset 765 | 90b738e4ede5 |
parent 675 | e5c5101c4f0b |
permissions | -rw-r--r-- |
413 | 1 |
/* |
2 |
This file is part of CanFestival, a library implementing CanOpen Stack. |
|
3 |
||
4 |
Copyright (C): Edouard TISSERANT and Francis DUPIN |
|
5 |
AVR Port: Andreas GLAUSER and Peter CHRISTEN |
|
6 |
||
7 |
See COPYING file for copyrights details. |
|
8 |
||
9 |
This library is free software; you can redistribute it and/or |
|
10 |
modify it under the terms of the GNU Lesser General Public |
|
11 |
License as published by the Free Software Foundation; either |
|
12 |
version 2.1 of the License, or (at your option) any later version. |
|
13 |
||
14 |
This library is distributed in the hope that it will be useful, |
|
15 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
17 |
Lesser General Public License for more details. |
|
18 |
||
19 |
You should have received a copy of the GNU Lesser General Public |
|
20 |
License along with this library; if not, write to the Free Software |
|
21 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
22 |
*/ |
|
23 |
/****************************************************************************** |
|
24 |
Project description: |
|
25 |
Test projekt for a DS 401 slave, running on Atmel's STK500 with AT90CAN128 |
|
26 |
Short description: |
|
27 |
PORTA: Inputs (Keys, low active) |
|
28 |
PORTB: Outputs (LEDs, low active) |
|
525 | 29 |
PORTC: Node ID (1 BCD switch, low active) |
413 | 30 |
|
31 |
******************************************************************************/ |
|
32 |
#include "hardware.h" |
|
33 |
#include "canfestival.h" |
|
34 |
#include "can_AVR.h" |
|
675
e5c5101c4f0b
FIXED: - The AVR example now compiles in linux.
Christian Taedcke <hacking@taedcke.com>
parents:
525
diff
changeset
|
35 |
#include "ObjDict.h" |
413 | 36 |
#include "ds401.h" |
37 |
||
38 |
unsigned char timer_interrupt = 0; // Set if timer interrupt eclapsed |
|
39 |
unsigned char inputs; |
|
40 |
||
41 |
// CAN |
|
42 |
unsigned char nodeID; |
|
43 |
unsigned char digital_input[1] = {0}; |
|
44 |
unsigned char digital_output[1] = {0}; |
|
45 |
||
46 |
static Message m = Message_Initializer; // contain a CAN message |
|
47 |
||
48 |
void sys_init(); |
|
49 |
||
50 |
// macros to handle the schedule timer |
|
51 |
#define sys_timer timer_interrupt |
|
52 |
#define reset_sys_timer() timer_interrupt = 0 |
|
53 |
#define CYCLE_TIME 1000 // Sample Timebase [us] |
|
54 |
||
55 |
int main(void) |
|
56 |
{ |
|
57 |
sys_init(); // Initialize system |
|
58 |
canInit(CAN_BAUDRATE); // Initialize the CANopen bus |
|
59 |
initTimer(); // Start timer for the CANopen stack |
|
60 |
nodeID = read_bcd(); // Read node ID first |
|
61 |
setNodeId (&ObjDict_Data, nodeID); |
|
62 |
setState(&ObjDict_Data, Initialisation); // Init the state |
|
63 |
||
64 |
for(;;) // forever loop |
|
65 |
{ |
|
66 |
if (sys_timer) // Cycle timer, invoke action on every time slice |
|
67 |
{ |
|
68 |
reset_sys_timer(); // Reset timer |
|
69 |
digital_input[0] = get_inputs(); |
|
70 |
digital_input_handler(&ObjDict_Data, digital_input, sizeof(digital_input)); |
|
71 |
digital_output_handler(&ObjDict_Data, digital_output, sizeof(digital_output)); |
|
72 |
set_outputs(digital_output[0]); |
|
73 |
||
74 |
// Check if CAN address has been changed |
|
75 |
if(!( nodeID == read_bcd())) |
|
76 |
{ |
|
77 |
nodeID = read_bcd(); // Save the new CAN adress |
|
78 |
setState(&ObjDict_Data, Stopped); // Stop the node, to change the node ID |
|
79 |
setNodeId(&ObjDict_Data, nodeID); // Now the CAN adress is changed |
|
80 |
setState(&ObjDict_Data, Pre_operational); // Set to Pre_operational, master must boot it again |
|
81 |
} |
|
82 |
} |
|
83 |
||
84 |
// a message was received pass it to the CANstack |
|
85 |
if (canReceive(&m)) // a message reveived |
|
86 |
canDispatch(&ObjDict_Data, &m); // process it |
|
87 |
else |
|
88 |
{ |
|
89 |
// Enter sleep mode |
|
90 |
#ifdef WD_SLEEP // Watchdog and Sleep |
|
91 |
wdt_reset(); |
|
92 |
sleep_enable(); |
|
93 |
sleep_cpu(); |
|
94 |
#endif // Watchdog and Sleep |
|
95 |
} |
|
96 |
} |
|
97 |
} |
|
98 |
||
99 |
void sys_init() |
|
100 |
/****************************************************************************** |
|
101 |
Initialize the relays, the main states and the modbus protocol stack. |
|
102 |
INPUT LOCK_STATES *lock_states |
|
103 |
OUTPUT void |
|
104 |
******************************************************************************/ |
|
105 |
{ |
|
525 | 106 |
OSCCAL = 0x43; // adjust the RC oszillator |
413 | 107 |
|
108 |
PORTA = 0xFF; // Inputs (Keys, low active) with pullup |
|
109 |
DDRA = 0x00; // |
|
110 |
PORTB = 0xFF; // Outputs (LEDs, low active) all 1 |
|
111 |
DDRB = 0xFF; // |
|
112 |
PORTC = 0xFF; // 1 BCD switch with pullup |
|
113 |
DDRC = 0x00; // |
|
114 |
PORTD = 0x2C; // 2xCOM, unused, CAN, unused |
|
115 |
DDRD = 0x2A; // All init 0 or without pullup |
|
116 |
PORTE = 0x00; // Output |
|
117 |
DDRE = 0x3C; // 2x not used, 2x not used |
|
118 |
PORTF = 0x00; // Not used |
|
119 |
DDRF = 0xFF; // All output |
|
120 |
PORTG = 0x00; // Not used |
|
121 |
DDRG = 0x1F; // Output for debug (only 5 pins) |
|
122 |
||
123 |
// Set timer 0 for main schedule time |
|
124 |
TCCR0A |= 1 << WGM01 | 1 << CS01 | 1 << CS00;// Timer 0 CTC , Timer 0 mit CK/64 starten |
|
125 |
TIMSK0 = 1 << OCIE0A; // Timer Interrupts: Timer 0 Compare |
|
126 |
OCR0A = (unsigned char)(F_CPU / 64 * CYCLE_TIME/1000000 - 1); // Reloadvalue for timer 0 |
|
127 |
#ifdef WD_SLEEP // Watchdog and Sleep |
|
128 |
wdt_reset(); |
|
129 |
wdt_enable(WDTO_15MS); // Watchdogtimer start with 16 ms timeout |
|
130 |
#endif // Watchdog and Sleep |
|
131 |
sei(); // Enable Interrupts |
|
132 |
} |
|
133 |
||
134 |
||
135 |
#ifdef __IAR_SYSTEMS_ICC__ |
|
136 |
#pragma type_attribute = __interrupt |
|
137 |
#pragma vector=TIMER0_COMP_vect |
|
138 |
void TIMER0_COMP_interrupt(void) |
|
139 |
#else // GCC |
|
140 |
ISR(TIMER0_COMP_vect) |
|
141 |
#endif // GCC |
|
142 |
/****************************************************************************** |
|
143 |
Interruptserviceroutine Timer 2 Compare A for the main cycle |
|
144 |
******************************************************************************/ |
|
145 |
||
146 |
{ |
|
147 |
timer_interrupt = 1; // Set flag |
|
148 |
} |