Troubles when mapping registers into microblaze address space

Hallo, I have developed an opb spi 16 bit peripheral with regiter support to interface it to bus.

Here c code:

unsigned int (*control_reg) = (unsigned int(*))XPAR_OPB_SPI_16_BIT_0_BASEADDR + OPB_SPI_16_BIT_SLAVE_REG2_OFFSET;

int main (void) {

(*control_reg) = 10;

xil_printf("%X %X\n", &(*control_reg), XPAR_OPB_SPI_16_BIT_0_BASEADDR + OPB_SPI_16_BIT_SLAVE_REG2_OFFSET);

Using "unsigned int" the two address to be printed don't match. If I use "unsigned char" they match, but in this way I truncate some datas because they are 16 bit registers.

What could I do?

Many Thanks Marco

Reply to
Marco
Loading thread data ...

I solved a part of my trouble.

Here the c code:

#define OPB_SPI_16_BIT_SLAVE_REG0 (XPAR_OPB_SPI_16_BIT_0_BASEADDR + OPB_SPI_16_BIT_SLAVE_REG0_OFFSET) #define OPB_SPI_16_BIT_SLAVE_REG1 (XPAR_OPB_SPI_16_BIT_0_BASEADDR + OPB_SPI_16_BIT_SLAVE_REG1_OFFSET) #define OPB_SPI_16_BIT_SLAVE_REG2 (XPAR_OPB_SPI_16_BIT_0_BASEADDR + OPB_SPI_16_BIT_SLAVE_REG2_OFFSET)

unsigned int (*buffer_rx) = (unsigned int(*))OPB_SPI_16_BIT_SLAVE_REG0; unsigned int (*buffer_tx) = (unsigned int(*))OPB_SPI_16_BIT_SLAVE_REG1; unsigned int (*control_reg) = (unsigned int(*))OPB_SPI_16_BIT_SLAVE_REG2;

int main (void) {

(*control_reg) = 125;

xil_printf("%d\n", (*control_reg)); xil_printf("%d\n", OPB_SPI_16_BIT_mReadSlaveReg2(XPAR_OPB_SPI_16_BIT_0_BASEADDR));

Now I should see:

125 125

Instead, even if now the address of (*control_reg) seems to be correct, I see:

125 0

What could I do?

Many Thanks Marco

Reply to
Marco

  • OPB_SPI_16_BIT_SLAVE_REG2_OFFSET;

OPB_SPI_16_BIT_SLAVE_REG2_OFFSET);

Hi Marco,

You're doing pointer arithmatic here, which is not the same as just adding addresses. If you add a number to a pointer of a specific type, the pointer will be increased by the number times the size of the type.

for example, on a 32 bit system, the following snippet of code

int *p = 100; p = p + 5; wil result in p being 120, not 105 as you might expect, since sizeof(int) equals

4, and 100 + (5*4) = 120.

If you want to add a specific number of bytes to a pointer, typecase the pointer to char before adding :

int *p = 100; p = (char *)p + 5; will result in p = 105.

_Ico

--
:wq
^X^Cy^K^X^C^C^C
Reply to
usenet

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.