Subversion Repositories HomeAutomation

Rev

Rev 391 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 391 Rev 392
1
/**
1
/**
2
 * CAN Relay.
2
 * CAN Relay.
3
 *
3
 *
4
 *
4
 *
5
 * @date    2006-12-17
5
 * @date    2006-12-17
6
 * @author  Erik Larsson
6
 * @author  Erik Larsson
7
 *
7
 *
8
 *   ADC=Vin*1024/Vref
8
 *   ADC=Vin*1024/Vref
9
 *   Vout=( 10 mV/C )( Temperature C ) + 500 mV
9
 *   Vout=( 10 mV/C )( Temperature C ) + 500 mV
10
 *
10
 *
11
 *  Transmitts: DATA: <temperature low byte> <temperature high byte> <relay status> <failsafe mode>
11
 *  Transmitts: DATA: <temperature low byte> <temperature high byte> <relay status> <failsafe mode>
12
 *
12
 *
13
 *
13
 *
14
 */
14
 */
15
 
15
 
16
#define TWO_BUTTON_MODE 0 /* If using two (or just one but nothing more) auxiliary buttons */
16
#define TWO_BUTTON_MODE 0 /* If using two (or just one but nothing more) auxiliary buttons */
17
 
17
 
18
/*-----------------------------------------------------------------------------
18
/*-----------------------------------------------------------------------------
19
 * Includes
19
 * Includes
20
 *---------------------------------------------------------------------------*/
20
 *---------------------------------------------------------------------------*/
21
/* system files */
21
/* system files */
22
#include <avr/io.h>
22
#include <avr/io.h>
23
#include <avr/interrupt.h>
23
#include <avr/interrupt.h>
24
#include <stdio.h>
24
#include <stdio.h>
25
/* lib files */
25
/* lib files */
26
#include <bios.h>
26
#include <bios.h>
27
#include <config.h>
27
#include <config.h>
28
#include <drivers/timer/timebase.h>
28
#include <drivers/timer/timebase.h>
29
#include <tc1047.h>
29
#include <tc1047.h>
30
#include <funcdefs.h>
30
#include <funcdefs.h>
31
 
31
 
32
/* defines */
32
/* defines */
33
#define GROUP_ID        0x01UL
33
#define GROUP_ID        0x01UL
34
 
34
 
35
#define RELAY_OFF           0x01
35
#define RELAY_OFF           0x01
36
#define RELAY_ON            0x02
36
#define RELAY_ON            0x02
37
#define FAILSAFE_MODE       0x0F
37
#define FAILSAFE_MODE       0x0F
38
#define FAILSAFE_TRESHOLD   0xFF  /* Degrees when nodeRelay goes into failsafe mode */
38
#define FAILSAFE_TRESHOLD   0xFF  /* Degrees when nodeRelay goes into failsafe mode */
39
 
39
 
40
#define RELAY_CMD_ON        0x01
40
#define RELAY_CMD_ON        0x01
41
#define RELAY_CMD_OFF       0x02
41
#define RELAY_CMD_OFF       0x02
42
#define RELAY_CMD_TOGGLE    0x03
42
#define RELAY_CMD_TOGGLE    0x03
43
#define RELAY_CMD_STATUS    0x04
43
#define RELAY_CMD_STATUS    0x04
44
#define RELAY_CMD_RESET     0x05
44
#define RELAY_CMD_RESET     0x05
45
#define STATUS_SEND_PERIOD  10000UL /* milliseconds */
45
#define STATUS_SEND_PERIOD  10000UL /* milliseconds */
46
#define FAILSAFE_SEND_PERIOD    10000UL /* milliseconds */
46
#define FAILSAFE_SEND_PERIOD    10000UL /* milliseconds */
47
#define BOUNCE_TIME         10 /* milliseconds */
47
#define BOUNCE_TIME         10 /* milliseconds */
48
#define BUTTON1             1
48
#define BUTTON1             1
49
#define BUTTON2             2
49
#define BUTTON2             2
50
 
50
 
51
/*-------------------------------------------------------------------------
51
/*-------------------------------------------------------------------------
52
 * Global variables
52
 * Global variables
53
 * -----------------------------------------------------------------------*/
53
 * -----------------------------------------------------------------------*/
54
uint8_t relayStatus, failsafe_flag = 0, rxMsg_Data_bytes[8], msg_received = 0;
54
uint8_t relayStatus, failsafe_flag = 0, rxMsg_Data_bytes[8], msg_received = 0;
55
uint32_t boardTemperature = 0;
55
uint32_t boardTemperature = 0;
56
 
