Subversion Repositories HomeAutomation

Rev

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

Rev 1528 Rev 1532
Line 5... Line 5...
5
 
5
 
6
 
6
 
7
/*-----------------------------------------------------------------------------
7
/*-----------------------------------------------------------------------------
8
 * Defines
8
 * Defines
9
 *---------------------------------------------------------------------------*/
9
 *---------------------------------------------------------------------------*/
10
#define OUTPUT0_ON      (act_protectedOutput_OUTPUT0_PIN_ACTIVE_STATE)
-
 
11
#define OUTPUT0_OFF     (1 - OUTPUT0_ON)
-
 
12
#define OUTPUT0_PIN     act_protectedOutput_OUTPUT0_PIN
-
 
13
 
10
 
14
#define OUTPUT1_ON      (act_protectedOutput_OUTPUT1_PIN_ACTIVE_STATE)
-
 
15
#define OUTPUT1_OFF     (1 - OUTPUT1_ON)
11
// make sure all config params are present
16
#define OUTPUT1_PIN     act_protectedOutput_OUTPUT1_PIN
-
 
17
 
12
 
-
 
13
#ifndef act_protectedOutput_CH_COUNT
-
 
14
#error Config value missing: "act_protectedOutput_CH_COUNT"
-
 
15
#endif
-
 
16
 
-
 
17
#ifndef act_protectedOutput_EEPROM_ENABLED
-
 
18
#error Config value missing: "act_protectedOutput_EEPROM_ENABLED"
-
 
19
#endif
-
 
20
 
-
 
21
#if act_protectedOutput_CH_COUNT >= 1
-
 
22
#ifndef act_protectedOutput_CH0
-
 
23
#error Config value missing: "act_protectedOutput_CH0"
-
 
24
#endif
-
 
25
#ifndef act_protectedOutput_CH0_POLARITY
-
 
26
#error Config value missing: "act_protectedOutput_CH0_POLARITY"
-
 
27
#endif
-
 
28
#endif
-
 
29
 
-
 
30
#if act_protectedOutput_CH_COUNT >= 2
-
 
31
#ifndef act_protectedOutput_CH1
-
 
32
#error Config value missing: "act_protectedOutput_CH1"
-
 
33
#endif
-
 
34
#ifndef act_protectedOutput_CH1_POLARITY
-
 
35
#error Config value missing: "act_protectedOutput_CH1_POLARITY"
-
 
36
#endif
-
 
37
#endif
-
 
38
 
-
 
39
#if act_protectedOutput_CH_COUNT >= 3
-
 
40
#ifndef act_protectedOutput_CH2
-
 
41
#error Config value missing: "act_protectedOutput_CH2"
-
 
42
#endif
-
 
43
#ifndef act_protectedOutput_CH2_POLARITY
-
 
44
#error Config value missing: "act_protectedOutput_CH2_POLARITY"
-
 
45
#endif
-
 
46
#endif
-
 
47
 
-
 
48
#if act_protectedOutput_CH_COUNT >= 4
-
 
49
#ifndef act_protectedOutput_CH3
-
 
50
#error Config value missing: "act_protectedOutput_CH3"
-
 
51
#endif
-
 
52
#ifndef act_protectedOutput_CH3_POLARITY
-
 
53
#error Config value missing: "act_protectedOutput_CH3_POLARITY"
-
 
54
#endif
-
 
55
#endif
-
 
56
 
-
 
57
#if act_protectedOutput_CH_COUNT >= 5
-
 
58
#error Config value out of range: "act_protectedOutput_CH_COUNT"
-
 
59
#endif
-
 
60
 
-
 
61
#define CH0_ON      (act_protectedOutput_CH0_POLARITY)
-
 
62
#define CH0_OFF     (1 - CH0_ON)
-
 
63
#define CH0_PIN     act_protectedOutput_CH0
-
 
64
 
-
 
65
#define CH1_ON      (act_protectedOutput_CH1_POLARITY)
-
 
66
#define CH1_OFF     (1 - CH1_ON)
-
 
