/* Retrieves the current year, month and day from the RTC. Month, day and week values are numbered starting at 1.  */
void RTC_GetDate(unsigned short *pYear, unsigned char *pMonth, unsigned char *pDay, unsigned char *pWeek)
/* Retrieves the current year, month and day from the RTC. Month, day and week values are numbered starting at 1.  */
void RTC_GetDate(unsigned short *pYear, unsigned char *pMonth, unsigned char *pDay, unsigned char *pWeek)
{
    unsigned int date;
    do {
        date = AT91C_BASE_RTC->RTC_CALR;
    }
    while (date != AT91C_BASE_RTC->RTC_CALR);
    if (pYear) {
        *pYear = (((date  >> 4) & 0x7) * 1000)
                 + ((date & 0xF) * 100)
                 + (((date >> 12) & 0xF) * 10)
                 + ((date >> 8) & 0xF);
    }
    if (pMonth) {
        *pMonth = (((date >> 20) & 1) * 10) + ((date >> 16) & 0xF);
    }
    if (pDay) {
        *pDay = (((date >> 28) & 0x3) * 10) + ((date >> 24) & 0xF);
    }
    if (pWeek) {
        *pWeek = ((date >> 21) & 0x7);
    }
}