cancel
Showing results for 
Search instead for 
Did you mean: 

Data structure for data write and Data read

ebommer
Associate II
Posted on September 19, 2014 at 22:27

I would like create a variable that contains port and pin information, so I can use the high level name to perform all reads and writes.

I am looking for help with the syntax to create a:

  • Structure for the port and pin assignments.
  • How to assign the values to the variables

Note: Line number 1-32 are mycode attempt do, of course it doesn't work. The rest of the code are from the STM32 libraries


typedef struct

{

__IO uint32_t Port;

uint32_t Pin;


}SwitchInputs;


typedef struct

{

__IO uint32_t Port;

uint32_t Pin;


}LED_Outputs;


SwitchInputs Switch1;

LED_Outputs LED1;



Switch1.Port = GPIOA;

Switch1. Pin = GPIO_PIN_8;


LED1.Port = GPIOB;

LED1. Pin = GPIO_PIN_0;



GPIO_PinState HAL_GPIO_ReadPin(Switch1.Port, Switch1. Pin); 



HAL_GPIO_WritePin(LED1.Port, LED. Pin);



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

void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)

{

/* Check the parameters */

assert_param(IS_GPIO_PIN(GPIO_Pin));

assert_param(IS_GPIO_PIN_ACTION(PinState));


if (PinState != GPIO_PIN_RESET)

{

GPIOx->BSRRL = GPIO_Pin;

}

else

{

GPIOx->BSRRH = GPIO_Pin ;

}

}

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

GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)

{

GPIO_PinState bitstatus;


/* Check the parameters */

assert_param(IS_GPIO_PIN(GPIO_Pin));


if ((GPIOx->IDR & GPIO_Pin) != (uint32_t)GPIO_PIN_RESET)

{

bitstatus = GPIO_PIN_SET;

}

else

{

bitstatus = GPIO_PIN_RESET;

}

return bitstatus;

}


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

#define GPIOA ((GPIO_TypeDef *) GPIOA_BASE)

#define GPIOB ((GPIO_TypeDef *) GPIOB_BASE)

#define GPIOC ((GPIO_TypeDef *) GPIOC_BASE)

#define GPIOD ((GPIO_TypeDef *) GPIOD_BASE)

#define GPIOE ((GPIO_TypeDef *) GPIOE_BASE)

#define GPIOF ((GPIO_TypeDef *) GPIOF_BASE)


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

typedef struct

{

__IO uint32_t MODER; /*!< GPIO port mode register, Address offset: 0x00 */

__IO uint32_t OTYPER; /*!< GPIO port output type register, Address offset: 0x04 */

__IO uint32_t OSPEEDR; /*!< GPIO port output speed register, Address offset: 0x08 */

__IO uint32_t PUPDR; /*!< GPIO port pull-up/pull-down register, Address offset: 0x0C */

__IO uint32_t IDR; /*!< GPIO port input data register, Address offset: 0x10 */

__IO uint32_t ODR; /*!< GPIO port output data register, Address offset: 0x14 */

__IO uint16_t BSRRL; /*!< GPIO port bit set/reset low register, Address offset: 0x18 */

__IO uint16_t BSRRH; /*!< GPIO port bit set/reset high register, Address offset: 0x1A */

__IO uint32_t LCKR; /*!< GPIO port configuration lock register, Address offset: 0x1C */

__IO uint32_t AFR[2]; /*!< GPIO alternate function registers, Address offset: 0x20-0x24 */

__IO uint32_t BRR; /*!< GPIO bit reset register, Address offset: 0x28 */

}GPIO_TypeDef;

2 REPLIES 2
Posted on September 19, 2014 at 22:47

In the Standard Peripheral Library the following syntax is appropriate

typedef struct _SwitchInputs
{
GPIO_TypeDef* GPIOx;
uint16_t GPIO_Pin;
} SwitchInputs;
SwitchInputs Switch1 = { GPIOA, GPIO_Pin_8 };
SwitchInputs Switchs[] = {
{ GPIOA, GPIO_Pin_8 },
{ GPIOB, GPIO_Pin_7 }
};

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
ebommer
Associate II
Posted on September 19, 2014 at 22:55

Thank you, that is exactly what I was looking for.