67
#define CH1_PIN     act_protectedOutput_CH1
-
 
68
 
18
#define OUTPUT2_ON      (act_protectedOutput_OUTPUT2_PIN_ACTIVE_STATE)
69
#define CH2_ON      (act_protectedOutput_CH2_POLARITY)
19
#define OUTPUT2_OFF     (1 - OUTPUT2_ON)
70
#define CH2_OFF     (1 - CH2_ON)
20
#define OUTPUT2_PIN     act_protectedOutput_OUTPUT2_PIN
71
#define CH2_PIN     act_protectedOutput_CH2
21
 
72
 
22
#define OUTPUT3_ON      (act_protectedOutput_OUTPUT3_PIN_ACTIVE_STATE)
73
#define CH3_ON      (act_protectedOutput_CH3_POLARITY)
23
#define OUTPUT3_OFF     (1 - OUTPUT3_ON)
74
#define CH3_OFF     (1 - CH3_ON)
24
#define OUTPUT3_PIN     act_protectedOutput_OUTPUT3_PIN
75
#define CH3_PIN     act_protectedOutput_CH3
25
 
76
 
26
#define DIAG_ASSERTED   (act_protectedOutput_DIAG_PIN_ASSERTED_STATE)
77
#define DIAG_ASSERTED   (act_protectedOutput_DIAG_PIN_POLARITY)
27
#define DIAG_NORMAL     (1 - DIAG_ASSERTED)
78
#define DIAG_NORMAL     (1 - DIAG_ASSERTED)
28
#define DIAG_PIN        act_protectedOutput_DIAG_PIN
79
#define DIAG_PIN        act_protectedOutput_DIAG_PIN
29
 
80
 
30
 
81
 
31
/*-----------------------------------------------------------------------------
82
/*-----------------------------------------------------------------------------
32
 * Types
83
 * Types
33
 *---------------------------------------------------------------------------*/
84
 *---------------------------------------------------------------------------*/
-
 
85
 
-
 
86
 
-
 
87
#if act_protectedOutput_EEPROM_ENABLED == 1
-
 
88
#include "act_protectedOutput_eeprom.h"
-
 
89
struct eeprom_act_protectedOutput EEMEM eeprom_act_protectedOutput =
-
 
90
{
-
 
91
    {
-
 
92
        ///TODO: Define initialization values on the EEPROM variables here, this will generate a *.eep file that can be used to store this values to the node, can in future be done with a EEPROM module and the make-scrips. Write the values in the exact same order as the struct is defined in the *.h file.
-
 
93
#if act_protectedOutput_CH_COUNT >= 1
-
 
94
        0x00,
-
 
95
#endif
-
 
96
#if act_protectedOutput_CH_COUNT >= 2
-
 
97
        0x00,
-
 
98
#endif
-
 
99
#if act_protectedOutput_CH_COUNT >= 3
-
 
100
        0x00,
-
 
101
#endif
-
 
102
#if act_protectedOutput_CH_COUNT >= 4
-
 
103
        0x00,
-
 
104
#endif
-
 
105
    },
-
 
106
    0   // crc, must be a correct value, but this will also be handled by the EEPROM module or make scripts
-
 
107
};
-
 
108
#endif
34
 
109
 
35
 
110
 
36
/*-----------------------------------------------------------------------------
111
/*-----------------------------------------------------------------------------
37
 * Variables
112
 * Variables
38
 *---------------------------------------------------------------------------*/
113
 *---------------------------------------------------------------------------*/
39
 
114
 
40
static uint8_t outputStateTarget[act_protectedOutput_NR_OUTPUT_PINS];
115
static uint8_t outputStateTarget[act_protectedOutput_CH_COUNT];
41
 
-
 
42
static uint8_t diagState = DIAG_NORMAL;
116
static uint8_t diagState = DIAG_NORMAL;
43
 
117
 
44
// is there a recent status change that we should report via CAN?
118
// is there a recent status change that we should report via CAN?
45
static uint8_t diagReportPending = 0;
119
static uint8_t diagReportPending = 0;
46
 
