Is this the right way to access function?

Viewed 66

I am currently using "STM32F429I-DISC1" with joystick. I am trying to draw something on the LCD screen and using joystick move this object. My drawing is working fine, but I have the error: " void value not ignored as it ought to be".

This two lines have problems...

    localX = Joy_ReadXY(CTRL_REG_IN3);
    localY = Joy_ReadXY(CTRL_REG_IN4);

Can someone please tell me, how I can fix this error?
And why I see this error?

Main.c

#include "stm32f429i_discovery_lcd.h"

#define CTRL_REG_IN3                0b00011000
#define CTRL_REG_IN4                0b00100000

SemaphoreHandle_t xMutex;
Joystick_data xy;

void vTaskFunction1(void *pvParameters) {
    uint16_t localX;
    uint16_t localY;

    for(;;) {
        localX = Joy_ReadXY(CTRL_REG_IN3);
        localY = Joy_ReadXY(CTRL_REG_IN4);

        xSemaphoreTake( xMutex, portMAX_DELAY );

        xy.x = localX;
        xy.y = localY;

        xSemaphoreGive( xMutex );
        HAL_Delay(10);
    }
}

void vTaskFunction2(void *pvParameters) {
    uint32_t xCoord = 240/2;
    uint32_t yCoord = 320/2;
    uint8_t reads = 0;
    uint8_t ballRadius = 5;
    uint16_t xLimitMin = ballRadius+25;
    uint16_t xLimitMax = 240-ballRadius-25;
    uint16_t yLimitMin = ballRadius+25;
    uint16_t yLimitMax = 320-ballRadius-25;
    for(;;) {
        xSemaphoreTake( xMutex, portMAX_DELAY );
        if (xy.x > 3000 && !(xCoord < xLimitMin))
            xCoord -= 5;
        if (xy.x < 1000 && !(xCoord > xLimitMax))
            xCoord += 5;

        if (xy.y > 3000 && !(yCoord < yLimitMin))
            yCoord -= 5;
        if (xy.y < 1000 && !(yCoord > yLimitMax))
            yCoord += 5;
        reads++;
        BSP_LCD_Clear(LCD_COLOR_WHITE);
        BSP_LCD_DrawCircle(xCoord, yCoord, ballRadius);
        BSP_LCD_FillCircle(xCoord, yCoord, ballRadius);
        xSemaphoreGive(xMutex);
        HAL_Delay(20);
    }
}

int main(void)
{
    HAL_Init();
    SystemClock_Config();
    MX_GPIO_Init();
    MX_SPI4_Init();
    MX_TIM1_Init();
    MX_USART1_UART_Init();
    // LCD Things
    BSP_LCD_Init();
    BSP_LCD_LayerDefaultInit(1, LCD_FRAME_BUFFER);
    BSP_LCD_SelectLayer(1);
    BSP_LCD_SetBackColor(LCD_COLOR_WHITE); // Vali meelepärane värv
    BSP_LCD_Clear(LCD_COLOR_WHITE);
    BSP_LCD_SetTextColor(LCD_COLOR_DARKBLUE); // Vali meelepärane värv

    MX_FREERTOS_Init();

    if ( xMutex == NULL )
    {
        xMutex = xSemaphoreCreateMutex();
        if ( ( xMutex ) != NULL )
            xSemaphoreGive( ( xMutex ) );
    }

    xTaskCreate(vTaskFunction1, "Task 1", 100, NULL, 1, NULL);
    xTaskCreate(vTaskFunction2, "Task 2", 100, NULL, 1, NULL);

    vTaskStartScheduler();

    osKernelStart();

    while (1)
    {
    }
}

Read joystick function (joystick.c)

#include <stdio.h>
#include <main.h>
#include "gpio.h"
#include "spi.h"

#define READ_SLAVE_OPERATION        0b10000000
#define READ_INCR_SLAVE_OPERATION   0b11000000
#define WRITE_SLAVE_OPERATION       0b00000000
#define CTRL_REG_IN3                0b00000011
#define CTRL_REG_IN4                0b00000100

#define OUT_X_L 0x28
#define OUT_X_H 0x29
#define OUT_Y_L 0x2A
#define OUT_Y_H 0x2B
#define OUT_Z_L 0x2C
#define OUT_Z_H 0x2D

#define JOY_CS_LOW()     HAL_GPIO_WritePin(JOY_CS_GPIO_PORT, JOY_CS_PIN, 0)
#define JOY_CS_HIGH()     HAL_GPIO_WritePin(JOY_CS_GPIO_PORT, JOY_CS_PIN, 1)

#define JOY_CS_GPIO_PORT GPIOC
#define JOY_CS_PIN       GPIO_PIN_13

int16_t Joy_ReadXY(uint8_t reg1){

    uint8_t pTxData1[2] = {reg1, 0};
    uint8_t pRxData1[2] = {0, 0};
    JOY_CS_LOW();
    HAL_SPI_TransmitReceive(&hspi4, pTxData1, pRxData1, 2, HAL_MAX_DELAY);
    JOY_CS_HIGH();

    return pRxData1[0] << 8 | pRxData1[1];
}
1 Answers

Here, in Main.c, you call the function before telling the compiler about what parameters and what return value types it has.

        localX = Joy_ReadXY(CTRL_REG_IN3);
        localY = Joy_ReadXY(CTRL_REG_IN4)

That confused the compiler and it starts "guessing" about them.
Guessing that it is a void-returning function, the compiler then complains that you are expecting a return value from a function which does return void i.e. nothing.
The returned void should be ignored, instead of attempting to write it to a variable. At least that is what the compiler thinks...

To fix it, you should explain to the compiler that there is a function elsewhere, with name, parameters and return value type. That is done by providing the prototype

int16_t Joy_ReadXY(uint8_t reg1);

It needs to be done before the function body in which the the extern function is first called. (And you already confirmed in comments that it fixes the described problem in your code.)

Note that for the other shown functions this is not needed, because they are defined (with head and body) before they are called.
Similar for other functions, which have their prototype provided in the header you include early on.

Actually, putting the prototype of your function into a header and including that similarily would be the best way to solve this.

Related