Rev 1119 | Go to most recent revision | Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1113 | linlun | 1 | |
| 2 | #include "act_SlowPWM.h" |
||
| 3 | |||
| 4 | #ifdef act_SlowPWM_USEEEPROM |
||
| 5 | #include "act_SlowPWM_eeprom.h" |
||
| 6 | struct eeprom_act_SlowPWM EEMEM eeprom_act_SlowPWM = |
||
| 7 | { |
||
| 8 | { |
||
| 9 | ///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. |
||
| 10 | 0xABcd, // x |
||
| 11 | 0x1234, // y |
||
| 12 | 0x00 |
||
| 13 | }, |
||
| 14 | |||
| 15 | }; |
||
| 16 | #endif |
||
| 17 | |||
| 18 | uint8_t pwmDefaultState,pwmDefaultStartState; |
||
| 19 | uint16_t pwmDefaultValue; |
||
| 20 | uint16_t pwmPeriod, pwmValue; |
||
| 21 | uint8_t pwmStatus; |
||
| 22 | |||
| 23 | #define PWM_LOW 0 |
||
| 24 | #define PWM_HIGH 1 |
||
| 25 | #define PWM_OFF 2 |
||
| 26 | void updatePWM_callback(uint8_t timer) { |
||
| 27 | //pwmValue = DEFAULT_PWM_VALUE; |
||
| 28 | if (pwmStatus == PWM_HIGH) { //Pwm output is low |
||
| 29 | gpio_clr_pin(PWM_PIN); //Sets low |
||
| 30 | pwmStatus = 1; |
||
| 31 | Timer_SetTimeout(act_SlowPWM_TIMER, pwmValue, TimerTypeOneShot, &updatePWM_callback); |
||
| 32 | |||
| 33 | } else if (pwmStatus == PWM_LOW){ //Pwm output is high |
||
| 34 | gpio_set_pin(PWM_PIN); //Sets high |
||
| 35 | pwmStatus = 0; |
||
| 36 | Timer_SetTimeout(act_SlowPWM_TIMER, pwmPeriod - pwmValue, TimerTypeOneShot, &updatePWM_callback); |
||
| 37 | } else //PWM_OFF |
||
| 38 | { |
||
| 39 | if (pwmDefaultState == CAN_MODULE_ENUM_SLOWPWM_CONFIG_DEFAULTSTATE_LOW) |
||
| 40 | { |
||
| 41 | gpio_set_out(PWM_PIN); |
||
| 42 | gpio_clr_pin(PWM_PIN); |
||
| 43 | } else if (pwmDefaultState == CAN_MODULE_ENUM_SLOWPWM_CONFIG_DEFAULTSTATE_HIGH) |
||
| 44 | { |
||
| 45 | gpio_set_out(PWM_PIN); |
||
| 46 | gpio_set_pin(PWM_PIN); |
||
| 47 | } else if (pwmDefaultState == CAN_MODULE_ENUM_SLOWPWM_CONFIG_DEFAULTSTATE_TRISTATE) |
||
| 48 | { |
||
| 49 | gpio_set_in(PWM_PIN); |
||
| 50 | gpio_clr_pin(PWM_PIN); |
||
| 51 | } |
||
| 52 | } |
||
| 53 | } |
||
| 54 | |||
| 55 | |||
| 56 | void pwmInit(void) { |
||
| 57 | if (pwmDefaultState == CAN_MODULE_ENUM_SLOWPWM_CONFIG_DEFAULTSTATE_LOW) |
||
| 58 | { |
||
| 59 | gpio_set_out(PWM_PIN); |
||
| 60 | gpio_clr_pin(PWM_PIN); |
||
| 61 | } else if (pwmDefaultState == CAN_MODULE_ENUM_SLOWPWM_CONFIG_DEFAULTSTATE_HIGH) |
||
| 62 | { |
||
| 63 | gpio_set_out(PWM_PIN); |
||
| 64 | gpio_set_pin(PWM_PIN); |
||
| 65 | } else if (pwmDefaultState == CAN_MODULE_ENUM_SLOWPWM_CONFIG_DEFAULTSTATE_TRISTATE) |
||
| 66 | { |
||
| 67 | gpio_set_in(PWM_PIN); |
||
| 68 | gpio_clr_pin(PWM_PIN); |
||
| 69 | } |
||
| 70 | |||
| 71 | pwmValue = pwmDefaultValue; |
||
| 72 | pwmStatus = PWM_OFF; |
||
| 73 | if (pwmDefaultStartState==CAN_MODULE_ENUM_SLOWPWM_CONFIG_STARTUPSTATE_ON) { |
||
| 74 | Timer_SetTimeout(act_SlowPWM_TIMER, pwmValue, TimerTypeOneShot, &updatePWM_callback); |
||
| 75 | pwmStatus = PWM_LOW; |
||
| 76 | gpio_set_out(PWM_PIN); |
||
| 77 | gpio_clr_pin(PWM_PIN); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | void act_SlowPWM_Init(void) |
||
| 82 | { |
||
| 83 | #ifdef act_SlowPWM_USEEEPROM |
||
| 84 | if (EEDATA_OK) |
||
| 85 | { |
||
| 86 | ; |
||
| 87 | } else |
||
| 88 | { //The CRC of the EEPROM is not correct, store default values and update CRC |
||
| 89 | eeprom_write_word_crc(EEDATA16.PwmPeriod, DEFAULT_PWM_PERIOD , WITHOUT_CRC); |
||
| 90 | eeprom_write_word_crc(EEDATA16.defaultPwmValue, DEFAULT_PWM_VALUE , WITHOUT_CRC); |
||
| 91 | eeprom_write_byte_crc(EEDATA.defaultStates, 0xa0 , WITHOUT_CRC); |
||
| 92 | EEDATA_UPDATE_CRC; |
||
| 93 | } |
||
| 94 | pwmDefaultValue = eeprom_read_word(EEDATA16.defaultPwmValue); |
||
| 95 | pwmPeriod = eeprom_read_word(EEDATA16.PwmPeriod); |
||
| 96 | pwmDefaultState = (eeprom_read_byte(EEDATA.defaultStates)>>6) & 0x03; |
||
| 97 | pwmDefaultStartState = ((eeprom_read_byte(EEDATA.defaultStates)>>5) & 0x01); |
||
| 98 | #endif |
||
| 99 | ///TODO: Initialize hardware etc here |
||
| 100 | pwmInit(); |
||
| 101 | } |
||
| 102 | |||
| 103 | void act_SlowPWM_Process(void) |
||
| 104 | { |
||
| 105 | ///TODO: Stuff that needs doing is done here |
||
| 106 | } |
||
| 107 | |||
| 108 | void act_SlowPWM_HandleMessage(StdCan_Msg_t *rxMsg) |
||
| 109 | { |
||
| 110 | if ( StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_ACT && |
||
| 111 | StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER && |
||
| 112 | rxMsg->Header.ModuleType == CAN_MODULE_TYPE_ACT_SLOWPWM && |
||
| 113 | rxMsg->Header.ModuleId == act_SlowPWM_ID) |
||
| 114 | { |
||
| 115 | switch (rxMsg->Header.Command) |
||
| 116 | { |
||
| 117 | case CAN_MODULE_CMD_PHYSICAL_SLOWPWM: |
||
| 118 | if (rxMsg->Data[0] == PWMSENSORID) |
||
| 119 | { |
||
| 120 | if (rxMsg->Length == 3) |
||
| 121 | { |
||
| 122 | pwmValue= (rxMsg->Data[1]<<8) + rxMsg->Data[2]; |
||
| 123 | if (pwmValue > pwmPeriod) |
||
| 124 | pwmValue = pwmPeriod; |
||
| 125 | } else |
||
| 126 | { |
||
| 127 | rxMsg->Data[1] = (pwmValue>>8)&0xff; |
||
| 128 | rxMsg->Data[2] = (pwmValue)&0xff; |
||
| 129 | StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER); |
||
| 130 | rxMsg->Length = 3; |
||
| 131 | while (StdCan_Put(rxMsg) != StdCan_Ret_OK); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | break; |
||
| 135 | case CAN_MODULE_CMD_SLOWPWM_CONFIG: |
||
| 136 | if (rxMsg->Length == 5) |
||
| 137 | { |
||
| 138 | eeprom_write_word_crc(EEDATA16.PwmPeriod, (rxMsg->Data[0]<<8) + rxMsg->Data[1] , WITHOUT_CRC); |
||
| 139 | eeprom_write_word_crc(EEDATA16.defaultPwmValue, (rxMsg->Data[2]<<8) + rxMsg->Data[3] , WITHOUT_CRC); |
||
| 140 | eeprom_write_byte_crc(EEDATA.defaultStates, rxMsg->Data[2] , WITH_CRC); |
||
| 141 | pwmDefaultValue = eeprom_read_word(EEDATA16.defaultPwmValue); |
||
| 142 | pwmPeriod = eeprom_read_word(EEDATA16.PwmPeriod); |
||
| 143 | pwmDefaultState = (eeprom_read_byte(EEDATA.defaultStates)>>6) & 0x03; |
||
| 144 | pwmDefaultStartState = ((eeprom_read_byte(EEDATA.defaultStates)>>5) & 0x01); |
||
| 145 | } else |
||
| 146 | { |
||
| 147 | rxMsg->Data[0] = (eeprom_read_word(EEDATA16.PwmPeriod)>>8)&0xff; |
||
| 148 | rxMsg->Data[1] = (eeprom_read_word(EEDATA16.PwmPeriod))&0xff; |
||
| 149 | rxMsg->Data[2] = (eeprom_read_word(EEDATA16.defaultPwmValue)>>8)&0xff; |
||
| 150 | rxMsg->Data[3] = (eeprom_read_word(EEDATA16.defaultPwmValue))&0xff; |
||
| 151 | rxMsg->Data[4] = eeprom_read_byte(EEDATA.defaultStates); |
||
| 152 | StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER); |
||
| 153 | rxMsg->Length = 5; |
||
| 154 | while (StdCan_Put(rxMsg) != StdCan_Ret_OK); |
||
| 155 | } |
||
| 156 | break; |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | } |
||
| 161 | |||
| 162 | void act_SlowPWM_List(uint8_t ModuleSequenceNumber) |
||
| 163 | { |
||
| 164 | StdCan_Msg_t txMsg; |
||
| 165 | |||
| 166 | StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT); ///TODO: Change this to the actual class type |
||
| 167 | StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER); |
||
| 168 | txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_SLOWPWM; ///TODO: Change this to the actual module type |
||
| 169 | txMsg.Header.ModuleId = act_SlowPWM_ID; |
||
| 170 | txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST; |
||
| 171 | txMsg.Length = 6; |
||
| 172 | |||
| 173 | txMsg.Data[0] = NODE_HW_ID_BYTE0; |
||
| 174 | txMsg.Data[1] = NODE_HW_ID_BYTE1; |
||
| 175 | txMsg.Data[2] = NODE_HW_ID_BYTE2; |
||
| 176 | txMsg.Data[3] = NODE_HW_ID_BYTE3; |
||
| 177 | |||
| 178 | txMsg.Data[4] = NUMBER_OF_MODULES; |
||
| 179 | txMsg.Data[5] = ModuleSequenceNumber; |
||
| 180 | |||
| 181 | while (StdCan_Put(&txMsg) != StdCan_Ret_OK); |
||
| 182 | } |