LectEPE2206 A C Programming Struct
LectEPE2206 A C Programming Struct
Microprocessors
C Structures
Bit Fields
C Structures
Introduction
Structures
● Collections of related variables under one name
● Can contain variables of different data types
Syntax error
Inserting space between the - and > components of the
structure pointer operator (or between the components of
any other multiple keystroke operator) is a syntax error.
EPE2206 Computer Systems and
Microprocessors
Bit Fields
Bit Fields
Bit field
● Specifies how many bits a field of a struct uses
● Enables better memory utilization
● Essential when modeling hardware
● Must be defined as int or unsigned
15 7 6 3 2 1 0
CSR
ERROR
CONTROL READY
INTERRUPT ENABLE
FUNCTION
GO
Hard drive struct example (3)
Code:
int *csr;
*csr = *csr | 4;
Hard drive struct example (4)
Firstly, we put the least significant bit first, then we add some
fillers for the parts we are not interested in:
struct cntl_stat {
int go: 1;
int func: 3;
int filler1: 2;
int intr_enable: 1;
int cont_ready: 1;
int filler2: 7;
int error: 1;
};
After this declaration, we can now refer to the bits using their
name, after declaring the appropriate variables:
pt->func = 2;
But we must make sure that the UART is not still busy
with the previous character. Again we can make use of
the Status register to discover the state of the device.
// function prototypes
char readUart();
void writeUart(char);
struct epci {
int status;
int mode;
int tx;
int rx;
};
for(;;) {
if ((uart->status & 2) != 0){
// read it, and remove parity bit
c = uart->rx & 127;
return(c);
}
}
}
Programming the UART (5)
// main loop
main() {
char c;
for(;;) {
c = readUart();
writeUart(c);
}
}