Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Have you used 'struct' in your embedded system?

Status
Not open for further replies.

Maverickmax

Advanced Member level 1
Joined
Dec 6, 2004
Messages
404
Helped
8
Reputation
16
Reaction score
3
Trophy points
1,298
Activity points
3,689
Hi

I have been looking up struct in C language and I cannot see how struct can be used i n embedded system. Have you used 'struct' in embedded system and why?

Maverick Max
 

There is no reason why structs cannot be used in embedded systems. Structs are used to group variables of different data types, or may be used just for organization purposes, ie makes code easier to read. One example is the message packet for wireless comms. Another example is an entry of a table stored in eeprom.
 

structures are widely used is comples embedded system firmwares....


They are used to represent a block of momory / Register /register feilds in a System.

The structures in embedded C must be bit aligned.

I have given a typical embedded application source code which uses structures...


Imagine that you are writing code that controls a hardware device by placing appropriate values in hardware registers at known absolute addresses.

Let's imagine that the device has two registers, each 16 bits long, at ascending memory addresses; the first one is the control and status register (csr) and the second is a data port. The traditional way of accessing such a device is like this:

/* Standard C example */
/*
* Declare the device registers
* Whether to use int or short
* is implementation dependent
*/

struct devregs{
unsigned short csr; /* control & status */
unsigned short data; /* data port */
};

/* bit patterns in the csr */
#define ERROR 0x1
#define READY 0x2
#define RESET 0x4

/* absolute address of the device */
#define DEVADDR ((struct devregs *)0xffff0004)

/* number of such devices in system */
#define NDEVS 4

/*
* Busy-wait function to read a byte from device n.
* check range of device number.
* Wait until READY or ERROR
* if no error, read byte, return it
* otherwise reset error, return 0xffff
*/
unsigned int read_dev(unsigned devno){

struct devregs *dvp = DEVADDR + devno;

if(devno >= NDEVS)
return(0xffff);

while((dvp->csr & (READY | ERROR)) == 0)
; /* NULL - wait till done */

if(dvp->csr & ERROR){
dvp->csr = RESET;
return(0xffff);
}

return((dvp->data) & 0xff);
}



good programers dont need comments to the code ... cos the code is very simple and self explainatory



hoped it helped u


- helios
 

Hi

I have used.
If you have used a RTOS it uses a Link List internally.

Regards
 

Everything in the C language should work fine in an embedded system, except perhaps for library functions that deal with I/O and file access, since those devices may not exist in an embedded system.
 

I have used 'struct' in embeded system (Keil C compiler - AT89C52). It works OK!
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top