/****** C_Communication::~C_Communication() ************************************ * NAME * C_Communication::~C_Communication() -- destructor, cleans up communication * * SYNOPSIS * C_Communication::~C_Communication() * * FUNCTION * Stops the server from listening on a port, closes the socket where the * server listened, cleans up windows sockets. * * NOTES *******************************************************************************/ C_Communication::~C_Communication() { ExitComm(); }
/*----------------------------------------------------------------------------- * process received bus telegrams */ static void ProcessBus(void) { uint8_t ret; TBusMsgType msgType; uint8_t i; uint8_t *p; bool msgForMe = false; uint8_t flags; uint8_t old_osccal; static uint8_t sOsccal = 0; uint16_t startCnt; uint16_t stopCnt; uint16_t clocks; uint16_t diff; uint16_t seqLen; static int sCount = 0; static uint16_t sMinDiff = 0xffff; static uint8_t sMinOsccal = 0; uint8_t osccal_corr; ret = BusCheck(); if (ret == BUS_MSG_OK) { msgType = spRxBusMsg->type; switch (msgType) { case eBusDevReqReboot: case eBusDevReqInfo: case eBusDevReqSetAddr: case eBusDevReqEepromRead: case eBusDevReqEepromWrite: case eBusDevReqDoClockCalib: if (spRxBusMsg->msg.devBus.receiverAddr == MY_ADDR) { msgForMe = true; } break; default: break; } if (msgForMe == false) { return; } switch (msgType) { case eBusDevReqReboot: /* reset controller with watchdog */ /* set watchdog timeout to shortest value (14 ms) */ cli(); wdt_enable(WDTO_15MS); /* wait for reset */ while (1); break; case eBusDevReqInfo: sTxBusMsg.type = eBusDevRespInfo; sTxBusMsg.senderAddr = MY_ADDR; sTxBusMsg.msg.devBus.receiverAddr = spRxBusMsg->senderAddr; sTxBusMsg.msg.devBus.x.devResp.info.devType = eBusDevTypeSw8Cal; strncpy((char *)(sTxBusMsg.msg.devBus.x.devResp.info.version), version, BUS_DEV_INFO_VERSION_LEN); sTxBusMsg.msg.devBus.x.devResp.info.version[BUS_DEV_INFO_VERSION_LEN - 1] = '\0'; BusSend(&sTxBusMsg); break; case eBusDevReqSetAddr: sTxBusMsg.senderAddr = MY_ADDR; sTxBusMsg.type = eBusDevRespSetAddr; sTxBusMsg.msg.devBus.receiverAddr = spRxBusMsg->senderAddr; p = &(spRxBusMsg->msg.devBus.x.devReq.setAddr.addr); eeprom_write_byte((uint8_t *)MODUL_ADDRESS, *p); BusSend(&sTxBusMsg); break; case eBusDevReqEepromRead: sTxBusMsg.senderAddr = MY_ADDR; sTxBusMsg.type = eBusDevRespEepromRead; sTxBusMsg.msg.devBus.receiverAddr = spRxBusMsg->senderAddr; sTxBusMsg.msg.devBus.x.devResp.readEeprom.data = eeprom_read_byte((const uint8_t *)spRxBusMsg->msg.devBus.x.devReq.readEeprom.addr); BusSend(&sTxBusMsg); break; case eBusDevReqEepromWrite: sTxBusMsg.senderAddr = MY_ADDR; sTxBusMsg.type = eBusDevRespEepromWrite; sTxBusMsg.msg.devBus.receiverAddr = spRxBusMsg->senderAddr; p = &(spRxBusMsg->msg.devBus.x.devReq.writeEeprom.data); eeprom_write_byte((uint8_t *)spRxBusMsg->msg.devBus.x.devReq.readEeprom.addr, *p); BusSend(&sTxBusMsg); break; case eBusDevReqDoClockCalib: if (spRxBusMsg->msg.devBus.x.devReq.doClockCalib.command == eBusDoClockCalibInit) { sCount = 0; sOsccal = 0; sMinDiff = 0xffff; } else if (sCount > MAX_CAL_TEL) { sTxBusMsg.msg.devBus.x.devResp.doClockCalib.state = eBusDoClockCalibStateError; sTxBusMsg.senderAddr = MY_ADDR; sTxBusMsg.type = eBusDevRespDoClockCalib; sTxBusMsg.msg.devBus.receiverAddr = spRxBusMsg->senderAddr; BusSend(&sTxBusMsg); break; } flags = DISABLE_INT; BUS_TRANSCEIVER_POWER_UP; PORTB = 0; /* 8 bytes 0x00: 8 * (1 start bit + 8 data bits) + 7 stop bits */ seqLen = 8 * 1000000 * 9 / 9600 + 7 * 1000000 * 1 / 9600; /* = 8229 us */ old_osccal = OSCCAL; ExitComm(); InitTimer1(); TCCR1B = (1 << ICES1) | TIMER1_PRESCALER; OSCCAL = sOsccal; NOP_10; for (i = 0; i < 8; i++) { startCnt = Synchronize(); stopCnt = ClkMeasure(); clocks = stopCnt - startCnt; if (clocks > seqLen) { diff = clocks - seqLen; } else { diff = seqLen - clocks; } if (diff < sMinDiff) { sMinDiff = diff; sMinOsccal = OSCCAL; } OSCCAL++; NOP_4; } BUS_TRANSCEIVER_POWER_DOWN; InitTimer1(); InitComm(); sOsccal = OSCCAL; OSCCAL = old_osccal; RESTORE_INT(flags); if (sCount < MAX_CAL_TEL) { sTxBusMsg.msg.devBus.x.devResp.doClockCalib.state = eBusDoClockCalibStateContiune; sCount++; } else { sTxBusMsg.msg.devBus.x.devResp.doClockCalib.state = eBusDoClockCalibStateSuccess; /* save the osccal correction value to eeprom */ osccal_corr = eeprom_read_byte((const uint8_t *)OSCCAL_CORR); osccal_corr += sMinOsccal - old_osccal; eeprom_write_byte((uint8_t *)OSCCAL_CORR, osccal_corr); OSCCAL = sMinOsccal; NOP_10; } sTxBusMsg.senderAddr = MY_ADDR; sTxBusMsg.type = eBusDevRespDoClockCalib; sTxBusMsg.msg.devBus.receiverAddr = spRxBusMsg->senderAddr; BusSend(&sTxBusMsg); break; default: break; } } }