Combination of enumeration and structure in STM32 programming

Posted by inkdrop on Sat, 08 Jan 2022 07:57:28 +0100

01. Structure definition

Basic definition: structure, in popular terms, is like packaging. It encapsulates some variables with common characteristics (such as attributes belonging to a certain kind of things, often aggregation of business-related attributes) and accesses and modifies internal variables through certain methods.

Definition of structure:

First: only structure definition

struct stuff{  
  char *name;  //full name
  int num;  //Student number
  int age;  //Age
  float score;  //achievement
};

The second is to attach the initialized structure definition of "structure variable" of this structure type. The following code is to directly define a variable when defining a structure

struct stuff{  
  char *name;  //full name
  int num;  //Student number
  int age;  //Age
  float score;  //achievement
}xiaoming;

In fact, this is equivalent to defining a structure first, and then defining a structure variable:

struct stuff{  
  char *name;  //full name
  int num;  //Student number
  int age;  //Age
  float score;  //achievement 
};  
struct stuff xiaoming;  

The third way: using the typedef keyword, you can write less struct when defining structural variables, which is easier.

typedef struct stuff{  
  char *name;  //full name
  int num;  //Student number
  int age;  //Age
  float score;  //achievement 
}stuff_s;  
stuff_s xiaoming;

Using typedef can further simplify and omit the structure name, which is also a common way

typedef struct{  
  char *name;  //full name
  int num;  //Student number
  int age;  //Age
  float score;  //achievement 
}stuff_s;  
stuff_s xiaoming;

The standard peripheral Library of STM32 has a large number of such applications, as follows

typedef struct
{
  uint32_t GPIO_Pin;
  GPIOMode_TypeDef GPIO_Mode;
  GPIOSpeed_TypeDef GPIO_Speed;
  GPIOOType_TypeDef GPIO_OType;
  GPIOPuPd_TypeDef GPIO_PuPd;
}GPIO_InitTypeDef;

There are many ways to write "operation" about the definition of structure pointer. I generally define the pointer according to the following

stuff_s *cuerrent_student;

02. Structure initialization

In most applications, the structure is generally defined and initialized in the code, as shown below

typedef struct{  
  char *name;  //full name
  int num;  //Student number
  int age;  //Age
  float score;  //achievement 
}stuff_s;
stuff_s xiaoming;
void xiaoming_inf_init()
{
  xiaoming.name = "xiaoming";
  xiaoming.num = 1;
  xiaoming.age = 18.0;
  xiaoming.score = 100;
}

Of course, there are also methods that can initialize data when defined

typedef struct{  
  char *name;  //full name
  int num;  //Student number
  int age;  //Age
  float score;  //achievement 
}stuff_s;
stuff_s xiaoming={"xiaoming",1,18.0,100};

C99 and C11 provide designateinitializers for structures. Its initializers use dot operators and member names.

For the knowledge of C99 and C11, see my previous articles< Development of C language >, remember to check C99 in IAR and Keil.

For example, to initialize only the name member in the xiaoming structure:

stuff_s xiaoming=
{
  .name = "xiaoming"
};

You can also use the specified initializers in any order:

stuff_s xiaoming=
{
  .age = 18.0,
  .name = "xiaoming"
};

This assignment method is very common in linux. Take the platform driver framework as an example:

static struct platform_driver leds_platform_driver = {
  .driver = {
    .name = "imx6ul-led",
    .of_match_table = leds_of_match,
  },
  .probe = leds_probe,
  .remove = leds_remove,
};

03. Accessing structure members

Access to structure members requires the help of the structure member operator (.), as follows

stuff_s xiaoming,xiaohong;
void student_inf_init()
{
  xiaoming.name = "xiaoming";
  xiaoming.num = 1;
  xiaoming.age = 18.0;
  xiaoming.score = 100;
  
  
  xiaohong.name = "xiaohong";
  xiaohong.num = xiaoming.num+1;
}

When using pointers, use the (- >) symbol to access structure members

stuff_s xiaoming,xiaohong;
stuff_s *cuerrent_student;
void student_inf_init()
{
  xiaoming.name = "xiaoming";
  xiaoming.num = 1;
  xiaoming.age = 18.0;
  xiaoming.score = 100;
  cuerrent_student = &xiaohong;
  
  
  cuerrent_student->name = "xiaohong";
  cuerrent_student->num = xiaoming.num+1;
}

04. Combination of enumeration and structure

Briefly introduce the following enumeration: the values of some data are often limited and can only be a very small number of integers, and it is best to give a name for each value to facilitate its use in subsequent codes. For example, there are only seven days a week, only twelve months a year, six courses a class a week, etc.

Of course, you can use macro definitions

#define Mon 1
#define Tues 2
#define Wed 3
#define Thurs 4
#define Fri 5
#define Sat 6
#define Sun 7

If enumeration is used, it is as follows

enum week{
  Mon,
  Tues,
  Wed,
  Thurs,
  Fri,
  Sat,
  Sun
};

Enumeration is a type through which you can define enumeration variables:

enum week a, b, c;

So what happens when enumerations are used with structs? Suppose we want to protocol the driver of a voice chip, we need to represent the state of the voice chip

typedef enum//Voice chip status
{
    VOICE_INIT_OK =            0x4A,  //After the voice chip is powered on and initialized successfully, the command will be returned automatically
    VOICE_RECEIVE_OK =           0x41,  //The voice chip receives the correct command frame
    VOICE_ORDER_ERROR=         0x45,  //Voice received wrong command frame
    VOICE_BUSY     =            0x4E,  //Voice busy(Synthesizing state)
    VOICE_FREE     =           0x4F  //Voice idle
} VOICE_STATUS;
typedef struct {
    VOICE_STATUS status ;                 //!< Voice chip status
    Ouint32  delayTicks;                   //!< Playback time
    Ouint32  playtimes;                    //!< Playback times
} voicechip_Para_S;
voicechip_Para_S voicechip_Para;

Then, when changing the state of the voice chip, we can write as follows

voicechip_Para.status = VOICE_RECEIVE_OK;

When judging the status of the voice chip, we can write as follows

if((voicechip_Para.status == VOICE_FREE)

Of course, you can define it with macros, and the code is neat. Here, I hope you can understand the first sentence of the article: a structure is an aggregation of business-related attributes.

05. Operation

There are many operations on structures. If all of them are summarized, this article will be very cumbersome. For example, the nested operations of structures can be used while defining structure B:

struct A{  
    struct B{  
int c;  
    }b;  
    struct B sb;  
}a;

In this case, I generally advocate that you can understand it and write your own code with less such operations

struct B{  
     int c;  
}b;
struct A{  
     struct B sb;  
}a;

Click to view the topic: Advanced C language