How can I convert binary to hex and write the value at a certain address?

-1

I have the following lines of code:

u8 SW = Xil_In8(XPAR_AXI_GPIO_0_BASEADDR);


Xil_Out32(XPAR_AXI4STREAM_TPG_0_S00_AXI_BASEADDR, 0x00000020 + SW);

What I am doing here, is reading from the address XPAR_AXI_GPIO_0_BASEADDR and store the value in SW. The relevant data for me is stored only in the last 4 bits.

What I would like to accomplish, is to write at the other address (XPAR_AXI4STREAM_TPG_0_S00_AXI_BASEADDR) in such a way, as in the value 0x00000020 the last byte (which is now 0) to always contain the current value of SW. Well, the last 4 bits of it. So basically to have something like this 0x0000002SW. How can this be done, so that even if the value of SW changes, the last byte of 0x00000020 to always contain the current value of SW (last 4 bits) instead of 0. What conversions must I perform?

Thank you in advance for any help!

c
sdk
fpga
xilinx
asked on Stack Overflow Jul 14, 2018 by Levente Rigán

1 Answer

0
u8 SW = Xil_In8(XPAR_AXI_GPIO_0_BASEADDR);
u8 last_4_bits = SW & 0xf //gets you the last 4 bits
Xil_Out32(XPAR_AXI4STREAM_TPG_0_S00_AXI_BASEADDR, 0x00000020 | last_4_bits); //OR'ing sets those 4 bits

You can do it in one single line.

Xil_Out32(XPAR_AXI4STREAM_TPG_0_S00_AXI_BASEADDR, 0x00000020 | (SW & 0xf));

I will leave it to you as an exercise to understand the bitwise math operation. It would be a lot easier if you write down the bits on a piece of paper and do the math. Don't do it in your head if you are new to it.

answered on Stack Overflow Jul 14, 2018 by (unknown user)

User contributions licensed under CC BY-SA 3.0