Przeglądaj źródła

更新了MCU工程(iram版本和sram-nos版本)的RTC模块BUSY问题相关代码
1.优化RTC模块等待busy卡死问题。

helen 4 miesięcy temu
rodzic
commit
ecf4dbfb70

+ 2 - 2
amt630hv160-mcu/amt630hv160-mcu-iram/src/ArkmicroFiles/libcpu-amt630hv160/include/amt630hv160_rtc.h

@@ -155,8 +155,8 @@ static inline int is_leap_year(unsigned int year)
 
 /* Exported functions ------------------------------------------------------- */
 //	Sleep and wake-up related
-void RTC_SendCmd(eRTCCmd cmd, int enable);
-void RTC_WriteReg(eRTCCmd cmd, vu32 *reg, u32 value);
+int RTC_SendCmd(eRTCCmd cmd, int enable);
+int RTC_WriteReg(eRTCCmd cmd, vu32 *reg, u32 value);
 void RTC_SetWatchdog(eRTCWdtTmo tmo, int enable);
 void RTC_PowerDown(int fastboot);
 void RTC_CanStbOut(int id, int value);

+ 39 - 6
amt630hv160-mcu/amt630hv160-mcu-iram/src/ArkmicroFiles/libcpu-amt630hv160/source/amt630hv160_rtc.c

