Subversion Repositories HomeAutomation

Rev

Rev 1243 | Rev 1370 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1156 linlun 1
 
2
#include "act_softPWM.h"
1183 linlun 3
 
4
 
1177 linlun 5
uint16_t currentTimer = 0;
6
uint16_t maxTimer = 10;
7
uint8_t resolution = 1;
1369 linlun 8
uint8_t act_softPWM_ReportInterval;
1180 linlun 9
uint8_t currentSendChannelId = 0;
1156 linlun 10
 
1183 linlun 11
 
1156 linlun 12
#ifdef act_softPWM_USEEEPROM
13
#include "act_softPWM_eeprom.h"
14
struct eeprom_act_softPWM EEMEM eeprom_act_softPWM =
15
{
16
    {
17
        ///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. 
1369 linlun 18
        10000,  // x
19
        10,
1180 linlun 20
        0x14    //Reportinteval 20 sec.
1156 linlun 21
    },
22
 
23
};
24
#endif
1183 linlun 25
 
1180 linlun 26
uint16_t pwmValue[NUMBEROFCHANNELS];
1369 linlun 27
float pwmValueFloat[NUMBEROFCHANNELS];
1156 linlun 28
 
29
void act_softPWM_Init(void)
30
{
31
#ifdef act_softPWM_USEEEPROM
32
    if (EEDATA_OK)
33
    {
1180 linlun 34
      ;
1156 linlun 35
    } else
36
    {   //The CRC of the EEPROM is not correct, store default values and update CRC
1369 linlun 37
        eeprom_write_word_crc(EEDATA16.maxTimer, 10000 , WITHOUT_CRC);
38
        eeprom_write_byte_crc(EEDATA.resolution, 0x10 , WITHOUT_CRC);
1180 linlun 39
        eeprom_write_byte_crc(EEDATA.ReportInterval, 0x14 , WITHOUT_CRC);
40
        EEDATA_UPDATE_CRC;
1156 linlun 41
    }
1369 linlun 42
    maxTimer = eeprom_read_word(EEDATA16.maxTimer);
43
    resolution = eeprom_read_byte(EEDATA.resolution);
1180 linlun 44
    Timer_SetTimeout(act_softPWM_SEND_TIMER, act_softPWM_ReportInterval*1000 , TimerTypeFreeRunning, 0);
1156 linlun 45
#endif  
46
    ///TODO: Initialize hardware etc here
1369 linlun 47
uint8_t i;
48
for (i=0, i < NUMBEROFCHANNELS; i++) {
49
    pwmValue[i] = 0;
50
    pwmValueFloat[i] = 0;
51
}
1177 linlun 52
#ifdef PIN_0
53
    gpio_set_out(PIN_0);
54
    gpio_clr_pin(PIN_0);
55
#endif
56
#ifdef PIN_1
57
    gpio_set_out(PIN_1);
58
    gpio_clr_pin(PIN_1);
59
#endif
60
#ifdef PIN_2
61
    gpio_set_out(PIN_2);
62
    gpio_clr_pin(PIN_2);
63
#endif
64
#ifdef PIN_3
65
    gpio_set_out(PIN_3);
66
    gpio_clr_pin(PIN_3);
67
#endif
68
#ifdef PIN_4
69
    gpio_set_out(PIN_4);
70
    gpio_clr_pin(PIN_4);
71
#endif
72
#ifdef PIN_5
73
    gpio_set_out(PIN_5);
74
    gpio_clr_pin(PIN_5);
75
#endif
76
#ifdef PIN_6
77
    gpio_set_out(PIN_6);
78
    gpio_clr_pin(PIN_6);
79
#endif
80
#ifdef PIN_7
81
    gpio_set_out(PIN_7);
82
    gpio_clr_pin(PIN_7);
83
#endif
1156 linlun 84
 
1177 linlun 85
    Timer_SetTimeout(act_softPWM_TIMER, resolution, TimerTypeFreeRunning, NULL);
1156 linlun 86
}
87
 
88
void act_softPWM_Process(void)
89
{
90
    if (Timer_Expired(act_softPWM_TIMER)) {
91
        currentTimer++;
1369 linlun 92
        if (currentTimer == maxTimer) {
1156 linlun 93
            currentTimer=0;
1177 linlun 94
            #ifdef PIN_0 
95
                gpio_set_pin(PIN_0);
96
            #endif
97
            #ifdef PIN_1 
98
                gpio_set_pin(PIN_1);
99
            #endif
100
            #ifdef PIN_2 
101
                gpio_set_pin(PIN_2);
102
            #endif
103
            #ifdef PIN_3 
104
                gpio_set_pin(PIN_3);
105
            #endif
106
            #ifdef PIN_4 
107
                gpio_set_pin(PIN_4);
108
            #endif
109
            #ifdef PIN_5 
110
                gpio_set_pin(PIN_5);
111
            #endif
112
            #ifdef PIN_6 
113
                gpio_set_pin(PIN_6);
114
            #endif
115
            #ifdef PIN_7 
116
                gpio_set_pin(PIN_7);
117
            #endif
1156 linlun 118
        }
1177 linlun 119
        #ifdef PIN_0
1184 linlun 120
        if (currentTimer >= pwmValue[0]) {
1177 linlun 121
            gpio_clr_pin(PIN_0);
1156 linlun 122
        }
1177 linlun 123
        #endif
124
        #ifdef PIN_1
1184 linlun 125
        if (currentTimer >= pwmValue[1]) {
1177 linlun 126
            gpio_clr_pin(PIN_1);
1156 linlun 127
        }
1177 linlun 128
        #endif
129
        #ifdef PIN_2
1184 linlun 130
        if (currentTimer >= pwmValue[2]) {
1177 linlun 131
            gpio_clr_pin(PIN_2);
1156 linlun 132
        }
1177 linlun 133
        #endif
134
        #ifdef PIN_3
1184 linlun 135
        if (currentTimer >= pwmValue[3]) {
1177 linlun 136
            gpio_clr_pin(PIN_3);
137
        }
138
        #endif
139
        #ifdef PIN_4
1184 linlun 140
        if (currentTimer >= pwmValue[4]) {
1177 linlun 141
            gpio_clr_pin(PIN_4);
142
        }
143
        #endif
144
        #ifdef PIN_5
1184 linlun 145
        if (currentTimer >= pwmValue[5]) {
1177 linlun 146
            gpio_clr_pin(PIN_5);
147
        }
148
        #endif
149
        #ifdef PIN_6
1184 linlun 150
        if (currentTimer >= pwmValue[6]) {
1177 linlun 151
            gpio_clr_pin(PIN_6);
152
        }
153
        #endif
154
        #ifdef PIN_7
1184 linlun 155
        if (currentTimer >= pwmValue[7]) {
1177 linlun 156
            gpio_clr_pin(PIN_7);
157
        }
158
        #endif
1156 linlun 159
    }
1180 linlun 160
    if (Timer_Expired(act_softPWM_SEND_TIMER)) {
1184 linlun 161
        while(1)
162
        {
163
            if (0
164
                #ifdef PIN_0
165
                || currentSendChannelId == 0
166
                #endif
167
                #ifdef PIN_1
168
                || currentSendChannelId == 1
169
                #endif
170
                #ifdef PIN_2
171
                || currentSendChannelId == 2
172
                #endif
173
                #ifdef PIN_3
174
                || currentSendChannelId == 3
175
                #endif
176
                #ifdef PIN_4
177
                || currentSendChannelId == 4
178
                #endif
179
                #ifdef PIN_5
180
                || currentSendChannelId == 5
181
                #endif
182
                #ifdef PIN_6
183
                || currentSendChannelId == 6
184
                #endif
185
                #ifdef PIN_7
186
                || currentSendChannelId == 7
187
                #endif
188
            ) {
189
                break;
190
            }
191
            currentSendChannelId++;
192
            if (currentSendChannelId >= NUMBEROFCHANNELS)
193
                currentSendChannelId=0;
194
        }
1180 linlun 195
        StdCan_Msg_t txMsg;
196
        StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT);
197
        StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
198
        txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_SOFTPWM;
199
        txMsg.Header.ModuleId = act_softPWM_ID;
200
        txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_PWM;
201
        txMsg.Length = 3;
202
        txMsg.Data[0] = currentSendChannelId;
203
        txMsg.Data[1] = (pwmValue[currentSendChannelId]>>8)&0xff;
204
        txMsg.Data[2] = (pwmValue[currentSendChannelId])&0xff;
1369 linlun 205
        while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
1180 linlun 206
        currentSendChannelId++;
207
        if (currentSendChannelId >= NUMBEROFCHANNELS)
208
            currentSendChannelId=0;
209
    }
1156 linlun 210
}
211
 
1180 linlun 212
 
213
 
1156 linlun 214
void act_softPWM_HandleMessage(StdCan_Msg_t *rxMsg)
215
{
216
    if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_ACT &&
217
        StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
218
        rxMsg->Header.ModuleType == CAN_MODULE_TYPE_ACT_SOFTPWM &&
219
        rxMsg->Header.ModuleId == act_softPWM_ID)
