Skip to content

Instantly share code, notes, and snippets.

@goog
Created April 22, 2019 10:53
Show Gist options
  • Save goog/29b1a18fb99f308ab8887daa0b731f08 to your computer and use it in GitHub Desktop.
Save goog/29b1a18fb99f308ab8887daa0b731f08 to your computer and use it in GitHub Desktop.
void rtc_init(int minor)
{
printk("rtc \n");
volatile uint32_t *RCC_APB1ENR = 0x40023840;
volatile uint32_t *RCC_BDCR = 0x40023870;
uint16_t retry = 0X1FFF;
*RCC_APB1ENR |= 1<<28; // Power interface clock enable
rtc_set_dbp();
//if(rtc_read_bkr(0) != 0x5050)
if(1)
{
*RCC_BDCR |= 1<<0; //LSE enable
while(retry && (((*RCC_BDCR) & 0x02) == 0)) // External low-speed oscillator ready
{
retry--;
//delay_ms(5);
usleep(5000);
}
printk("after while BDCR %08x\n", *RCC_BDCR);
if(retry == 0)
{
return 1;
}
*RCC_BDCR |=1<<8; //LSE as rtc clock
*RCC_BDCR |=1<<15; // RTC clock enable
printk("BDCR %08x\n", *RCC_BDCR);
// access write
rtc_write_enable();
printk("after rtc_write_enable\n");
if(rtc_enter_init_mode()) return 2; // rtc into init mode
printk("enter init mode\n");
STM32F4_RTC_PRER = 0xff;
STM32F4_RTC_PRER |= 0x007F<<16;
//STM32F4_RTC_PRER = 0xff;
//STM32F4_RTC_PRER = 0x007F00ff;
printk("prer %08x\n", STM32F4_RTC_PRER);
STM32F4_RTC_CR &= ~(1<<6); // 24h Hour format
// set wakeup clock
// Disable the wake-up counter
printk("wakeup block begin\n");
STM32F4_RTC_CR &= ~STM32F4_RTC_CR_WUTE;
// Wait for the RTC WUTWF flag is set or timeout
int wait = RTC_INIT_TIMEOUT;
while (!(STM32F4_RTC_ISR & STM32F4_RTC_ISR_WUTWF) && --wait);
if(!(STM32F4_RTC_ISR & STM32F4_RTC_ISR_WUTWF))
{
rtc_write_disable();
rtc_clear_dbp();
return 1;
}
printk("will sett wutr\n");
STM32F4_RTC_WUTR = 3;
STM32F4_RTC_WUTR = 3;
printk("before set CR %08X\n", STM32F4_RTC_CR);
// disable alarm a
STM32F4_RTC_CR &= ~(1<<8);
STM32F4_RTC_CR &= (uint32_t)~(7); // clear lowest 3bits
//STM32F4_RTC_CR |= 0b100;
STM32F4_RTC_CR |= 0x00000004; // (uint32_t)0x00000004
printk("CR %08X \n", STM32F4_RTC_CR);
printk("WUTR %08X\n", STM32F4_RTC_WUTR);
STM32F4_RTC_ISR &= ~STM32F4_RTC_ISR_WUTF; // clear Wakeup timer flag
STM32F4_RTC_CR |= STM32F4_RTC_CR_WUTIE;
STM32F4_RTC_CR |= STM32F4_RTC_CR_WUTE; // enable wakeup timer
printk("CR %08X \n", STM32F4_RTC_CR);
//usleep(1000);
STM32F4_RTC_ISR &= ~(1<<7); // exit init mode
printk("isr value %08x \n", STM32F4_RTC_ISR);
volatile uint32_t *EXIT_PR = (uint32_t *)0x40013C14;
(*EXIT_PR) = 1<<22;
//printk("clear line 22 exit flag \n");
// open it
(*(uint32_t*)0x40013C00) |= (1<<22);
(*(uint32_t*)0x40013C08) |= (1<<22);
(*(uint32_t*)0xE000ED0C) = 0x05FA0000 | 0x400;
(*(uint8_t*)0xE000E403) = 0xe0;
// nvic enable interrupter number
// 0xE000E100
(*(uint32_t*)0xE000E100) |= (1<<3);
rtc_write_disable();
rtems_time_of_day time_set_t = {0};
rtems_time_of_day r;
time_set_t.second = 6;
time_set_t.minute = 6;
time_set_t.hour = 23;
time_set_t.day = 19;
time_set_t.month = 4;
time_set_t.year = 2019;
stm32f4_rtc_settime(0, &time_set_t);
#if 1
stm32f4_rtc_gettime(0, &r);
printk("Secs %d",r.second);
printk("Mins %d",r.minute);
printk("Hours %d",r.hour);
printk("Days %d",r.day);
printk("Months %d",r.month);
printk("Years %d\n", r.year);
#endif
printk("final CR %08X \n", STM32F4_RTC_CR);
rtc_write_bkr(0, 0x5050);
}
rtc_clear_dbp();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment