Subversion Repositories HomeAutomation

Rev

Rev 405 | Rev 486 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 405 Rev 483
Line 21... Line 21...
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 <config.h>
26
#include <bios.h>
27
#include <bios.h>
27
#include <config.h>
-
 
28
#include <drivers/timer/timebase.h>
28
#include <drivers/timer/timebase.h>
29
#include <tc1047.h>
-
 
30
#include <funcdefs.h>
29
//#include <funcdefs.h>
31
 
30
 
32
/* defines */
31
/* defines */
33
#define GROUP_ID        0x01UL
-
 
34
 
-
 
35
#define RELAY_OFF           0x01
32
#define RELAY_OFF           0x01
36
#define RELAY_ON            0x02
33
#define RELAY_ON            0x02
37
#define FAILSAFE_MODE       0x0F
34
#define FAILSAFE_MODE       0x0F
38
#define FAILSAFE_TRESHOLD   0xFF  /* Degrees when nodeRelay goes into failsafe mode */
35
#define FAILSAFE_TRESHOLD   0xFF  /* Degrees when nodeRelay goes into failsafe mode */
39
 
36
 
Line 49... Line 46...
49
#define BUTTON2             2
46
#define BUTTON2             2
50
 
47
 
51
/*-------------------------------------------------------------------------
48
/*-------------------------------------------------------------------------
52
 * Global variables
49
 * Global variables
53
 * -----------------------------------------------------------------------*/
50
 * -----------------------------------------------------------------------*/
54
uint8_t relayStatus, failsafe_flag = 0, rxMsg_Data_bytes[8], msg_received = 0;
51
uint8_t relayStatus, failsafe_flag = 0;
55
uint32_t boardTemperature = 0;
52
uint32_t boardTemperature = 0;
56
 
53
 
-
 
54
volatile Can_Message_t rxMsg; // Message storage
-
 
55
volatile uint8_t rxMsgFull;   // Synchronization flag
-
 
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;
-
 
69
    uint8_t m, act_type, group, node;
-
 
70
 
-
 
71
    if( ((msg->Id & CLASS_MASK)>>CLASS_MASK_BITS) == CLASS_ACT){
-
 
72
        // Actuator package
68
    if (!rxMsgFull) {
73
        act = (msg->Id & TYPE_MASK) >> TYPE_MASK_BITS;
-
 
74
        act_type =(uint8_t)((act & ACT_TYPE_MASK) >> ACT_TYPE_BITS);
-
 
75
        group = (uint8_t)((act & ACT_GROUP_MASK) >> ACT_GROUP_BITS);
-
 
76
        node = (uint8_t)((act & ACT_NODE_MASK) >> ACT_NODE_BITS);
-
 
77
 
-
 
78
        if( act_type == ACT_TYPE_RELAY && (group == GROUP_ID || node == NODE_ID) ){
-
 
79
            // This is a message for this node
69
        memcpy((void*)&rxMsg, msg, sizeof(rxMsg));
80
 
-
 
81
            if(rxMsg_Data_bytes[0] == RELAY_CMD_RESET){
-
 
82
                // Return to normal operation
-
 
83
                failsafe_flag = 0;
70
        rxMsgFull = 1;
84
            }
-
 
85
 
-
 
86
            for(m=0;m<(msg->DataLength);m++){
-
 
87
                rxMsg_Data_bytes[m] = msg->Data.bytes[m];
-
 
88
            }
-
 
89
            msg_received = 1; // Tell application that message has arrived
-
 
90
        }
-
 
91
    }
71
    }
92
}
72
}
93
 
73
 
94
/*----------------------------------------------------------------------------
74
/*----------------------------------------------------------------------------
95
 * Interrupt routines // FIXME ordna och testa tryckknappar/rotary encoder
75
 * Interrupt routines // FIXME ordna och testa tryckknappar/rotary encoder
Line 105... Line 85...
105
 * Main Program
85
 * Main Program
106
 *---------------------------------------------------------------------------*/
86
 *---------------------------------------------------------------------------*/
