Port MSP430. Troubles with pdMS_TO_TICKS

Gentlemen!

I had some trobles with the macro pdMS_TO_TICKS (…) - Port MSP430:
(configTICK_RATE_HZ = 2000)
pdMS_TO_TICKS (3333) = 46

The reason is parenthes in the macro.
TickType_t for MSP430 is uint16_t
The macro in projdefs.h is:
#define pdMS_TO_TICKS( xTimeInMs ) ( ( TickType_t ) ( ( ( TickType_t ) ( xTimeInMs ) * ( TickType_t ) configTICK_RATE_HZ ) / ( TickType_t ) 1000U ) )
so as for the macro 3333 * 2000 = 6666000 = 0x65b710, for TickType_t it is 0xb710 = 46864
46864 / 1000 = 46

I changed of the parenthes and all is Ok:
#define pdMS_TO_TICKS( xTimeInMs ) ( ( TickType_t ) ( ( TickType_t ) ( xTimeInMs ) * ( ( TickType_t ) configTICK_RATE_HZ / ( TickType_t ) 1000U ) ) )

Sicerelly your’s
John

Hi John,

Your change works well when configTICK_RATE_HZ is a whole multiple of 1000. However, if configTICK_RATE_HZ were 500, your change wouldn’t work well because
(TickType_t)(500) / (TickType_t)(1000) is zero.

How about using an intermediate 32-bit value, like this:

( (TickType_t) ( ( (uint32_t)(xTimeInMs) * configTICK_RATE_HZ ) / 1000U ) )

Luckily, the macro pdMS_TO_TICKS() is defined conditionally:

#ifndef pdMS_TO_TICKS
    #define pdMS_TO_TICKS() ...
#endif

In some of my projects I even use a temporary cast to uint64_t:

#define pdMS_TO_TICKS( xTimeInMs ) \
        ( TickType_t ) \
		( ( ( uint64_t ) ( xTimeInMs ) * ( configTICK_RATE_HZ ) ) / 1000U )

The 32-bit implementation will overflow when a period last longer than 4294 seconds (provided that the tick-rate is 1000 Hz).

For an MSP430 project with 16-bit ticks, the 32-bit implementation might best after all. It covers all possible tick counts (0 through 65535). And if there are cases where the delay is calculated at run time, then the minimal work is done (32-bit math instead of 64).