void sns_power_Init(void) { #ifdef sns_power_USEEEPROM if (EEDATA_OK) { ///TODO: Use stored data to set initial values for the module sns_power_ReportInterval = eeprom_read_byte(EEDATA.reportInterval); EnergyCounter = eeprom_read_word(EEDATA16.EnergyCounterLower); EnergyCounter += (((uint32_t)(eeprom_read_word(EEDATA16.EnergyCounterUpper)))<<16); #ifdef POWER_SNS_PIN_ch2 EnergyCounter_ch2 = eeprom_read_word(EEDATA16.EnergyCounterLower_ch2); EnergyCounter_ch2 += (((uint32_t)(eeprom_read_word(EEDATA16.EnergyCounterUpper_ch2)))<<16); #endif } else { //The CRC of the EEPROM is not correct, store default values and update CRC eeprom_write_byte_crc(EEDATA.reportInterval, sns_power_SEND_PERIOD, WITHOUT_CRC); eeprom_write_word_crc(EEDATA16.EnergyCounterUpper, 0, WITHOUT_CRC); eeprom_write_word_crc(EEDATA16.EnergyCounterLower, 0, WITHOUT_CRC); #ifdef POWER_SNS_PIN_ch2 eeprom_write_word_crc(EEDATA16.EnergyCounterUpper_ch2, 0, WITHOUT_CRC); eeprom_write_word_crc(EEDATA16.EnergyCounterLower_ch2, 0, WITHOUT_CRC); #endif EEDATA_UPDATE_CRC; sns_power_ReportInterval = eeprom_read_byte(EEDATA.reportInterval); } #endif ///Initialize hardware etc gpio_set_in(POWER_SNS_PIN); // Set to input gpio_set_pin(POWER_SNS_PIN); // Enable pull-up Pcint_SetCallbackPin(sns_power_PCINT, POWER_SNS_PIN, &sns_power_pcint_callback); MeasurmentBufferPointer = 0; #ifdef POWER_SNS_PIN_ch2 gpio_set_in(POWER_SNS_PIN_ch2); // Set to input gpio_set_pin(POWER_SNS_PIN_ch2); // Enable pull-up Pcint_SetCallbackPin(sns_power_PCINT_ch2, POWER_SNS_PIN_ch2, &sns_power_pcint_callback_ch2); MeasurmentBufferPointer_ch2 = 0; #endif Timer_SetTimeout(sns_power_SEND_TIMER, sns_power_ReportInterval*1000 , TimerTypeFreeRunning, 0); #if sns_power_SEND_1_MIN_AVG == 1 Timer_SetTimeout(sns_power_SEND_TIMER_1_MIN_AVG, 60000-10 , TimerTypeFreeRunning, &sns_power_timer_callback); #endif }
void act_linearAct_Init(void) { #if act_linearAct_USEEEPROM==1 if (EEDATA_OK) { ///TODO: Use stored data to set initial values for the module pulses = eeprom_read_word(EEDATA16.pulses_ee); min = eeprom_read_word(EEDATA16.min_ee); low = eeprom_read_word(EEDATA16.low_ee); high = eeprom_read_word(EEDATA16.high_ee); max = eeprom_read_word(EEDATA16.max_ee); } else { //The CRC of the EEPROM is not correct, store default values and update CRC eeprom_write_word_crc(EEDATA16.pulses_ee, 1200, WITHOUT_CRC); eeprom_write_word_crc(EEDATA16.min_ee, 300, WITHOUT_CRC); eeprom_write_word_crc(EEDATA16.low_ee, 815, WITHOUT_CRC); eeprom_write_word_crc(EEDATA16.high_ee, 5600, WITHOUT_CRC); eeprom_write_word_crc(EEDATA16.max_ee, 6100, WITHOUT_CRC); EEDATA_UPDATE_CRC; } #else // Only for testing/debugging... pulses = 1200; min = 300; low = 815; high = 5600; max = 6100; #endif // PD5 (EXP_G) is used as Timer1 input. Hardwired, no move possible! gpio_set_in(EXP_G); gpio_set_pullup(EXP_G); //PORTD &= ~(_BV(DIR_RELAY) | _BV(ON_RELAY)); gpio_clr_pin(RELAY_ON); gpio_clr_pin(RELAY_DIR); // initiera utgångar //DDRD = (_BV(DIR_RELAY) | _BV(ON_RELAY)); gpio_set_out(RELAY_ON); gpio_set_out(RELAY_DIR); // Start report timer Timer_SetTimeout(act_linearAct_TIMER_report, act_linearAct_ReportInterval*1000 , TimerTypeFreeRunning, 0); #if (USE_STIMULI == 1) // Set stimuli as an output gpio_set_out(STIMULI); Timer_SetTimeout(act_linearAct_TIMER_stimuli, 10, TimerTypeFreeRunning,0); #endif }
void sns_flower_Process(void) { static uint8_t measureState=0; static uint16_t results[6]; static uint16_t resultsInv[6]; uint16_t temp; static uint8_t flowerChannelToSend = 0; if (Timer_Expired(sns_flower_WAIT_TIMER)) { gpio_set_out(sns_flower_highSide_PIN); gpio_set_out(sns_flower_lowSide_PIN); gpio_set_pin(sns_flower_highSide_PIN); gpio_clr_pin(sns_flower_lowSide_PIN); measureState=0; Timer_SetTimeout(sns_flower_MEASUREMENT_TIMER, sns_flower_MEASUREMENT_PERIOD_MS , TimerTypeOneShot, 0); } if (Timer_Expired(sns_flower_MEASUREMENT_TIMER)) { StdCan_Msg_t txMsg; StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS); StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER); txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_VOLTAGECURRENT; txMsg.Header.ModuleId = sns_flower_ID; txMsg.Length = 3; switch (measureState) { case 0: #ifdef sns_flower0AD results[0] = ADC_Get(sns_flower0AD); #endif #ifdef sns_flower1AD results[1] = ADC_Get(sns_flower1AD); #endif #ifdef sns_flower2AD results[2] = ADC_Get(sns_flower2AD); #endif #ifdef sns_flower3AD results[3] = ADC_Get(sns_flower3AD); #endif #ifdef sns_flower4AD results[4] = ADC_Get(sns_flower4AD); #endif #ifdef sns_flower5AD results[5] = ADC_Get(sns_flower5AD); #endif gpio_clr_pin(sns_flower_highSide_PIN); gpio_set_pin(sns_flower_lowSide_PIN); measureState=1; Timer_SetTimeout(sns_flower_MEASUREMENT_TIMER, sns_flower_MEASUREMENT_PERIOD_MS , TimerTypeOneShot, 0); break; case 1: #ifdef sns_flower0AD resultsInv[0] = 1023-ADC_Get(sns_flower0AD); #endif #ifdef sns_flower1AD resultsInv[1] = 1023-ADC_Get(sns_flower1AD); #endif #ifdef sns_flower2AD resultsInv[2] = 1023-ADC_Get(sns_flower2AD); #endif #ifdef sns_flower3AD resultsInv[3] = 1023-ADC_Get(sns_flower3AD); #endif #ifdef sns_flower4AD resultsInv[4] = 1023-ADC_Get(sns_flower4AD); #endif #ifdef sns_flower5AD resultsInv[5] = 1023-ADC_Get(sns_flower5AD); #endif gpio_clr_pin(sns_flower_highSide_PIN); gpio_clr_pin(sns_flower_lowSide_PIN); gpio_set_in(sns_flower_highSide_PIN); gpio_set_in(sns_flower_lowSide_PIN); measureState=2; flowerChannelToSend=0; Timer_SetTimeout(sns_flower_MEASUREMENT_TIMER, sns_flower_MEASUREMENT_PERIOD_MS , TimerTypeOneShot, 0); break; case 2: switch(flowerChannelToSend) { case 0: #ifdef sns_flower0AD txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_PERCENT; txMsg.Data[0] = 0; temp = (uint16_t)((float)(results[0]+resultsInv[0])*4.888); txMsg.Data[1] = (temp>>8)&0xff; txMsg.Data[2] = (temp)&0xff; while (StdCan_Put(&txMsg) != StdCan_Ret_OK) {} flowerChannelToSend = 1; # if !(defined(sns_flower1AD) || defined(sns_flower2AD) || defined(sns_flower3AD) || defined(sns_flower4AD) || defined(sns_flower5AD)) flowerChannelToSend = 0; measureState=0; # endif break; #endif case 1: #ifdef sns_flower1AD txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_PERCENT; txMsg.Data[0] = 1; temp = (uint16_t)((float)(results[1]+resultsInv[1])*4.888); txMsg.Data[1] = (temp>>8)&0xff; txMsg.Data[2] = (temp)&0xff; while (StdCan_Put(&txMsg) != StdCan_Ret_OK) {} flowerChannelToSend = 2; # if !(defined(sns_flower2AD) || defined(sns_flower3AD) || defined(sns_flower4AD) || defined(sns_flower5AD)) flowerChannelToSend = 0; measureState=0; # endif break; #endif case 2: #ifdef sns_flower2AD txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_PERCENT; txMsg.Data[0] = 2; temp = (uint16_t)((float)(results[2]+resultsInv[2])*4.888); txMsg.Data[1] = (temp>>8)&0xff; txMsg.Data[2] = (temp)&0xff; while (StdCan_Put(&txMsg) != StdCan_Ret_OK) {} flowerChannelToSend = 3; #if !(defined(sns_flower3AD) || defined(sns_flower4AD) || defined(sns_flower5AD)) flowerChannelToSend = 0; measureState=0; #endif break; #endif case 3: #ifdef sns_flower3AD txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_PERCENT; txMsg.Data[0] = 3; temp = (uint16_t)((float)(results[3]+resultsInv[3])*4.888); txMsg.Data[1] = (temp>>8)&0xff; txMsg.Data[2] = (temp)&0xff; while (StdCan_Put(&txMsg) != StdCan_Ret_OK) {} flowerChannelToSend = 4; #if !( defined(sns_flower4AD) || defined(sns_flower5AD)) flowerChannelToSend = 0; measureState=0; #endif break; #endif case 4: #ifdef sns_flower4AD txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_PERCENT; txMsg.Data[0] = 4; temp = (uint16_t)((float)(results[4]+resultsInv[4])*4.888); txMsg.Data[1] = (temp>>8)&0xff; txMsg.Data[2] = (temp)&0xff; while (StdCan_Put(&txMsg) != StdCan_Ret_OK) {} flowerChannelToSend = 5; #if !(defined(sns_flower5AD)) flowerChannelToSend = 0; measureState=0; #endif break; #endif case 5: #ifdef sns_flower5AD txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_PERCENT; txMsg.Data[0] = 5; temp = (uint16_t)((float)(results[5]+resultsInv[5])*4.888); txMsg.Data[1] = (temp>>8)&0xff; txMsg.Data[2] = (temp)&0xff; while (StdCan_Put(&txMsg) != StdCan_Ret_OK) {} flowerChannelToSend = 0; measureState=0; break; #endif default: measureState=0; break; } if (measureState != 0) { Timer_SetTimeout(sns_flower_MEASUREMENT_TIMER, sns_flower_CAN_MSG_PAUS_MS , TimerTypeOneShot, 0); } break; } } }
void sns_counter_Init(void) { #if sns_counter_USEEEPROM==1 if (EEDATA_OK) { //Use stored data to set initial values for the module sns_counter_ReportInterval = eeprom_read_byte(EEDATA.reportInterval); #ifdef sns_counter_CH0 Count[0] = eeprom_read_dword(EEDATA32.Count[0]); #endif #ifdef sns_counter_CH1 Count[1] = eeprom_read_dword(EEDATA32.Count[1]); #endif #ifdef sns_counter_CH2 Count[2] = eeprom_read_dword(EEDATA32.Count[2]); #endif #ifdef sns_counter_CH3 Count[3] = eeprom_read_dword(EEDATA32.Count[3]); #endif #ifdef sns_counter_CH4 Count[4] = eeprom_read_dword(EEDATA32.Count[4]); #endif #ifdef sns_counter_CH5 Count[5] = eeprom_read_dword(EEDATA32.Count[5]); #endif #ifdef sns_counter_CH6 Count[6] = eeprom_read_dword(EEDATA32.Count[6]); #endif #ifdef sns_counter_CH7 Count[7] = eeprom_read_dword(EEDATA32.Count[7]); #endif } else { //The CRC of the EEPROM is not correct, store default values and update CRC sns_counter_save_data(); } #endif //Initialize hardware etc here #ifdef sns_counter_CH0 #if (sns_counter_CH0_pullup == 1) gpio_set_pullup(sns_counter_CH0); #else gpio_clr_pullup(sns_counter_CH0); #endif gpio_set_in(sns_counter_CH0); Pcint_SetCallbackPin(sns_counter_PCINT_CH0, sns_counter_CH0 , &sns_counter_pcint_callback); #endif #ifdef sns_counter_CH1 #if (sns_counter_CH1_pullup == 1) gpio_set_pullup(sns_counter_CH1); #else gpio_clr_pullup(sns_counter_CH1); #endif gpio_set_in(sns_counter_CH1); Pcint_SetCallbackPin(sns_counter_PCINT_CH1, sns_counter_CH1 , &sns_counter_pcint_callback); #endif #ifdef sns_counter_CH2 #if (sns_counter_CH2_pullup == 1) gpio_set_pullup(sns_counter_CH2); #else gpio_clr_pullup(sns_counter_CH2); #endif gpio_set_in(sns_counter_CH2); Pcint_SetCallbackPin(sns_counter_PCINT_CH2, sns_counter_CH2 , &sns_counter_pcint_callback); #endif #ifdef sns_counter_CH3 #if (sns_counter_CH3_pullup == 1) gpio_set_pullup(sns_counter_CH3); #else gpio_clr_pullup(sns_counter_CH3); #endif gpio_set_in(sns_counter_CH3); Pcint_SetCallbackPin(sns_counter_PCINT_CH3, sns_counter_CH3 , &sns_counter_pcint_callback); #endif #ifdef sns_counter_CH4 #if (sns_counter_CH4_pullup == 1) gpio_set_pullup(sns_counter_CH4); #else gpio_clr_pullup(sns_counter_CH4); #endif gpio_set_in(sns_counter_CH4); Pcint_SetCallbackPin(sns_counter_PCINT_CH4, sns_counter_CH4 , &sns_counter_pcint_callback); #endif #ifdef sns_counter_CH5 #if (sns_counter_CH5_pullup == 1) gpio_set_pullup(sns_counter_CH5); #else gpio_clr_pullup(sns_counter_CH5); #endif gpio_set_in(sns_counter_CH5); Pcint_SetCallbackPin(sns_counter_PCINT_CH5, sns_counter_CH5 , &sns_counter_pcint_callback); #endif #ifdef sns_counter_CH6 #if (sns_counter_CH6_pullup == 1) gpio_set_pullup(sns_counter_CH6); #else gpio_clr_pullup(sns_counter_CH6); #endif gpio_set_in(sns_counter_CH6); Pcint_SetCallbackPin(sns_counter_PCINT_CH6, sns_counter_CH6 , &sns_counter_pcint_callback); #endif #ifdef sns_counter_CH7 #if (sns_counter_CH7_pullup == 1) gpio_set_pullup(sns_counter_CH7); #else gpio_clr_pullup(sns_counter_CH7); #endif gpio_set_in(sns_counter_CH7); Pcint_SetCallbackPin(sns_counter_PCINT_CH7, sns_counter_CH7 , &sns_counter_pcint_callback); #endif #ifdef sns_counter_SEND_TIMER Timer_SetTimeout(sns_counter_SEND_TIMER, sns_counter_ReportInterval*1000 , TimerTypeFreeRunning, 0); #endif }
void act_protectedOutput_Init() { /* * Init target state vector, in case EEPROM is disabled */ for (uint8_t i=0; i<act_protectedOutput_CH_COUNT; i++) { chTargetState[i] = 0; } /* * Configure DIAG input pin */ if (act_protectedOutput_DIAG_PIN_PULL_ENABLED) { // if DIAG is asserted low, we need pull-up gpio_set_statement(act_protectedOutput_DIAG_PIN_POLARITY == 0 ? 1 : 0, DIAG_PIN); } gpio_set_in(DIAG_PIN); Pcint_SetCallbackPin(act_protectedOutput_DIAG_PIN_PCINT, DIAG_PIN, &pcIntCallback); /* * Read EEPROM data */ #if act_protectedOutput_EEPROM_ENABLED == 1 if (EEDATA_OK) { #if act_protectedOutput_CH_COUNT >= 1 chTargetState[0] = eeprom_read_byte(EEDATA.ch0); #endif #if act_protectedOutput_CH_COUNT >= 2 chTargetState[1] = eeprom_read_byte(EEDATA.ch1); #endif #if act_protectedOutput_CH_COUNT >= 3 chTargetState[2] = eeprom_read_byte(EEDATA.ch2); #endif #if act_protectedOutput_CH_COUNT >= 4 chTargetState[3] = eeprom_read_byte(EEDATA.ch3); #endif } else { //The CRC of the EEPROM is not correct, store default values and update CRC #if act_protectedOutput_CH_COUNT >= 1 eeprom_write_byte_crc(EEDATA.ch0, 0x00, WITHOUT_CRC); #endif #if act_protectedOutput_CH_COUNT >= 2 eeprom_write_byte_crc(EEDATA.ch1, 0x00, WITHOUT_CRC); #endif #if act_protectedOutput_CH_COUNT >= 3 eeprom_write_byte_crc(EEDATA.ch2, 0x00, WITHOUT_CRC); #endif #if act_protectedOutput_CH_COUNT >= 4 eeprom_write_byte_crc(EEDATA.ch3, 0x00, WITHOUT_CRC); #endif EEDATA_UPDATE_CRC; } #endif /* * Configure OUTPUT pins */ #if act_protectedOutput_CH_COUNT >= 1 gpio_set_statement(CH0_OFF, CH0_PIN); gpio_set_out(act_protectedOutput_CH0); #endif #if act_protectedOutput_CH_COUNT >= 2 gpio_set_statement(CH1_OFF, CH1_PIN); gpio_set_out(act_protectedOutput_CH1); #endif #if act_protectedOutput_CH_COUNT >= 3 gpio_set_statement(CH2_OFF, CH2_PIN); gpio_set_out(act_protectedOutput_CH2); #endif #if act_protectedOutput_CH_COUNT >= 4 gpio_set_statement(CH3_OFF, CH3_PIN); gpio_set_out(act_protectedOutput_CH3); #endif diagState = DIAG_NORMAL; }