120
 
Line 57... Line 131...
57
 * Function Implementations
131
 * Function Implementations
58
 *---------------------------------------------------------------------------*/
132
 *---------------------------------------------------------------------------*/
59
 
133
 
60
static void updateOutput() {
134
static void updateOutput() {
61
 
135
 
62
    #if act_protectedOutput_NR_OUTPUT_PINS >= 1
136
    #if act_protectedOutput_CH_COUNT >= 1
63
    if (outputStateTarget[0] == 0) {
137
    if (outputStateTarget[0] == 0) {
64
        gpio_set_statement(OUTPUT0_OFF, OUTPUT0_PIN);
138
        gpio_set_statement(CH0_OFF, CH0_PIN);
65
    }
139
    }
66
    else if (outputStateTarget[0] == 1) {
140
    else if (outputStateTarget[0] == 1) {
67
        if (diagState == DIAG_NORMAL) {
141
        if (diagState == DIAG_NORMAL) {
68
            gpio_set_statement(OUTPUT0_ON, OUTPUT0_PIN);
142
            gpio_set_statement(CH0_ON, CH0_PIN);
69
        }
143
        }
70
        else {
144
        else {
71
            // DIAG override, we cannot set ON state right now
145
            // DIAG override, we cannot set ON state right now
72
            gpio_set_statement(OUTPUT0_OFF, OUTPUT0_PIN);
146
            gpio_set_statement(CH0_OFF, CH0_PIN);
73
        }
147
        }
74
    }
148
    }
75
    #endif
149
    #endif
76
 
150
 
77
    #if act_protectedOutput_NR_OUTPUT_PINS >= 2
151
    #if act_protectedOutput_CH_COUNT >= 2
78
    if (outputStateTarget[1] == 0) {
152
    if (outputStateTarget[1] == 0) {
79
        gpio_set_statement(OUTPUT1_OFF, OUTPUT1_PIN);
153
        gpio_set_statement(CH1_OFF, CH1_PIN);
80
    }
154
    }
81
    else if (outputStateTarget[1] == 1) {
155
    else if (outputStateTarget[1] == 1) {
82
        if (diagState == DIAG_NORMAL) {
156
        if (diagState == DIAG_NORMAL) {
83
            gpio_set_statement(OUTPUT1_ON, OUTPUT1_PIN);
157
            gpio_set_statement(CH1_ON, CH1_PIN);
84
        }
-
 
85
        else {
-
 
86
            // DIAG override, we cannot set ON state right now
-
 
87
            gpio_set_statement(OUTPUT1_OFF, OUTPUT1_PIN);
-
 
88
        }
-
 
89
    }
-
 
90
    #endif
-
 
91
 
-
 
92
    #if act_protectedOutput_NR_OUTPUT_PINS >= 3
-
 
93
    if (outputStateTarget[2] == 0) {
-
 
94
        gpio_set_statement(OUTPUT2_OFF, OUTPUT2_PIN);
-
 
95
    }
-
 
96
    else if (outputStateTarget[2] == 1) {
-
 
97
        if (diagState == DIAG_NORMAL) {
-
 
98
            gpio_set_statement(OUTPUT2_ON, OUTPUT2_PIN);
-
 
99
        }
158
        }
100
        else {
159
        else {
101
            // DIAG override, we cannot set ON state right now
160
            // DIAG override, we cannot set ON state right now
102
            gpio_set_statement(OUTPUT2_OFF, OUTPUT2_PIN);
161
            gpio_set_statement(CH1_OFF, CH1_PIN);
103
        }
162
        }
104
    }
163
    }
105
    #endif
164
    #endif
106
 
165
 
-
 
166
    #if act_protectedOutput_CH_COUNT >= 3
-
 
167
    if (outputStateTarget[2] == 0) {
-
 
168
        gpio_set_statement(CH2_OFF, CH2_PIN);
-
 
169
    }
-
 
170
    else if (outputStateTarget[2] == 1) {
-
 
171
        if (diagState == DIAG_NORMAL) {
-
 
172
            gpio_set_statement(CH2_ON, CH2_PIN);
-
 
173
        }
-
 
174
        else {
-
 
175
            // DIAG override, we cannot set ON state right now
-
 
176
            gpio_set_statement(CH2_OFF, CH2_PIN);
-
 
177
        }
-
 
178
    }
-
 
179
    #endif
-
 
180
 
107
    #if act_protectedOutput_NR_OUTPUT_PINS >= 4
181
    #if act_protectedOutput_CH_COUNT >= 4
108
    if (outputStateTarget[3] == 0) {
182
    if (outputStateTarget[3] == 0) {
109
        gpio_set_statement(OUTPUT3_OFF, OUTPUT3_PIN);
183
        gpio_set_statement(CH3_OFF, CH3_PIN);
110
    }
184
    }
111
    else if (outputStateTarget[3] == 1) {
185
    else if (outputStateTarget[3] == 1) {
112
        if (diagState == DIAG_NORMAL) {
186
        if (diagState == DIAG_NORMAL) {
113
            gpio_set_statement(OUTPUT3_ON, OUTPUT3_PIN);
187
            gpio_set_statement(CH3_ON, CH3_PIN);
114
        }
188
        }
115
        else {
189
        else {
116
            // DIAG override, we cannot set ON state right now
190
            // DIAG override, we cannot set ON state right now
117
            gpio_set_statement(OUTPUT3_OFF, OUTPUT3_PIN);
191
            gpio_set_statement(CH3_OFF, CH3_PIN);
118
        }
-
 
119
    }
192
        }
-
 
193
    }
120
    #endif
194
    #endif
121
}
195
}
122
 
196
 
123
 
197
 
124
static void readDiagPin() {
198
static void readDiagPin() {
125
    diagState = gpio_get_state(DIAG_PIN);
199
    diagState = gpio_get_state(DIAG_PIN);
126
}
200
}
127
 
201
 
128
 
202
 
129
static void pcIntCallback(uint8_t id, uint8_t status) {
203
static void pcIntCallback(uint8_t id, uint8_t status) {
130
    // new state of the DIAG pin?
204
    // new state of the DIAG pin?
131
    if (id == act_protectedOutput_DIAG_PIN_PCINT) {
205
    if (id == act_protectedOutput_DIAG_PIN_PCINT) {
132
        diagState = status;
206
        diagState = status;
133
        updateOutput();
207
        updateOutput();
134
        // DIAG asserted?
208
        // DIAG asserted?
135
        if (diagState == DIAG_ASSERTED) {
209
        if (diagState == DIAG_ASSERTED) {
136
            // if retry-timer mechanism is enabled, initiate timer
210
            // if retry-timer mechanism is enabled, initiate timer
137
            if (act_protectedOutput_RETRY_TIMER_TIME_S > 0) {
211
            if (act_protectedOutput_RETRY_TIMER_TIME_S > 0) {
138
                Timer_SetTimeout(act_protectedOutput_RETRY_TIMER, act_protectedOutput_RETRY_TIMER_TIME_S*1000, TimerTypeOneShot, 0);
212
                Timer_SetTimeout(act_protectedOutput_RETRY_TIMER, act_protectedOutput_RETRY_TIMER_TIME_S*1000, TimerTypeOneShot, 0);
139
            }
213
            }
140
            // if the retry-mechanism is disabled, change the target output state to OFF
214
            // if the retry-mechanism is disabled, change the target output state to OFF
141
            else {
215
            else {
142
                for (uint8_t i=0; i<act_protectedOutput_NR_OUTPUT_PINS; i++) {
216
                for (uint8_t i=0; i<act_protectedOutput_CH_COUNT; i++) {
143
                    outputStateTarget[i] = 0;
217
                    outputStateTarget[i] = 0;
144
                }
218
                }
145
            }
219
            }
146
        }
220
        }
147
        // something interesting obviously happened. let's report it
221
        // something interesting obviously happened. let's report it
148
        diagReportPending = 1;
222
        diagReportPending = 1;
149
    }
223
    }
150
}
224
}
151
 
