fucntion in C++ in CUBEide

I am trying to write a function in c for stm32 board.

    float computeMotorSpeed(TIM_HandleTypeDef &htimer, uint32_t &PreviousCount ) {
    int32_t DiffCount = 0;
    uint32_t  CurrentCount = 0;
    CurrentCount = __HAL_TIM_GET_COUNTER(&htimer); /*evaluate increment of timer counter from previous count*/

    if (__HAL_TIM_IS_TIM_COUNTING_DOWN(&htimer)) {
        /* check for counter underflow */
        if (CurrentCount <= PreviousCount) {
            DiffCount = CurrentCount - PreviousCount;
        }
        else {
            DiffCount = -((TIM3_ARR_VALUE+1) - CurrentCount) - PreviousCount;
        }
    }
    else {
        /* check for counter overflow */
        if (CurrentCount >= PreviousCount) {
            DiffCount = CurrentCount - PreviousCount;
        }
        else {
            DiffCount = ((TIM3_ARR_VALUE+1) - PreviousCount) + CurrentCount;
        }
    }

    PreviousCount = CurrentCount;
    return (float) DiffCount*60/(TS*TIM3_ARR_VALUE); // rpm (round per minute);
}

But i have the following error.

error: expected ';', ',' or ')' before '&' token

I am trying to pass the values by reference but I have an error. Yet when I pass by the value as follows the compiler does not show me errors.

    float computeMotorSpeed(TIM_HandleTypeDef htimer, uint32_t PreviousCount ) {
    int32_t DiffCount = 0;
    uint32_t  CurrentCount = 0;
    CurrentCount = __HAL_TIM_GET_COUNTER(&htimer); /*evaluate increment of timer counter from previous count*/

    if (__HAL_TIM_IS_TIM_COUNTING_DOWN(&htimer)) {
        /* check for counter underflow */
        if (CurrentCount <= PreviousCount) {
            DiffCount = CurrentCount - PreviousCount;
        }
        else {
            DiffCount = -((TIM3_ARR_VALUE+1) - CurrentCount) - PreviousCount;
        }
    }
    else {
        /* check for counter overflow */
        if (CurrentCount >= PreviousCount) {
            DiffCount = CurrentCount - PreviousCount;
        }
        else {
            DiffCount = ((TIM3_ARR_VALUE+1) - PreviousCount) + CurrentCount;
        }
    }

    PreviousCount = CurrentCount;
    return (float) DiffCount*60/(TS*TIM3_ARR_VALUE); // rpm (round per minute);
}

why the first case (passing by reference) is not correct?

>Solution :

There is no pass-by-reference in C, so the C compiler doesn’t know what to do with & in the argument-passing context. You’ll need to pass a pointer using * and dereference to get the same effect. Elaborating on comment:

// use * to denote pointers
float computeMotorSpeed(TIM_HandleTypeDef *htimer, uint32_t *PreviousCount ) {
   ...
   // don't "take the address" here anymore, the pointer is the address
   CurrentCount = __HAL_TIM_GET_COUNTER(htimer); /*evaluate increment of timer counter from previous count*/

   if (__HAL_TIM_IS_TIM_COUNTING_DOWN(htimer)) {
        /* check for counter underflow */
        // and you must dereference PreviousCount with * everywhere to access
        // its value
        if (CurrentCount <= *PreviousCount) {
            DiffCount = CurrentCount - *PreviousCount;
        }
   ...
   *PreviousCount = CurrentCount;
   return ...;
}

And you would call this function with something like

// here, & means "take the address of", as you used it in OP
float floatRet = computeMotorSpeed(&htimer, &PreviousCount);

Leave a Reply