Forum Discussion
Altera_Forum
Honored Contributor
12 years agoAlright, maybe I'll try the Socratic method :)
There's a defined base address in the BSP system.h file, which I'll call BASE as an example. Because it's a volatile address, I'll call a pointer in a macro or as a const, like this,#define PerBase ((uint32_t volatile *)BASE) or uint32_t volatile *const per_base = (uint32_t *) BASE; In either case, we have a pointer to a volatile unsigned int at the peripherals base address. The base address can now be accessed like this,
uint32_t data;
*per_base = data; /* write to peripheral*/
data = *per_base; /* read from peripheral*/
Based on the previous advice, this looks like a good method for providing un-cached I/O access to the registers and please correct me if I'm wrong. However, I'm still stuck on how to implement a portable solution for copying the data from bit-fields to an unsigned int. I understand that a union of the bit-field struct type and uint32_t can be used, union reg
{
struct fields
{
uint32_t data0 : 12;
uint32_t data1 : 1;
...
};
uint32_t all_data;
}; but that doesn't eliminate the bit-field problems with regards to portability. An options which I did think about, but I'm not quite sure will work, is to create a structure similar to the bit-field one, but without breaking apart the members and just keeping them as unsigned ints. I would then create a function for each register which would shift the values into the correct position for their associated register, mask the members together, and then copy them over using the volatile uint32_t pointer to the base address. Does this seem like a viable solution? Any thoughts on portability?