Rev 1177 | Rev 1183 | 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" |
||
| 1177 | linlun | 3 | uint16_t currentTimer = 0; |
| 4 | uint16_t maxTimer = 10; |
||
| 5 | uint8_t resolution = 1; |
||
| 1180 | linlun | 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; |
||
| 1156 | linlun | 11 | |
| 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. |
||
| 1180 | linlun | 18 | 0xABcd, // x |
| 19 | 0x1234, // y |
||
| 20 | 0x00, |
||
| 21 | 0x14 //Reportinteval 20 sec. |
||
| 1156 | linlun | 22 | }, |
| 23 | |||
| 24 | }; |
||
| 25 | #endif |
||
| 1180 | linlun | 26 | #define NUMBEROFCHANNELS 0 |
| 1177 | linlun | 27 | #ifdef PIN_0 |
| 1180 | linlun | 28 | #define PIN_0_ID NUMBEROFCHANNELS |
| 29 | #undefine NUMBEROFCHANNELS |
||
| 30 | #define NUMBEROFCHANNELS PIN_0_ID + 1 |
||
| 1177 | linlun | 31 | #endif |
| 32 | #ifdef PIN_1 |
||
| 1180 | linlun | 33 | #define PIN_1_ID NUMBEROFCHANNELS |
| 34 | #undefine NUMBEROFCHANNELS |
||
| 35 | #define NUMBEROFCHANNELS PIN_1_ID + 1 |
||
| 1177 | linlun | 36 | #endif |
| 37 | #ifdef PIN_2 |
||
| 1180 | linlun | 38 | #define PIN_2_ID NUMBEROFCHANNELS |
| 39 | #undefine NUMBEROFCHANNELS |
||
| 40 | #define NUMBEROFCHANNELS PIN_2_ID + 1 |
||
| 1177 | linlun | 41 | #endif |
| 42 | #ifdef PIN_3 |
||
| 1180 | linlun | 43 | #define PIN_3_ID NUMBEROFCHANNELS |
| 44 | #undefine NUMBEROFCHANNELS |
||
| 45 | #define NUMBEROFCHANNELS PIN_3_ID + 1 |
||
| 1177 | linlun | 46 | #endif |
| 47 | #ifdef PIN_4 |
||
| 1180 | linlun | 48 | #define PIN_4_ID NUMBEROFCHANNELS |
| 49 | #undefine NUMBEROFCHANNELS |
||
| 50 | #define NUMBEROFCHANNELS PIN_4_ID + 1 |
||
| 1177 | linlun | 51 | #endif |
| 52 | #ifdef PIN_5 |
||
| 1180 | linlun | 53 | #define PIN_5_ID NUMBEROFCHANNELS |
| 54 | #undefine NUMBEROFCHANNELS |
||
| 55 | #define NUMBEROFCHANNELS PIN_5_ID + 1 |
||
| 1177 | linlun | 56 | #endif |
| 57 | #ifdef PIN_6 |
||
| 1180 | linlun | 58 | #define PIN_6_ID NUMBEROFCHANNELS |
| 59 | #undefine NUMBEROFCHANNELS |
||
| 60 | #define NUMBEROFCHANNELS PIN_6_ID + 1 |
||
| 1177 | linlun | 61 | #endif |
| 62 | #ifdef PIN_7 |
||
| 1180 | linlun | 63 | #define PIN_7_ID NUMBEROFCHANNELS |
| 64 | #undefine NUMBEROFCHANNELS |
||
| 65 | #define NUMBEROFCHANNELS PIN_7_ID + 1 |
||
| 1177 | linlun | 66 | #endif |
| 1180 | linlun | 67 | uint16_t pwmValue[NUMBEROFCHANNELS]; |
| 1156 | linlun | 68 | |
| 69 | void act_softPWM_Init(void) |
||
| 70 | { |
||
| 71 | #ifdef act_softPWM_USEEEPROM |
||
| 72 | if (EEDATA_OK) |
||
| 73 | { |
||
| 1180 | linlun | 74 | ; |
| 1156 | linlun | 75 | } else |
| 76 | { //The CRC of the EEPROM is not correct, store default values and update CRC |
||
| 1180 | linlun | 77 | eeprom_write_word_crc(EEDATA16.PwmPeriod, DEFAULT_PWM_PERIOD , WITHOUT_CRC); |
| 78 | eeprom_write_word_crc(EEDATA16.defaultPwmValue, DEFAULT_PWM_VALUE , WITHOUT_CRC); |
||
| 79 | eeprom_write_byte_crc(EEDATA.defaultStates, 0xa0 , WITHOUT_CRC); |
||
| 80 | eeprom_write_byte_crc(EEDATA.ReportInterval, 0x14 , WITHOUT_CRC); |
||
| 81 | EEDATA_UPDATE_CRC; |
||
| 1156 | linlun | 82 | } |
| 1180 | linlun | 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); |
||
| 1156 | linlun | 89 | #endif |
| 90 | ///TODO: Initialize hardware etc here |
||
| 1177 | linlun | 91 | #ifdef PIN_0 |
| 92 | gpio_set_out(PIN_0); |
||
| 93 | gpio_clr_pin(PIN_0); |
||
| 94 | #endif |
||
| 95 | #ifdef PIN_1 |
||
| 96 | gpio_set_out(PIN_1); |
||
| 97 | gpio_clr_pin(PIN_1); |
||
| 98 | #endif |
||
| 99 | #ifdef PIN_2 |
||
| 100 | gpio_set_out(PIN_2); |
||
| 101 | gpio_clr_pin(PIN_2); |
||
| 102 | #endif |
||
| 103 | #ifdef PIN_3 |
||
| 104 | gpio_set_out(PIN_3); |
||
| 105 | gpio_clr_pin(PIN_3); |
||
| 106 | #endif |
||
| 107 | #ifdef PIN_4 |
||
| 108 | gpio_set_out(PIN_4); |
||
| 109 | gpio_clr_pin(PIN_4); |
||
| 110 | #endif |
||
| 111 | #ifdef PIN_5 |
||
| 112 | gpio_set_out(PIN_5); |
||
| 113 | gpio_clr_pin(PIN_5); |
||
| 114 | #endif |
||
| 115 | #ifdef PIN_6 |
||
| 116 | gpio_set_out(PIN_6); |
||
| 117 | gpio_clr_pin(PIN_6); |
||
| 118 | #endif |
||
| 119 | #ifdef PIN_7 |
||
| 120 | gpio_set_out(PIN_7); |
||
| 121 | gpio_clr_pin(PIN_7); |
||
| 122 | #endif |
||
| 1156 | linlun | 123 | |
| 1177 | linlun | 124 | Timer_SetTimeout(act_softPWM_TIMER, resolution, TimerTypeFreeRunning, NULL); |
| 1156 | linlun | 125 | // to use PCINt lib, call this function: (the callback function look as a timer callback function) |
| 126 | // Pcint_SetCallbackPin(act_softPWM_PCINT, EXP_C , &act_softPWM_pcint_callback); |
||
| 127 | |||
| 128 | } |
||
| 129 | |||
| 130 | void act_softPWM_Process(void) |
||
| 131 | { |
||
| 132 | if (Timer_Expired(act_softPWM_TIMER)) { |
||
| 133 | currentTimer++; |
||
| 134 | if (currentTimer == 10) { |
||
| 135 | currentTimer=0; |
||
| 1177 | linlun | 136 | #ifdef PIN_0 |
| 137 | gpio_set_pin(PIN_0); |
||
| 138 | #endif |
||
| 139 | #ifdef PIN_1 |
||
| 140 | gpio_set_pin(PIN_1); |
||
| 141 | #endif |
||
| 142 | #ifdef PIN_2 |
||
| 143 | gpio_set_pin(PIN_2); |
||
| 144 | #endif |
||
| 145 | #ifdef PIN_3 |
||
| 146 | gpio_set_pin(PIN_3); |
||
| 147 | #endif |
||
| 148 | #ifdef PIN_4 |
||
| 149 | gpio_set_pin(PIN_4); |
||
| 150 | #endif |
||
| 151 | #ifdef PIN_5 |
||
| 152 | gpio_set_pin(PIN_5); |
||
| 153 | #endif |
||
| 154 | #ifdef PIN_6 |
||
| 155 | gpio_set_pin(PIN_6); |
||
| 156 | #endif |
||
| 157 | #ifdef PIN_7 |
||
| 158 | gpio_set_pin(PIN_7); |
||
| 159 | #endif |
||
| 1156 | linlun | 160 | } |
| 1177 | linlun | 161 | #ifdef PIN_0 |
| 1180 | linlun | 162 | if (currentTimer >= pwmValue[PIN_0_ID]) { |
| 1177 | linlun | 163 | gpio_clr_pin(PIN_0); |
| 1156 | linlun | 164 | } |
| 1177 | linlun | 165 | #endif |
| 166 | #ifdef PIN_1 |
||
| 1180 | linlun | 167 | if (currentTimer >= pwmValue[PIN_1_ID]) { |
| 1177 | linlun | 168 | gpio_clr_pin(PIN_1); |
| 1156 | linlun | 169 | } |
| 1177 | linlun | 170 | #endif |
| 171 | #ifdef PIN_2 |
||
| 1180 | linlun | 172 | if (currentTimer >= pwmValue[PIN_2_ID]) { |
| 1177 | linlun | 173 | gpio_clr_pin(PIN_2); |
| 1156 | linlun | 174 | } |
| 1177 | linlun | 175 | #endif |
| 176 | #ifdef PIN_3 |
||
| 1180 | linlun | 177 | if (currentTimer >= pwmValue[PIN_3_ID]) { |
| 1177 | linlun | 178 | gpio_clr_pin(PIN_3); |
| 179 | } |
||
| 180 | #endif |
||
| 181 | #ifdef PIN_4 |
||
| 1180 | linlun | 182 | if (currentTimer >= pwmValue[PIN_4_ID]) { |
| 1177 | linlun | 183 | gpio_clr_pin(PIN_4); |
| 184 | } |
||
| 185 | #endif |
||
| 186 | #ifdef PIN_5 |
||
| 1180 | linlun | 187 | if (currentTimer >= pwmValue[PIN_5_ID]) { |
| 1177 | linlun | 188 | gpio_clr_pin(PIN_5); |
| 189 | } |
||
| 190 | #endif |
||
| 191 | #ifdef PIN_6 |
||
| 1180 | linlun | 192 | if (currentTimer >= pwmValue[PIN_6_ID]) { |
| 1177 | linlun | 193 | gpio_clr_pin(PIN_6); |
| 194 | } |
||
| 195 | #endif |
||
| 196 | #ifdef PIN_7 |
||
| 1180 | linlun | 197 | if (currentTimer >= pwmValue[PIN_7_ID]) { |
| 1177 | linlun | 198 | gpio_clr_pin(PIN_7); |
| 199 | } |
||
| 200 | #endif |
||
| 1156 | linlun | 201 | } |
| 1180 | linlun | 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; |
||
| 217 | } |
||
| 1156 | linlun | 218 | } |
| 219 | |||
| 1180 | linlun | 220 | |
| 221 | |||
| 1156 | linlun | 222 | void act_softPWM_HandleMessage(StdCan_Msg_t *rxMsg) |
| 223 | { |
||
| 224 | if ( StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_ACT && |
||
| 225 | StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER && |
||
| 226 | rxMsg->Header.ModuleType == CAN_MODULE_TYPE_ACT_SOFTPWM && |
||
| 227 | rxMsg->Header.ModuleId == act_softPWM_ID) |
||
| 228 | { |
||
| 229 | switch (rxMsg->Header.Command) |
||
| 230 | { |
||
| 1177 | linlun | 231 | case CAN_MODULE_CMD_PHYSICAL_PWM: |
| 232 | if ((((uint16_t)rxMsg->Data[1])<8) + rxMsg->Data[2] > maxTimer) |
||
| 233 | { |
||
| 234 | rxMsg->Data[2] = maxTimer & 0x00ff; |
||
| 235 | rxMsg->Data[1] = (maxTimer>>8) & 0x00ff; |
||
| 236 | } |
||
| 237 | switch (rxMsg->Data[0]) |
||
| 238 | { |
||
| 239 | #ifdef PIN_0 |
||
| 240 | case 0: |
||
| 1180 | linlun | 241 | pwmValue[PIN_0_ID] = (((uint16_t)rxMsg->Data[1])<<8) + rxMsg->Data[2]; |
| 1177 | linlun | 242 | break; |
| 243 | #endif |
||
| 244 | #ifdef PIN_1 |
||
| 245 | case 1: |
||
| 1180 | linlun | 246 | pwmValue[PIN_1_ID] = (((uint16_t)rxMsg->Data[1])<<8) + rxMsg->Data[2]; |
| 1177 | linlun | 247 | break; |
| 248 | #endif |
||
| 249 | #ifdef PIN_2 |
||
| 250 | case 2: |
||
| 1180 | linlun | 251 | pwmValue[PIN_2_ID] = (((uint16_t)rxMsg->Data[1])<<8) + rxMsg->Data[2]; |
| 1177 | linlun | 252 | break; |
| 253 | #endif |
||
| 254 | #ifdef PIN_3 |
||
| 255 | case 3: |
||
| 1180 | linlun | 256 | pwmValue[PIN_3_ID] = (((uint16_t)rxMsg->Data[1])<<8) + rxMsg->Data[2]; |
| 1177 | linlun | 257 | break; |
| 258 | #endif |
||
| 259 | #ifdef PIN_4 |
||
| 260 | case 4: |
||
| 1180 | linlun | 261 | pwmValue[PIN_4_ID] = (((uint16_t)rxMsg->Data[1])<<8) + rxMsg->Data[2]; |
| 1177 | linlun | 262 | break; |
| 263 | #endif |
||
| 264 | #ifdef PIN_5 |
||
| 265 | case 5: |
||
| 1180 | linlun | 266 | pwmValue[PIN_5_ID] = (((uint16_t)rxMsg->Data[1])<<8) + rxMsg->Data[2]; |
| 1177 | linlun | 267 | break; |
| 268 | #endif |
||
| 269 | #ifdef PIN_6 |
||
| 270 | case 6: |
||
| 1180 | linlun | 271 | pwmValue[PIN_6_ID] = (((uint16_t)rxMsg->Data[1])<<8) + rxMsg->Data[2]; |
| 1177 | linlun | 272 | break; |
| 273 | #endif |
||
| 274 | #ifdef PIN_7 |
||
| 275 | case 7: |
||
| 1180 | linlun | 276 | pwmValue[PIN_7_ID] = (((uint16_t)rxMsg->Data[1])<<8) + rxMsg->Data[2]; |
| 1177 | linlun | 277 | break; |
| 278 | #endif |
||
| 279 | } |
||
| 280 | break; |
||
| 281 | case CAN_MODULE_CMD_SOFTPWM_CONFIG: |
||
| 282 | maxTimer = (((uint16_t)rxMsg->Data[0])<<8) + rxMsg->Data[1]; |
||
| 283 | resolution = rxMsg->Data[2]; |
||
| 284 | Timer_SetTimeout(act_softPWM_TIMER, resolution, TimerTypeFreeRunning, NULL); |
||
| 285 | break; |
||
| 1180 | linlun | 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 | } |
||
| 1177 | linlun | 293 | |
| 1180 | linlun | 294 | StdCan_Msg_t txMsg; |
| 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; |
||
| 1156 | linlun | 305 | } |
| 306 | } |
||
| 307 | } |
||
| 308 | |||
| 309 | void act_softPWM_List(uint8_t ModuleSequenceNumber) |
||
| 310 | { |
||
| 311 | StdCan_Msg_t txMsg; |
||
| 312 | |||
| 313 | StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT); ///TODO: Change this to the actual class type |
||
| 314 | StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER); |
||
| 315 | txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_SOFTPWM; ///TODO: Change this to the actual module type |
||
| 316 | txMsg.Header.ModuleId = act_softPWM_ID; |
||
| 317 | txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST; |
||
| 318 | txMsg.Length = 6; |
||
| 319 | |||
| 320 | txMsg.Data[0] = NODE_HW_ID_BYTE0; |
||
| 321 | txMsg.Data[1] = NODE_HW_ID_BYTE1; |
||
| 322 | txMsg.Data[2] = NODE_HW_ID_BYTE2; |
||
| 323 | txMsg.Data[3] = NODE_HW_ID_BYTE3; |
||
| 324 | |||
| 325 | txMsg.Data[4] = NUMBER_OF_MODULES; |
||
| 326 | txMsg.Data[5] = ModuleSequenceNumber; |
||
| 327 | |||
| 328 | while (StdCan_Put(&txMsg) != StdCan_Ret_OK); |
||
| 329 | } |