Search code examples
structstm32eeprom

How can I use the Pins in the Struct


Hello I have a problem STM32 . In my library I wrote for eeprom, I want to use the pins and ports in the struct and then use them in the HAL_GPIO_Write_Pin function, but I could not succeed. ERROR: incompatible type for argument 2 of 'HAL_GPIO_WritePin' Eeprom.

(Sorry if I didn't ask the question properly, it's my first time using it.)

EEPROM.h   
 typedef struct
{

    SPI_HandleTypeDef* SPI_EEPROM ;
    uint16_t CS_PIN ;
    GPIO_TypeDef* CS_PORT ;
    uint32_t Write_Protect ;
    GPIO_PinState CS_LOW ;
    GPIO_PinState CS_HIGH ;

}EEPROM;

/*****************************************************************/

EEPROM.c 


SPI_HandleTypeDef* hspi1 ;
EEPROM    EEPROM_1 ;

void Eeprom_Init( )
{

    EEPROM_1.SPI_EEPROM    = hspi1 ;
    EEPROM_1.Write_Protect = EEPROM_WRITE ;
    EEPROM_1.CS_PIN        = SPI2_SS_Pin ;
    EEPROM_1.CS_PORT       = SPI2_SS_GPIO_Port ;
    EEPROM_1.CS_LOW        = GPIO_PIN_RESET ;
    EEPROM_1.CS_HIGH       = GPIO_PIN_SET ;

}


void EEPROM_1_CS_HIGH (EEPROM CS_PORT, EEPROM CS_PIN, EEPROM CS_HIGH)
{
    HAL_GPIO_WritePin( &CS_PORT, CS_PIN, CS_HIGH ) ;
}


void EEPROM_1_CS_LOW (EEPROM CS_PORT, EEPROM CS_PIN, EEPROM CS_LOW)
{
    HAL_GPIO_WritePin( &CS_PORT, CS_PIN, CS_LOW ) ;
}

Solution

  • In the line:

    void EEPROM_1_CS_HIGH (EEPROM CS_PORT, EEPROM CS_PIN, EEPROM CS_HIGH)
    

    You are saying that the function EEPROM_1_CS_HIGH has three arguments, all of which are a whole struct, passed by value.

    You probably meant to have just one single argument, which is the struct, and then access its members inside the function. Also you should pass the struct by reference, using a pointer *.

    void EEPROM_CS_HIGH (EEPROM *memory)
    {
      HAL_GPIO_WritePin(memory->CS_PORT, memory->CS_PIN, memory->CS_HIGH);
    }
    

    If you then want to have a function that accesses your global data of the particular instance, you can do:

    void EEPROM_1_CS_HIGH (void)
    {
      EEPROM_CS_HIGH(&EEPROM_1);
    }