56
 
57
 
57
 
58
/*----------------------------------------------------------------------------
58
/*----------------------------------------------------------------------------
59
 * Functions
59
 * Functions
60
 * -------------------------------------------------------------------------*/
60
 * -------------------------------------------------------------------------*/
61
uint8_t relayOff();
61
uint8_t relayOff();
62
uint8_t relayOn();
62
uint8_t relayOn();
63
void relayFailsafe();
63
void relayFailsafe();
64
void sendStatus();
64
void sendStatus();
65
 
65
 
66
void can_receive(Can_Message_t *msg)
66
void can_receive(Can_Message_t *msg)
67
{ // CAN callback function
67
{ // CAN callback function
68
    uint32_t act;
68
    uint32_t act;
69
    uint8_t m, act_type, group, node;
69
    uint8_t m, act_type, group, node;
70
 
70
 
71
    if( ((msg->Id & CLASS_MASK)>>CLASS_MASK_BITS) == CLASS_ACT){
71
    if( ((msg->Id & CLASS_MASK)>>CLASS_MASK_BITS) == CLASS_ACT){
72
        // Actuator package
72
        // Actuator package
73
        act = (msg->Id & TYPE_MASK) >> TYPE_MASK_BITS;
73
        act = (msg->Id & TYPE_MASK) >> TYPE_MASK_BITS;
74
        act_type =(uint8_t)((act & ACT_TYPE_MASK) >> ACT_TYPE_BITS);
74
        act_type =(uint8_t)((act & ACT_TYPE_MASK) >> ACT_TYPE_BITS);
75
        group = (uint8_t)((act & ACT_GROUP_MASK) >> ACT_GROUP_BITS);
75
        group = (uint8_t)((act & ACT_GROUP_MASK) >> ACT_GROUP_BITS);
76
        node = (uint8_t)((act & ACT_NODE_MASK) >> ACT_NODE_BITS);
76
        node = (uint8_t)((act & ACT_NODE_MASK) >> ACT_NODE_BITS);
77
 
77
 
78
        if( act_type == ACT_TYPE_RELAY && (group == GROUP_ID || node == NODE_ID) ){
78
        if( act_type == ACT_TYPE_RELAY && (group == GROUP_ID || node == NODE_ID) ){
79
            // This is a message for this node
79
            // This is a message for this node
80
 
80
 
81
            if(rxMsg_Data_bytes[0] == RELAY_CMD_RESET){
81
            if(rxMsg_Data_bytes[0] == RELAY_CMD_RESET){
82
                // Return to normal operation
82
                // Return to normal operation
83
                failsafe_flag = 0;
83
                failsafe_flag = 0;
84
            }
84
            }
85
 
85
 
86
            for(m=0;m<(msg->DataLength);m++){
86
            for(m=0;m<(msg->DataLength);m++){
87
                rxMsg_Data_bytes[m] = msg->Data.bytes[m];
87
                rxMsg_Data_bytes[m] = msg->Data.bytes[m];
88
            }
88
            }
89
            msg_received = 1; // Tell application that message has arrived
89
            msg_received = 1; // Tell application that message has arrived
90
        }
90
        }
91
    }
91
    }
92
}
92
}
93
 
93
 
94
/*----------------------------------------------------------------------------
94
/*----------------------------------------------------------------------------
95
 * Interrupt routines // FIXME ordna och testa tryckknappar/rotary encoder
95
 * Interrupt routines // FIXME ordna och testa tryckknappar/rotary encoder
96
 * -------------------------------------------------------------------------*/
96
 * -------------------------------------------------------------------------*/
97
#if TWO_BUTTON_MODE
97
#if TWO_BUTTON_MODE
98
ISR( PCINT2_vect )
98
ISR( PCINT2_vect )
99
{
99
{
100
 
100
 
101
}
101
}
102
#endif
102
#endif
103
 
103
 
104
/*-----------------------------------------------------------------------------
104
/*-----------------------------------------------------------------------------
105
 * Main Program
105
 * Main Program
106
 *---------------------------------------------------------------------------*/
106
 *---------------------------------------------------------------------------*/