107
int main(void) {
87
int main(void) {
108
 
88
 
109
    uint32_t timeStamp = Timebase_CurrentTime(), temp = timeStamp;
89
    uint32_t timeStamp = Timebase_CurrentTime(), temp = timeStamp;
110
 
-
 
111
    Can_Message_t txMsg;
-
 
112
    txMsg.RemoteFlag = 0;
-
 
113
    txMsg.ExtendedFlag = 1;
-
 
114
    txMsg.DataLength = 4;
-
 
115
 
90
 
116
    adcTemperatureInit();
91
    adcTemperatureInit();
117
 
92
 
118
    // Set up callback for CAN messages
93
    // Set up callback for CAN messages
119
    BIOS_CanCallback = &can_receive;
94
    BIOS_CanCallback = &can_receive;
120
 
95
 
121
    sei();
96
    sei();
122
   
97
   
123
    Timebase_Init();
98
    Timebase_Init();
124
 
99
 
125
    // Turn relay off
100
    // Turn relay off
126
    relayStatus = relayOff();
101
    relayStatus = relayOff();
127
//  txMsg.Id = 0x8000110; // FIXME
-
 
128
//  bios->can_send(&txMsg);
-
 
129
 
102
 
130
    // Main loop
103
    // Main loop
131
    while (1) {
104
    while (1) {
132
 
105
 
-
 
106
        temp = Timebase_CurrentTime();
133
        if(msg_received && !failsafe_flag ){
107
        if( temp - timeStamp >= STATUS_SEND_PERIOD ){
-
 
108
            timeStamp = temp;
-
 
109
            // Send relay status to CAN
-
 
110
            sendStatus();
-
 
111
        }
134
 
112
 
-
 
113
        if (rxMsgFull) {
-
 
114
            uint8_t acttype;
-
 
115
            uint16_t servoid;
-
 
116
       
-
 
117
            if ( ((rxMsg.Id & CAN_MASK_CLASS)>>CAN_SHIFT_CLASS) == CAN_ACT){
-
 
118
                // Actuator package
-
 
119
                acttype =(uint8_t)((rxMsg.Id & CAN_MASK_ACT_FUNCC) >> CAN_SHIFT_ACT_FUNCC);
-
 
120
                servoid = (uint16_t)((rxMsg.Id & CAN_MASK_ACT_NID) >> CAN_SHIFT_ACT_NID);
-
 
121
               
-
 
122
                if ((acttype == ACT_FUNCC_RELAY) && (servoid == NODE_ID)) {
-
 
123
                    // This is a message for this relay
-
 
124
                    if (rxMsg.Data.bytes[0] == RELAY_CMD_RESET) {
-
 
125
                        // Return to normal operation
-
 
126
                        failsafe_flag = 0;
-
 
127
                    }
-
 
128
                   
-
 
129
                    if (!failsafe_flag) {
135
            if( rxMsg_Data_bytes[0] == RELAY_CMD_ON ){
130
                        if (rxMsg.Data.bytes[0] == RELAY_CMD_ON ){
136
                // Turn relay on
131
                            // Turn relay on
137
                relayStatus = relayOn();
132
                            relayStatus = relayOn();
138
 
133
           
139
            }else if(rxMsg_Data_bytes[0] == RELAY_CMD_OFF ){
134
                        } else if (rxMsg.Data.bytes[0] == RELAY_CMD_OFF ){
140
                // Turn relay off
135
                            // Turn relay off
141
                relayStatus = relayOff();
136
                            relayStatus = relayOff();
142
 
137
           
143
            }else if(rxMsg_Data_bytes[0] == RELAY_CMD_TOGGLE ){
138
                        } else if (rxMsg.Data.bytes[0] == RELAY_CMD_TOGGLE ){
144
                    // Toggle relay
139
                                // Toggle relay
145
                    if(relayStatus == RELAY_OFF){
140
                                if(relayStatus == RELAY_OFF){
146
                        relayStatus = relayOn();
141
                                    relayStatus = relayOn();
147
                    }else{
142
                                }else{
148
                        relayStatus = relayOff();
143
                                    relayStatus = relayOff();
149
                    }
144
                                }
150
 
145
           
151
            }else if(rxMsg_Data_bytes[0] == RELAY_CMD_STATUS ){
146
                        }else if(rxMsg.Data.bytes[0] == RELAY_CMD_STATUS ){
152
                // Send relay status to CAN
147
                            // Send relay status to CAN
153
                sendStatus();
148
                            sendStatus();
154
            }
149
                        }
155
            msg_received = 0;
-
 
156
        }
150
                    }
-
 
151
                }
-
 
152
            }
-
 
153
            rxMsgFull = 0; //  
157
 
154
 
158
        temp = Timebase_CurrentTime();
-
 
159
        if( temp - timeStamp >= STATUS_SEND_PERIOD ){
-
 
160
            timeStamp = temp;
-
 
161
/*          txMsg.Id = 0x8000111;
-
 
162
            txMsg.Data.bytes[0] = timeStamp & 0x000000FF;
-
 
163
            txMsg.Data.bytes[0] = (timeStamp & 0x0000FF00)>>8;
-
 
164
            txMsg.Data.bytes[0] = (timeStamp & 0x00FF0000)>>16;
-
 
165
            txMsg.Data.bytes[0] = (timeStamp & 0xFF000000)>>24;
-
 
166
            bios->can_send(&txMsg);
-
 
167
*/          // Send relay status to CAN
-
 
168
            sendStatus();
-
 
169
        }
155
        }
-
 
156
       
170
    }
157
    }
171
    return 0;
158
    return 0;
172
}
159
}
173
 
160
 
174
uint8_t relayOff()
161
uint8_t relayOff()
Line 211... Line 198...
211
    uint32_t temperature;
198
    uint32_t temperature;
212
    Can_Message_t statusMsg;
199
    Can_Message_t statusMsg;
213
    statusMsg.RemoteFlag = 0;
200
    statusMsg.RemoteFlag = 0;
214
    statusMsg.ExtendedFlag = 1;
201
    statusMsg.ExtendedFlag = 1;
215
    statusMsg.DataLength = 4;
202
    statusMsg.DataLength = 4;
216
    statusMsg.Id = ( CLASS_SNS<<CLASS_MASK_BITS )|( SNS_ACT_STATUS_RELAY<<TYPE_MASK_BITS )|(NODE_ID);
203
    statusMsg.Id = ( CAN_SNS<<CAN_SHIFT_CLASS )|( SNS_FUNCC_RELAY_STATUS<<CAN_SHIFT_ACT_FUNCC )|( NODE_ID<<CAN_SHIFT_SNS_SID );
217
 
204
   
218
    temperature = getTC1047temperature();
205
    temperature = getTC1047temperature();
219
    if( (int32_t)temperature >= FAILSAFE_TRESHOLD && !failsafe_flag ){
206
    if( (int32_t)temperature >= FAILSAFE_TRESHOLD && !failsafe_flag ){
220
        // Goto failsafe mode
207
        // Goto failsafe mode
221
        relayFailsafe();
208
        relayFailsafe();