Go Down

Topic: STM32F4 (Read 19 times) previous topic - next topic

wanderson

a uint32_t is the same as an unsigned long (not long) on the arduino...  Other platforms may not have the same equivalence. 

The captital in the typedef above is simply a means of using the term typedef as part of the variable name, when the keyword typedef is use in c/c++ it is a way of assigning a synonym to an existing data type.
New true random number library available at: http://code.google.com/p/avr-hardware-random-number-generation/

Current version 1.0.1

westfw

Quote
So am I right that a uint32_t is identical to an Int i.e both 32 bits long so no difference at all

On an ARM, yes.  On an AVR, they would be different sizes.  If you wanted to hold something like an IP address (32bits for v4), you could use an "int" on an ARM, but you'd have to use a "long" on an AVR: an incompatibility.  Instead, your program should use "uint32_t" for the IP address, and it would run on both AVR and ARM (assuming all other architectural dependencies are fixed as well.)

"typedef" defines a name that acts like a type (ie like "int" or "long"); it's a shorthand convenience, especially when you get to more complex structures (containing other structures, etc.)

So someplace in an AVR include file there is likely to be a line that looks like:
typedef unsigned long uint32_t; that means "treat the name "uint32_t" as a variable type, that is the same as "unsigned long".  The similar file on an ARM might contain "typedef unsigned int uint32_t;"
Code: [Select]
typedef uint32_t ip_addr_t;

typedef struct ip_pheader {ip_addr_t phdr_ip;
uint16_t phdr_sourceport;
uint16_t phdr_destport;} ip_pheader_t;

typedef struct socket {ip_pheader_t socket_local;
ip_pheader_t socket_remote;
uint32_t socket_timestamp;
// etc
} socket_t;

int open_socket (socket_t *s) {
  // code
}

is somewhat clearer than having to having to include the "struct xxx yyy" at each point.

I don't know anything about "Typedef" with a capital T.  As wanderson noted, there are all sorts of (local) conventions to indentifying a type as such, and coming up with a name that is easy to understand.

wanderson

#17
Jun 06, 2012, 12:31 am Last Edit: Jun 06, 2012, 12:33 am by wanderson Reason: 1

Quote
So am I right that a uint32_t is identical to an Int i.e both 32 bits long so no difference at all

On an ARM, yes.  On an AVR, they would be different sizes.  


Even on the ARM wouldn't uint32_t be an unsigned value, int be a signed value?  So while they would have the same physical size and could be cast to one another with no loss, they would be interpreted differently by the compiler, correct?
New true random number library available at: http://code.google.com/p/avr-hardware-random-number-generation/

Current version 1.0.1

Resinator

Quote
On an ARM, yes.  On an AVR, they would be different sizes.  If you wanted to hold something like an IP address (32bits for v4), you could use an "int" on an ARM, but you'd have to use a "long" on an AVR: an incompatibility.  Instead, your program should use "uint32_t" for the IP address, and it would run on both AVR and ARM (assuming all other architectural dependencies are fixed as well.


Thats clear thanks
Quote

"typedef" defines a name that acts like a type (ie like "int" or "long"); it's a shorthand convenience, especially when you get to more complex structures (containing other structures, etc.)

So someplace in an AVR include file there is likely to be a line that looks like:
typedef unsigned long uint32_t; that means "treat the name "uint32_t" as a variable type, that is the same as "unsigned long".  The similar file on an ARM might contain "typedef unsigned int uint32_t;"


Not so clear!

Its the code that confuses me

Heres the form for the STM32F4, the name appears after the closing }


//
Code: [Select]
In a header file//
typedef struct
{
        Hours_Spent;                
                               
        Hairs_Pulled_Out;  

}Learning_CTypeDef; //Capital T


Learning_CTypeDef Resinators_Learning_C;

 Resinators_Learning_C.Hours_Spent = 999;
 Resinators_Learning_C.Hairs_Pulled_Out = LOTS;




Code: [Select]
Learning_CTypeDef Resinators_Learning_C;


This makes a structure called Resinators_Learning_C which identical to the Learning_C

Code: [Select]
Resinators_Learning_C.Hours_Spent = 999;
 Resinators_Learning_C.Hairs_Pulled_Out = LOTS;


This populates the structure with the values

What I dont get is the typedef it appears twice once at start with no capital and at the end where I name the structure




wanderson

Where it appears at the end with the Capital the word Typedef is being used as part of the name used to indicate that particular structure.  For instance it could have been created like so, and would still perform the same function

Code: [Select]

typedef struct
{
         Hours_Spent;               
                                 
         Hairs_Pulled_Out;   

} ArbitraryName; //Capital T


ArbitraryName Resinators_Learning_C;

  Resinators_Learning_C.Hours_Spent = 999;
  Resinators_Learning_C.Hairs_Pulled_Out = LOTS;
New true random number library available at: http://code.google.com/p/avr-hardware-random-number-generation/

Current version 1.0.1

Go Up