107
int main(void) {
107
int main(void) {
108
 
108
 
109
    uint32_t timeStamp = Timebase_CurrentTime(), temp = timeStamp;
109
    uint32_t timeStamp = Timebase_CurrentTime(), temp = timeStamp;
110
 
110
 
111
    Can_Message_t txMsg;
111
    Can_Message_t txMsg;
112
    txMsg.RemoteFlag = 0;
112
    txMsg.RemoteFlag = 0;
113
    txMsg.ExtendedFlag = 1;
113
    txMsg.ExtendedFlag = 1;
114
    txMsg.DataLength = 4;
114
    txMsg.DataLength = 4;
115
 
115
 
116
    adcTemperatureInit();
116
    adcTemperatureInit();
117
 
117
 
118
    // Set up callback for CAN messages
118
    // Set up callback for CAN messages
119
    BIOS_CanCallback = &can_receive;
119
    BIOS_CanCallback = &can_receive;
120
 
120
 
121
    sei();
121
    sei();
122
   
122
   
123
    Timebase_Init();
123
    Timebase_Init();
124
 
124
 
125
    // Turn relay off
125
    // Turn relay off
126
    relayStatus = relayOff();
126
    relayStatus = relayOff();
127
//  txMsg.Id = 0x8000110; // FIXME
127
//  txMsg.Id = 0x8000110; // FIXME
128
//  bios->can_send(&txMsg);
128
//  bios->can_send(&txMsg);
129
 
129
 
130
    // Main loop
130
    // Main loop
131
    while (1) {
131
    while (1) {
132
 
132
 
133
        if(msg_received && !failsafe_flag ){
133
        if(msg_received && !failsafe_flag ){
134
 
134
 
135
            if( rxMsg_Data_bytes[0] == RELAY_CMD_ON ){
135
            if( rxMsg_Data_bytes[0] == RELAY_CMD_ON ){
136
                // Turn relay on
136
                // Turn relay on
137
                relayStatus = relayOn();
137
                relayStatus = relayOn();
138
 
138
 
139
            }else if(rxMsg_Data_bytes[0] == RELAY_CMD_OFF ){
139
            }else if(rxMsg_Data_bytes[0] == RELAY_CMD_OFF ){
140
                // Turn relay off
140
                // Turn relay off
141
                relayStatus = relayOff();
141
                relayStatus = relayOff();
142
 
142
 
143
            }else if(rxMsg_Data_bytes[0] == RELAY_CMD_TOGGLE ){
143
            }else if(rxMsg_Data_bytes[0] == RELAY_CMD_TOGGLE ){
144
                    // Toggle relay
144
                    // Toggle relay
145
                    if(relayStatus == RELAY_OFF){
145
                    if(relayStatus == RELAY_OFF){
146
                        relayStatus = relayOn();
146
                        relayStatus = relayOn();
147
                    }else{
147
                    }else{
148
                        relayStatus = relayOff();
148
                        relayStatus = relayOff();
149
                    }
149
                    }
150
 
150
 
151
            }else if(rxMsg_Data_bytes[0] == RELAY_CMD_STATUS ){
151
            }else if(rxMsg_Data_bytes[0] == RELAY_CMD_STATUS ){
152
                // Send relay status to CAN
152
                // Send relay status to CAN
153
                sendStatus();
153
                sendStatus();
154
            }
154
            }
155
            msg_received = 0;
155
            msg_received = 0;
156
        }
156
        }
157
 
157
 
158
        temp = Timebase_CurrentTime();
158
        temp = Timebase_CurrentTime();
159
        if( temp - timeStamp >= STATUS_SEND_PERIOD ){
159
        if( temp - timeStamp >= STATUS_SEND_PERIOD ){
160
            timeStamp = temp;
160
            timeStamp = temp;
161
/*          txMsg.Id = 0x8000111;
161
/*          txMsg.Id = 0x8000111;
162
            txMsg.Data.bytes[0] = timeStamp & 0x000000FF;
162
            txMsg.Data.bytes[0] = timeStamp & 0x000000FF;
163
            txMsg.Data.bytes[0] = (timeStamp & 0x0000FF00)>>8;
163
            txMsg.Data.bytes[0] = (timeStamp & 0x0000FF00)>>8;
164
            txMsg.Data.bytes[0] = (timeStamp & 0x00FF0000)>>16;
164
            txMsg.Data.bytes[0] = (timeStamp & 0x00FF0000)>>16;
165
            txMsg.Data.bytes[0] = (timeStamp & 0xFF000000)>>24;
165
            txMsg.Data.bytes[0] = (timeStamp & 0xFF000000)>>24;
166
            bios->can_send(&txMsg);
166
            bios->can_send(&txMsg);
167
*/          // Send relay status to CAN
167
*/          // Send relay status to CAN
168
            sendStatus();
168
            sendStatus();
169
        }
169
        }
170
    }
170
    }
171
    return 0;
171
    return 0;
172
}
172
}
173
 
