0

I have this line of C code from a real time clock (RTC) driver for a SoC chip from Nordic. I tried to look for what it does but it's a bit difficult to search. Can anyone please let me know what does it do? Especially the parentheses and asterisk.

#define NRF_RTC0                        ((NRF_RTC_Type            *) NRF_RTC0_BASE)

NRF_RTC_Type is a typedef of a struct and NRF_RTC0_BASE is #define'ed as 0x4000B000UL.

Thank you!

jleng
  • 55
  • 1
  • 7
  • It's just a simple text replacement, it doesn't do anything special. – Barmar Apr 29 '21 at 00:07
  • It's a typecast, casting `NRF_RTC0_BASE` to a pointer to `NRF_RTC_TYPE` – Barmar Apr 29 '21 at 00:08
  • What do you mean by `NRF_RTC0_BASE is typedef'ed as 0x4000B000UL`? You can't typedef to a value, you can only typedef to a type. – Barmar Apr 29 '21 at 00:09
  • If that's a mystery that's not a bad thing, but it sure is a sign you need a good [reference book](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) on how C works. We can't explain all of C in an answer here, you're going to need to do a lot of learning to understand what's going on. This isn't all that complex, but there's a lot of principles to absorb before any explanation we give could make any sense. – tadman Apr 29 '21 at 00:10
  • @Barmar Thanks for the answer. It makes sense. – jleng Apr 29 '21 at 00:15
  • 1
    @tadman. Thank you for sharing this. I'll definitely work on my C. – jleng Apr 29 '21 at 00:15
  • See this: [How to access a hardware register from firmware?](https://electrical.codidact.com/posts/276290) – Lundin Apr 29 '21 at 08:25
  • @Lundin This is great! Thank you! – jleng Apr 29 '21 at 17:44

1 Answers1

1

NRF_RTC0_BASE identifies a set of hardware registers located starting at address 4000B000. NRF_RTC_Type defines the mapping of the individual registers, and it probably adds the volatile qualifier to ensure that they are treated as I/O and not ordinary memory.

The parentheses make a typecast operation and the asterisk signifies a pointer type.

Use the macro NRF_RTC0 to access the registers in a structured fashion.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421