HelloResponse::HelloResponse(const HelloRequest& UNUSED(request), const std::vector<uint8_t>& response) : Response(Type::Hello) { auto payload = getPayload(response); // getPayload will do size checking for us uint8_t protocolVersion = payload[0]; uint8_t resetFlags = payload[1]; std::string deviceConfig(payload.begin() + 2, payload.end()); m_devInfo = std::make_shared<DeviceInfo>(protocolVersion, resetFlags, deviceConfig); }
/* * ======== postInit ======== */ static Int postInit(Timer_Object *obj, Error_Block *eb) { if (Timer_checkFrequency && Timer_module->firstInit) { /* Verify timer frequency */ Timer_checkFreq(obj); } Timer_module->firstInit = FALSE; /* configure the timer */ if (deviceConfig(obj, eb)) { return (3); /* device config failure */ } return (0); }
/* * ======== Timer_checkFreq ======== */ Void Timer_checkFreq(Timer_Object *obj) { UInt key; UInt32 timerCountStart, timerCountEnd, tsCountStart, tsCountEnd; UInt32 deltaTs, deltaCnt; Types_FreqHz timerFreq, timestampFreq; UInt freqRatio; UInt32 actualFrequency; Timer_Object tempObj; /* * Make a temporary copy of 'obj' and modify it to be used for the timer * frequency check. Set the period to Timer_MAX_PERIOD to ensure that * the timer does not roll over while performing the check. */ memcpy((void *)&tempObj, (void *)obj, sizeof(Timer_Object)); tempObj.period = Timer_MAX_PERIOD; tempObj.periodType = Timer_PeriodType_COUNTS; tempObj.runMode = Timer_RunMode_ONESHOT; tempObj.startMode = Timer_StartMode_USER; /* Initialize the timer registers */ deviceConfig(&tempObj, NULL); /* Get the frequencies of the Timer and the Timestamp */ Timer_getFreq(&tempObj, &timerFreq); Timestamp_getFreq(×tampFreq); /* Assume that timer frequency is less than 2^32 Hz */ Assert_isTrue(timestampFreq.hi == 0 && timerFreq.hi == 0, NULL); freqRatio = timestampFreq.lo / timerFreq.lo; key = Hwi_disable(); /* * Warning: halting the core between Timer_start and the point of * code indicated below can cause the frequency check to fail. This is * is because the DMTimer will continue to run while this core is halted, * this causing the ratio between timer counts to change */ Timer_start(&tempObj); /* Record the initial timer & timestamp counts */ timerCountStart = Timer_getCount(&tempObj); tsCountStart = Timestamp_get32(); /* Wait for 'TIMERCOUNTS' timer counts to elapse */ while (Timer_getCount(&tempObj) < timerCountStart + TIMERCOUNTS); timerCountEnd = Timer_getCount(&tempObj); /* Record the timestamp ticks that have elapsed during the above loop */ tsCountEnd = Timestamp_get32(); /* End of code segment where core should not be halted */ Hwi_restore(key); deltaTs = tsCountEnd - tsCountStart; deltaCnt = timerCountEnd - timerCountStart; /* Check the timer frequency. Allow a margin of error. */ if (((deltaTs / deltaCnt) > freqRatio * 2) || ((deltaTs / deltaCnt) < freqRatio / 2)) { actualFrequency = ((UInt64)timestampFreq.lo * (UInt64)deltaCnt) / (UInt64)deltaTs; Error_raise(NULL, Timer_E_freqMismatch, Timer_module->intFreq.lo, actualFrequency); } }