AnsweredAssumed Answered

Data structure for data write and Data read

Question asked by ebommer on Sep 19, 2014
Latest reply on Sep 19, 2014 by ebommer

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 my code attempt do, of course it doesn't work.  The rest of the code are from the STM32 libraries



01.typedef struct
02.{
03.    __IO uint32_t Port;
04.    uint32_t Pin;
05.     
06.}SwitchInputs;
07. 
08.typedef struct
09.{
10.    __IO uint32_t Port;
11.    uint32_t Pin;
12.     
13.}LED_Outputs;
14. 
15.SwitchInputs    Switch1;
16.LED_Outputs     LED1;
17. 
18. 
19.Switch1.Port = GPIOA;
20.Switch1. Pin = GPIO_PIN_8;
21. 
22.LED1.Port = GPIOB;
23.LED1. Pin = GPIO_PIN_0;
24. 
25. 
26.GPIO_PinState HAL_GPIO_ReadPin(Switch1.Port, Switch1. Pin);
27. 
28. 
29.HAL_GPIO_WritePin(LED1.Port, LED. Pin);
30. 
31. 
32./********************************************************************************/
33.void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
34.{
35.  /* Check the parameters */
36.  assert_param(IS_GPIO_PIN(GPIO_Pin));
37.  assert_param(IS_GPIO_PIN_ACTION(PinState));
38. 
39.  if (PinState != GPIO_PIN_RESET)
40.  {
41.    GPIOx->BSRRL = GPIO_Pin;
42.  }
43.  else
44.  {
45.    GPIOx->BSRRH = GPIO_Pin ;
46.  }
47.}
48./********************************************************************************/
49.GPIO_PinState HAL_GPIO_ReadPin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
50.{
51.  GPIO_PinState bitstatus;
52. 
53.  /* Check the parameters */
54.  assert_param(IS_GPIO_PIN(GPIO_Pin));
55. 
56.  if ((GPIOx->IDR & GPIO_Pin) != (uint32_t)GPIO_PIN_RESET)
57.  {
58.    bitstatus = GPIO_PIN_SET;
59.  }
60.  else
61.  {
62.    bitstatus = GPIO_PIN_RESET;
63.  }
64.  return bitstatus;
65.  }
66.   
67.  /********************************************************************************/
68.#define GPIOA               ((GPIO_TypeDef *) GPIOA_BASE)
69.#define GPIOB               ((GPIO_TypeDef *) GPIOB_BASE)
70.#define GPIOC               ((GPIO_TypeDef *) GPIOC_BASE)
71.#define GPIOD               ((GPIO_TypeDef *) GPIOD_BASE)
72.#define GPIOE               ((GPIO_TypeDef *) GPIOE_BASE)
73.#define GPIOF               ((GPIO_TypeDef *) GPIOF_BASE)
74. 
75./********************************************************************************/
76.typedef struct
77.{
78.  __IO uint32_t MODER;        /*!< GPIO port mode register,               Address offset: 0x00      */
79.  __IO uint32_t OTYPER;       /*!< GPIO port output type register,        Address offset: 0x04      */
80.  __IO uint32_t OSPEEDR;      /*!< GPIO port output speed register,       Address offset: 0x08      */
81.  __IO uint32_t PUPDR;        /*!< GPIO port pull-up/pull-down register,  Address offset: 0x0C      */
82.  __IO uint32_t IDR;          /*!< GPIO port input data register,         Address offset: 0x10      */
83.  __IO uint32_t ODR;          /*!< GPIO port output data register,        Address offset: 0x14      */
84.  __IO uint16_t BSRRL;        /*!< GPIO port bit set/reset low register,  Address offset: 0x18      */
85.  __IO uint16_t BSRRH;        /*!< GPIO port bit set/reset high register, Address offset: 0x1A      */
86.  __IO uint32_t LCKR;         /*!< GPIO port configuration lock register, Address offset: 0x1C      */
87.  __IO uint32_t AFR[2];       /*!< GPIO alternate function registers,     Address offset: 0x20-0x24 */
88.  __IO uint32_t BRR;          /*!< GPIO bit reset register,              Address offset: 0x28 */
89.}GPIO_TypeDef;



Outcomes