Subversion Repositories HomeAutomation

Rev

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

Rev 1373 Rev 1379
Line 22... Line 22...
22
    0   // crc, must be a correct value, but this will also be handled by the EEPROM module or make scripts
22
    0   // crc, must be a correct value, but this will also be handled by the EEPROM module or make scripts
23
};
23
};
24
#endif
24
#endif
25
 
25
 
26
uint16_t pwmValue[NUMBEROFCHANNELS];
26
uint16_t pwmValue[NUMBEROFCHANNELS];
27
float pwmValueFloat[NUMBEROFCHANNELS];
27
uint16_t pwmValueCAN[NUMBEROFCHANNELS];
28
#if act_softPWM_ACTIVATE_AUTOOFF != 0
28
#if act_softPWM_ACTIVATE_AUTOOFF != 0
29
uint8_t offCounter[NUMBEROFCHANNELS];
29
uint8_t offCounter[NUMBEROFCHANNELS];
30
#endif
30
#endif
31
void act_softPWM_Init(void)
31
void act_softPWM_Init(void)
32
{
32
{
33
#ifdef act_softPWM_USEEEPROM
33
#ifdef act_softPWM_USEEEPROM
34
    if (EEDATA_OK)
34
    //if (EEDATA_OK)
35
    {
35
    //{
36
      ;
36
     // ;
37
    } else
37
    //} else
38
    {   //The CRC of the EEPROM is not correct, store default values and update CRC
38
    //{ //The CRC of the EEPROM is not correct, store default values and update CRC
39
        eeprom_write_word_crc(EEDATA16.maxTimer, 10000 , WITHOUT_CRC);
39
        eeprom_write_word_crc(EEDATA16.maxTimer, 10000 , WITHOUT_CRC);
40
        eeprom_write_byte_crc(EEDATA.resolution, 0x10 , WITHOUT_CRC);
40
        eeprom_write_byte_crc(EEDATA.resolution, 1 , WITHOUT_CRC);
41
        eeprom_write_byte_crc(EEDATA.ReportInterval, 0x14 , WITHOUT_CRC);
41
        eeprom_write_byte_crc(EEDATA.ReportInterval, 0x05 , WITHOUT_CRC);
42
        EEDATA_UPDATE_CRC;
42
        EEDATA_UPDATE_CRC;
43
    }
43
    //}
-
 
44
    act_softPWM_ReportInterval = eeprom_read_byte(EEDATA.ReportInterval);
44
    maxTimer = eeprom_read_word(EEDATA16.maxTimer);
45
    maxTimer = eeprom_read_word(EEDATA16.maxTimer);
45
    resolution = eeprom_read_byte(EEDATA.resolution);
46
    resolution = eeprom_read_byte(EEDATA.resolution);
46
    Timer_SetTimeout(act_softPWM_SEND_TIMER, act_softPWM_ReportInterval*1000 , TimerTypeFreeRunning, 0);
47
    Timer_SetTimeout(act_softPWM_SEND_TIMER, act_softPWM_ReportInterval*1000 , TimerTypeFreeRunning, 0);
47
#endif  
48
#endif  
48
    ///TODO: Initialize hardware etc here
49
    ///TODO: Initialize hardware etc here
49
uint8_t i;
50
uint8_t i;
50
for (i=0; i < NUMBEROFCHANNELS; i++) {
51
for (i=0; i < NUMBEROFCHANNELS; i++) {
51
    pwmValue[i] = 0;
52
    pwmValue[i] = 0;
52
    pwmValueFloat[i] = 0;
53
    pwmValueCAN[i] = 0;
53
}
54
}
54
#ifdef PIN_0
55
#ifdef PIN_0
55
    gpio_set_out(PIN_0);
56
    gpio_set_out(PIN_0);
56
    gpio_clr_pin(PIN_0);
57
    gpio_clr_pin(PIN_0);
57
#endif
58
#endif
58
#ifdef PIN_1
59
#ifdef PIN_1
59
    gpio_set_out(PIN_1);
60
    gpio_set_out(PIN_1);
60
    gpio_clr_pin(PIN_1);
61
    gpio_clr_pin(PIN_1);
61
#endif
62
#endif
62
#ifdef PIN_2
63
#ifdef PIN_2
63
    gpio_set_out(PIN_2);
64
    gpio_set_out(PIN_2);
64
    gpio_clr_pin(PIN_2);
65
    gpio_clr_pin(PIN_2);
65
#endif
66
#endif
66
#ifdef PIN_3
67
#ifdef PIN_3
67
    gpio_set_out(PIN_3);
68
    gpio_set_out(PIN_3);
68
    gpio_clr_pin(PIN_3);
69
    gpio_clr_pin(PIN_3);
69
#endif
70
#endif
70
#ifdef PIN_4
71
#ifdef PIN_4
Line 89... Line 90...
89
 
90
 
90
void act_softPWM_Process(void)
91
void act_softPWM_Process(void)
91
{
92
{
92
    if (Timer_Expired(act_softPWM_TIMER)) {
93
    if (Timer_Expired(act_softPWM_TIMER)) {
93
        currentTimer++;
94
        currentTimer++;
94
        if (currentTimer == maxTimer) {
95
        if (currentTimer >= maxTimer) {
-
 
96
//printf("cleard Timer %d\n",pwmValue[0]);
95
            currentTimer=0;
97
            currentTimer=0;
96
            #ifdef PIN_0
98
            #ifdef PIN_0
97
                if (pwmValue[0]!=0)
99
                if (pwmValue[0]!=0) {
98
                    gpio_set_pin(PIN_0);
100
                    gpio_set_pin(PIN_0);
-
 
101
//printf("pwm_0 On %d %d %d\n", pwmValue[0], currentTimer, maxTimer); 
-
 
102
}
99
            #endif
103
            #endif
100
            #ifdef PIN_1 
104
            #ifdef PIN_1 
101
                if (pwmValue[1]!=0)
105
                if (pwmValue[1]!=0)
102
                    gpio_set_pin(PIN_1);
106
                    gpio_set_pin(PIN_1);
103
            #endif
107
            #endif
Line 125... Line 129...
125
                if (pwmValue[7]!=0)
129
                if (pwmValue[7]!=0)
126
                    gpio_set_pin(PIN_7);
130
                    gpio_set_pin(PIN_7);
127
            #endif
131
            #endif
128
        }
132
        }
129
        #ifdef PIN_0
133
        #ifdef PIN_0
130
        if (currentTimer >= pwmValue[0]) {
134
        if (currentTimer >= pwmValue[0] && gpio_get_state(PIN_0) != 0) {
131
            gpio_clr_pin(PIN_0);
135
            gpio_clr_pin(PIN_0);
-
 
136
//printf("pwm_0 Off %d %d %d\n", pwmValue[0], currentTimer, maxTimer);
132
        }
137
        }
133
        #endif
138
        #endif
134
        #ifdef PIN_1
139
        #ifdef PIN_1
135
        if (currentTimer >= pwmValue[1]) {
140
        if (currentTimer >= pwmValue[1] && gpio_get_state(PIN_1) != 0) {
136
            gpio_clr_pin(PIN_1);
141
            gpio_clr_pin(PIN_1);
137
        }
142
        }
138
        #endif
143
        #endif
139
        #ifdef PIN_2
144
        #ifdef PIN_2
140
        if (currentTimer >= pwmValue[2]) {
145
        if (currentTimer >= pwmValue[2] && gpio_get_state(PIN_2) != 0) {
141
            gpio_clr_pin(PIN_2);
146
            gpio_clr_pin(PIN_2);
142
        }
147
        }
143
        #endif
148
        #endif
144
        #ifdef PIN_3
149
        #ifdef PIN_3
145
        if (currentTimer >= pwmValue[3]) {
150
        if (currentTimer >= pwmValue[3] && gpio_get_state(PIN_3) != 0) {
146
            gpio_clr_pin(PIN_3);
151
            gpio_clr_pin(PIN_3);
147
        }
152
        }
148
        #endif
153
        #endif
149
        #ifdef PIN_4
154
        #ifdef PIN_4
150
        if (currentTimer >= pwmValue[4]) {
155
        if (currentTimer >= pwmValue[4] && gpio_get_state(PIN_4) != 0) {
151
            gpio_clr_pin(PIN_4);
156
            gpio_clr_pin(PIN_4);
152
        }
157
        }
153
        #endif
158
        #endif
154
        #ifdef PIN_5
159
        #ifdef PIN_5
155
        if (currentTimer >= pwmValue[5]) {
160
        if (currentTimer >= pwmValue[5] && gpio_get_state(PIN_5) != 0) {
156
            gpio_clr_pin(PIN_5);
161
            gpio_clr_pin(PIN_5);
157
        }
162
        }
158
        #endif
163
        #endif
159
        #ifdef PIN_6
164
        #ifdef PIN_6
160
        if (currentTimer >= pwmValue[6]) {
165
        if (currentTimer >= pwmValue[6] && gpio_get_state(PIN_6) != 0) {
161
            gpio_clr_pin(PIN_6);
166
            gpio_clr_pin(PIN_6);
162
        }
167
        }
163
        #endif
168
        #endif
164
        #ifdef PIN_7
169
        #ifdef PIN_7
165
        if (currentTimer >= pwmValue[7]) {
170
        if (currentTimer >= pwmValue[7] && gpio_get_state(PIN_7) != 0) {
166
            gpio_clr_pin(PIN_7);
171
            gpio_clr_pin(PIN_7);
167
        }
172
        }
168
        #endif
173
        #endif
169
    }
174
    }
170
    if (Timer_Expired(act_softPWM_SEND_TIMER)) {
175
    if (Timer_Expired(act_softPWM_SEND_TIMER)) {
-
 
176
//printf("sending...\n");
171
        while(1)
177
        while(1)
172
        {
178
        {
173
            if (0
179
            if (0
174
                #ifdef PIN_0
180
                #ifdef PIN_0
175
                || currentSendChannelId == 0
181
                || currentSendChannelId == 0
Line 195... Line 201...
195
                #ifdef PIN_7
201
                #ifdef PIN_7
196
                || currentSendChannelId == 7
202
                || currentSendChannelId == 7
197
                #endif
203
                #endif
198
            ) {
204
            ) {
199
                break;
205
                break;
200
            }
206
            }
201
            currentSendChannelId++;
207
            currentSendChannelId++;
202
            if (currentSendChannelId >= NUMBEROFCHANNELS)
208
            if (currentSendChannelId >= NUMBEROFCHANNELS)
203
                currentSendChannelId=0;
209
                currentSendChannelId=0;
204
        }
210
        }
-
 
211
//printf(" %d\n",currentSendChannelId);
205
        StdCan_Msg_t txMsg;
212
        StdCan_Msg_t txMsg;
206
        StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT);
213
        StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT);
207
        StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
214
        StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
208
        txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_SOFTPWM;
215
        txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_SOFTPWM;
209
        txMsg.Header.ModuleId = act_softPWM_ID;
216
        txMsg.Header.ModuleId = act_softPWM_ID;
210
        txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_PWM;
217
        txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_PWM;
211
        txMsg.Length = 3;
218
        txMsg.Length = 3;
212
        txMsg.Data[0] = currentSendChannelId;
219
        txMsg.Data[0] = currentSendChannelId;
213
        txMsg.Data[1] = (pwmValue[currentSendChannelId]>>8)&0xff;
220
        txMsg.Data[1] = (pwmValueCAN[currentSendChannelId]>>8)&0xff;
214
        txMsg.Data[2] = (pwmValue[currentSendChannelId])&0xff;
221
        txMsg.Data[2] = (pwmValueCAN[currentSendChannelId])&0xff;
215
        while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
222
        while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
-
 
223
//printf("sent pwm2: %d %d\n",pwmValueCAN[currentSendChannelId],currentSendChannelId); 
216
#if act_softPWM_ACTIVATE_AUTOOFF != 0
224
#if act_softPWM_ACTIVATE_AUTOOFF != 0
217
        offCounter[currentSendChannelId]--;
225
        offCounter[currentSendChannelId]--;
218
        if (offCounter[currentSendChannelId] == 0) {
226
        if (offCounter[currentSendChannelId] == 0) {
219
            pwmValue[currentSendChannelId] = 0;
227
            pwmValue[currentSendChannelId] = 0;
220
            pwmValueFloat[currentSendChannelId] = 0;
228
            pwmValueCAN[currentSendChannelId] = 0;
221
            offCounter[currentSendChannelId] = 1;
229
            offCounter[currentSendChannelId] = 1;
222
        }
230
        }
223
#endif
231
#endif
224
        currentSendChannelId++;
232
        currentSendChannelId++;
225
        if (currentSendChannelId >= NUMBEROFCHANNELS)
233
        if (currentSendChannelId >= NUMBEROFCHANNELS)
Line 236... Line 244...
236
        rxMsg->Header.ModuleType == CAN_MODULE_TYPE_ACT_SOFTPWM &&
244
        rxMsg->Header.ModuleType == CAN_MODULE_TYPE_ACT_SOFTPWM &&
237
        rxMsg->Header.ModuleId == act_softPWM_ID)
245
        rxMsg->Header.ModuleId == act_softPWM_ID)
238
    {
246
    {
239
        switch (rxMsg->Header.Command)
247
        switch (rxMsg->Header.Command)
240
        {
248
        {
-
 
249
//printf("Got can\n");
241
        case CAN_MODULE_CMD_PHYSICAL_PWM:
250
        case CAN_MODULE_CMD_PHYSICAL_PWM:
-
 
251
//printf("Got pwm value\n");
242
            if (rxMsg->Length == 3)
252
            if (rxMsg->Length == 3)
243
            {
253
            {
-
 
254
//printf("3\n");
244
                if (rxMsg->Data[0] < NUMBEROFCHANNELS) {
255
                if (rxMsg->Data[0] < NUMBEROFCHANNELS) {
245
                    pwmValueFloat[rxMsg->Data[0]] = ((rxMsg->Data[1]<<8) + rxMsg->Data[2])/64;
256
                    pwmValueCAN[rxMsg->Data[0]] = ((uint16_t)((rxMsg->Data[1]<<8) + rxMsg->Data[2]));
246
                    pwmValue[rxMsg->Data[0]] = (uint16_t)(pwmValueFloat[rxMsg->Data[0]]*maxTimer/(resolution*100));
257
                    pwmValue[rxMsg->Data[0]] = ((uint16_t)((((uint32_t)pwmValueCAN[rxMsg->Data[0]])*(maxTimer))/10000));
247
                    rxMsg->Data[1] = (uint8_t)0x00ff & (((uint32_t)(pwmValueFloat[rxMsg->Data[0]]*64))>>8);
258
                    rxMsg->Data[1] = (uint8_t)(0x00ff & (pwmValueCAN[rxMsg->Data[0]]>>8));
248
                    rxMsg->Data[2] = (uint8_t)0x00ff & ((uint32_t)pwmValueFloat[rxMsg->Data[0]]*64);
259
                    rxMsg->Data[2] = (uint8_t)(0x00ff & pwmValueCAN[rxMsg->Data[0]]);
249
                    StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER);
260
                    StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER);
250
                    rxMsg->Length = 3;
261
                    rxMsg->Length = 3;
251
                    while (StdCan_Put(rxMsg) != StdCan_Ret_OK);
262
                    while (StdCan_Put(rxMsg) != StdCan_Ret_OK);
-
 
263
//printf("sent pwm3: %d %d %d\n",pwmValue[rxMsg->Data[0]],maxTimer,resolution); 
252
                    #if act_softPWM_ACTIVATE_AUTOOFF != 0
264
                    #if act_softPWM_ACTIVATE_AUTOOFF != 0
253
                    offCounter[rxMsg->Data[0]] = act_softPWM_ACTIVATE_AUTOOFF;
265
                    offCounter[rxMsg->Data[0]] = act_softPWM_ACTIVATE_AUTOOFF;
254
                    #endif
266
                    #endif
255
                }
267
                }
256
            } else if (rxMsg->Length == 1) {
268
            } else if (rxMsg->Length == 1) {
257
                rxMsg->Data[1] = (uint8_t)0x00ff & (((uint32_t)(pwmValueFloat[rxMsg->Data[0]]*64))>>8);
269
                rxMsg->Data[1] = (uint8_t)(0x00ff & (pwmValueCAN[rxMsg->Data[0]]>>8));
258
                rxMsg->Data[2] = (uint8_t)0x00ff & ((uint32_t)pwmValueFloat[rxMsg->Data[0]]*64);
270
                rxMsg->Data[2] = (uint8_t)(0x00ff & pwmValueCAN[rxMsg->Data[0]]);
-
 
271
                StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER);
-
 
272
                rxMsg->Length = 3;
-
 
273
                while (StdCan_Put(rxMsg) != StdCan_Ret_OK);
-
 
274
            } else {
-
 
275
                rxMsg->Data[0] = (8);
-
 
276
                rxMsg->Data[1] = (uint8_t)(0x00ff & (pwmValueCAN[0]>>8));
-
 
277
                rxMsg->Data[2] = (uint8_t)(0x00ff & pwmValueCAN[0]);
259
                StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER);
278
                StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER);
260
                rxMsg->Length = 3;
279
                rxMsg->Length = 3;
261
                while (StdCan_Put(rxMsg) != StdCan_Ret_OK);
280
                while (StdCan_Put(rxMsg) != StdCan_Ret_OK);
262
            }
281
            }
263
            break;
282
            break;