Forum Discussion
Altera_Forum
Honored Contributor
16 years agoExcept that such code breaks the 'strict aliasing' rules of C, and gcc might optimise away the assignment.
Better would be a cast on the LHS, eg: *(volatile float *)SQRT_BASE = data_in; An alternative is to define a C structure, and variable of a type that matches the IO device, and then get the linker to assign a fixed address (or use a pointer). eg:
extern struct {
volatile float sqrt_in;
volatile float sqrt_out;
} *sqrt_device = (void *)(1u << 31 | SQRT_BASE);
...
sqrt_device->sqrt_in = data_in;
delay();
printf("%f\n", sqrt_device->sqrt_out);
IMHO using structures to access hardware devices is much less error prone than trying to use constants and access macros. Although not 100% portable, it is portable enough.