173
 
174
uint8_t relayOff()
174
uint8_t relayOff()
175
{
175
{
176
    // Turn off relay
176
    // Turn off relay
177
    PORTC &= ~(1<<PC1);
177
    PORTC &= ~(1<<PC1);
178
    DDRC |= (1<<DDC1);
178
    DDRC |= (1<<DDC1);
179
 
179
 
180
    return RELAY_OFF;
180
    return RELAY_OFF;
181
}
181
}
182
 
182
 
183
uint8_t relayOn()
183
uint8_t relayOn()
184
{
184
{
185
    // Turn on relay
185
    // Turn on relay
186
    DDRC &= ~(1<<DDC1);
186
    DDRC &= ~(1<<DDC1);
187
    PORTC |= (1<<PC1);
187
    PORTC |= (1<<PC1);
188
 
188
 
189
    return RELAY_ON;
189
    return RELAY_ON;
190
}
190
}
191
 
191
 
192
void relayFailsafe()
192
void relayFailsafe()
193
{
193
{
194
    uint32_t failsafe_timeStamp, temp;
194
    uint32_t failsafe_timeStamp, temp;
195
    relayStatus = relayOff();
195
    relayStatus = relayOff();
196
    failsafe_flag = 1;
196
    failsafe_flag = 1;
197
 
197
 
198
    while(1){
198
    while(1){
199
 
199
 
200
        if( !failsafe_flag ){
200
        if( !failsafe_flag ){
201
            // Return from failsafe mode
201
            // Return from failsafe mode
202
            return;
202
            return;
203
        }
203
        }
204
// TODO skicka med period
204
// TODO skicka med period
205
        sendStatus( );
205
        sendStatus( );
206
    }
206
    }
207
}
207
}
208
 
208
 
209
void sendStatus()
209
void sendStatus()
210
{
210
{
211
    uint32_t temperature;
211
    uint32_t temperature;
212
    Can_Message_t statusMsg;
212
    Can_Message_t statusMsg;
213
    statusMsg.RemoteFlag = 0;
213
    statusMsg.RemoteFlag = 0;
214
    statusMsg.ExtendedFlag = 1;
214
    statusMsg.ExtendedFlag = 1;
215
    statusMsg.DataLength = 4;
215
    statusMsg.DataLength = 4;
216
    statusMsg.Id = ( CLASS_SNS<<CLASS_MASK_BITS )|( SNS_ACT_STATUS_RELAY<<TYPE_MASK_BITS )|(NODE_ID);
216
    statusMsg.Id = ( CLASS_SNS<<CLASS_MASK_BITS )|( SNS_ACT_STATUS_RELAY<<TYPE_MASK_BITS )|(NODE_ID);
217
 
217
 
218
    temperature = getTC1047temperature();
218
    temperature = getTC1047temperature();
219
    if( (int32_t)temperature >= FAILSAFE_TRESHOLD && !failsafe_flag ){
219
    if( (int32_t)temperature >= FAILSAFE_TRESHOLD && !failsafe_flag ){
220
        // Goto failsafe mode
220
        // Goto failsafe mode
221
        relayFailsafe();
221
        relayFailsafe();
222
    }else{
222
    }else{
223
        // Transmitt node status
223
        // Transmitt node status
224
        statusMsg.Data.bytes[0] = temperature & 0x00FF;
224
        statusMsg.Data.bytes[0] = temperature & 0x00FF;
225
        statusMsg.Data.bytes[1] = (temperature & 0xFF00)>>8;
225
        statusMsg.Data.bytes[1] = (temperature & 0xFF00)>>8;
226
        statusMsg.Data.bytes[2] = relayStatus;
226
        statusMsg.Data.bytes[2] = relayStatus;
227
        statusMsg.Data.bytes[3] = failsafe_flag;
227
        statusMsg.Data.bytes[3] = failsafe_flag;
228
 
228
 
229
        BIOS_CanSend( &statusMsg );
229
        BIOS_CanSend( &statusMsg );
230
    }
230
    }
231
}
231
}
232
 
232