225
 
152
 
226
 
153
void act_protectedOutput_Init() {
227
void act_protectedOutput_Init() {
-
 
228
    /*
-
 
229
     * Init target state vector, in case EEPROM is disabled
-
 
230
     */
-
 
231
    for (uint8_t i=0; i<act_protectedOutput_CH_COUNT; i++) {
-
 
232
        outputStateTarget[i] = 0;
-
 
233
    }
-
 
234
 
154
    /*
235
    /*
155
     * Configure DIAG input pin
236
     * Configure DIAG input pin
156
     */
237
     */
157
    if (act_protectedOutput_DIAG_PIN_PULL_ENABLED) {
238
    if (act_protectedOutput_DIAG_PIN_PULL_ENABLED) {
158
        // if DIAG is asserted low, we need pull-up
239
        // if DIAG is asserted low, we need pull-up
159
        gpio_set_statement(act_protectedOutput_DIAG_PIN_ASSERTED_STATE == 0 ? 1 : 0, DIAG_PIN);
240
        gpio_set_statement(act_protectedOutput_DIAG_PIN_POLARITY == 0 ? 1 : 0, DIAG_PIN);
160
    }
241
    }
161
    gpio_set_in(DIAG_PIN);
242
    gpio_set_in(DIAG_PIN);
162
    Pcint_SetCallbackPin(act_protectedOutput_DIAG_PIN_PCINT, DIAG_PIN, &pcIntCallback);
243
    Pcint_SetCallbackPin(act_protectedOutput_DIAG_PIN_PCINT, DIAG_PIN, &pcIntCallback);
-
 
244
   
-
 
245
    /*
-
 
246
     * Read EEPROM data
-
 
247
     */
-
 
248
#ifdef act_protectedOutput_EEPROM_ENABLED
-
 
249
    if (EEDATA_OK)
-
 
250
    {
-
 
251
      ///TODO: Use stored data to set initial values for the module
-
 
252
      uint8_t index = 0;
-
 
253
#if act_protectedOutput_CH_COUNT >= 1
-
 
254
      outputStateTarget[0] = eeprom_read_byte(EEDATA.ch0);
-
 
255
#endif
-
 
256
#if act_protectedOutput_CH_COUNT >= 2
-
 
257
      outputStateTarget[1] = eeprom_read_byte(EEDATA.ch1);
-
 
258
#endif
-
 
259
#if act_protectedOutput_CH_COUNT >= 3
-
 
260
      outputStateTarget[2] = eeprom_read_byte(EEDATA.ch2);
-
 
261
#endif
-
 
262
#if act_protectedOutput_CH_COUNT >= 4
-
 
263
      outputStateTarget[3] = eeprom_read_byte(EEDATA.ch3);
-
 
264
#endif
-
 
265
    }
-
 
266
    else {
-
 
267
        //The CRC of the EEPROM is not correct, store default values and update CRC
-
 
268
#if act_protectedOutput_CH_COUNT >= 1
-
 
269
        eeprom_write_byte_crc(EEDATA.ch0, 0x00, WITHOUT_CRC);
-
 
270
#endif
-
 
271
#if act_protectedOutput_CH_COUNT >= 2
-
 
272
        eeprom_write_byte_crc(EEDATA.ch1, 0x00, WITHOUT_CRC);
-
 
273
#endif
-
 
274
#if act_protectedOutput_CH_COUNT >= 3
-
 
275
        eeprom_write_byte_crc(EEDATA.ch2, 0x00, WITHOUT_CRC);
-
 
276
#endif
-
 
277
#if act_protectedOutput_CH_COUNT >= 4
-
 
278
        eeprom_write_byte_crc(EEDATA.ch3, 0x00, WITHOUT_CRC);
-
 
279
#endif
-
 
280
        EEDATA_UPDATE_CRC;
-
 
281
    }
-
 
282
#endif
163
   
283
 
164
    /*
284
    /*
165
     * Configure OUTPUT pins
285
     * Configure OUTPUT pins
166
     */
286
     */
167
    #if act_protectedOutput_NR_OUTPUT_PINS >= 1
287
    #if act_protectedOutput_CH_COUNT >= 1
168
    gpio_set_statement(OUTPUT0_OFF, OUTPUT0_PIN);
288
    gpio_set_statement(CH0_OFF, CH0_PIN);
169
    gpio_set_out(act_protectedOutput_OUTPUT0_PIN);
289
    gpio_set_out(act_protectedOutput_CH0);
170
    #endif
-
 
171
   
-
 
172
    #if act_protectedOutput_NR_OUTPUT_PINS >= 2
-
 
173
    gpio_set_statement(OUTPUT1_OFF, OUTPUT1_PIN);
-
 
174
    gpio_set_out(act_protectedOutput_OUTPUT1_PIN);
-
 
175
    #endif
-
 
176
 
-
 
177
    #if act_protectedOutput_NR_OUTPUT_PINS >= 3
-
 
178
    gpio_set_statement(OUTPUT2_OFF, OUTPUT2_PIN);
-
 
179
    gpio_set_out(act_protectedOutput_OUTPUT2_PIN);
-
 
180
    #endif
290
    #endif
181
 
291
   
-
 
292
    #if act_protectedOutput_CH_COUNT >= 2
-
 
293
    gpio_set_statement(CH1_OFF, CH1_PIN);
-
 
294
    gpio_set_out(act_protectedOutput_CH1);
-
 
295
    #endif
-
 
296
 
-
 
297
    #if act_protectedOutput_CH_COUNT >= 3
-
 
298
    gpio_set_statement(CH2_OFF, CH2_PIN);
-
 
299
    gpio_set_out(act_protectedOutput_CH2);
-
 
300
    #endif
-
 
301
 
182
    #if act_protectedOutput_NR_OUTPUT_PINS >= 4
302
    #if act_protectedOutput_CH_COUNT >= 4
183
    gpio_set_statement(OUTPUT3_OFF, OUTPUT3_PIN);
303
    gpio_set_statement(CH3_OFF, CH3_PIN);
184
    gpio_set_out(act_protectedOutput_OUTPUT3_PIN);
304
    gpio_set_out(act_protectedOutput_CH3);
185
    #endif
305
    #endif
186
 
-
 
187
    for (uint8_t i=0; i<act_protectedOutput_NR_OUTPUT_PINS; i++) {
-
 
188
        outputStateTarget[i] = 0;
-
 
189
    }
-
 
190
 
306
 
191
    diagState = DIAG_NORMAL;
307
    diagState = DIAG_NORMAL;
192
}
308
}
193
 
