Reputation: 91
I have question in relation to the line rtc_set_alarm(RTC_0, (RTC[RTC_0].rtc_ccvr + ALARM));
in main()
below.
In the second argument of rtc_set_alarm()
I understand RTC
to be a pointer of struct
type that points to the address 0xB0000400. It then access the first member of the struct by using .rtc_ccvr
.
My question is, why is it necessary to use RTC_0
of the enum rtc_t
.
I would of thought that it would be just RTC.rtc_ccvr
?
Apologies, I'm new to struct pointers.
** Number of RTC controllers. */
typedef enum { RTC_0 = 0, RTC_NUM } rtc_t;
/** RTC register map. */
typedef struct {
RW uint32_t rtc_ccvr; /**< Current Counter Value Register */
RW uint32_t rtc_cmr; /**< Current Match Register */
RW uint32_t rtc_clr; /**< Counter Load Register */
RW uint32_t rtc_ccr; /**< Counter Control Register */
RW uint32_t rtc_stat; /**< Interrupt Status Register */
RW uint32_t rtc_rstat; /**< Interrupt Raw Status Register */
RW uint32_t rtc_eoi; /**< End of Interrupt Register */
RW uint32_t rtc_comp_version; /**< End of Interrupt Register */
} rtc_reg_t;
/* RTC register base address. */
#define RTC_BASE (0xB0000400)
/* RTC register block. */
#define RTC ((rtc_reg_t *)RTC_BASE)
//--------------------------------------------------------------------------
//function declaration
int qm_rtc_set_alarm(const rtc_t rtc, const uint32_t alarm_val)
//--------------------------------------------------------------------------
int main(void)
{
#define ALARM (RTC_ALARM_MINUTE / 6)
rtc_set_alarm(RTC_0, (RTC[RTC_0].rtc_ccvr + ALARM));
}
Upvotes: 1
Views: 130
Reputation: 2590
Let's examine the code a bit. You probably know that an enum
is just a way of giving names to numbers, so every value in an enum
translates to an integer - RTC_0
is exactly the same as 0
in this code. So the call:
rtc_set_alarm(RTC_0, (RTC[RTC_0].rtc_ccvr + ALARM));
is identical to
rtc_set_alarm(RTC_0, (RTC[0].rtc_ccvr + ALARM));
Now what is RTC
? It's a pointer to a struct, but there is no information about how many register sets of rtc_reg_t
type there are. So if you have three such register sets, it would be equally valid to do:
rtc_set_alarm(RTC_0, (RTC[2].rtc_ccvr + ALARM));
So the purpose of writing RTC[RTC_0]
is to explicitly access the first RTC controller's register, and to access others in the same way, if there were any. In your example there is only one, and it's possible that rtc_set_alarm
checks that you're not accessing index RTC_NUM
or beyond.
Upvotes: 0
Reputation: 50110
its so you can have more than one RTC. To access the second one you would use
rtc_set_alarm(RTC_1, (RTC[RTC_1].rtc_ccvr + ALARM));
Its nothing to do with the fields inside the struct, its which struct to use
Upvotes: 2