@@ -39,27 +39,60 @@ static const unsigned short rtc_ydays[2][13] = {
 
 #define LEAPS_THRU_END_OF(y) ((y)/4 - (y)/100 + (y)/400)
 
-static void RTC_WaitNotBusy(void)
+static int RTC_WaitNotBusy(void)
 {
+	int timeout = 1000;
+
 	TIMER_Udelay(10);
-	while (RTC->RTCSTA & RTC_STA_BUSY);
+	while ((RTC->RTCSTA & RTC_STA_BUSY) && timeout--) {
+		TIMER_Udelay(1);
+	}
+	if (timeout < 0) {
+		printf("RTC_WaitNotBusy timeout.\n");
+		RTC->RTCCTL |= 1;
+		return -1;
+	}
+
+	return 0;
 }
 
-void RTC_SendCmd(eRTCCmd cmd, int enable)
+int RTC_SendCmd(eRTCCmd cmd, int enable)
 {
+	int retries = 10;
+
+retry:
 	RTC->ANAWEN = 1 << (cmd - RTC_PowerOff);
 	if (enable)
 		RTC->ANACTL = 1 << (cmd - RTC_PowerOff);
 	else
 		RTC->ANACTL = 0;
-	RTC_WaitNotBusy();
+	if ((RTC_WaitNotBusy() < 0) && retries--) {
+		goto retry;
+	}
+	if (retries < 0) {
+		printf("RTC_SendCmd %d fail!\n", cmd);
+		return -1;
+	}
+
+	return 0;
 }
 
-void RTC_WriteReg(eRTCCmd cmd, vu32 *reg, u32 value)
+int RTC_WriteReg(eRTCCmd cmd, vu32 *reg, u32 value)
 {
+	int retries = 10;
+
+retry:
 	RTC->ANAWEN = 1 << (cmd - RTC_PowerOff);
 	*(vu32*)reg = value;
-	RTC_WaitNotBusy();
+	if ((RTC_WaitNotBusy() < 0) && retries--) {
+		goto retry;
+	}
+	if (retries < 0) {
+		printf("RTC_WriteReg %d fail!\n", cmd);
+		return -1;
+	}
+
+	return 0;
 }
 
 void RTC_SetWatchdog(eRTCWdtTmo tmo, int enable)

+ 1 - 1
amt630hv160-mcu/amt630hv160-mcu-iram/src/main.c

@@ -88,7 +88,7 @@ static void prvSetupHardware( void )
 	DDR_Init();
 #endif
 #ifdef _RTC
-	RTC_SendCmd(RTC_Clk32KSel, 1);
+	RTC_SendCmd(RTC_Clk32KSel, 0);
 	rtc_time_init();
 #endif
 #ifdef _SPI0

+ 2 - 2
amt630hv160-mcu/amt630hv160-mcu-sram-nos/src/ArkmicroFiles/libcpu-amt630hv160/include/amt630hv160_rtc.h

@@ -155,8 +155,8 @@ static inline int is_leap_year(unsigned int year)
 
 /* Exported functions ------------------------------------------------------- */
 //	Sleep and wake-up related
-void RTC_SendCmd(eRTCCmd cmd, int enable);
-void RTC_WriteReg(eRTCCmd cmd, vu32 *reg, u32 value);
+int RTC_SendCmd(eRTCCmd cmd, int enable);
+int RTC_WriteReg(eRTCCmd cmd, vu32 *reg, u32 value);
 void RTC_SetWatchdog(eRTCWdtTmo tmo, int enable);
 void RTC_PowerDown(int fastboot);
 void RTC_CanStbOut(int id, int value);

+ 39 - 6
amt630hv160-mcu/amt630hv160-mcu-sram-nos/src/ArkmicroFiles/libcpu-amt630hv160/source/amt630hv160_rtc.c

@@ -39,27 +39,60 @@ static const unsigned short rtc_ydays[2][13] = {
 
 #define LEAPS_THRU_END_OF(y) ((y)/4 - (y)/100 + (y)/400)
 
-static void RTC_WaitNotBusy(void)
+static int RTC_WaitNotBusy(void)
 {
+	int timeout = 1000;
+
 	TIMER_Udelay(10);
-	while (RTC->RTCSTA & RTC_STA_BUSY);
+	while ((RTC->RTCSTA & RTC_STA_BUSY) && timeout--) {
+		TIMER_Udelay(1);
+	}
+	if (timeout < 0) {
+		printf("RTC_WaitNotBusy timeout.\n");
+		RTC->RTCCTL |= 1;
+		return -1;
+	}
+
+	return 0;
 }
 
-void RTC_SendCmd(eRTCCmd cmd, int enable)
+int RTC_SendCmd(eRTCCmd cmd, int enable)
 {
+	int retries = 10;
+
+retry:
 	RTC->ANAWEN = 1 << (cmd - RTC_PowerOff);
 	if (enable)
 		RTC->ANACTL = 1 << (cmd - RTC_PowerOff);
 	else
 		RTC->ANACTL = 0;
-	RTC_WaitNotBusy();
+	if ((RTC_WaitNotBusy() < 0) && retries--) {
+		goto retry;
+	}
+	if (retries < 0) {
+		printf("RTC_SendCmd %d fail!\n", cmd);
+		return -1;
+	}
+
+	return 0;
 }
 
-void RTC_WriteReg(eRTCCmd cmd, vu32 *reg, u32 value)
+int RTC_WriteReg(eRTCCmd cmd, vu32 *reg, u32 value)
 {
+	int retries = 10;
+
+retry:
 	RTC->ANAWEN = 1 << (cmd - RTC_PowerOff);
 	*(vu32*)reg = value;
-	RTC_WaitNotBusy();
+	if ((RTC_WaitNotBusy() < 0) && retries--) {
+		goto retry;
+	}
+	if (retries < 0) {
+		printf("RTC_WriteReg %d fail!\n", cmd);
+		return -1;
+	}
+
+	return 0;
 }
 
 void RTC_SetWatchdog(eRTCWdtTmo tmo, int enable)

+ 1 - 1
amt630hv160-mcu/amt630hv160-mcu-sram-nos/src/main.c

@@ -82,7 +82,7 @@ static void prvSetupHardware( void )
 	printf("gpu_pll=%d\n", CLK_GetPLLFreq(SYSCTRL->GPUPLL_CFG));
 
 #ifdef _RTC
-	RTC_SendCmd(RTC_Clk32KSel, 1);
+	RTC_SendCmd(RTC_Clk32KSel, 0);
 	//rtc_time_init();
 #endif
 #ifdef _SPI0