简体   繁体   English

C程序-结构指针

[英]c program - struct pointer

I have question in relation to the line rtc_set_alarm(RTC_0, (RTC[RTC_0].rtc_ccvr + ALARM)); 我对rtc_set_alarm(RTC_0, (RTC[RTC_0].rtc_ccvr + ALARM));行有疑问rtc_set_alarm(RTC_0, (RTC[RTC_0].rtc_ccvr + ALARM)); in main() below. 在下面的main()中。

In the second argument of rtc_set_alarm() I understand RTC to be a pointer of struct type that points to the address 0xB0000400. rtc_set_alarm()的第二个参数中,我理解RTC是指向地址0xB0000400的struct类型的指针。 It then access the first member of the struct by using .rtc_ccvr . 然后,它使用.rtc_ccvr访问结构的第一个成员。

My question is, why is it necessary to use RTC_0 of the enum rtc_t . 我的问题是,为什么有必要使用RTC_0的的enum rtc_t

I would of thought that it would be just RTC.rtc_ccvr ? 我认为这只是RTC.rtc_ccvr吗?

Apologies, I'm new to struct pointers. 抱歉,我是struct指针的新手。

** 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));
}

its so you can have more than one RTC. 因此,您可以拥有多个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 它与该结构内部的字段无关,它使用哪个结构

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. 您可能知道enum只是给数字命名的一种方式,因此enum每个值都转换为整数RTC_0在此代码中与0完全相同。 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 ? 现在什么是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. 它是一个指向结构的指针,但是没有关于rtc_reg_t类型的寄存器集多少的信息。 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. 因此,写入RTC[RTC_0]的目的是显式访问第一个RTC控制器的寄存器,并以相同的方式访问其他寄存器(如果有)。 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. 在您的示例中,只有一个,并且rtc_set_alarm可能检查您是否没有访问索引RTC_NUM或更高。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM