Subversion Repositories HomeAutomation

Rev

Rev 1183 | Rev 1240 | 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;
1180 linlun 8
uint8_t pwmDefaultState,pwmDefaultStartState,act_softPWM_ReportInterval;
9
uint16_t pwmDefaultValue;
10
uint16_t pwmPeriod;
11
uint8_t pwmStatus;
12
uint8_t currentSendChannelId = 0;
1156 linlun 13
 
1183 linlun 14
 
1156 linlun 15
#ifdef act_softPWM_USEEEPROM
16
#include "act_softPWM_eeprom.h"
17
struct eeprom_act_softPWM EEMEM eeprom_act_softPWM =
18
{
19
    {
20
        ///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. 
1180 linlun 21
        0xABcd, // x
22
        0x1234, // y
23
        0x00,
24
        0x14    //Reportinteval 20 sec.
1156 linlun 25
    },
26
 
27
};
28
#endif
1183 linlun 29
 
1180 linlun 30
uint16_t pwmValue[NUMBEROFCHANNELS];
1156 linlun 31
 
32
void act_softPWM_Init(void)
33
{
34
#ifdef act_softPWM_USEEEPROM
35
    if (EEDATA_OK)
36
    {
1180 linlun 37
      ;
1156 linlun 38
    } else
39
    {   //The CRC of the EEPROM is not correct, store default values and update CRC
1183 linlun 40
        eeprom_write_word_crc(EEDATA16.PwmPeriod, 10 , WITHOUT_CRC);
41
        eeprom_write_word_crc(EEDATA16.defaultPwmValue, 0 , WITHOUT_CRC);
1180 linlun 42
        eeprom_write_byte_crc(EEDATA.defaultStates, 0xa0 , WITHOUT_CRC);
43
        eeprom_write_byte_crc(EEDATA.ReportInterval, 0x14 , WITHOUT_CRC);
44
        EEDATA_UPDATE_CRC;
1156 linlun 45
    }
1180 linlun 46
    pwmDefaultValue = eeprom_read_word(EEDATA16.defaultPwmValue);
47
    pwmPeriod = eeprom_read_word(EEDATA16.PwmPeriod);
48
    pwmDefaultState = (eeprom_read_byte(EEDATA.defaultStates)>>6) & 0x03;
49
    pwmDefaultStartState = ((eeprom_read_byte(EEDATA.defaultStates)>>5) & 0x01);
50
    act_softPWM_ReportInterval = (eeprom_read_byte(EEDATA.ReportInterval));
51
    Timer_SetTimeout(act_softPWM_SEND_TIMER, act_softPWM_ReportInterval*1000 , TimerTypeFreeRunning, 0);
1156 linlun 52
#endif  
53
    ///TODO: Initialize hardware etc here
1177 linlun 54
#ifdef PIN_0
55
    gpio_set_out(PIN_0);
56
    gpio_clr_pin(PIN_0);
57
#endif
58
#ifdef PIN_1
59
    gpio_set_out(PIN_1);
60
    gpio_clr_pin(PIN_1);
61
#endif
62
#ifdef PIN_2
63
    gpio_set_out(PIN_2);
64
    gpio_clr_pin(PIN_2);
65
#endif
66
#ifdef PIN_3
67
    gpio_set_out(PIN_3);
68
    gpio_clr_pin(PIN_3);
69
#endif
70
#ifdef PIN_4
71
    gpio_set_out(PIN_4);
72
    gpio_clr_pin(PIN_4);
73
#endif
74
#ifdef PIN_5
75
    gpio_set_out(PIN_5);
76
    gpio_clr_pin(PIN_5);
77
#endif
78
#ifdef PIN_6
79
    gpio_set_out(PIN_6);
80
    gpio_clr_pin(PIN_6);
81
#endif
82
#ifdef PIN_7
83
    gpio_set_out(PIN_7);
84
    gpio_clr_pin(PIN_7);
85
#endif
1156 linlun 86
 
1177 linlun 87
    Timer_SetTimeout(act_softPWM_TIMER, resolution, TimerTypeFreeRunning, NULL);
1156 linlun 88
    // to use PCINt lib, call this function: (the callback function look as a timer callback function)
89
    // Pcint_SetCallbackPin(act_softPWM_PCINT, EXP_C , &act_softPWM_pcint_callback);
90
 
91
}
92
 
