cpointerscpu-registers

How to print contents of register in c


I'm trying to debug a driver that I'm writing for a UART that reads a string of chars from a serial console until the user press 'l'. The function is called 'getstring()' below.

I want to examine the contents of a status register to see which bits are set. The status register is offset by 2. I need to print it when 'getstring()' is called. I can use printf().

This is the register map for the UART.

Register_map

When I call the How could I print out the contents of a register in c?

#define UART 0x00080000
void getchar(char *str) {

    volatile uint32_t *uart = (volatile uint32_t*) UART;

    char c = 0;
    do
    {
        while ((uart[2] & (1<<7)) == 0);
        c = uart[0];
        *str++ = c;
    }
    while (c!='l');
}

Solution

  • To convert from binary to an ASCII string of ones and zeroes, simply do this:

    uint32_t local = *uart;
    for(size_t i=0; i<32; i++)
    {
      *str = (local & (1u << 31-i) ? '1' : '0';
      str++;
    }
    *str = '\0';