Rev 1243 | Rev 1370 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1243 | Rev 1369 | ||
|---|---|---|---|
| Line 3... | Line 3... | ||
| 3 | 3 | ||
| 4 | 4 | ||
| 5 | uint16_t currentTimer = 0; |
5 | uint16_t currentTimer = 0; |
| 6 | uint16_t maxTimer = 10; |
6 | uint16_t maxTimer = 10; |
| 7 | uint8_t resolution = 1; |
7 | uint8_t resolution = 1; |
| 8 | uint8_t |
8 | uint8_t act_softPWM_ReportInterval; |
| 9 | uint16_t pwmDefaultValue; |
- | |
| 10 | uint16_t pwmPeriod; |
- | |
| 11 | uint8_t pwmStatus; |
- | |
| 12 | uint8_t currentSendChannelId = 0; |
9 | uint8_t currentSendChannelId = 0; |
| 13 | 10 | ||
| 14 | 11 | ||
| 15 | #ifdef act_softPWM_USEEEPROM |
12 | #ifdef act_softPWM_USEEEPROM |
| 16 | #include "act_softPWM_eeprom.h" |
13 | #include "act_softPWM_eeprom.h" |
| 17 | struct eeprom_act_softPWM EEMEM eeprom_act_softPWM = |
14 | struct eeprom_act_softPWM EEMEM eeprom_act_softPWM = |
| 18 | { |
15 | { |
| 19 | { |
16 | { |
| 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. |
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. |
| 21 |
|
18 | 10000, // x |
| 22 | 0x1234, // y |
- | |
| 23 |
|
19 | 10, |
| 24 | 0x14 //Reportinteval 20 sec. |
20 | 0x14 //Reportinteval 20 sec. |
| 25 | }, |
21 | }, |
| 26 | 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 |
| 27 | }; |
23 | }; |
| 28 | #endif |
24 | #endif |
| 29 | 25 | ||
| 30 | uint16_t pwmValue[NUMBEROFCHANNELS]; |
26 | uint16_t pwmValue[NUMBEROFCHANNELS]; |
| - | 27 | float pwmValueFloat[NUMBEROFCHANNELS]; |
|
| 31 | 28 | ||
| 32 | void act_softPWM_Init(void) |
29 | void act_softPWM_Init(void) |
| 33 | { |
30 | { |
| 34 | #ifdef act_softPWM_USEEEPROM |
31 | #ifdef act_softPWM_USEEEPROM |
| 35 | if (EEDATA_OK) |
32 | if (EEDATA_OK) |
| 36 | { |
33 | { |
| 37 | ; |
34 | ; |
| 38 | } else |
35 | } else |
| 39 | { //The CRC of the EEPROM is not correct, store default values and update CRC |
36 | { //The CRC of the EEPROM is not correct, store default values and update CRC |
| 40 | eeprom_write_word_crc(EEDATA16. |
37 | eeprom_write_word_crc(EEDATA16.maxTimer, 10000 , WITHOUT_CRC); |
| 41 | eeprom_write_word_crc(EEDATA16.defaultPwmValue, 0 , WITHOUT_CRC); |
- | |
| 42 | eeprom_write_byte_crc(EEDATA. |
38 | eeprom_write_byte_crc(EEDATA.resolution, 0x10 , WITHOUT_CRC); |
| 43 | eeprom_write_byte_crc(EEDATA.ReportInterval, 0x14 , WITHOUT_CRC); |
39 | eeprom_write_byte_crc(EEDATA.ReportInterval, 0x14 , WITHOUT_CRC); |
| 44 | EEDATA_UPDATE_CRC; |
40 | EEDATA_UPDATE_CRC; |
| 45 | } |
41 | } |
| 46 | pwmDefaultValue = eeprom_read_word(EEDATA16.defaultPwmValue); |
- | |
| 47 |
|
42 | maxTimer = eeprom_read_word(EEDATA16.maxTimer); |
| 48 |
|
43 | resolution = eeprom_read_byte(EEDATA.resolution); |
| 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); |
44 | Timer_SetTimeout(act_softPWM_SEND_TIMER, act_softPWM_ReportInterval*1000 , TimerTypeFreeRunning, 0); |
| 52 | #endif |
45 | #endif |
| 53 | ///TODO: Initialize hardware etc here |
46 | ///TODO: Initialize hardware etc here |
| - | 47 | uint8_t i; |
|
| - | 48 | for (i=0, i < NUMBEROFCHANNELS; i++) { |
|
| - | 49 | pwmValue[i] = 0; |
|
| - | 50 | pwmValueFloat[i] = 0; |
|
| - | 51 | } |
|
| 54 | #ifdef PIN_0 |
52 | #ifdef PIN_0 |
| 55 | gpio_set_out(PIN_0); |
53 | gpio_set_out(PIN_0); |
| 56 | gpio_clr_pin(PIN_0); |
54 | gpio_clr_pin(PIN_0); |
| 57 | #endif |
55 | #endif |
| 58 | #ifdef PIN_1 |
56 | #ifdef PIN_1 |
| 59 | gpio_set_out(PIN_1); |
57 | gpio_set_out(PIN_1); |
| 60 | gpio_clr_pin(PIN_1); |
58 | gpio_clr_pin(PIN_1); |
| 61 | #endif |
59 | #endif |
| 62 | #ifdef PIN_2 |
60 | #ifdef PIN_2 |
| 63 | gpio_set_out(PIN_2); |
61 | gpio_set_out(PIN_2); |
| 64 | gpio_clr_pin(PIN_2); |
62 | gpio_clr_pin(PIN_2); |
| 65 | #endif |
63 | #endif |
| 66 | #ifdef PIN_3 |
64 | #ifdef PIN_3 |
| Line 68... | Line 66... | ||
| 68 | gpio_clr_pin(PIN_3); |
66 | gpio_clr_pin(PIN_3); |
| 69 | #endif |
67 | #endif |
| 70 | #ifdef PIN_4 |
68 | #ifdef PIN_4 |
| 71 | gpio_set_out(PIN_4); |
69 | gpio_set_out(PIN_4); |
| 72 | gpio_clr_pin(PIN_4); |
70 | gpio_clr_pin(PIN_4); |
| 73 | #endif |
71 | #endif |
| 74 | #ifdef PIN_5 |
72 | #ifdef PIN_5 |
| 75 | gpio_set_out(PIN_5); |
73 | gpio_set_out(PIN_5); |
| 76 | gpio_clr_pin(PIN_5); |
74 | gpio_clr_pin(PIN_5); |
| 77 | #endif |
75 | #endif |
| 78 | #ifdef PIN_6 |
76 | #ifdef PIN_6 |
| 79 | gpio_set_out(PIN_6); |
77 | gpio_set_out(PIN_6); |
| 80 | gpio_clr_pin(PIN_6); |
78 | gpio_clr_pin(PIN_6); |
| 81 | #endif |
79 | #endif |
| 82 | #ifdef PIN_7 |
80 | #ifdef PIN_7 |
| 83 | gpio_set_out(PIN_7); |
81 | gpio_set_out(PIN_7); |
| 84 | gpio_clr_pin(PIN_7); |
82 | gpio_clr_pin(PIN_7); |
| 85 | #endif |
83 | #endif |
| 86 | - | ||
| 87 | Timer_SetTimeout(act_softPWM_TIMER, resolution, TimerTypeFreeRunning, NULL); |
- | |
| 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 | 84 | ||
| - | 85 | Timer_SetTimeout(act_softPWM_TIMER, resolution, TimerTypeFreeRunning, NULL); |
|
| 91 | } |
86 | } |
| 92 | 87 | ||
| 93 | void act_softPWM_Process(void) |
88 | void act_softPWM_Process(void) |
| 94 | { |
89 | { |
| 95 | if (Timer_Expired(act_softPWM_TIMER)) { |
90 | if (Timer_Expired(act_softPWM_TIMER)) { |
| 96 | currentTimer++; |
91 | currentTimer++; |
| 97 | if (currentTimer == |
92 | if (currentTimer == maxTimer) { |
| 98 | currentTimer=0; |
93 | currentTimer=0; |
| 99 | #ifdef PIN_0 |
94 | #ifdef PIN_0 |
| 100 | gpio_set_pin(PIN_0); |
95 | gpio_set_pin(PIN_0); |
| 101 | #endif |
96 | #endif |
| 102 | #ifdef PIN_1 |
97 | #ifdef PIN_1 |
| 103 | gpio_set_pin(PIN_1); |
98 | gpio_set_pin(PIN_1); |
| 104 | #endif |
99 | #endif |
| 105 | #ifdef PIN_2 |
100 | #ifdef PIN_2 |
| 106 | gpio_set_pin(PIN_2); |
101 | gpio_set_pin(PIN_2); |
| 107 | #endif |
102 | #endif |
| 108 | #ifdef PIN_3 |
103 | #ifdef PIN_3 |
| 109 | gpio_set_pin(PIN_3); |
104 | gpio_set_pin(PIN_3); |
| 110 | #endif |
105 | #endif |
| 111 | #ifdef PIN_4 |
106 | #ifdef PIN_4 |
| 112 | gpio_set_pin(PIN_4); |
107 | gpio_set_pin(PIN_4); |
| 113 | #endif |
108 | #endif |
| 114 | #ifdef PIN_5 |
109 | #ifdef PIN_5 |
| 115 | gpio_set_pin(PIN_5); |
110 | gpio_set_pin(PIN_5); |
| 116 | #endif |
111 | #endif |
| 117 | #ifdef PIN_6 |
112 | #ifdef PIN_6 |
| 118 | gpio_set_pin(PIN_6); |
113 | gpio_set_pin(PIN_6); |
| 119 | #endif |
114 | #endif |
| 120 | #ifdef PIN_7 |
115 | #ifdef PIN_7 |
| 121 | gpio_set_pin(PIN_7); |
116 | gpio_set_pin(PIN_7); |
| 122 | #endif |
117 | #endif |
| 123 | } |
118 | } |
| 124 | #ifdef PIN_0 |
119 | #ifdef PIN_0 |
| 125 | if (currentTimer >= pwmValue[0]) { |
120 | if (currentTimer >= pwmValue[0]) { |
| 126 | gpio_clr_pin(PIN_0); |
121 | gpio_clr_pin(PIN_0); |
| 127 | } |
122 | } |
| 128 | #endif |
123 | #endif |
| 129 | #ifdef PIN_1 |
124 | #ifdef PIN_1 |
| 130 | if (currentTimer >= pwmValue[1]) { |
125 | if (currentTimer >= pwmValue[1]) { |
| 131 | gpio_clr_pin(PIN_1); |
126 | gpio_clr_pin(PIN_1); |
| 132 | } |
127 | } |
| Line 189... | Line 184... | ||
| 189 | #endif |
184 | #endif |
| 190 | #ifdef PIN_7 |
185 | #ifdef PIN_7 |
| 191 | || currentSendChannelId == 7 |
186 | || currentSendChannelId == 7 |
| 192 | #endif |
187 | #endif |
| 193 | ) { |
188 | ) { |
| 194 | break; |
189 | break; |
| 195 | } |
190 | } |
| 196 | currentSendChannelId++; |
191 | currentSendChannelId++; |
| 197 | if (currentSendChannelId >= NUMBEROFCHANNELS) |
192 | if (currentSendChannelId >= NUMBEROFCHANNELS) |
| 198 | currentSendChannelId=0; |
193 | currentSendChannelId=0; |
| 199 | } |
194 | } |
| 200 | StdCan_Msg_t txMsg; |
195 | StdCan_Msg_t txMsg; |
| 201 | StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT); |
196 | StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT); |
| 202 | StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER); |
197 | StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER); |
| 203 | txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_SOFTPWM; |
198 | txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_SOFTPWM; |
| Line 205... | Line 200... | ||
| 205 | txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_PWM; |
200 | txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_PWM; |
| 206 | txMsg.Length = 3; |
201 | txMsg.Length = 3; |
| 207 | txMsg.Data[0] = currentSendChannelId; |
202 | txMsg.Data[0] = currentSendChannelId; |
| 208 | txMsg.Data[1] = (pwmValue[currentSendChannelId]>>8)&0xff; |
203 | txMsg.Data[1] = (pwmValue[currentSendChannelId]>>8)&0xff; |
| 209 | txMsg.Data[2] = (pwmValue[currentSendChannelId])&0xff; |
204 | txMsg.Data[2] = (pwmValue[currentSendChannelId])&0xff; |
| 210 | StdCan_Put |
205 | while (StdCan_Put(&txMsg) != StdCan_Ret_OK); |
| 211 | currentSendChannelId++; |
206 | currentSendChannelId++; |
| 212 | if (currentSendChannelId >= NUMBEROFCHANNELS) |
207 | if (currentSendChannelId >= NUMBEROFCHANNELS) |
| 213 | currentSendChannelId=0; |
208 | currentSendChannelId=0; |
| 214 | } |
209 | } |
| 215 | } |
210 | } |
| Line 224... | Line 219... | ||
| 224 | rxMsg->Header.ModuleId == act_softPWM_ID) |
219 | rxMsg->Header.ModuleId == act_softPWM_ID) |
| 225 | { |
220 | { |
| 226 | switch (rxMsg->Header.Command) |
221 | switch (rxMsg->Header.Command) |
| 227 | { |
222 | { |
| 228 | case CAN_MODULE_CMD_PHYSICAL_PWM: |
223 | case CAN_MODULE_CMD_PHYSICAL_PWM: |
| 229 | if (rxMsg->Length == |
224 | if (rxMsg->Length == 3) |
| 230 | { |
225 | { |
| - | 226 | if (rxMsg->Data[0] < NUMBEROFCHANNELS) { |
|
| - | 227 | pwmValueFloat[rxMsg->Data[0]] = ((rxMsg->Data[1]<<8) + rxMsg->Data[2])/64; |
|
| 231 | pwmValue[rxMsg-> |
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); |
|
| 232 | StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER); |
231 | StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER); |
| 233 | rxMsg->Length = |
232 | rxMsg->Length = 3; |
| 234 | StdCan_Put |
233 | while (StdCan_Put(rxMsg) != StdCan_Ret_OK); |
| - | 234 | } |
|
| 235 | } else if (rxMsg->Length == 1) { |
235 | } else if (rxMsg->Length == 1) { |
| 236 | rxMsg->Data[1] = ( |
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); |
|
| 237 | StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER); |
238 | StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER); |
| 238 | rxMsg->Length = |
239 | rxMsg->Length = 3; |
| 239 | StdCan_Put |
240 | while (StdCan_Put(rxMsg) != StdCan_Ret_OK); |
| 240 | } |
241 | } |
| 241 | break; |
242 | break; |
| 242 | case CAN_MODULE_CMD_SOFTPWM_CONFIG: |
243 | case CAN_MODULE_CMD_SOFTPWM_CONFIG: |
| 243 | maxTimer = (((uint16_t)rxMsg->Data[0])<<8) + rxMsg->Data[1]; |
244 | maxTimer = (((uint16_t)rxMsg->Data[0])<<8) + rxMsg->Data[1]; |
| 244 | resolution = rxMsg->Data[2]; |
245 | resolution = rxMsg->Data[2]; |
| 245 | Timer_SetTimeout(act_softPWM_TIMER, resolution, TimerTypeFreeRunning, NULL); |
246 | Timer_SetTimeout(act_softPWM_TIMER, resolution, TimerTypeFreeRunning, NULL); |
| - | 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); |
|
| 246 | break; |
252 | break; |
| 247 | case CAN_MODULE_CMD_GLOBAL_REPORT_INTERVAL: |
253 | case CAN_MODULE_CMD_GLOBAL_REPORT_INTERVAL: |
| 248 | if (rxMsg->Length > 0) |
254 | if (rxMsg->Length > 0) |
| 249 | { |
255 | { |
| 250 | act_softPWM_ReportInterval = rxMsg->Data[0]; |
256 | act_softPWM_ReportInterval = rxMsg->Data[0]; |
| 251 | #ifdef act_softPWM_USEEEPROM |
- | |
| 252 | eeprom_write_byte_crc(EEDATA.ReportInterval, act_softPWM_ReportInterval , WITH_CRC); |
257 | eeprom_write_byte_crc(EEDATA.ReportInterval, act_softPWM_ReportInterval , WITH_CRC); |
| 253 | #endif |
- | |
| 254 | Timer_SetTimeout(act_softPWM_SEND_TIMER, act_softPWM_ReportInterval*1000 , TimerTypeFreeRunning, 0); |
258 | Timer_SetTimeout(act_softPWM_SEND_TIMER, act_softPWM_ReportInterval*1000 , TimerTypeFreeRunning, 0); |
| 255 | } |
259 | } |
| 256 | 260 | ||
| 257 | StdCan_Msg_t txMsg; |
261 | StdCan_Msg_t txMsg; |
| 258 | 262 | ||
| Line 261... | Line 265... | ||
| 261 | txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_SOFTPWM; |
265 | txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_SOFTPWM; |
| 262 | txMsg.Header.ModuleId = act_softPWM_ID; |
266 | txMsg.Header.ModuleId = act_softPWM_ID; |
| 263 | txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_REPORT_INTERVAL; |
267 | txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_REPORT_INTERVAL; |
| 264 | txMsg.Length = 1; |
268 | txMsg.Length = 1; |
| 265 | txMsg.Data[0] = act_softPWM_ReportInterval; |
269 | txMsg.Data[0] = act_softPWM_ReportInterval; |
| 266 | StdCan_Put |
270 | while (StdCan_Put(&txMsg) != StdCan_Ret_OK); |
| 267 | break; |
271 | break; |
| 268 | } |
272 | } |
| 269 | } |
273 | } |
| 270 | } |
274 | } |
| 271 | 275 | ||