2012-07-18 01:11 PM
Hi I am trying to write date and time to the RTC which I later will read.
But something in my RTC configuration doesn’t work and reading the values in debug mode shows that the RTC registers that I try to set never get the new values.
Here is my code:
void Set_RTC(){/* Enable the PWR clock */RCC_APB1ENR.PWREN = 1;/* Allow access to RTC */PWR_CR.DBP = 1;/* LSE used as RTC source clock */RCC_BDCR.LSEON = 1; //External low-speed oscillator enable /* Wait till LSE is ready */while(RCC_BDCR.LSERDY != 1){}/* Select the RTC Clock Source to LSE */RCC_BDCR.F8 = 1;RCC_BDCR.F9 = 0;/* Wait for RTC APB registers synchronisation */while(RTC_ISR.RSF != 1){}RCC_BDCR.RTCEN = 1; //Enable RTC clock <<<RTC_WPR = 0xCA; //unlock write protectionRTC_WPR = 0x53; //unlock write protection/* Configure the RTC prescaler */RTC_PRER = 0x7f00ff; // set SynchPrediv to FF and AsynchPrediv to 7F//RCC_BDCR.RTCEN = 1; //Enable RTC clock <<<RTC_ISR.INIT = 1; //enter initialization mode <<<<<< THIS REGISTER NEVER GETS THE NEW VALUEwhile(RTC_ISR.INITF != 1) //poll INITF{}/* Configure the RTC PRER */ RTC_PRER = 0x7F; RTC_PRER |= 0xFF << 16; RTC_TR = 0x123500; //setting time to 12.35.00 RTC_DR = 0x126718; // set date to 2012-07-18 RTC_CR.F6 = 0; // set FMT 24H format RTC_ISR.INIT = 0; //exit initialization mode /* Enable the write protection for RTC registers */ RTC_WPR = 0xFF;}I am using a STM32F407 @ 140Mz internal HSI oscillator.
What am I doing wrong?
2015-09-28 02:14 AM
The problem is in this function
/** * @brief Enters the RTC Initialization mode. * @note The RTC Initialization mode is write protected, use the * RTC_WriteProtectionCmd(DISABLE) before calling this function. * @param None * @retval An ErrorStatus enumeration value: * - SUCCESS: RTC is in Init mode * - ERROR: RTC is not in Init mode */ErrorStatus RTC_EnterInitMode(void){ __IO uint32_t initcounter = 0x00; ErrorStatus status = ERROR; uint32_t initstatus = 0x00; /* Check if the Initialization mode is set */ if ((RTC->ISR & RTC_ISR_INITF) == (uint32_t)RESET) { /* Set the Initialization mode */ RTC->ISR = (uint32_t)RTC_INIT_MASK; /* Wait till RTC is in INIT state and if Time out is reached exit */ do { initstatus = RTC->ISR & RTC_ISR_INITF; initcounter++; } while((initcounter != INITMODE_TIMEOUT) && (initstatus == 0x00)); if ((RTC->ISR & RTC_ISR_INITF) != RESET) { status = SUCCESS; } else { status = ERROR; } } else { status = SUCCESS; } return (status); }NOTESin the do...whileinitstatus = RTC->ISR & RTC_ISR_INITF;
ALWAYS IS 0/* Wait till RTC is in INIT statet */RTC does not go to INIT state and the return status is ERROR2015-09-28 05:55 AM
The problem is observable there, the real issue is likely upstream.
2015-09-28 06:15 AM
2015-09-28 09:03 AM
You can't probe the crystal directly.
Try attached test code, outputs via USART3 at 9600 8N1, post back the output The LSE clock should be output via PA8 (MCO1), you should scope that. ________________ Attachments : USART3-LSE-FORUM-REL1.hex : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I0RD&d=%2Fa%2F0X0000000bi9%2FcmRw9ukRTFimvhsM.FdlcKWSdjvDKF40t67qe8vRYrM&asPdf=false2015-09-29 05:59 AM
The attached file is
and I cannot insert in my project I am creating code to use PA8 to view LSE and I will reply as soon as possible2015-09-29 06:30 AM
You don't need to add it to your project, just download it into the processor (using for example the ST-Link Utility) and run.
JW2015-09-29 06:34 AM
/* With this code I see on PA8 pin 32.7 Khz Clock */
/* Enable the GPIO_Clock */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); /* Configure the GPIO_pin */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_MCO); RCC_MCO1Config(RCC_MCO1Source_LSE , RCC_MCO1Div_1);Do you have other suggestions?Carlo2015-09-29 08:32 AM
Do you have other suggestions?
I'd like to see the output from the code I provided.2015-09-30 12:37 AM
HELLO,
I HAVE DOWNLOADED THE CODE IN MY BOARD TROUGH THE PROGRAM ''FLASH LOADER DEMO'' BUT UNFORTUNATELY 2 THINGS HAVE HAPPENED.- I HAD NOT ANSWERS FROM THE BOARD - I AM NOT ABLE TO LOAD THE PREVIOUS PROGRAM WHAT KIND OF ANSWERS YOU EXPECT FROM THE SERIAL AND WHICH INFORMATION WE COULD GIVE?- THEREFORE I EQUIP TO PREPARE A NEW CARD2015-09-30 04:15 AM
You should be able to set BOOT0 High, and none of this code should run. And presumably you can JTAG/SWD into it and erase it too.
I'm trying out a couple of working theories about why your system isn't working, I understand all the code that's built in and the tools used to build it, so I'm not trying to troubleshoot the issue with a keyhole perspective.It should initialize the board based on your clocks, pins and USART, and it should test and time the LSE wrt the internal references. Based on the totality of the output, it might provide some clues about where things are broken.The code fragment you have provided, and others have provided, has been tested on working hardware, it works, so there's clearly some other system level issue at play here. I'm not looking to play 20-Questions to do a system review and figure out the hardware/software interactions here.