220
    {
221
        switch (rxMsg->Header.Command)
222
        {
1177 linlun 223
        case CAN_MODULE_CMD_PHYSICAL_PWM:
1369 linlun 224
            if (rxMsg->Length == 3)
1184 linlun 225
            {
1369 linlun 226
                if (rxMsg->Data[0] < NUMBEROFCHANNELS) {
227
                    pwmValueFloat[rxMsg->Data[0]] = ((rxMsg->Data[1]<<8) + rxMsg->Data[2])/64;
228
                    pwmValue[rxMsg->Data[0]] = (uint16_t)(pwmValueFloat[rxMsg->Data[0]]*maxTimer/(resolution*100));
229
                    rxMsg->Data[1] = (uint8_t)0x00ff & (((uint32_t)(pwmValueFloat[rxMsg->Data[0]]*64))>>8);
230
                    rxMsg->Data[2] = (uint8_t)0x00ff & ((uint32_t)pwmValueFloat[rxMsg->Data[0]]*64);
231
                    StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER);
232
                    rxMsg->Length = 3;
233
                    while (StdCan_Put(rxMsg) != StdCan_Ret_OK);
234
                }
1243 arune 235
            } else if (rxMsg->Length == 1) {
1369 linlun 236
                rxMsg->Data[1] = (uint8_t)0x00ff & (((uint32_t)(pwmValueFloat[rxMsg->Data[0]]*64))>>8);
237
                rxMsg->Data[2] = (uint8_t)0x00ff & ((uint32_t)pwmValueFloat[rxMsg->Data[0]]*64);
1184 linlun 238
                StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER);
1369 linlun 239
                rxMsg->Length = 3;
240
                while (StdCan_Put(rxMsg) != StdCan_Ret_OK);
1177 linlun 241
            }
242
            break;
243
        case CAN_MODULE_CMD_SOFTPWM_CONFIG:
244
            maxTimer = (((uint16_t)rxMsg->Data[0])<<8) + rxMsg->Data[1];
245
            resolution = rxMsg->Data[2];
246
            Timer_SetTimeout(act_softPWM_TIMER, resolution, TimerTypeFreeRunning, NULL);
1369 linlun 247
            eeprom_write_word_crc(EEDATA16.maxTimer, maxTimer , WITHOUT_CRC);
248
            eeprom_write_byte_crc(EEDATA.resolution, resolution , WITHOUT_CRC);
249
            EEDATA_UPDATE_CRC;
250
            StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER);
251
            while (StdCan_Put(rxMsg) != StdCan_Ret_OK);
1177 linlun 252
            break;
1180 linlun 253
        case CAN_MODULE_CMD_GLOBAL_REPORT_INTERVAL:
254
            if (rxMsg->Length > 0)
255
            {
256
                act_softPWM_ReportInterval = rxMsg->Data[0];
1242 arune 257
                eeprom_write_byte_crc(EEDATA.ReportInterval, act_softPWM_ReportInterval , WITH_CRC);
1180 linlun 258
                Timer_SetTimeout(act_softPWM_SEND_TIMER, act_softPWM_ReportInterval*1000 , TimerTypeFreeRunning, 0);
259
            }
1177 linlun 260
 
1180 linlun 261
            StdCan_Msg_t txMsg;
262
 
263
            StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT);
264
            StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
265
            txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_SOFTPWM;
266
            txMsg.Header.ModuleId = act_softPWM_ID;
267
            txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_REPORT_INTERVAL;
268
            txMsg.Length = 1;
269
            txMsg.Data[0] = act_softPWM_ReportInterval;
1369 linlun 270
            while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
1180 linlun 271
            break;
1156 linlun 272
        }
273
    }
274
}
275
 
276
void act_softPWM_List(uint8_t ModuleSequenceNumber)
277
{
278
    StdCan_Msg_t txMsg;
279
 
280
    StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT); ///TODO: Change this to the actual class type
281
    StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
282
    txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_SOFTPWM; ///TODO: Change this to the actual module type
283
    txMsg.Header.ModuleId = act_softPWM_ID;
284
    txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
285
    txMsg.Length = 6;
286
 
287
    txMsg.Data[0] = NODE_HW_ID_BYTE0;
288
    txMsg.Data[1] = NODE_HW_ID_BYTE1;
289
    txMsg.Data[2] = NODE_HW_ID_BYTE2;
290
    txMsg.Data[3] = NODE_HW_ID_BYTE3;
291
 
292
    txMsg.Data[4] = NUMBER_OF_MODULES;
293
    txMsg.Data[5] = ModuleSequenceNumber;
294
 
295
    while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
296
}