Rev 1177 | Rev 1183 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1177 | Rev 1180 | ||
|---|---|---|---|
| Line 1... | Line 1... | ||
| 1 | 1 | ||
| 2 | #include "act_softPWM.h" |
2 | #include "act_softPWM.h" |
| 3 | uint16_t currentTimer = 0; |
3 | uint16_t currentTimer = 0; |
| 4 | uint16_t maxTimer = 10; |
4 | uint16_t maxTimer = 10; |
| 5 | uint8_t resolution = 1; |
5 | uint8_t resolution = 1; |
| - | 6 | uint8_t pwmDefaultState,pwmDefaultStartState,act_softPWM_ReportInterval; |
|
| - | 7 | uint16_t pwmDefaultValue; |
|
| - | 8 | uint16_t pwmPeriod; |
|
| - | 9 | uint8_t pwmStatus; |
|
| - | 10 | uint8_t currentSendChannelId = 0; |
|
| 6 | 11 | ||
| 7 | #ifdef act_softPWM_USEEEPROM |
12 | #ifdef act_softPWM_USEEEPROM |
| 8 | #include "act_softPWM_eeprom.h" |
13 | #include "act_softPWM_eeprom.h" |
| 9 | struct eeprom_act_softPWM EEMEM eeprom_act_softPWM = |
14 | struct eeprom_act_softPWM EEMEM eeprom_act_softPWM = |
| 10 | { |
15 | { |
| 11 | { |
16 | { |
| 12 | ///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. |
| 13 |
|
18 | 0xABcd, // x |
| 14 | 0x1234 |
19 | 0x1234, // y |
| - | 20 | 0x00, |
|
| - | 21 | 0x14 //Reportinteval 20 sec. |
|
| 15 | }, |
22 | }, |
| 16 | 0 // crc, must be a correct value, but this will also be handled by the EEPROM module or make scripts |
23 | 0 // crc, must be a correct value, but this will also be handled by the EEPROM module or make scripts |
| 17 | }; |
24 | }; |
| 18 | #endif |
25 | #endif |
| - | 26 | #define NUMBEROFCHANNELS 0 |
|
| 19 | #ifdef PIN_0 |
27 | #ifdef PIN_0 |
| 20 |
|
28 | #define PIN_0_ID NUMBEROFCHANNELS |
| - | 29 | #undefine NUMBEROFCHANNELS |
|
| - | 30 | #define NUMBEROFCHANNELS PIN_0_ID + 1 |
|
| 21 | #endif |
31 | #endif |
| 22 | #ifdef PIN_1 |
32 | #ifdef PIN_1 |
| 23 |
|
33 | #define PIN_1_ID NUMBEROFCHANNELS |
| - | 34 | #undefine NUMBEROFCHANNELS |
|
| - | 35 | #define NUMBEROFCHANNELS PIN_1_ID + 1 |
|
| 24 | #endif |
36 | #endif |
| 25 | #ifdef PIN_2 |
37 | #ifdef PIN_2 |
| 26 |
|
38 | #define PIN_2_ID NUMBEROFCHANNELS |
| - | 39 | #undefine NUMBEROFCHANNELS |
|
| - | 40 | #define NUMBEROFCHANNELS PIN_2_ID + 1 |
|
| 27 | #endif |
41 | #endif |
| 28 | #ifdef PIN_3 |
42 | #ifdef PIN_3 |
| 29 |
|
43 | #define PIN_3_ID NUMBEROFCHANNELS |
| - | 44 | #undefine NUMBEROFCHANNELS |
|
| - | 45 | #define NUMBEROFCHANNELS PIN_3_ID + 1 |
|
| 30 | #endif |
46 | #endif |
| 31 | #ifdef PIN_4 |
47 | #ifdef PIN_4 |
| 32 |
|
48 | #define PIN_4_ID NUMBEROFCHANNELS |
| - | 49 | #undefine NUMBEROFCHANNELS |
|
| - | 50 | #define NUMBEROFCHANNELS PIN_4_ID + 1 |
|
| 33 | #endif |
51 | #endif |
| 34 | #ifdef PIN_5 |
52 | #ifdef PIN_5 |
| 35 |
|
53 | #define PIN_5_ID NUMBEROFCHANNELS |
| - | 54 | #undefine NUMBEROFCHANNELS |
|
| - | 55 | #define NUMBEROFCHANNELS PIN_5_ID + 1 |
|
| 36 | #endif |
56 | #endif |
| 37 | #ifdef PIN_6 |
57 | #ifdef PIN_6 |
| 38 |
|
58 | #define PIN_6_ID NUMBEROFCHANNELS |
| - | 59 | #undefine NUMBEROFCHANNELS |
|
| - | 60 | #define NUMBEROFCHANNELS PIN_6_ID + 1 |
|
| 39 | #endif |
61 | #endif |
| 40 | #ifdef PIN_7 |
62 | #ifdef PIN_7 |
| 41 |
|
63 | #define PIN_7_ID NUMBEROFCHANNELS |
| - | 64 | #undefine NUMBEROFCHANNELS |
|
| - | 65 | #define NUMBEROFCHANNELS PIN_7_ID + 1 |
|
| 42 | #endif |
66 | #endif |
| - | 67 | uint16_t pwmValue[NUMBEROFCHANNELS]; |
|
| 43 | 68 | ||
| 44 | void act_softPWM_Init(void) |
69 | void act_softPWM_Init(void) |
| 45 | { |
70 | { |
| 46 | #ifdef act_softPWM_USEEEPROM |
71 | #ifdef act_softPWM_USEEEPROM |
| 47 | if (EEDATA_OK) |
72 | if (EEDATA_OK) |
| 48 | { |
73 | { |
| 49 | ///TODO: Use stored data to set initial values for the module |
- | |
| 50 | blablaX = eeprom_read_byte(EEDATA.x); |
- | |
| 51 |
|
74 | ; |
| 52 | } else |
75 | } else |
| 53 | { //The CRC of the EEPROM is not correct, store default values and update CRC |
76 | { //The CRC of the EEPROM is not correct, store default values and update CRC |
| - | 77 | eeprom_write_word_crc(EEDATA16.PwmPeriod, DEFAULT_PWM_PERIOD , WITHOUT_CRC); |
|
| - | 78 | eeprom_write_word_crc(EEDATA16.defaultPwmValue, DEFAULT_PWM_VALUE , WITHOUT_CRC); |
|
| 54 | eeprom_write_byte_crc( |
79 | eeprom_write_byte_crc(EEDATA.defaultStates, 0xa0 , WITHOUT_CRC); |
| 55 |
|
80 | eeprom_write_byte_crc(EEDATA.ReportInterval, 0x14 , WITHOUT_CRC); |
| 56 | EEDATA_UPDATE_CRC; |
81 | EEDATA_UPDATE_CRC; |
| 57 | } |
82 | } |
| - | 83 | pwmDefaultValue = eeprom_read_word(EEDATA16.defaultPwmValue); |
|
| - | 84 | pwmPeriod = eeprom_read_word(EEDATA16.PwmPeriod); |
|
| - | 85 | pwmDefaultState = (eeprom_read_byte(EEDATA.defaultStates)>>6) & 0x03; |
|
| - | 86 | pwmDefaultStartState = ((eeprom_read_byte(EEDATA.defaultStates)>>5) & 0x01); |
|
| - | 87 | act_softPWM_ReportInterval = (eeprom_read_byte(EEDATA.ReportInterval)); |
|
| - | 88 | Timer_SetTimeout(act_softPWM_SEND_TIMER, act_softPWM_ReportInterval*1000 , TimerTypeFreeRunning, 0); |
|
| 58 | #endif |
89 | #endif |
| 59 | ///TODO: Initialize hardware etc here |
90 | ///TODO: Initialize hardware etc here |
| 60 | #ifdef PIN_0 |
91 | #ifdef PIN_0 |
| 61 | gpio_set_out(PIN_0); |
92 | gpio_set_out(PIN_0); |
| 62 | gpio_clr_pin(PIN_0); |
93 | gpio_clr_pin(PIN_0); |
| Line 126... | Line 157... | ||
| 126 | #ifdef PIN_7 |
157 | #ifdef PIN_7 |
| 127 | gpio_set_pin(PIN_7); |
158 | gpio_set_pin(PIN_7); |
| 128 | #endif |
159 | #endif |
| 129 | } |
160 | } |
| 130 | #ifdef PIN_0 |
161 | #ifdef PIN_0 |
| 131 | if (currentTimer >= |
162 | if (currentTimer >= pwmValue[PIN_0_ID]) { |
| 132 | gpio_clr_pin(PIN_0); |
163 | gpio_clr_pin(PIN_0); |
| 133 | } |
164 | } |
| 134 | #endif |
165 | #endif |
| 135 | #ifdef PIN_1 |
166 | #ifdef PIN_1 |
| 136 | if (currentTimer >= |
167 | if (currentTimer >= pwmValue[PIN_1_ID]) { |
| 137 | gpio_clr_pin(PIN_1); |
168 | gpio_clr_pin(PIN_1); |
| 138 | } |
169 | } |
| 139 | #endif |
170 | #endif |
| 140 | #ifdef PIN_2 |
171 | #ifdef PIN_2 |
| 141 | if (currentTimer >= |
172 | if (currentTimer >= pwmValue[PIN_2_ID]) { |
| 142 | gpio_clr_pin(PIN_2); |
173 | gpio_clr_pin(PIN_2); |
| 143 | } |
174 | } |
| 144 | #endif |
175 | #endif |
| 145 | #ifdef PIN_3 |
176 | #ifdef PIN_3 |
| 146 | if (currentTimer >= |
177 | if (currentTimer >= pwmValue[PIN_3_ID]) { |
| 147 | gpio_clr_pin(PIN_3); |
178 | gpio_clr_pin(PIN_3); |
| 148 | } |
179 | } |
| 149 | #endif |
180 | #endif |
| 150 | #ifdef PIN_4 |
181 | #ifdef PIN_4 |
| 151 | if (currentTimer >= |
182 | if (currentTimer >= pwmValue[PIN_4_ID]) { |
| 152 | gpio_clr_pin(PIN_4); |
183 | gpio_clr_pin(PIN_4); |
| 153 | } |
184 | } |
| 154 | #endif |
185 | #endif |
| 155 | #ifdef PIN_5 |
186 | #ifdef PIN_5 |
| 156 | if (currentTimer >= |
187 | if (currentTimer >= pwmValue[PIN_5_ID]) { |
| 157 | gpio_clr_pin(PIN_5); |
188 | gpio_clr_pin(PIN_5); |
| 158 | } |
189 | } |
| 159 | #endif |
190 | #endif |
| 160 | #ifdef PIN_6 |
191 | #ifdef PIN_6 |
| 161 | if (currentTimer >= |
192 | if (currentTimer >= pwmValue[PIN_6_ID]) { |
| 162 | gpio_clr_pin(PIN_6); |
193 | gpio_clr_pin(PIN_6); |
| 163 | } |
194 | } |
| 164 | #endif |
195 | #endif |
| 165 | #ifdef PIN_7 |
196 | #ifdef PIN_7 |
| 166 | if (currentTimer >= |
197 | if (currentTimer >= pwmValue[PIN_7_ID]) { |
| 167 | gpio_clr_pin(PIN_7); |
198 | gpio_clr_pin(PIN_7); |
| 168 | } |
199 | } |
| 169 | #endif |
200 | #endif |
| 170 | } |
201 | } |
| - | 202 | if (Timer_Expired(act_softPWM_SEND_TIMER)) { |
|
| - | 203 | StdCan_Msg_t txMsg; |
|
| - | 204 | StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT); |
|
| - | 205 | StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER); |
|
| - | 206 | txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_SOFTPWM; |
|
| - | 207 | txMsg.Header.ModuleId = act_softPWM_ID; |
|
| - | 208 | txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_PWM; |
|
| - | 209 | txMsg.Length = 3; |
|
| - | 210 | txMsg.Data[0] = currentSendChannelId; |
|
| - | 211 | txMsg.Data[1] = (pwmValue[currentSendChannelId]>>8)&0xff; |
|
| - | 212 | txMsg.Data[2] = (pwmValue[currentSendChannelId])&0xff; |
|
| - | 213 | StdCan_Put(&txMsg); |
|
| - | 214 | currentSendChannelId++; |
|
| - | 215 | if (currentSendChannelId >= NUMBEROFCHANNELS) |
|
| - | 216 | currentSendChannelId=0; |
|
| 171 | } |
217 | } |
| - | 218 | } |
|
| - | 219 | ||
| - | 220 | ||
| 172 | 221 | ||
| 173 | void act_softPWM_HandleMessage(StdCan_Msg_t *rxMsg) |
222 | void act_softPWM_HandleMessage(StdCan_Msg_t *rxMsg) |
| 174 | { |
223 | { |
| 175 | if ( StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_ACT && |
224 | if ( StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_ACT && |
| 176 | StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER && |
225 | StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER && |
| 177 | rxMsg->Header.ModuleType == CAN_MODULE_TYPE_ACT_SOFTPWM && |
226 | rxMsg->Header.ModuleType == CAN_MODULE_TYPE_ACT_SOFTPWM && |
| 178 | rxMsg->Header.ModuleId == act_softPWM_ID) |
227 | rxMsg->Header.ModuleId == act_softPWM_ID) |
| 179 | { |
228 | { |
| 180 | switch (rxMsg->Header.Command) |
229 | switch (rxMsg->Header.Command) |
| 181 | { |
230 | { |
| 182 | case CAN_MODULE_CMD_PHYSICAL_PWM: |
231 | case CAN_MODULE_CMD_PHYSICAL_PWM: |
| 183 | if ((((uint16_t)rxMsg->Data[1])<8) + rxMsg->Data[2] > maxTimer) |
232 | if ((((uint16_t)rxMsg->Data[1])<8) + rxMsg->Data[2] > maxTimer) |
| 184 | { |
233 | { |
| Line 187... | Line 236... | ||
| 187 | } |
236 | } |
| 188 | switch (rxMsg->Data[0]) |
237 | switch (rxMsg->Data[0]) |
| 189 | { |
238 | { |
| 190 | #ifdef PIN_0 |
239 | #ifdef PIN_0 |
| 191 | case 0: |
240 | case 0: |
| 192 |
|
241 | pwmValue[PIN_0_ID] = (((uint16_t)rxMsg->Data[1])<<8) + rxMsg->Data[2]; |
| 193 | break; |
242 | break; |
| 194 | #endif |
243 | #endif |
| 195 | #ifdef PIN_1 |
244 | #ifdef PIN_1 |
| 196 | case 1: |
245 | case 1: |
| 197 |
|
246 | pwmValue[PIN_1_ID] = (((uint16_t)rxMsg->Data[1])<<8) + rxMsg->Data[2]; |
| 198 | break; |
247 | break; |
| 199 | #endif |
248 | #endif |
| 200 | #ifdef PIN_2 |
249 | #ifdef PIN_2 |
| 201 | case 2: |
250 | case 2: |
| 202 |
|
251 | pwmValue[PIN_2_ID] = (((uint16_t)rxMsg->Data[1])<<8) + rxMsg->Data[2]; |
| 203 | break; |
252 | break; |
| 204 | #endif |
253 | #endif |
| 205 | #ifdef PIN_3 |
254 | #ifdef PIN_3 |
| 206 | case 3: |
255 | case 3: |
| 207 |
|
256 | pwmValue[PIN_3_ID] = (((uint16_t)rxMsg->Data[1])<<8) + rxMsg->Data[2]; |
| 208 | break; |
257 | break; |
| 209 | #endif |
258 | #endif |
| 210 | #ifdef PIN_4 |
259 | #ifdef PIN_4 |
| 211 | case 4: |
260 | case 4: |
| 212 |
|
261 | pwmValue[PIN_4_ID] = (((uint16_t)rxMsg->Data[1])<<8) + rxMsg->Data[2]; |
| 213 | break; |
262 | break; |
| 214 | #endif |
263 | #endif |
| 215 | #ifdef PIN_5 |
264 | #ifdef PIN_5 |
| 216 | case 5: |
265 | case 5: |
| 217 |
|
266 | pwmValue[PIN_5_ID] = (((uint16_t)rxMsg->Data[1])<<8) + rxMsg->Data[2]; |
| 218 | break; |
267 | break; |
| 219 | #endif |
268 | #endif |
| 220 | #ifdef PIN_6 |
269 | #ifdef PIN_6 |
| 221 | case 6: |
270 | case 6: |
| 222 |
|
271 | pwmValue[PIN_6_ID] = (((uint16_t)rxMsg->Data[1])<<8) + rxMsg->Data[2]; |
| 223 | break; |
272 | break; |
| 224 | #endif |
273 | #endif |
| 225 | #ifdef PIN_7 |
274 | #ifdef PIN_7 |
| 226 | case 7: |
275 | case 7: |
| 227 |
|
276 | pwmValue[PIN_7_ID] = (((uint16_t)rxMsg->Data[1])<<8) + rxMsg->Data[2]; |
| 228 | break; |
277 | break; |
| 229 | #endif |
278 | #endif |
| 230 | } |
279 | } |
| 231 | break; |
280 | break; |
| 232 | case CAN_MODULE_CMD_SOFTPWM_CONFIG: |
281 | case CAN_MODULE_CMD_SOFTPWM_CONFIG: |
| 233 | maxTimer = (((uint16_t)rxMsg->Data[0])<<8) + rxMsg->Data[1]; |
282 | maxTimer = (((uint16_t)rxMsg->Data[0])<<8) + rxMsg->Data[1]; |
| 234 | resolution = rxMsg->Data[2]; |
283 | resolution = rxMsg->Data[2]; |
| 235 | Timer_SetTimeout(act_softPWM_TIMER, resolution, TimerTypeFreeRunning, NULL); |
284 | Timer_SetTimeout(act_softPWM_TIMER, resolution, TimerTypeFreeRunning, NULL); |
| 236 | break; |
285 | break; |
| - | 286 | case CAN_MODULE_CMD_GLOBAL_REPORT_INTERVAL: |
|
| - | 287 | if (rxMsg->Length > 0) |
|
| - | 288 | { |
|
| - | 289 | act_softPWM_ReportInterval = rxMsg->Data[0]; |
|
| - | 290 | eeprom_write_byte_crc(EEDATA.ReportInterval, act_softPWM_ReportInterval , WITH_CRC); |
|
| - | 291 | Timer_SetTimeout(act_softPWM_SEND_TIMER, act_softPWM_ReportInterval*1000 , TimerTypeFreeRunning, 0); |
|
| - | 292 | } |
|
| - | 293 | ||
| - | 294 | StdCan_Msg_t txMsg; |
|
| 237 | 295 | ||
| - | 296 | StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT); |
|
| - | 297 | StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER); |
|
| - | 298 | txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_SOFTPWM; |
|
| - | 299 | txMsg.Header.ModuleId = act_softPWM_ID; |
|
| - | 300 | txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_REPORT_INTERVAL; |
|
| - | 301 | txMsg.Length = 1; |
|
| - | 302 | txMsg.Data[0] = act_softPWM_ReportInterval; |
|
| - | 303 | StdCan_Put(&txMsg); |
|
| - | 304 | break; |
|
| 238 | } |
305 | } |
| 239 | } |
306 | } |
| 240 | } |
307 | } |
| 241 | 308 | ||
| 242 | void act_softPWM_List(uint8_t ModuleSequenceNumber) |
309 | void act_softPWM_List(uint8_t ModuleSequenceNumber) |