void sns_VoltageCurrent_Init(void) { #ifdef sns_VoltageCurrent_USEEEPROM if (EEDATA_OK) { ///TODO: Use stored data to set initial values for the module blablaX = eeprom_read_byte(EEDATA.x); blablaY = eeprom_read_word(EEDATA.y); } else { //The CRC of the EEPROM is not correct, store default values and update CRC eeprom_write_byte_crc(EEDATA.x, 0xAB, WITHOUT_CRC); eeprom_write_word_crc(EEDATA.y, 0x1234, WITHOUT_CRC); EEDATA_UPDATE_CRC; } #endif // to use PCINt lib, call this function: (the callback function look as a timer callback function) // Pcint_SetCallbackPin(sns_VoltageCurrent_PCINT, EXP_C , &sns_VoltageCurrent_pcint_callback); ADC_Init(); #ifdef sns_VoltageCurrent_SEND_PERIOD Timer_SetTimeout(sns_VoltageCurrent_TIMER, sns_VoltageCurrent_SEND_PERIOD*1000 , TimerTypeFreeRunning, 0); #else #ifdef sns_VoltageCurrent_SEND_PERIOD_MS Timer_SetTimeout(sns_VoltageCurrent_TIMER, sns_VoltageCurrent_SEND_PERIOD_MS , TimerTypeFreeRunning, 0); #endif #endif }
static void pcIntCallback(uint8_t id, uint8_t status) { // new state of the DIAG pin? if (id == act_protectedOutput_DIAG_PIN_PCINT) { //diagState = status; readDiagPin(); updateOutput(0); // DIAG asserted? if (diagState == DIAG_ASSERTED) { // if retry-timer mechanism is enabled, initiate timer if (act_protectedOutput_RETRY_TIMER_TIME_S > 0) { Timer_SetTimeout(act_protectedOutput_RETRY_TIMER, act_protectedOutput_RETRY_TIMER_TIME_S*1000, TimerTypeOneShot, 0); } // if the retry-mechanism is disabled, change the target output state to OFF else { for (uint8_t i=0; i<act_protectedOutput_CH_COUNT; i++) { chTargetState[i] = 0; } #if act_protectedOutput_EEPROM_ENABLED == 1 // output states changed, store to EE after this timer delay Timer_SetTimeout(act_protectedOutput_STORE_VALUE_TIMEOUT, act_protectedOutput_STORE_VALUE_TIMEOUT_TIME_S*1000, TimerTypeOneShot, 0); #endif } } // something interesting obviously happened. let's report it diagReportPending = 1; } }
void tst_Benchmark_Init(void) { /* Set up benchmark timer (timer1, 16bit) */ //Normal port operation, Normal mode TCCR1A = ((0<<COM1A1)|(0<<COM1A0)|(0<<COM1B1)|(0<<COM1B0)|(0<<WGM11)|(0<<WGM10)); TCCR1B = ((0<<ICNC1)|(0<<ICES1)|(0<<WGM12)|(0<<WGM13)); /* Start timer */ //prescaler /8 TCCR1B |= ((0<<CS12)|(1<<CS11)|(0<<CS10)); #if tst_Benchmark_CanSend_TestActive == 1 StdCan_Set_class(tst_Benchmark_CanSend_txMsg.Header, CAN_MODULE_CLASS_SNS); StdCan_Set_direction(tst_Benchmark_CanSend_txMsg.Header, DIRECTIONFLAG_FROM_OWNER); tst_Benchmark_CanSend_txMsg.Header.ModuleType = CAN_MODULE_TYPE_TST_DEBUG; tst_Benchmark_CanSend_txMsg.Header.ModuleId = tst_Benchmark_ID; tst_Benchmark_CanSend_txMsg.Header.Command = CAN_MODULE_CMD_DEBUG_BENCHMARK; tst_Benchmark_CanSend_txMsg.Length = 8; Timer_SetTimeout(tst_Benchmark_CanSend_TIMER, tst_Benchmark_CanSend_PERIOD_MS , TimerTypeFreeRunning, 0); #endif #if tst_Benchmark_CpuTime_TestActive == 1 StdCan_Set_class(tst_Benchmark_CpuTime_txMsg.Header, CAN_MODULE_CLASS_SNS); StdCan_Set_direction(tst_Benchmark_CpuTime_txMsg.Header, DIRECTIONFLAG_FROM_OWNER); tst_Benchmark_CpuTime_txMsg.Header.ModuleType = CAN_MODULE_TYPE_TST_DEBUG; tst_Benchmark_CpuTime_txMsg.Header.ModuleId = tst_Benchmark_ID; tst_Benchmark_CpuTime_txMsg.Header.Command = CAN_MODULE_CMD_DEBUG_BENCHMARK; tst_Benchmark_CpuTime_txMsg.Length = 8; Timer_SetTimeout(tst_Benchmark_CpuTime_SEND_TIMER, tst_Benchmark_CpuTime_SEND_PERIOD_MS , TimerTypeFreeRunning, 0); #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 linearAct_cleanUp (uint8_t dummy) { if (stopfactor) { stopfactor = 0; // Add number of pulses if (direction == UP) { pulses += TCNT1; // adjust official counter // keep track on how far beyond target we have moved if (TCNT1 > OCR1A) latest_stop_pulses_up = (uint8_t) TCNT1 - OCR1A; else latest_stop_pulses_up = 0; } else { pulses -= TCNT1; // keep track on how far beyond target we have moved if (TCNT1 > OCR1A) latest_stop_pulses_down = (uint8_t) TCNT1 - OCR1A; else latest_stop_pulses_down = 0; } TCCR1B = 0; TCNT1 = 0; // reset counter before next run direction = 0; eeprom_write_word_crc(EEDATA16.pulses_ee, pulses, WITH_CRC); //printf("In linearAct_cleanUp\n"); Timer_SetTimeout(act_linearAct_TIMER_report, act_linearAct_ReportInterval*1000 , TimerTypeFreeRunning, 0); linearAct_sendPosition(0); } }
void sns_BusVoltage_HandleMessage(StdCan_Msg_t *rxMsg) { if ( StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS && StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER && rxMsg->Header.ModuleType == CAN_MODULE_TYPE_SNS_BUSVOLTAGE && rxMsg->Header.ModuleId == sns_BusVoltage_ID) { switch (rxMsg->Header.Command) { case CAN_MODULE_CMD_GLOBAL_REPORT_INTERVAL: if (rxMsg->Length > 0) { sns_BusVoltage_ReportInterval = rxMsg->Data[0]; Timer_SetTimeout(sns_BusVoltage_TIMER, sns_BusVoltage_ReportInterval*1000 , TimerTypeFreeRunning, 0); } 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_BUSVOLTAGE; txMsg.Header.ModuleId = sns_BusVoltage_ID; txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_REPORT_INTERVAL; txMsg.Length = 1; txMsg.Data[0] = sns_BusVoltage_ReportInterval; StdCan_Put(&txMsg); break; } } }
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 tst_CoreLED_Init(void) { /// Initialize hardware etc here Timer_SetTimeout(tst_CoreLED_Blink_TIMER, tst_CoreLED_Blink_Interval , TimerTypeFreeRunning, 0); gpio_set_out(tst_CoreLED_PIN); }
void linearAct_stop(void) { gpio_clr_pin(RELAY_ON); _delay_ms(10); gpio_clr_pin(RELAY_DIR); stopfactor = 1; // start stop-timer Timer_SetTimeout(act_linearAct_TIMER_stop, stopTimeout , TimerTypeOneShot, linearAct_cleanUp); //printf("In linearAct_stop\n"); }
void sns_inputAnalog_Init(void) { #ifdef sns_inputAnalog_USEEEPROM if (EEDATA_OK) { /* Use stored data to set initial values for the module */ for (uint8_t i=0; i<sns_inputAnalog_NUM_SUPPORTED; i++) { //eeprom_write_block( &sns_inputAnalog_Config[i], &eeprom_sns_inputAnalog, sizeof(sns_inputAnalog_Config)*i ); eeprom_read_block( &sns_inputAnalog_Config[i], &eeprom_sns_inputAnalog+sizeof(sns_inputAnalog_Config)*i, sizeof(sns_inputAnalog_Config) ); } } else { /* The CRC of the EEPROM is not correct, store default values and update CRC */ for (uint8_t i=0; i<sns_inputAnalog_NUM_SUPPORTED; i++) { sns_inputAnalog_Config[i].LowTh=50; //Config, low level threshold voltage sns_inputAnalog_Config[i].HighTh=100; //Config, high level threshold voltage sns_inputAnalog_Config[i].Periodicity=5000+i*100; //Config, periodicity sns_inputAnalog_Config[i].Type=CAN_MODULE_ENUM_INPUTANALOG_ANALOGCONFIG_SETTING_PERIODICMEASURE; //Config, if sensor is of type periodic or digital input sns_inputAnalog_Config[i].PullupEnable=CAN_MODULE_ENUM_INPUTANALOG_ANALOGCONFIG_PULLUP_DISABLE; //Config, if the pullup should be enabled sns_inputAnalog_Config[i].RefEnable=CAN_MODULE_ENUM_INPUTANALOG_ANALOGCONFIG_REFERENCE_DISABLE; //Config, if the reference to GND should be enabled #if ((__AVR_LIBC_MAJOR__ == 1 && __AVR_LIBC_MINOR__ == 6 && __AVR_LIBC_REVISION__ >= 7)||(__AVR_LIBC_MAJOR__ == 1 && __AVR_LIBC_MINOR__ > 6)||__AVR_LIBC_MAJOR__ > 1) eeprom_update_block( &sns_inputAnalog_Config[i], &eeprom_sns_inputAnalog+sizeof(sns_inputAnalog_Config)*i, sizeof(sns_inputAnalog_Config) ); #else eeprom_write_block( &sns_inputAnalog_Config[i], &eeprom_sns_inputAnalog+sizeof(sns_inputAnalog_Config)*i, sizeof(sns_inputAnalog_Config) ); #warning Using old version of AVRlibc, does not support eeprom_update-functions #endif } EEDATA_UPDATE_CRC; } #endif #if sns_inputAnalog_ENABLE_PCA95xx==1 Pca95xx_Init(0); #endif /* Initiate ADC */ ADC_Init(); /* Start timer for reading inputs */ Timer_SetTimeout(sns_inputAnalog_TIMER, sns_inputAnalog_POLL_PERIOD_MS , TimerTypeFreeRunning, 0); /* Set pullups according config */ setPullups(); /* Set gnd references according config */ setReferences(); }
void sns_rfid_Process(void) { uint8_t version; uint32_t id; if( 0 == rfid_fetch( &version, &id ) ) { if( !sns_rfid_got_card ) { sns_rfid_got_card = 1; sns_rfid_card.id = id; sns_rfid_card.version = version; // Card arrived sns_rfid_HandleCardEvent(1); } Timer_SetTimeout(sns_rfid_CARD_LEFT_TIMER, sns_rfid_CARD_LEFT_TIME, TimerTypeOneShot, 0); } if ( Timer_Expired(sns_rfid_CARD_LEFT_TIMER) ) { sns_rfid_got_card = 0; // Card left sns_rfid_HandleCardEvent(0); } }
void act_protectedOutput_HandleMessage(StdCan_Msg_t *rxMsg) { if ( StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_ACT && StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER && rxMsg->Header.ModuleType == CAN_MODULE_TYPE_SNS_PROTECTEDOUTPUT && rxMsg->Header.ModuleId == act_protectedOutput_ID) { switch (rxMsg->Header.Command) { case CAN_MODULE_CMD_PHYSICAL_SETPIN: /* * This message is used to activate/deactivate * an output channel. */ if (rxMsg->Length == 2) { uint8_t channel = rxMsg->Data[0]; if (channel >= 0 && channel < act_protectedOutput_CH_COUNT) { chTargetState[channel] = rxMsg->Data[1]; readDiagPin(); #if act_protectedOutput_FORCED_RETRIES == 1 updateOutput(1); #else updateOutput(0); #endif #if act_protectedOutput_EEPROM_ENABLED == 1 // output states changed, store to EE after this timer delay Timer_SetTimeout(act_protectedOutput_STORE_VALUE_TIMEOUT, act_protectedOutput_STORE_VALUE_TIMEOUT_TIME_S*1000, TimerTypeOneShot, 0); #endif } StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER); rxMsg->Length = 2; while (StdCan_Put(rxMsg) != StdCan_Ret_OK); } break; } } }
void act_linearAct_HandleMessage(StdCan_Msg_t *rxMsg) { uint16_t i; if ( StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_ACT && StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER && rxMsg->Header.ModuleType == CAN_MODULE_TYPE_ACT_LINEARACT && rxMsg->Header.ModuleId == act_linearAct_ID) { switch (rxMsg->Header.Command) { case CAN_MODULE_CMD_GLOBAL_REPORT_INTERVAL: act_linearAct_ReportInterval = rxMsg->Data[0]; Timer_SetTimeout(act_linearAct_TIMER_report, act_linearAct_ReportInterval*1000 , TimerTypeFreeRunning, 0); break; case CAN_MODULE_CMD_LINEARACT_POSITION: if (rxMsg->Length >= 3) { // remain forward compatible by allowing longer messages, but not shorter if (rxMsg->Data[2] != 0) { Timer_SetTimeout(act_linearAct_TIMER_report, 750 , TimerTypeFreeRunning, 0); linearAct_setPosition(((uint16_t)rxMsg->Data[0] << 8) + rxMsg->Data[1]); } else { // Data[2] signals moving speed, where zero stands for stop and also triggers transmission of limits linearAct_stop(); // wait until complete stop (with timeout) i = 0; do { if (direction == 0) break; _delay_ms(10); i++; } while (i < 200); linearAct_sendPosition(0); linearAct_sendLimits(0); } } break; case CAN_MODULE_CMD_LINEARACT_LIMITS: if (rxMsg->Length == 8) { min = (rxMsg->Data[0] << 8) + rxMsg->Data[1]; low = (rxMsg->Data[2] << 8) + rxMsg->Data[3]; high = (rxMsg->Data[4] << 8) + rxMsg->Data[5]; max = (rxMsg->Data[6] << 8) + rxMsg->Data[7]; // pulses are already saved. eeprom_write_word_crc(EEDATA16.pulses_ee, 1200, WITHOUT_CRC); eeprom_write_word_crc(EEDATA16.min_ee, min, WITHOUT_CRC); eeprom_write_word_crc(EEDATA16.low_ee, low, WITHOUT_CRC); eeprom_write_word_crc(EEDATA16.high_ee, high, WITHOUT_CRC); eeprom_write_word_crc(EEDATA16.max_ee, max, WITHOUT_CRC); EEDATA_UPDATE_CRC; } break; case CAN_MODULE_CMD_LINEARACT_CALIBRATION: // set limits to absolute maximum to allow full movement min = 0; low = 0; high = 0xFFFF; max = 0xFFFF; if (rxMsg->Length >= 2) { // optional: set start position. Note! This will definitly ruin the old calibration. pulses = (rxMsg->Data[0] << 8) + rxMsg->Data[1]; } break; default: break; } } }
void act_protectedOutput_Process() { #if act_protectedOutput_EEPROM_ENABLED == 1 if (Timer_Expired(act_protectedOutput_STORE_VALUE_TIMEOUT)) { #if act_protectedOutput_CH_COUNT >= 1 eeprom_write_byte_crc(EEDATA.ch0, chTargetState[0], WITHOUT_CRC); #endif #if act_protectedOutput_CH_COUNT >= 2 eeprom_write_byte_crc(EEDATA.ch1, chTargetState[1], WITHOUT_CRC); #endif #if act_protectedOutput_CH_COUNT >= 3 eeprom_write_byte_crc(EEDATA.ch2, chTargetState[2], WITHOUT_CRC); #endif #if act_protectedOutput_CH_COUNT >= 4 eeprom_write_byte_crc(EEDATA.ch3, chTargetState[3], WITHOUT_CRC); #endif EEDATA_UPDATE_CRC; } #endif // shall we retry to set target output state? if (Timer_Expired(act_protectedOutput_RETRY_TIMER)) { // read DIAG pin again and update outputs accordingly readDiagPin(); #if act_protectedOutput_FORCED_RETRIES == 1 // forced retry to set output states, regardless of DIAG updateOutput(1); // read DIAG pin again and hope we have a better flag readDiagPin(); #else // if DIAG allows it, retry to set the output states updateOutput(0); #endif if (diagState == DIAG_ASSERTED) { // if DIAG was still asserted, initiate another timer run Timer_SetTimeout(act_protectedOutput_RETRY_TIMER, act_protectedOutput_RETRY_TIMER_TIME_S*1000, TimerTypeOneShot, 0); } else { // things went back to normal. report this diagReportPending = 1; } } // shall we report diag status to CAN? if (diagReportPending) { 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_PROTECTEDOUTPUT; txMsg.Header.ModuleId = act_protectedOutput_ID; txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_PINSTATUS; txMsg.Length = 2; txMsg.Data[0] = 0; //TODO: add support for more channels // we follow the standard SNS_INPUT format, but the data corresponds to the "health" rather than physical level if (diagState == DIAG_NORMAL) { txMsg.Data[1] = 1; //healthy, target output state stable } else { txMsg.Data[1] = 0; //not healthy, DIAG pin ASSERTED, not in target output state } while (StdCan_Put(&txMsg) != StdCan_Ret_OK); diagReportPending = 0; } }
void sns_counter_HandleMessage(StdCan_Msg_t *rxMsg) { if ( StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS && StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER && rxMsg->Header.ModuleType == CAN_MODULE_TYPE_SNS_COUNTER && rxMsg->Header.ModuleId == sns_counter_ID) { 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_COUNTER; txMsg.Header.ModuleId = sns_counter_ID; switch (rxMsg->Header.Command) { case CAN_MODULE_CMD_GLOBAL_REPORT_INTERVAL: if (rxMsg->Length > 0) { sns_counter_ReportInterval = rxMsg->Data[0]; Timer_SetTimeout(sns_counter_SEND_TIMER, sns_counter_ReportInterval*1000 , TimerTypeFreeRunning, 0); } txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_REPORT_INTERVAL; txMsg.Length = 1; txMsg.Data[0] = sns_counter_ReportInterval; StdCan_Put(&txMsg); break; case CAN_MODULE_CMD_COUNTER_SETCOUNTER: if (rxMsg->Length != 5) break; if ( #ifdef sns_counter_CH0 (rxMsg->Data[0] != 0) && #endif #ifdef sns_counter_CH1 (rxMsg->Data[0] != 1) && #endif #ifdef sns_counter_CH2 (rxMsg->Data[0] != 2) && #endif #ifdef sns_counter_CH3 (rxMsg->Data[0] != 3) && #endif #ifdef sns_counter_CH4 (rxMsg->Data[0] != 4) && #endif #ifdef sns_counter_CH5 (rxMsg->Data[0] != 5) && #endif #ifdef sns_counter_CH6 (rxMsg->Data[0] != 6) && #endif #ifdef sns_counter_CH7 (rxMsg->Data[0] != 7) && #endif 1) break; Count[rxMsg->Data[0]] = rxMsg->Data[4]; Count[rxMsg->Data[0]] += ((uint32_t)rxMsg->Data[3])<<8; Count[rxMsg->Data[0]] += ((uint32_t)rxMsg->Data[2])<<16; Count[rxMsg->Data[0]] += ((uint32_t)rxMsg->Data[1])<<24; txMsg.Header.Command = CAN_MODULE_CMD_COUNTER_SETCOUNTER; txMsg.Length = 5; txMsg.Data[0] = rxMsg->Data[0]; // CH txMsg.Data[4] = (uint8_t)Count[rxMsg->Data[0]] & 0xff; txMsg.Data[3] = (uint8_t)(Count[rxMsg->Data[0]] >> 8) & 0xff; txMsg.Data[2] = (uint8_t)(Count[rxMsg->Data[0]] >> 16) & 0xff; txMsg.Data[1] = (uint8_t)(Count[rxMsg->Data[0]] >> 24) & 0xff; StdCan_Put(&txMsg); 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 sns_irTransmit_Process(void) { if (sns_irTransmit_state == sns_irTransmit_STATE_IDLE) { } else if (sns_irTransmit_state == sns_irTransmit_STATE_START_TRANSMIT) { if (expandProtocol(sns_irTransmit_txbuffer, &sns_irTransmit_length, &sns_irTransmit_proto) == IR_OK) { if (IrTransceiver_Transmit(sns_irTransmit_txbuffer, sns_irTransmit_length, sns_irTransmit_proto.modfreq) == IR_OK) { sns_irTransmit_state = sns_irTransmit_STATE_TRANSMITTING; } else { sns_irTransmit_state = sns_irTransmit_STATE_IDLE; } } else { sns_irTransmit_state = sns_irTransmit_STATE_IDLE; } } else if (sns_irTransmit_state == sns_irTransmit_STATE_TRANSMITTING){ //polla om den är klar, om klar gå till sns_irTransmit_STATE_START_PAUSE if (IrTransceiver_Transmit_Poll() != IR_NOT_FINISHED) { sns_irTransmit_state = sns_irTransmit_STATE_START_PAUSE; } } else if (sns_irTransmit_state == sns_irTransmit_STATE_START_PAUSE) { if (sns_irTransmit_repeatCount < sns_irTransmit_proto.repeats) { sns_irTransmit_repeatCount++; } Timer_SetTimeout(sns_irTransmit_REPEAT_TIMER, sns_irTransmit_proto.timeout, TimerTypeOneShot, 0); if (sns_irTransmit_proto.framecnt != 255) { sns_irTransmit_proto.framecnt++; } sns_irTransmit_state = sns_irTransmit_STATE_PAUSING; } else if (sns_irTransmit_state == sns_irTransmit_STATE_PAUSING) { //när timeout har gått (timebase) så gå till sns_irTransmit_STATE_START_TRANSMIT if (Timer_Expired(sns_irTransmit_REPEAT_TIMER)) { sns_irTransmit_state = sns_irTransmit_STATE_START_TRANSMIT; } if (sns_irTransmit_stop == 1 && sns_irTransmit_repeatCount >= sns_irTransmit_proto.repeats) { sns_irTransmit_state = sns_irTransmit_STATE_STOP; } } else if (sns_irTransmit_state == sns_irTransmit_STATE_STOP) { sns_irTransmit_stop = 0; sns_irTransmit_proto.timeout = 0; sns_irTransmit_proto.framecnt = 0; sns_irTransmit_repeatCount = 0; sns_irTransmit_state = sns_irTransmit_STATE_IDLE; } }
void act_output_HandleMessage(StdCan_Msg_t *rxMsg) { if ( StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_ACT && StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER && rxMsg->Header.ModuleType == CAN_MODULE_TYPE_ACT_OUTPUT && rxMsg->Header.ModuleId == act_output_ID) { uint8_t index = 0; uint8_t value = 0; switch (rxMsg->Header.Command) { case CAN_MODULE_CMD_PHYSICAL_SETPIN: index = 0; if (rxMsg->Length == 2) { #ifdef act_output_CH0 if (rxMsg->Data[0] == 0) { chnValue[index] = rxMsg->Data[1]; #if act_output_CH0PCA95xxIO==0 gpio_set_statement(chnValue[index],act_output_CH0); #else Pca95xx_set_statement(chnValue[index],act_output_CH0); #endif } index++; #endif #ifdef act_output_CH1 if (rxMsg->Data[0] == 1) { chnValue[index] = rxMsg->Data[1]; #if act_output_CH1PCA95xxIO==0 gpio_set_statement(chnValue[index],act_output_CH1); #else Pca95xx_set_statement(chnValue[index],act_output_CH1); #endif } index++; #endif #ifdef act_output_CH2 if (rxMsg->Data[0] == 2) { chnValue[index] = rxMsg->Data[1]; #if act_output_CH2PCA95xxIO==0 gpio_set_statement(chnValue[index],act_output_CH2); #else Pca95xx_set_statement(chnValue[index],act_output_CH2); #endif } index++; #endif #ifdef act_output_CH3 if (rxMsg->Data[0] == 3) { chnValue[index] = rxMsg->Data[1]; #if act_output_CH3PCA95xxIO==0 gpio_set_statement(chnValue[index],act_output_CH3); #else Pca95xx_set_statement(chnValue[index],act_output_CH3); #endif } index++; #endif #ifdef act_output_CH4 if (rxMsg->Data[0] == 4) { chnValue[index] = rxMsg->Data[1]; #if act_output_CH4PCA95xxIO==0 gpio_set_statement(chnValue[index],act_output_CH4); #else Pca95xx_set_statement(chnValue[index],act_output_CH4); #endif } index++; #endif #ifdef act_output_CH5 if (rxMsg->Data[0] == 5) { chnValue[index] = rxMsg->Data[1]; #if act_output_CH5PCA95xxIO==0 gpio_set_statement(chnValue[index],act_output_CH5); #else Pca95xx_set_statement(chnValue[index],act_output_CH5); #endif } index++; #endif #ifdef act_output_CH6 if (rxMsg->Data[0] == 6) { chnValue[index] = rxMsg->Data[1]; #if act_output_CH6PCA95xxIO==0 gpio_set_statement(chnValue[index],act_output_CH6); #else Pca95xx_set_statement(chnValue[index],act_output_CH6); #endif } index++; #endif #ifdef act_output_CH7 if (rxMsg->Data[0] == 7) { chnValue[index] = rxMsg->Data[1]; #if act_output_CH7PCA95xxIO==0 gpio_set_statement(chnValue[index],act_output_CH7); #else Pca95xx_set_statement(chnValue[index],act_output_CH7); #endif } index++; #endif #ifdef act_output_CH8 if (rxMsg->Data[0] == 8) { chnValue[index] = rxMsg->Data[1]; #if act_output_CH8PCA95xxIO==0 gpio_set_statement(chnValue[index],act_output_CH8); #else Pca95xx_set_statement(chnValue[index],act_output_CH8); #endif } index++; #endif #ifdef act_output_CH9 if (rxMsg->Data[0] == 9) { chnValue[index] = rxMsg->Data[1]; #if act_output_CH9PCA95xxIO==0 gpio_set_statement(chnValue[index],act_output_CH9); #else Pca95xx_set_statement(chnValue[index],act_output_CH9); #endif } index++; #endif Timer_SetTimeout(act_output_STORE_VALUE_TIMEOUT, act_output_STORE_VALUE_TIMEOUT_TIME_S*1000, TimerTypeOneShot, 0); StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER); rxMsg->Length = 2; while (StdCan_Put(rxMsg) != StdCan_Ret_OK); } else { index = 0; #ifdef act_output_CH0 if (rxMsg->Data[0] == 0) {value = chnValue[index];} index++; #endif #ifdef act_output_CH1 if (rxMsg->Data[0] == 1) {value = chnValue[index];} index++; #endif #ifdef act_output_CH2 if (rxMsg->Data[0] == 2) {value = chnValue[index];} index++; #endif #ifdef act_output_CH3 if (rxMsg->Data[0] == 3) {value = chnValue[index];} index++; #endif #ifdef act_output_CH4 if (rxMsg->Data[0] == 4) {value = chnValue[index];} index++; #endif #ifdef act_output_CH5 if (rxMsg->Data[0] == 5) {value = chnValue[index];} index++; #endif #ifdef act_output_CH6 if (rxMsg->Data[0] == 6) {value = chnValue[index];} index++; #endif #ifdef act_output_CH7 if (rxMsg->Data[0] == 7) {value = chnValue[index];} index++; #endif #ifdef act_output_CH8 if (rxMsg->Data[0] == 8) {value = chnValue[index];} index++; #endif #ifdef act_output_CH9 if (rxMsg->Data[0] == 9) {value = chnValue[index];} index++; #endif rxMsg->Data[1] = value; StdCan_Set_direction(rxMsg->Header, DIRECTIONFLAG_FROM_OWNER); rxMsg->Length = 2; while (StdCan_Put(rxMsg) != StdCan_Ret_OK); } break; } } }
int main( void ) { // Enable interrupts as early as possible sei(); Timer_Init(); // Set as outputs and stop rotor DDRD |= (1 << PD1)|(1 << PD2); PORTD &= ~((1 << PD1)|(1 << PD2)); // Setup ADC initAdcFeedback(); Can_Message_t txMsg; txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID); txMsg.DataLength = 4; txMsg.RemoteFlag = 0; txMsg.ExtendedFlag = 1; txMsg.Data.words[0] = APP_TYPE; txMsg.Data.words[1] = APP_VERSION; // Set up callback for CAN reception BIOS_CanCallback = &can_receive; // Send CAN_NMT_APP_START BIOS_CanSend(&txMsg); // Read calibration value from eeprom azimuthCalibration = eeprom_read_word( CALIBRATE_AZIMUTH ); // Timer for reading position feedback Timer_SetTimeout(0, 100, TimerTypeFreeRunning, 0); Timer_SetTimeout(1, 1000, TimerTypeFreeRunning, 0); sendStatus( STATUS ); while (1) { if (Timer_Expired(0)) { // Periodicly read antennas position readFeedback(); } if (Timer_Expired(1)) { sendStatus(STATUS); } if (rxMsgFull) { switch (rxMsg.Id){ case MSG_CAL_SET: // Set calibration value if( 2 == rxMsg.DataLength ){ calibration( SET, rxMsg.Data.words[0] ); } break; case MSG_CAL_GET: // Get calibration value if( 0 == rxMsg.DataLength ){ txMsg.Id = MSG_CAL_RET; txMsg.DataLength = 2; txMsg.Data.words[0] = calibration( GET, 0 ); BIOS_CanSend(&txMsg); } break; case MSG_ABS: // Start turning to absolute position if( 2 == rxMsg.DataLength ){ } break; case MSG_REL: // Start turning to relative position if( 2 == rxMsg.DataLength ){ } break; case MSG_START: // Start turning if( 1 == rxMsg.DataLength ){ // First data byte decides direction controlRelay( rxMsg.Data.bytes[0] ); } break; case MSG_STOP: // Stop turning //if( 1 == rxMsg.DataLength ){ controlRelay( ROTATE_STOP ); //} break; case MSG_STATUS: // Get position if( 0 == rxMsg.DataLength ){ sendStatus(STATUS); } break; default: break; } rxMsgFull = 0; // } } return 0; }
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_BusVoltage_Init(void) { ///TODO: Initialize hardware etc here ADC_Init(); Timer_SetTimeout(sns_BusVoltage_TIMER, sns_BusVoltage_ReportInterval*1000 , TimerTypeFreeRunning, 0); }