void sns_power_timer_callback(uint8_t timer) { StdCan_Msg_t txMsg; StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS); ///TODO: Change this to the actual class type StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER); txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_POWER; ///TODO: Change this to the actual module type txMsg.Header.ModuleId = sns_power_ID; txMsg.Header.Command = CAN_MODULE_CMD_POWER_AVGPOWER; txMsg.Length = 2; cli(); txMsg.Data[0] = (uint8_t)((avgCounter>>8) & 0xff); txMsg.Data[1] = (uint8_t)(avgCounter & 0xff); avgCounter = 0; sei(); while (StdCan_Put(&txMsg) != StdCan_Ret_OK); #ifdef POWER_SNS_PIN_ch2 txMsg.Length = 2; txMsg.Header.ModuleId = sns_power_ID_ch2; cli(); txMsg.Data[0] = (uint8_t)((avgCounter_ch2>>8) & 0xff); txMsg.Data[1] = (uint8_t)(avgCounter_ch2 & 0xff); avgCounter = 0; sei(); while (StdCan_Put(&txMsg) != StdCan_Ret_OK); #endif }
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_VoltageCurrent_Process(void) { if (Timer_Expired(sns_VoltageCurrent_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_VoltageCurrent_ID; txMsg.Length = 3; uint16_t ADvalue; #ifdef sns_VoltageCurrent0AD if (VoltageCurrentChannelToSend==0) { ADvalue = ADC_Get(sns_VoltageCurrent0AD); ADvalue = ADvalue * sns_VoltageCurrent0Factor; #if sns_VoltageCurrent0VorA==0 txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_VOLTAGE; #else txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_CURRENT; #endif txMsg.Data[0] = 0; txMsg.Data[1] = (ADvalue>>(sns_VoltageCurrent0Scale-6+8))&0xff; txMsg.Data[2] = (ADvalue>>(sns_VoltageCurrent0Scale-6))&0xff; while (StdCan_Put(&txMsg) != StdCan_Ret_OK) {} } #endif #ifdef sns_VoltageCurrent1AD if (VoltageCurrentChannelToSend==1) { ADvalue = ADC_Get(sns_VoltageCurrent1AD); ADvalue = ADvalue * sns_VoltageCurrent1Factor; #if sns_VoltageCurrent1VorA==0 txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_VOLTAGE; #else txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_CURRENT; #endif txMsg.Data[0] = 1; txMsg.Data[1] = (ADvalue>>(sns_VoltageCurrent1Scale-6+8))&0xff; txMsg.Data[2] = (ADvalue>>(sns_VoltageCurrent1Scale-6))&0xff; while (StdCan_Put(&txMsg) != StdCan_Ret_OK) {} }
void chn_ChnMaster_UpdateChannel( uint16_t channel_id, uint16_t value ) { StdCan_Msg_t txMsg; txMsg.Id = 0; StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_CHN); txMsg.Id |= channel_id << 8; txMsg.Id |= chn_ChnMaster_ID; txMsg.Length = 2; txMsg.Data[0] = (value >>8)&0xFF; txMsg.Data[1] = (value )&0xFF; while( StdCan_Put(&txMsg) != StdCan_Ret_OK ); }
void send_debug(uint16_t *buffer, uint8_t len) { /* the protocol is unknown so the raw ir-data is sent, makes it easier to develop a new protocol */ StdCan_Set_class(irTxMsg.Header, CAN_MODULE_CLASS_SNS); StdCan_Set_direction(irTxMsg.Header, DIRECTIONFLAG_FROM_OWNER); irTxMsg.Length = 8; irTxMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_IRRECEIVE; irTxMsg.Header.ModuleId = sns_irReceive_ID; irTxMsg.Header.Command = CAN_MODULE_CMD_IRRECEIVE_IRRAW; for (uint8_t i = 0; i < len>>2; i++) { uint8_t index = i<<2; irTxMsg.Data[0] = (buffer[index]>>8)&0xff; irTxMsg.Data[1] = (buffer[index]>>0)&0xff; irTxMsg.Data[2] = (buffer[index+1]>>8)&0xff; irTxMsg.Data[3] = (buffer[index+1]>>0)&0xff; irTxMsg.Data[4] = (buffer[index+2]>>8)&0xff; irTxMsg.Data[5] = (buffer[index+2]>>0)&0xff; irTxMsg.Data[6] = (buffer[index+3]>>8)&0xff; irTxMsg.Data[7] = (buffer[index+3]>>0)&0xff; /* buffers will be filled when sending more than 2-3 messages, so retry until sent */ while (StdCan_Put(&irTxMsg) != StdCan_Ret_OK) {} } uint8_t lastpacketcnt = len&0x03; if (lastpacketcnt > 0) { irTxMsg.Length = lastpacketcnt<<1; for (uint8_t i = 0; i < lastpacketcnt; i++) { irTxMsg.Data[i<<1] = (buffer[(len&0xfc)|i]>>8)&0xff; irTxMsg.Data[(i<<1)+1] = (buffer[(len&0xfc)|i]>>0)&0xff; } /* buffers will be filled when sending more than 2-3 messages, so retry until sent */ while (StdCan_Put(&irTxMsg) != StdCan_Ret_OK) {} }
void sns_counter_send_counter(uint8_t index) { 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; txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_COUNTER; txMsg.Length = 5; txMsg.Data[0] = index; txMsg.Data[4] = (uint8_t)Count[index] & 0xff; txMsg.Data[3] = (uint8_t)(Count[index] >> 8) & 0xff; txMsg.Data[2] = (uint8_t)(Count[index] >> 16) & 0xff; txMsg.Data[1] = (uint8_t)(Count[index] >> 24) & 0xff; while (StdCan_Put(&txMsg) != StdCan_Ret_OK); }
void sendPID(void) { if (eeprom_read_byte(EEDATA.actuatorModuleType) != 0) { StdCan_Msg_t txMsg; StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT); ///TODO: Change this to the actual class type StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_TO_OWNER); txMsg.Header.ModuleType = eeprom_read_byte(EEDATA.actuatorModuleType); ///TODO: Change this to the actual module type txMsg.Header.ModuleId = eeprom_read_byte(EEDATA.actuatorModuleId); txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_PWM; txMsg.Length = 3; txMsg.Data[0] = eeprom_read_byte(EEDATA.actuatorId); //uint16_t tempPWM =(uint16_t) (pwmValue*10000); txMsg.Data[1] = ( ((uint16_t)outputValue)>>8)&0xff; txMsg.Data[2] = ( ((uint16_t)outputValue))&0xff; while (StdCan_Put(&txMsg) != StdCan_Ret_OK); }
void sns_rfid_HandleCardEvent( uint8_t event ) { StdCan_Msg_t txMsg; gpio_set_statement( event, sns_rfid_STATUS_LED ); StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS); ///TODO: Change this to the actual class type StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER); txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_RFID; ///TODO: Change this to the actual module type txMsg.Header.ModuleId = sns_rfid_ID; txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST; txMsg.Length = 6; txMsg.Data[0] = event; txMsg.Data[1] = sns_rfid_card.version; txMsg.Data[2] = sns_rfid_card.id&0xff; txMsg.Data[3] = (sns_rfid_card.id>>8)&0xff; txMsg.Data[4] = (sns_rfid_card.id>>16)&0xff; txMsg.Data[5] = (sns_rfid_card.id>>24)&0xff; while (StdCan_Put(&txMsg) != StdCan_Ret_OK); }
void act_output_List(uint8_t ModuleSequenceNumber) { StdCan_Msg_t txMsg; StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT); StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER); txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_OUTPUT; txMsg.Header.ModuleId = act_output_ID; txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST; txMsg.Length = 6; uint32_t HwId=BIOS_GetHwId(); txMsg.Data[0] = HwId&0xff; txMsg.Data[1] = (HwId>>8)&0xff; txMsg.Data[2] = (HwId>>16)&0xff; txMsg.Data[3] = (HwId>>24)&0xff; txMsg.Data[4] = NUMBER_OF_MODULES; txMsg.Data[5] = ModuleSequenceNumber; while (StdCan_Put(&txMsg) != StdCan_Ret_OK); }
void sns_BusVoltage_Process(void) { ///TODO: Stuff that needs doing is done here if (Timer_Expired(sns_BusVoltage_TIMER)) { uint16_t busVoltage = ADC_Get(BUSVOLTAGEAD); busVoltage = (busVoltage & 0x03ff) * ADC_FACTOR; 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_PHYSICAL_VOLTAGE; txMsg.Length = 3; txMsg.Data[0] = 0; txMsg.Data[1] = (busVoltage>>(ADC_SCALE-6+8))&0xff; txMsg.Data[2] = (busVoltage>>(ADC_SCALE-6))&0xff; StdCan_Put(&txMsg); } }
void tst_CoreLED_List(uint8_t ModuleSequenceNumber) { StdCan_Msg_t txMsg; StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_TST); StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER); txMsg.Header.ModuleType = CAN_MODULE_TYPE_TST_DEBUG; txMsg.Header.ModuleId = tst_CoreLED_ID; txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST; txMsg.Length = 6; txMsg.Data[0] = NODE_HW_ID_BYTE0; txMsg.Data[1] = NODE_HW_ID_BYTE1; txMsg.Data[2] = NODE_HW_ID_BYTE2; txMsg.Data[3] = NODE_HW_ID_BYTE3; txMsg.Data[4] = NUMBER_OF_MODULES; txMsg.Data[5] = ModuleSequenceNumber; while (StdCan_Put(&txMsg) != StdCan_Ret_OK); }
void linearAct_sendPosition(uint8_t dummy) { StdCan_Msg_t txMsg; uint16_t w_tmp; StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT); StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER); txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_LINEARACT; txMsg.Header.ModuleId = act_linearAct_ID; txMsg.Header.Command = CAN_MODULE_CMD_LINEARACT_POSITION; txMsg.Length = 3; w_tmp = linearAct_getPosition(); txMsg.Data[0] = w_tmp >> 8; txMsg.Data[1] = w_tmp; txMsg.Data[2] = direction; //while (StdCan_Put(&txMsg) != StdCan_Ret_OK); StdCan_Put(&txMsg); // No problem if we miss one }
void sns_rfid_List(uint8_t ModuleSequenceNumber) { StdCan_Msg_t txMsg; StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS); ///TODO: Change this to the actual class type StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER); txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_RFID; ///TODO: Change this to the actual module type txMsg.Header.ModuleId = sns_rfid_ID; txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST; txMsg.Length = 6; uint32_t HwId=BIOS_GetHwId(); txMsg.Data[0] = HwId&0xff; txMsg.Data[1] = (HwId>>8)&0xff; txMsg.Data[2] = (HwId>>16)&0xff; txMsg.Data[3] = (HwId>>24)&0xff; txMsg.Data[4] = NUMBER_OF_MODULES; txMsg.Data[5] = ModuleSequenceNumber; while (StdCan_Put(&txMsg) != StdCan_Ret_OK); }
void linearAct_sendLimits (uint8_t dummy) { StdCan_Msg_t txMsg; StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT); StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER); txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_LINEARACT; txMsg.Header.ModuleId = act_linearAct_ID; txMsg.Header.Command = CAN_MODULE_CMD_LINEARACT_LIMITS; txMsg.Length = 8; txMsg.Data[0] = min >> 8; txMsg.Data[1] = min; txMsg.Data[2] = low >> 8; txMsg.Data[3] = low; txMsg.Data[4] = high >> 8; txMsg.Data[5] = high; txMsg.Data[6] = max >> 8; txMsg.Data[7] = max; while (StdCan_Put(&txMsg) != StdCan_Ret_OK); }
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 sns_inputAnalog_Process(void) { /* When the timer has overflowed the AD channels shall be read */ if (Timer_Expired(sns_inputAnalog_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_INPUTANALOG; txMsg.Header.ModuleId = sns_inputAnalog_ID; uint16_t AdValue=0; /* For each channel */ for (uint8_t i=0; i<sns_inputAnalog_NUM_SUPPORTED; i++) { /* Do reading of AD channel */ switch (i) { case 0: AdValue = ADC_Get(sns_inputAnalog0AD); break; case 1: AdValue = ADC_Get(sns_inputAnalog1AD); break; case 2: AdValue = ADC_Get(sns_inputAnalog2AD); break; case 3: AdValue = ADC_Get(sns_inputAnalog3AD); break; } /* If this channel is configured as periodic transmission of voltage */ if (sns_inputAnalog_Config[i].Type == CAN_MODULE_ENUM_INPUTANALOG_ANALOGCONFIG_SETTING_PERIODICMEASURE) { /* Count periodicity */ sns_inputAnalog_Sensor[i].PeriodCnt += sns_inputAnalog_POLL_PERIOD_MS; /* If periodicity overflowed or AD value changed more than threshold since last sent value */ if (sns_inputAnalog_Sensor[i].PeriodCnt >= sns_inputAnalog_Config[i].Periodicity || MAX(AdValue,sns_inputAnalog_Sensor[i].LastSentAdVal)-MIN(AdValue,sns_inputAnalog_Sensor[i].LastSentAdVal) > sns_inputAnalog_Config[i].LowTh) //if (sns_inputAnalog_Sensor[i].PeriodCnt >= sns_inputAnalog_Config[i].Periodicity || // abs(AdValue-sns_inputAnalog_Sensor[i].LastSentAdVal) > sns_inputAnalog_Config[i].LowTh) { /* Reset periodicity counter */ sns_inputAnalog_Sensor[i].PeriodCnt = 0; /* Store value as last sent */ sns_inputAnalog_Sensor[i].LastSentAdVal = AdValue; /* send sensor value on CAN with command CAN_MODULE_CMD_PHYSICAL_VOLTAGE */ txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_VOLTAGE; txMsg.Length = 3; /* The channel should be transmitted in byte 0 */ txMsg.Data[0] = i; uint8_t analogScale = 10; /* Select parameters for AD conversion */ switch (i) { case 0: analogScale = sns_inputAnalog0Scale; AdValue = AdValue * sns_inputAnalog0Factor; break; case 1: analogScale = sns_inputAnalog1Scale; AdValue = AdValue * sns_inputAnalog1Factor; break; case 2: analogScale = sns_inputAnalog2Scale; AdValue = AdValue * sns_inputAnalog2Factor; break; case 3: analogScale = sns_inputAnalog3Scale; AdValue = AdValue * sns_inputAnalog3Factor; break; } txMsg.Data[1] = (AdValue>>(analogScale-6+8))&0xff; txMsg.Data[2] = (AdValue>>(analogScale-6))&0xff; /* Send value on CAN */ while (StdCan_Put(&txMsg) != StdCan_Ret_OK) {} } } /* If this channel is configured as digital input */ else if (sns_inputAnalog_Config[i].Type == CAN_MODULE_ENUM_INPUTANALOG_ANALOGCONFIG_SETTING_DIGITALINPUT)
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; } }
/*----------------------------------------------------------------------------- * Main Program *---------------------------------------------------------------------------*/ int main(void) { // For calibrating internal oscillator // OSCCAL = eeprom_read_byte(0); Timebase_Init(); Serial_Init(); #if MCP_CS_BIT != PB2 /* If slave select is not set as output it might change SPI hw to slave * See ch 18.3.2 (18.3 SS Pin Functionality) in ATmega48/88/168-datasheet */ DDRB |= (1<<PB2); #endif #if USE_STDCAN == 0 Can_Init(); #else StdCan_Init(0); #endif DDRC |= 1<<PC1 | 1<<PC0; PORTC |= (1<<PC1) | (1<<PC0); sei(); #if SENDTIMESTAMP == 1 #if USE_STDCAN == 0 Can_Message_t timeMsg; #else StdCan_Msg_t timeMsg; #endif uint32_t time = Timebase_CurrentTime(); uint32_t time1 = time; uint32_t unixtime = 0; #endif uint16_t rxByte; uint8_t i = 0; #if SENDTIMESTAMP == 1 #if USE_STDCAN == 0 timeMsg.ExtendedFlag = 1; timeMsg.RemoteFlag = 0; timeMsg.DataLength = 8; #else /* Jag orkar inte fixa in datan i paketen, sätter längd till 0 sålänge */ timeMsg.Length = 0; #endif timeMsg.Id = (CAN_NMT << CAN_SHIFT_CLASS) | (CAN_NMT_TIME << CAN_SHIFT_NMT_TYPE); //timeMsg.Id = 0; //Same thing, and lib's can.h is not updated. #endif #if USE_STDCAN == 1 StdCan_Msg_t rxMsg; #endif /* main loop */ while (1) { /* any new CAN messages received? */ #if USE_STDCAN == 0 if (rxMsgFull) { #else if (StdCan_Get(&rxMsg) == StdCan_Ret_OK) { #endif // Toggle activity LED PORTC ^= (1<<PC1); /* send message to CanWatcher */ uart_putc(UART_START_BYTE); uart_putc((uint8_t)rxMsg.Id); uart_putc((uint8_t)(rxMsg.Id>>8)); uart_putc((uint8_t)(rxMsg.Id>>16)); uart_putc((uint8_t)(rxMsg.Id>>24)); #if USE_STDCAN == 0 uart_putc(rxMsg.ExtendedFlag); uart_putc(rxMsg.RemoteFlag); uart_putc(rxMsg.DataLength); for (i=0; i<8; i++) { uart_putc(rxMsg.Data.bytes[i]); } #else uart_putc(1); uart_putc(0); uart_putc(rxMsg.Length); for (i=0; i<8; i++) { uart_putc(rxMsg.Data[i]); } #endif uart_putc(UART_END_BYTE); #if USE_STDCAN == 0 rxMsgFull = 0; #endif } /* any UART bytes received? */ rxByte = uart_getc(); while (rxByte != UART_NO_DATA) { /* parse byte! */ UartParseByte((uint8_t)(rxByte & 0x00FF)); /* receive next */ rxByte = uart_getc(); } #if SENDTIMESTAMP == 1 time1 = Timebase_CurrentTime(); if ((time1 - time) > 1000) { time = time1; #if USE_STDCAN == 0 timeMsg.Data.dwords[0] = ++unixtime; IncTime(); timeMsg.Data.dwords[1] = date.packed; Can_Send(&timeMsg); #else /* Jag orkar inte fixa in datan i paketen, sätter längd till 0 sålänge */ StdCan_Put(&timeMsg); #endif } #endif } return 0; }
/** * Parses an incoming UART byte by maintaining a state machine. The UART * bytes will build up a CAN message according to the protocol described here: * * http://www.arune.se/projekt/doku.php?id=homeautomation:pc-mjukvara * * @param c * The received UART byte. */ void UartParseByte(uint8_t c) { #if USE_STDCAN == 0 static Can_Message_t cm; #else static StdCan_Msg_t cm; #endif static uint8_t waitingMessage = 0; static uint32_t startTime = 0; static int8_t count = 0; /* 50ms timeout */ if (waitingMessage && Timebase_PassedTimeMillis(startTime) > 50) { waitingMessage = 0; } if (waitingMessage) { /* save start time */ startTime = Timebase_CurrentTime(); /* UART END */ if (count >= 15) { if (c == UART_END_BYTE) { PORTC ^= (1<<PC0); #if USE_STDCAN == 0 Can_Send(&cm); #else StdCan_Put(&cm); #endif } waitingMessage = 0; return; } /* data */ else if (count >= 7) { #if USE_STDCAN == 0 cm.Data.bytes[count-7] = c; #else cm.Data[count-7] = c; #endif count++; return; } /* data length */ else if (count >= 6) { #if USE_STDCAN == 0 cm.DataLength = c; #else cm.Length = c; #endif count++; return; } /* remote request flag */ else if (count >= 5) { #if USE_STDCAN == 0 cm.RemoteFlag = c; #endif count++; return; } /* extended */ else if (count >= 4) { #if USE_STDCAN == 0 cm.ExtendedFlag = c; #endif count++; return; } /* ident */ else if (count >= 0) { cm.Id += ((uint32_t)c << (count*8)); count++; return; } } if (c == UART_START_BYTE && !waitingMessage) { waitingMessage = 1; startTime = Timebase_CurrentTime(); count = 0; cm.Id = 0; return; } }
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; } } }
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_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 act_hd44780_Process(void) { #if act_hd44780_USE_AUTO_BL == 1 if (autoMode == CAN_MODULE_ENUM_HD44789_LCD_BACKLIGHT_AUTOLIGHT_ON) { #if (act_hd44780_TYPE==0) if ( OCR0A == 0) { #else if ( OCR0B == 0) { #endif } else { uint16_t Voltage = (0x3ff & ADC_Get(act_hd44780_LIGHTSENSOR_AD)); #if (act_hd44780_TYPE==0) OCR0A = Voltage/4; if (OCR0A == 0) { OCR0A = 1; } #else OCR0B = Voltage/4; if (OCR0B == 0) { OCR0B = 1; } #endif } } #endif #if (act_hd44780_TYPE==0) if (OCR0A==0 && (TCCR0A & ((1<<COM0A1)|(1<<WGM01)|(1<<WGM00))) == ((1<<COM0A1)|(1<<WGM01)|(1<<WGM00))) { TCCR0A &= ~((1<<COM0A1)|(1<<WGM01)|(1<<WGM00)); } if (OCR0A!=0 && (TCCR0A & ((1<<COM0A1)|(1<<WGM01)|(1<<WGM00))) != ((1<<COM0A1)|(1<<WGM01)|(1<<WGM00))) { TCCR0A |= (1<<COM0A1)|(1<<WGM01)|(1<<WGM00); } #endif } void act_hd44780_HandleMessage(StdCan_Msg_t *rxMsg) { StdCan_Msg_t txMsg; uint8_t n = 0; 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_HD44789 && rxMsg->Header.ModuleId == act_hd44780_ID) { switch (rxMsg->Header.Command) { case CAN_MODULE_CMD_HD44789_LCD_CLEAR: lcd_clrscr(); break; case CAN_MODULE_CMD_HD44789_LCD_CURSOR: lcd_gotoxy(rxMsg->Data[0], rxMsg->Data[1]); break; case CAN_MODULE_CMD_HD44789_LCD_TEXTAT: lcd_gotoxy(rxMsg->Data[0], rxMsg->Data[1]); for (n = 2; n < rxMsg->Length; n++) { lcd_putc((char)rxMsg->Data[n]); } break; /* case CAN_MODULE_CMD_HD44789_LCD_CLEARROW: lcd_gotoxy(0, rxMsg->Data[0]); for (n = 0; n < act_hd44780_WIDTH; n++) { lcd_putc(' '); } break; */ case CAN_MODULE_CMD_HD44789_LCD_TEXT: for (n = 0; n < rxMsg->Length; n++) { lcd_putc((char)rxMsg->Data[n]); } break; case CAN_MODULE_CMD_HD44789_LCD_SIZE: StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT); StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER); txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_HD44789; txMsg.Header.ModuleId = act_hd44780_ID; txMsg.Header.Command = CAN_MODULE_CMD_HD44789_LCD_SIZE; txMsg.Length = 2; txMsg.Data[0] = act_hd44780_WIDTH; txMsg.Data[1] = act_hd44780_HEIGHT; while (StdCan_Put(&txMsg) != StdCan_Ret_OK); break; case CAN_MODULE_CMD_HD44789_LCD_BACKLIGHT: if (rxMsg->Length > 0) { #if (act_hd44780_TYPE==0) OCR0A = rxMsg->Data[0]; #else OCR0B = rxMsg->Data[0]; #endif #if act_hd44780_USE_AUTO_BL == 1 if (rxMsg->Length == 2) { autoMode = rxMsg->Data[0]; } #endif } StdCan_Msg_t txMsg; StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_ACT); StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER); txMsg.Header.ModuleType = CAN_MODULE_TYPE_ACT_HD44789; txMsg.Header.ModuleId = act_hd44780_ID; txMsg.Header.Command = CAN_MODULE_CMD_HD44789_LCD_BACKLIGHT; txMsg.Length = 1; #if (act_hd44780_TYPE==0) txMsg.Data[0] = OCR0A; #else txMsg.Data[0] = OCR0B; #endif #if act_hd44780_USE_AUTO_BL == 1 txMsg.Data[1] = autoMode; txMsg.Length = 2; #endif while (StdCan_Put(&txMsg) != StdCan_Ret_OK); break; } } }
void tst_Benchmark_Process(void) { #if tst_Benchmark_CanSend_TestActive == 1 if (Timer_Expired(tst_Benchmark_CanSend_TIMER)) { uint16_t timer_val; uint16_t timer_val_new; for (uint8_t i=0; i < tst_Benchmark_CanSend_NumTx; i++) { tst_Benchmark_CanSend_txMsg.Data[0] = 0; // Implement avarage? Write test number? tst_Benchmark_CanSend_txMsg.Data[1] = 0; tst_Benchmark_CanSend_txMsg.Data[2] = (tst_Benchmark_CanSend_Prev>>8)&0xff; tst_Benchmark_CanSend_txMsg.Data[3] = tst_Benchmark_CanSend_Prev&0xff; tst_Benchmark_CanSend_txMsg.Data[4] = (tst_Benchmark_CanSend_Min>>8)&0xff; tst_Benchmark_CanSend_txMsg.Data[5] = tst_Benchmark_CanSend_Min&0xff; tst_Benchmark_CanSend_txMsg.Data[6] = (tst_Benchmark_CanSend_Max>>8)&0xff; tst_Benchmark_CanSend_txMsg.Data[7] = tst_Benchmark_CanSend_Max&0xff; timer_val = TCNT1; /* buffers will be filled when sending more than 2-3 messages, so retry until sent */ while (StdCan_Put(&tst_Benchmark_CanSend_txMsg) != StdCan_Ret_OK) {} timer_val_new = TCNT1; tst_Benchmark_CanSend_Prev = timer_val_new - timer_val; if (tst_Benchmark_CanSend_Min > tst_Benchmark_CanSend_Prev) { tst_Benchmark_CanSend_Min = tst_Benchmark_CanSend_Prev; } if (tst_Benchmark_CanSend_Max < tst_Benchmark_CanSend_Prev) { tst_Benchmark_CanSend_Max = tst_Benchmark_CanSend_Prev; } } } #endif #if tst_Benchmark_CpuTime_TestActive == 1 uint16_t timer_val_new = TCNT1; uint16_t timeSpent = timer_val_new - tst_Benchmark_CpuTime_timer_val; if (tst_Benchmark_CpuTime_Min > timeSpent) { tst_Benchmark_CpuTime_Min = timeSpent; } if (tst_Benchmark_CpuTime_Max < timeSpent) { tst_Benchmark_CpuTime_Max = timeSpent; } uint64_t sum = tst_Benchmark_CpuTime_Avg * 127 + (timeSpent<<8); tst_Benchmark_CpuTime_Avg = sum / 128; if (Timer_Expired(tst_Benchmark_CpuTime_SEND_TIMER)) { tst_Benchmark_CpuTime_txMsg.Data[0] = 0; // Write test number? tst_Benchmark_CpuTime_txMsg.Data[1] = 0; tst_Benchmark_CpuTime_txMsg.Data[2] = (tst_Benchmark_CpuTime_Avg>>16)&0xff; tst_Benchmark_CpuTime_txMsg.Data[3] = (tst_Benchmark_CpuTime_Avg>>8)&0xff; tst_Benchmark_CpuTime_txMsg.Data[4] = (tst_Benchmark_CpuTime_Min>>8)&0xff; tst_Benchmark_CpuTime_txMsg.Data[5] = tst_Benchmark_CpuTime_Min&0xff; tst_Benchmark_CpuTime_txMsg.Data[6] = (tst_Benchmark_CpuTime_Max>>8)&0xff; tst_Benchmark_CpuTime_txMsg.Data[7] = tst_Benchmark_CpuTime_Max&0xff; /* buffers will be filled when sending more than 2-3 messages, so retry until sent */ while (StdCan_Put(&tst_Benchmark_CpuTime_txMsg) != StdCan_Ret_OK) {} } tst_Benchmark_CpuTime_timer_val = TCNT1; #endif }