Explorar el Código

更新CPU和MCU工程(iram版本和sram-nos版本)的RTC的实时时钟相关代码
1.CPU获取RTC的实时时钟由获取tm结构体(DDR共享内存)修改为直接获取RTC CNT寄存器值(mailbox传递)。
(原做法内部逻辑违背了MCU操作DDR使用方式)
2.优化MCU工程中调用rtc_time_init的时序,移到CPU启动后。

helen hace 6 meses
padre
commit
715a9a561f

+ 5 - 5
amt630hv160-freertos-beta/ArkmicroFiles/libboard-amt630hv160/include/access_module.h

@@ -12,7 +12,7 @@
 #endif
 
 #ifdef CPU_ACCESS_MODULE_SUPPORT
-//#define ACCESS_RTC_SUPPORT
+#define ACCESS_RTC_SUPPORT
 #endif
 
 #ifdef ACCESS_FLASH_SUPPORT
@@ -46,8 +46,8 @@ typedef enum {
 } AccessRtcType;
 
 typedef struct {
-	u32 mem_addr;
-	u32 mem_size;
+	u32 rtc_cnt_h;
+	u32 reserved;
 	u32 access_type;
 } AccessRtcMsg;
 #endif
@@ -56,7 +56,7 @@ typedef enum {
 	AMT_STATUS,
 	AMT_FLASH,
 	AMT_CRC,
-	AMT_RTC,
+	AMT_RTC = 16,
 } AccessModuleType;
 
 typedef enum {
@@ -78,6 +78,6 @@ typedef struct {
 void McuAccessModule(void);
 #endif
 #ifdef ACCESS_RTC_SUPPORT
-int AccessRTC(AccessRtcType type, SystemTime_t *tm);
+int AccessRTC(AccessRtcType type, uint32_t *time);
 #endif
 #endif

+ 17 - 9
amt630hv160-freertos-beta/ArkmicroFiles/libboard-amt630hv160/source/access_module.c

@@ -138,7 +138,7 @@ void McuAccessModule(void)
 #endif
 
 #ifdef CPU_ACCESS_MODULE_SUPPORT
-int WaitAccessModuleRsp(AccessModuleType type, u32 *result)
+int WaitAccessModuleRsp(AccessModuleType type, u32 *result, void *rsp_data)
 {
 	mb_rxmsg_t msg = {0};
 	AccessModuleHeader *header;
@@ -153,6 +153,10 @@ int WaitAccessModuleRsp(AccessModuleType type, u32 *result)
 		if (header->len != msg.dl) {
 			printf("%s, mailbox rev header info access length err.\n", __func__);
 			return -1;
+		} else {
+			if (type == AMT_RTC && rsp_data && (msg.dl == (sizeof(AccessModuleHeader) + sizeof(AccessRtcMsg)))) {
+				memcpy(rsp_data, (void *)(msg.data + sizeof(AccessModuleHeader)), sizeof(AccessRtcMsg));
+			}
 		}
 
 		if (header->msg_status == AMRMS_LEN_ERROR) {
@@ -169,6 +173,8 @@ int WaitAccessModuleRsp(AccessModuleType type, u32 *result)
 
 		return 0;
 	}
+
+	printf("%s, mailbox receive timeout.\n",__func__);
 	return -1;
 }
 
@@ -185,7 +191,8 @@ int SendAccessModuleReq(AccessModuleType type, void *msg, u32 msg_len)
 	header = (AccessModuleHeader *)buf;
 	header->type = type;
 	header->len = sizeof(AccessModuleHeader) + msg_len;
-	memcpy((void *)(buf + sizeof(AccessModuleHeader)), msg, msg_len);
+	if (msg)
+		memcpy((void *)(buf + sizeof(AccessModuleHeader)), msg, msg_len);
 
 	if (mailbox_msg_send(MB_MSG_T_CPU_MODULE_ACCESS, buf, header->len, pdMS_TO_TICKS(50))) {
 		printf("%s, mailbox send frame fail.\n", __func__);
@@ -196,34 +203,35 @@ int SendAccessModuleReq(AccessModuleType type, void *msg, u32 msg_len)
 }
 
 #ifdef ACCESS_RTC_SUPPORT
-int AccessRTC(AccessRtcType type, SystemTime_t *tm)
+int AccessRTC(AccessRtcType type, uint32_t *time)
 {
 	int ret = -1;
 	AccessRtcMsg tx_msg = {0};
+	AccessRtcMsg *rx_msg = NULL;
+	AccessRtcMsg msg = {0};
 
 	tx_msg.access_type = type;
-	tx_msg.mem_addr = DDR_CPU_ACCESS_MODULE_ADDR;
-	tx_msg.mem_size = sizeof(SystemTime_t);
 
 	if (type == ART_SET_TIME)
-		memcpy((void *)DDR_CPU_ACCESS_MODULE_ADDR, tm, tx_msg.mem_size);
+		tx_msg.rtc_cnt_h = *time;
+	else if (type == ART_GET_TIME)
+		rx_msg = &msg;
 
 	if (SendAccessModuleReq(AMT_RTC, &tx_msg, sizeof(AccessRtcMsg))) {
 		printf("%s, send frame fail.\n", __func__);
 		return -1;
 	}
 
-	if (WaitAccessModuleRsp(AMT_RTC, (u32 *)&ret)) {
+	if (WaitAccessModuleRsp(AMT_RTC, (u32 *)&ret, rx_msg)) {
 		printf("%s, wait rex frame abnormal.\n", __func__);
 		return -1;
 	}
 
 	if(!ret && type == ART_GET_TIME)
-		memcpy(tm, (void *)DDR_CPU_ACCESS_MODULE_ADDR, tx_msg.mem_size);
+		*time = rx_msg->rtc_cnt_h;
 
 	return ret;
 }
-
 #endif
 #endif
 #endif

+ 9 - 13
amt630hv160-freertos-beta/ArkmicroFiles/libcpu-amt630hv160/source/rtc.c

@@ -314,12 +314,14 @@ static void rtc_update_time(unsigned int time)
 static int rtc_read_time(struct rtc_time *tm)
 {
 	unsigned int time;
-
+#ifdef ACCESS_RTC_SUPPORT
+	AccessRTC(ART_GET_TIME, &time);
+#else
 	/* we don't report wday/yday/isdst ... */
 	rtc_wait_not_busy();
 
 	time = readl(REGS_RTC_BASE + RTC_CNTH);
-
+#endif
 	rtc_time_to_tm(time, tm);
 
 	return 0;
@@ -334,16 +336,18 @@ static int rtc_read_time(struct rtc_time *tm)
  */
 static int rtc_set_time(struct rtc_time *tm)
 {
-	long unsigned int time;
+	unsigned int time;
 
 	if (rtc_valid_tm(tm) < 0)
 		return -EINVAL;
 
 	/* convert tm to seconds. */
 	time = rtc_tm_to_time(tm);
-
+#ifdef ACCESS_RTC_SUPPORT
+	return AccessRTC(ART_SET_TIME, &time);
+#else
 	rtc_update_time(time);
-
+#endif
 	return 0;
 }
 
@@ -473,14 +477,10 @@ int iGetLocalTime(SystemTime_t *tm)
 	if(tm == NULL)
 		return -1;
 
-#ifdef ACCESS_RTC_SUPPORT
-	return AccessRTC(ART_GET_TIME, tm);
-#else
 	rtc_read_time(tm);
 	tm->tm_year += 1900;
 	tm->tm_mon += 1;
 	return 0;
-#endif
 }
 
 int iSetLocalTime(SystemTime_t *tm)
@@ -488,12 +488,8 @@ int iSetLocalTime(SystemTime_t *tm)
 	if(tm == NULL)
 		return -1;
 
-#ifdef ACCESS_RTC_SUPPORT
-	return AccessRTC(ART_SET_TIME, tm);
-#else
 	tm->tm_year -= 1900;
 	tm->tm_mon -= 1;
 	rtc_set_time(tm);
 	return 0;
-#endif
 }

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

@@ -170,6 +170,6 @@ int RTC_ReadSleepData(void);
 int rtc_time_init(void);
 int iGetLocalTime(SystemTime_t *tm);
 void vSetLocalTime(SystemTime_t *tm);
-
-
+uint32_t CpuGetLocalTime(void);
+void CpuSetLocalTime(uint32_t time);
 #endif /* __AMT630HV160_RTC_H */

+ 15 - 3
amt630hv160-mcu/amt630hv160-mcu-iram/src/ArkmicroFiles/libcpu-amt630hv160/source/amt630hv160_rtc.c

@@ -497,10 +497,10 @@ static void rtc_update_time(unsigned int time)
 
 	//wait rtc_busy;
 	RTC_WaitNotBusy();
-	while(RTC->RAM_H != time) {
+	while(RTC->CNT_H != time) {
 		if (timeout-- == 0)
 			break;
-		//taskYIELD();
+		taskYIELD();
 	}
 }
 
@@ -666,4 +666,16 @@ void vSetLocalTime(SystemTime_t *tm)
 	tm->tm_year -= 1900;
 	tm->tm_mon -= 1;
 	rtc_set_time(tm);
-}
+}
+
+uint32_t CpuGetLocalTime(void)
+{
+	RTC_WaitNotBusy();
+
+	return RTC->CNT_H;
+}
+
+void CpuSetLocalTime(uint32_t time)
+{
+	rtc_update_time(time);
+}