93
void act_softPWM_Process(void)
94
{
95
    if (Timer_Expired(act_softPWM_TIMER)) {
96
        currentTimer++;
97
        if (currentTimer == 10) {
98
            currentTimer=0;
1177 linlun 99
            #ifdef PIN_0 
100
                gpio_set_pin(PIN_0);
101
            #endif
102
            #ifdef PIN_1 
103
                gpio_set_pin(PIN_1);
104
            #endif
105
            #ifdef PIN_2 
106
                gpio_set_pin(PIN_2);
107
            #endif
108
            #ifdef PIN_3 
109
                gpio_set_pin(PIN_3);
110
            #endif
111
            #ifdef PIN_4 
112
                gpio_set_pin(PIN_4);
113
            #endif
114
            #ifdef PIN_5 
115
                gpio_set_pin(PIN_5);
116
            #endif
117
            #ifdef PIN_6 
118
                gpio_set_pin(PIN_6);
119
            #endif
120
            #ifdef PIN_7 
121
                gpio_set_pin(PIN_7);
122
            #endif
1156 linlun 123
        }
1177 linlun 124
        #ifdef PIN_0
1184 linlun 125
        if (currentTimer >= pwmValue[0]) {
1177 linlun 126
            gpio_clr_pin(PIN_0);
1156 linlun 127
        }
1177 linlun 128
        #endif
129
        #ifdef PIN_1
1184 linlun 130
        if (currentTimer >= pwmValue[1]) {
1177 linlun 131
            gpio_clr_pin(PIN_1);
1156 linlun 132
        }
1177 linlun 133
        #endif
134
        #ifdef PIN_2
1184 linlun 135
        if (currentTimer >= pwmValue[2]) {
1177 linlun 136
            gpio_clr_pin(PIN_2);
1156 linlun 137
        }
1177 linlun 138
        #endif
139
        #ifdef PIN_3
1184 linlun 140
        if (currentTimer >= pwmValue[3]) {
1177 linlun 141
            gpio_clr_pin(PIN_3);
142
        }
143
        #endif
144
        #ifdef PIN_4
1184 linlun 145
        if (currentTimer >= pwmValue[4]) {
1177 linlun 146
            gpio_clr_pin(PIN_4);
147
        }
148
        #endif
149
        #ifdef PIN_5
1184 linlun 150
        if (currentTimer >= pwmValue[5]) {
1177 linlun 151
            gpio_clr_pin(PIN_5);
152
        }
153
        #endif
154
        #ifdef PIN_6
1184 linlun 155
        if (currentTimer >= pwmValue[6]) {
1177 linlun 156
            gpio_clr_pin(PIN_6);
157
        }
158
        #endif
159
        #ifdef PIN_7
1184 linlun 160
        if (currentTimer >= pwmValue[7]) {
1177 linlun 161
            gpio_clr_pin(PIN_7);
162
        }
163
        #endif
1156 linlun 164
    }
1180 linlun 165
    if (Timer_Expired(act_softPWM_SEND_TIMER)) {
1184 linlun 166
        while(1)
167
        {
168
            if (0
169
                #ifdef PIN_0
170
                || currentSendChannelId == 0
171
                #endif
172
                #ifdef PIN_1
173
                || currentSendChannelId == 1
174
                #endif
175
                #ifdef PIN_2
176
                || currentSendChannelId == 2
177
                #endif
178
                #ifdef PIN_3
179
                || currentSendChannelId == 3
180
                #endif
181
                #ifdef PIN_4
182
                || currentSendChannelId == 4
183
                #endif
184
                #ifdef PIN_5
185
                || currentSendChannelId == 5
186
                #endif
187
                #ifdef PIN_6
188
                || currentSendChannelId == 6
189
                #endif
190
                #ifdef PIN_7
191
                || currentSendChannelId == 7
192
                #endif
193
            ) {
194
                break;
195
            }
196
            currentSendChannelId++;
197
            if (currentSendChannelId >= NUMBEROFCHANNELS)
198
                currentSendChannelId=0;
199
        }
1180 linlun 200
        StdCan_Msg_t txMsg;
201
        StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT);
202
        StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
203
        txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_SOFTPWM;
204
        txMsg.Header.ModuleId = act_softPWM_ID;
205
        txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_PWM;
206
        txMsg.Length = 3;
207
        txMsg.Data[0] = currentSendChannelId;
208
        txMsg.Data[1] = (pwmValue[currentSendChannelId]>>8)&0xff;
209
        txMsg.Data[2] = (pwmValue[currentSendChannelId])&0xff;
210
        StdCan_Put(&txMsg);
211
        currentSendChannelId++;
212
        if (currentSendChannelId >= NUMBEROFCHANNELS)
213
            currentSendChannelId=0;
214
    }
1156 linlun 215
}
216
 
1180 linlun 217
 
218
 
1156 linlun 219
void act_softPWM_HandleMessage(StdCan_Msg_t *rxMsg)
220
{
221
    if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_ACT &&
222
        StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
223
        rxMsg->Header.ModuleType == CAN_MODULE_TYPE_ACT_SOFTPWM &&
224
        rxMsg->Header.ModuleId == act_softPWM_ID)
225
    {
226
        switch (rxMsg->Header.Command)
227
        {
1177 linlun 228
        case CAN_MODULE_CMD_PHYSICAL_PWM:
1184 linlun 229
            if (rxMsg->Length > 3)
230
            {
231
                if ((((uint16_t)rxMsg->Data[1])<8) + rxMsg->Data[2] > maxTimer)
232
                {
233
                    rxMsg->Data[2] = maxTimer & 0x00ff;
234
                    rxMsg->Data[1] = (maxTimer>>8) & 0x00ff;
235
                }
236
                pwmValue[rxMsg->Data[0]] = (((uint16_t)rxMsg->Data[1])<<8) + rxMsg->Data[2];
237
                StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER);
238
                rxMsg->Length = 3;
239
                StdCan_Put(rxMsg);
240
            } else if (rxMsg->Length > 1) {
241
                rxMsg->Data[2] = pwmValue[rxMsg->Data[0]] & 0x00ff;
242
                rxMsg->Data[1] = (pwmValue[rxMsg->Data[0]]>>8) & 0x00ff;
243
                StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER);
244
                rxMsg->Length = 3;
245
                StdCan_Put(rxMsg);
1177 linlun 246
            }
247
            break;
248
        case CAN_MODULE_CMD_SOFTPWM_CONFIG:
249
            maxTimer = (((uint16_t)rxMsg->Data[0])<<8) + rxMsg->Data[1];
250
            resolution = rxMsg->Data[2];
251
            Timer_SetTimeout(act_softPWM_TIMER, resolution, TimerTypeFreeRunning, NULL);
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];
257
                eeprom_write_byte_crc(EEDATA.ReportInterval, act_softPWM_ReportInterval , WITH_CRC);
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;
270
            StdCan_Put(&txMsg);
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
}