309
 
194
 
310
 
Line 238... Line 354...
238
        rxMsg->Header.ModuleId == act_protectedOutput_ID)
354
        rxMsg->Header.ModuleId == act_protectedOutput_ID)
239
    {
355
    {
240
        switch (rxMsg->Header.Command) {
356
        switch (rxMsg->Header.Command) {
241
           
357
           
242
            case CAN_MODULE_CMD_PHYSICAL_SETPIN:
358
            case CAN_MODULE_CMD_PHYSICAL_SETPIN:
243
               
359
                /*
-
 
360
                 * This message is used to activate/deactivate
-
 
361
                 * an output channel.
-
 
362
                 */
244
                if (rxMsg->Length == 2) {
363
                if (rxMsg->Length == 2) {
245
                    uint8_t channel = rxMsg->Data[0];
364
                    uint8_t channel = rxMsg->Data[0];
246
                    if (channel >= 0 && channel < act_protectedOutput_NR_OUTPUT_PINS) {
365
                    if (channel >= 0 && channel < act_protectedOutput_CH_COUNT) {
247
                        outputStateTarget[channel] = rxMsg->Data[1];
366
                        outputStateTarget[channel] = rxMsg->Data[1];
248
                        readDiagPin();
367
                        readDiagPin();
249
                        updateOutput();
368
                        updateOutput();
250
                    }              
369
                    }              
251
                    StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER);
370
                    StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER);