Hello Tom,
Let me explain how the application works:
You have FC321:FreeTimer32 working on TPM1 counter that is not fully allocated for this component (see the property Component uses entire timer = no). Therefore the TPM1 counter is running at period 10.923ms as you can see in the TU2:TimerUnit_LDD component (this component is used by the FC321:FreeCounter32). To count precisely ticks per 10ms as you have selected, the TPM1_C1V - channel 1 compare register is updated during each interrupt (when the compare register match the counter), i.e. it allows counting 10ms tick precisely.
FreeCount32 use RealTimeLDD component that implements TU2_OnChannel0() event routine that is invoked by the TPM1_C1V interrupt request (match of channel 1 compare register with the counter register). This routine counts TimerTicks (each 10ms) and also contains Overflow flag:
void TU2_OnChannel0(LDD_TUserData *UserDataPtr)
{
RealTimeLdd1_TDeviceData *DeviceDataPrv = PE_LDD_DeviceDataList[PE_LDD_COMPONENT_RealTimeLdd1_ID];
uint16_t Ticks;
(void)UserDataPtr; /* Parameter is not used, suppress unused argument warning */
(void)TU2_GetOffsetTicks(DeviceDataPrv->LinkedDeviceDataPtr, CHANNEL, &Ticks);
Ticks += DeviceDataPrv->CmpVal;
(void)TU2_SetOffsetTicks(DeviceDataPrv->LinkedDeviceDataPtr, CHANNEL, Ticks);
DeviceDataPrv->TimerTicks++; /* Increment counter of timer ticks */
if (DeviceDataPrv->TimerTicks == 0U) { /* Testing counter overflow */
DeviceDataPrv->Overflow = TRUE; /* If yes then set overflow flag */
}
}
The GetTimeMS method of FreeCounter32 return value in milisecond that is computed by using TimerTicks, see below:
LDD_TError RealTimeLdd1_GetTimeMS(LDD_TDeviceData *DeviceDataPtr, uint16_t *TimePtr)
{
RealTimeLdd1_TDeviceData *DeviceDataPrv = (RealTimeLdd1_TDeviceData *)DeviceDataPtr;
uint32_t CopyTicks; /* Working copy of variable TimerTicks */
bool CopyOverflow; /* Working copy of variable Overflow */
LDD_RealTime_Tfloat rtval; /* Result of multiplication */
/* {Default RTOS Adapter} Critical section begin, general PE function is used */
EnterCritical();
CopyTicks = DeviceDataPrv->TimerTicks; /* Loading actual number of timer ticks */
CopyOverflow = DeviceDataPrv->Overflow; /* Loading actual state of "overflow flag" */
/* {Default RTOS Adapter} Critical section end, general PE function is used */
ExitCritical();
if (CopyOverflow) { /* Testing counter overflow */
return ERR_OVERFLOW; /* If yes then error */
}
rtval = CopyTicks * 10.0F; /* Multiply ticks and clock configuration 0 coefficient */
if (rtval > 0xFFFFUL) { /* Is the result greater than 65535 ? */
return ERR_MATH; /* If yes then error */
}
else {
*TimePtr = (uint16_t)rtval;
}
return ERR_OK;
}
When the value of time in milisecond cannot be stored in 16bits value it is returned ERR_MATH. When the TimerTicks value overflow it is returned ERR_OVERFLOW. In both error cases the TimePtr value is not returned. Therefore you must execute the FC321_Reset() until ERR_MATH is returned by GetTimeMS method - it happens after 0xFFFF miliseconds (uint16_t type).
You can modify the code of the loop in the main function by the following way:
while (1)
{
if (lticksDiff(wTimeStart,lticksGetMS()) > 500)
{
printf(" Blinking...\n\r");
PWM1_SetRatio16(wBrightness);
if (wBrightness > 0)
wBrightness= 0;
else
wBrightness= 5000;
//wTimeStart= lticksGetMS();
FC321_Reset();
wTimeStart=0;
}
}
This change enable running the application for unlimited time.
Best Regards,
Marek Neuzil