+ 36 - 20
amt630hv160-mcu/amt630hv160-mcu-iram/src/Drivers/access_module.c

@@ -8,15 +8,26 @@
 #ifdef _MAILBOX
 #define ACCESS_DEBUG(fmt, args...)		//printf(fmt, ##args)
 #ifdef CPU_ACCESS_MODULE_SUPPORT
-static int SendAccessModuleRsp(AccessModuleType type, uint32_t result, AccessModuleRespondMsgStatus msg_status)
+static int SendAccessModuleRsp(AccessModuleType type, uint32_t result, AccessModuleRespondMsgStatus msg_status, void *rsp_data)
 {
-	AccessModuleHeader header = {0};
+	u8 buf[26] = {0};
+
+	AccessModuleHeader *header = (AccessModuleHeader *)buf;
 
-	header.result = result;
-	header.type = type;
-	header.len = sizeof(AccessModuleHeader);
-	header.msg_status = msg_status;
-	if (mailbox_msg_send(MB_MSG_T_CPU_MODULE_ACCESS, (const uint8_t *)&header, header.len, pdMS_TO_TICKS(50))) {
+	header->result = result;
+	header->type = type;
+	header->len = sizeof(AccessModuleHeader);
+	header->msg_status = msg_status;
+	if (rsp_data) {
+		if (type == AMT_RTC) {
+			header->len += sizeof(AccessRtcMsg);
+			memcpy((void *)(buf + sizeof(AccessModuleHeader)), rsp_data, sizeof(sizeof(AccessRtcMsg)));
+		} else {
+			printf("%s, para error.\n", __func__);
+		}
+	}
+
+	if (mailbox_msg_send(MB_MSG_T_CPU_MODULE_ACCESS, (const uint8_t *)header, header->len, pdMS_TO_TICKS(50))) {
 		printf("%s, mailbox send frame fail.\n", __func__);
 		return -1;
 	}
@@ -26,26 +37,31 @@ static int SendAccessModuleRsp(AccessModuleType type, uint32_t result, AccessMod
 #ifdef ACCESS_RTC_SUPPORT
 static void CpuAccessRtc(AccessRtcMsg *rx_msg)
 {
-	SystemTime_t *tm = (SystemTime_t *)rx_msg->mem_addr;
-
-	if (rx_msg->mem_size != sizeof(SystemTime_t)) {
-		printf("mailbox receive access rtc size error.\n");
-		SendAccessModuleRsp(AMT_RTC, 0, AMRMS_PARA_ERROR);
-	}
+	AccessRtcMsg tx_msg= {0};
 
 	if (rx_msg->access_type == ART_GET_TIME) {
-		iGetLocalTime(tm);
-		ACCESS_DEBUG("Time: %d-%.2d-%.2d %.2d:%.2d:%.2d.\n", tm->tm_year, tm->tm_mon,
-				tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
+		tx_msg.rtc_cnt_h = CpuGetLocalTime();
+#if 0
+		SystemTime_t tm;
+		rtc_time_to_tm(tx_msg.rtc_cnt_h, &tm);
+		tm.tm_year += 1900;
+		tm.tm_mon += 1;
+		ACCESS_DEBUG("Time: %d-%.2d-%.2d %.2d:%.2d:%.2d.\n", tm.tm_year, tm.tm_mon,
+							tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
+
+		iGetLocalTime(&tm);
+		ACCESS_DEBUG("Time: %d-%.2d-%.2d %.2d:%.2d:%.2d.\n", tm.tm_year, tm.tm_mon,
+					tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
+#endif
 	} else if (rx_msg->access_type == ART_SET_TIME) {
-		vSetLocalTime(tm);
+		CpuSetLocalTime(rx_msg->rtc_cnt_h);
 	} else {
 		printf("%s, mailbox receive access rtc type error.\n", __func__);
-		SendAccessModuleRsp(AMT_RTC, 0, AMRMS_PARA_ERROR);
+		SendAccessModuleRsp(AMT_RTC, 0, AMRMS_PARA_ERROR, NULL);
 		return;
 	}
 
-	SendAccessModuleRsp(AMT_RTC, 0, AMRMS_NO_ERROR);
+	SendAccessModuleRsp(AMT_RTC, 0, AMRMS_NO_ERROR, &tx_msg);
 }
 #endif
 
@@ -75,7 +91,7 @@ void CpuAccessModuleThread(void *para)
 
 		if (msg.dl != header_size + msg_len) {
 			printf("%s, mailbox rev msg length err.\n", __func__);
-			SendAccessModuleRsp(header->type, 0, AMRMS_LEN_ERROR);
+			SendAccessModuleRsp(header->type, 0, AMRMS_LEN_ERROR, NULL);
 			continue;
 		}
 

+ 3 - 3
amt630hv160-mcu/amt630hv160-mcu-iram/src/Drivers/access_module.h

@@ -46,8 +46,8 @@ typedef enum {
 } AccessRtcType;
 
 typedef struct {
-	u32 mem_addr;
-	u32 mem_size;
+	u32 rtc_cnt_h;
+	u32 reserved;
 	u32 access_type;
 } AccessRtcMsg;
 #endif
@@ -63,7 +63,7 @@ typedef enum {
 	AMT_STATUS,
 	AMT_FLASH,
 	AMT_CRC,
-	AMT_RTC,
+	AMT_RTC = 16,
 } AccessModuleType;
 
 typedef struct {

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

@@ -89,7 +89,6 @@ static void prvSetupHardware( void )
 #endif
 #ifdef _RTC
 	RTC_SendCmd(RTC_Clk32KSel, 0);
-	rtc_time_init();
 #endif
 #ifdef _SPI0
 	SPI0_SetBusClk(SPI0_FREQ);
@@ -609,6 +608,9 @@ static void MainTask(void *pvParameters)
 	ReadSysInfo();
 	DetectCpuResetToReboot();
 	StartCpu();
+#ifdef _RTC
+	rtc_time_init();
+#endif
 #ifdef CPU_ACCESS_MODULE_SUPPORT
 	CpuAccessModule();
 #endif

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

@@ -170,6 +170,6 @@ int RTC_ReadSleepData(void);
 int rtc_time_init(void);
 int iGetLocalTime(SystemTime_t *tm);
 void vSetLocalTime(SystemTime_t *tm);
-
-
+uint32_t CpuGetLocalTime(void);
+void CpuSetLocalTime(uint32_t time);
 #endif /* __AMT630HV160_RTC_H */

+ 13 - 2
amt630hv160-mcu/amt630hv160-mcu-sram-nos/src/ArkmicroFiles/libcpu-amt630hv160/source/amt630hv160_rtc.c

@@ -497,10 +497,9 @@ static void rtc_update_time(unsigned int time)
 
 	//wait rtc_busy;
 	RTC_WaitNotBusy();
-	while(RTC->RAM_H != time) {
+	while(RTC->CNT_H != time) {
 		if (timeout-- == 0)
 			break;
-		//taskYIELD();
 	}
 }
 
@@ -666,4 +665,16 @@ void vSetLocalTime(SystemTime_t *tm)
 	tm->tm_year -= 1900;
 	tm->tm_mon -= 1;
 	rtc_set_time(tm);
+}
+
+uint32_t CpuGetLocalTime(void)
+{
+	RTC_WaitNotBusy();
+
+	return RTC->CNT_H;
+}
+
+void CpuSetLocalTime(uint32_t time)
+{
+	rtc_update_time(time);
 }

+ 37 - 21
amt630hv160-mcu/amt630hv160-mcu-sram-nos/src/Drivers/access_module.c

@@ -8,15 +8,26 @@
 #ifdef _MAILBOX
 #define ACCESS_DEBUG(fmt, args...)		//printf(fmt, ##args)
 #ifdef CPU_ACCESS_MODULE_SUPPORT
-static int SendAccessModuleRsp(AccessModuleType type, uint32_t result, AccessModuleRespondMsgStatus msg_status)
+static int SendAccessModuleRsp(AccessModuleType type, uint32_t result, AccessModuleRespondMsgStatus msg_status, void *rsp_data)
 {
-	AccessModuleHeader header = {0};
+	u8 buf[26] = {0};
+
+	AccessModuleHeader *header = (AccessModuleHeader *)buf;
+
+	header->result = result;
+	header->type = type;
+	header->len = sizeof(AccessModuleHeader);
+	header->msg_status = msg_status;
+	if (rsp_data) {
+		if (type == AMT_RTC) {
+			header->len += sizeof(AccessRtcMsg);
+			memcpy((void *)(buf + sizeof(AccessModuleHeader)), rsp_data, sizeof(sizeof(AccessRtcMsg)));
+		} else {
+			printf("%s, para error.\n", __func__);
+		}
+	}
 
-	header.result = result;
-	header.type = type;
-	header.len = sizeof(AccessModuleHeader);
-	header.msg_status = msg_status;
-	if (mailbox_msg_send(MB_MSG_T_CPU_MODULE_ACCESS, (const uint8_t *)&header, header.len, 50)) {
+	if (mailbox_msg_send(MB_MSG_T_CPU_MODULE_ACCESS, (const uint8_t *)header, header->len, 50)) {
 		printf("%s, mailbox send frame fail.\n", __func__);
 		return -1;
 	}
@@ -26,26 +37,31 @@ static int SendAccessModuleRsp(AccessModuleType type, uint32_t result, AccessMod
 #ifdef ACCESS_RTC_SUPPORT
 static void CpuAccessRtc(AccessRtcMsg *rx_msg)
 {
-	SystemTime_t *tm = (SystemTime_t *)rx_msg->mem_addr;
-
-	if (rx_msg->mem_size != sizeof(SystemTime_t)) {
-		printf("mailbox receive access rtc size error.\n");
-		SendAccessModuleRsp(AMT_RTC, 0, AMRMS_PARA_ERROR);
-	}
+	AccessRtcMsg tx_msg= {0};
 
 	if (rx_msg->access_type == ART_GET_TIME) {
-		iGetLocalTime(tm);
-		ACCESS_DEBUG("Time: %d-%.2d-%.2d %.2d:%.2d:%.2d.\n", tm->tm_year, tm->tm_mon,
-				tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
+		tx_msg.rtc_cnt_h = CpuGetLocalTime();
+#if 0
+		SystemTime_t tm;
+		rtc_time_to_tm(tx_msg.rtc_cnt_h, &tm);
+		tm.tm_year += 1900;
+		tm.tm_mon += 1;
+		ACCESS_DEBUG("Time: %d-%.2d-%.2d %.2d:%.2d:%.2d.\n", tm.tm_year, tm.tm_mon,
+							tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
+
+		iGetLocalTime(&tm);
+		ACCESS_DEBUG("Time: %d-%.2d-%.2d %.2d:%.2d:%.2d.\n", tm.tm_year, tm.tm_mon,
+					tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
+#endif
 	} else if (rx_msg->access_type == ART_SET_TIME) {
-		vSetLocalTime(tm);
+		CpuSetLocalTime(rx_msg->rtc_cnt_h);
 	} else {
 		printf("%s, mailbox receive access rtc type error.\n", __func__);
-		SendAccessModuleRsp(AMT_RTC, 0, AMRMS_PARA_ERROR);
+		SendAccessModuleRsp(AMT_RTC, 0, AMRMS_PARA_ERROR, NULL);
 		return;
 	}
 
-	SendAccessModuleRsp(AMT_RTC, 0, AMRMS_NO_ERROR);
+	SendAccessModuleRsp(AMT_RTC, 0, AMRMS_NO_ERROR, &tx_msg);
 }
 #endif
 
@@ -66,7 +82,7 @@ void DetectCpuAccessModule(void)
 	header = (AccessModuleHeader *)msg.data;
 	if (header->type == AMT_RTC) {
 		msg_len = sizeof(AccessRtcMsg);
-	}else {
+	} else {
 		printf("%s, unknown module type, access not supportd.\n", __func__);
 		//unknown module type, Unable to reply.
 		return;
@@ -74,7 +90,7 @@ void DetectCpuAccessModule(void)
 
 	if (msg.dl != header_size + msg_len) {
 		printf("%s, mailbox rev msg length err.\n", __func__);
-		SendAccessModuleRsp(header->type, 0, AMRMS_LEN_ERROR);
+		SendAccessModuleRsp(header->type, 0, AMRMS_LEN_ERROR, NULL);
 		return;
 	}
 

+ 3 - 3
amt630hv160-mcu/amt630hv160-mcu-sram-nos/src/Drivers/access_module.h

@@ -46,8 +46,8 @@ typedef enum {
 } AccessRtcType;
 
 typedef struct {
-	u32 mem_addr;
-	u32 mem_size;
+	u32 rtc_cnt_h;
+	u32 reserved;
 	u32 access_type;
 } AccessRtcMsg;
 #endif
@@ -63,7 +63,7 @@ typedef enum {
 	AMT_STATUS,
 	AMT_FLASH,
 	AMT_CRC,
-	AMT_RTC,
+	AMT_RTC = 16,
 } AccessModuleType;
 
 typedef struct {

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

@@ -83,7 +83,6 @@ static void prvSetupHardware( void )
 
 #ifdef _RTC
 	RTC_SendCmd(RTC_Clk32KSel, 0);
-	//rtc_time_init();
 #endif
 #ifdef _SPI0
 	SPI0_SetBusClk(SPI0_FREQ);
@@ -250,6 +249,9 @@ int main(void)
 	UPDATE_ReadImageHeader();
 	ReadSysInfo();
 	StartCpu();
+#ifdef _RTC
+	rtc_time_init();
+#endif
 #if ADC_DEMO
 	adc_demo();
 #endif