Browse Source

更新MCU工程(iram版本和sram-nos版本)
1. 修复rtc时钟在24MHz频率下计时不准的问题。
2. 添加adc参数的说明注释,解释如何设置adc阈值与中断触发模式。

helen 1 month ago
parent
commit
b261e807b5

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

@@ -87,6 +87,7 @@ static inline int is_leap_year(unsigned int year)
 #define RTC_STA_BUSY					(1 << 2)
 #define RTC_STA_SLEEPMODE				(1 << 3)
 #define RTC_STA_ISOFORCE				(1 << 6)
+#define RTC_STA_OSC32K_SEL				(1 << 7)
 #define RTC_STA_GPIO0ACT				(1 << 9)
 #define RTC_STA_GPIO1ACT				(1 << 10)
 #define RTC_STA_CAN0ACT					(1 << 11)
@@ -113,7 +114,7 @@ int RTC_ReadSleepData(void);
 //	Real-time clock related
 int rtc_time_init(void);
 int iGetLocalTime(SystemTime_t *tm);
-void vSetLocalTime(SystemTime_t *tm);
+int iSetLocalTime(SystemTime_t *tm);
 uint32_t CpuGetLocalTime(void);
 void CpuSetLocalTime(uint32_t time);
 #endif /* __AMT630HV160_RTC_H */

+ 33 - 8
amt630hv160-mcu/amt630hv160-mcu-iram/src/ArkmicroFiles/libcpu-amt630hv160/source/amt630hv160_rtc.c

@@ -37,6 +37,8 @@ static const unsigned short rtc_ydays[2][13] = {
 	{ 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
 };
 
+static u32 rtc_residual;
+
 #define LEAPS_THRU_END_OF(y) ((y)/4 - (y)/100 + (y)/400)
 
 static int RTC_WaitNotBusy(void)
@@ -450,13 +452,20 @@ uint32_t rtc_tm_to_time(struct rtc_time *tm)
 static void rtc_update_time(unsigned int time)
 {
 	int timeout = 100000;
+	u32 cnth;
+
+	if (RTC->RTCSTA & RTC_STA_OSC32K_SEL) {
+		cnth = time;
+	} else {
+		cnth = (u64)time * 46875 / 65536;
+		rtc_residual = ((u64)time * 46875) & (65536 - 1);
+	}
 
-	RTC_WriteReg(RTC_CNT, &RTC->CNT_L, 0);
-	RTC_WriteReg(RTC_CNT, &RTC->CNT_H, time);
+	RTC_WriteReg(RTC_CNT, &RTC->CNT_H, cnth);
 
 	//wait rtc_busy;
 	RTC_WaitNotBusy();
-	while (RTC->CNT_H != time) {
+	while(RTC->CNT_H != cnth) {
 		if (timeout-- == 0)
 			break;
 		taskYIELD();
@@ -476,9 +485,10 @@ static int rtc_read_time(struct rtc_time *tm)
 
 	/* we don't report wday/yday/isdst ... */
 	RTC_WaitNotBusy();
-
-	time = RTC->CNT_H;
-
+	if (RTC->RTCSTA & RTC_STA_OSC32K_SEL)
+		time = RTC->CNT_H;
+	else
+		time = ((u64)RTC->CNT_H * 65536 + rtc_residual) / 46875;
 	rtc_time_to_tm(time, tm);
 
 	return 0;
@@ -520,6 +530,11 @@ int rtc_time_init(void)
 
 int iGetLocalTime(SystemTime_t *tm)
 {
+	if (tm == NULL) {
+		printf("%s invalid tm.\n", __func__);
+		return -1;
+	}
+
 	rtc_read_time(tm);
 	tm->tm_year += 1900;
 	tm->tm_mon += 1;
@@ -527,18 +542,28 @@ int iGetLocalTime(SystemTime_t *tm)
 	return 0;
 }
 
-void vSetLocalTime(SystemTime_t *tm)
+int iSetLocalTime(SystemTime_t *tm)
 {
+	if (tm == NULL) {
+		printf("%s invalid tm.\n", __func__);
+		return -1;
+	}
+
 	tm->tm_year -= 1900;
 	tm->tm_mon -= 1;
 	rtc_set_time(tm);
+
+	return 0;
 }
 
 uint32_t CpuGetLocalTime(void)
 {
 	RTC_WaitNotBusy();
 
-	return RTC->CNT_H;
+	if (RTC->RTCSTA & RTC_STA_OSC32K_SEL)
+		return RTC->CNT_H;
+	else
+		return ((u64)RTC->CNT_H * 65536 + rtc_residual) / 46875;
 }
 
 void CpuSetLocalTime(uint32_t time)

+ 10 - 3
amt630hv160-mcu/amt630hv160-mcu-iram/src/amt630hv160_conf.h

@@ -133,9 +133,16 @@
 #define _ADC0
 #define _ADC1
 #define _ADC2
-#define ADC0_THR_SEL	1	/* 阈值:0->  0.2v,1->1.6V(adc电源1.8v时) */
-#define ADC1_THR_SEL	1	/* 阈值:0->  0.2v,1->1.6V(adc电源1.8v时) */
-#define ADC2_THR_SEL	1	/* 阈值:0->  0.2v,1->1.6V(adc电源1.8v时) */
+/*
+	阈值与adc_det_high_valid_enable函数联合使用(4种模式)
+	ADCx_THR_SEL为0时,阈值为0.2V。
+	ADCx_THR_SEL为1时,阈值为1.6V
+	adc_det_high_valid_enable函数传参enable = 1,ADC值大于阈值时,触发中断。
+	adc_det_high_valid_enable函数传参enable = 0,ADC值小于阈值时,触发中断。
+*/
+#define ADC0_THR_SEL	1
+#define ADC1_THR_SEL	1
+#define ADC2_THR_SEL	1
 #ifdef _ADC0
 #define _ADC0_0
 #define _ADC0_1

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

@@ -87,6 +87,7 @@ static inline int is_leap_year(unsigned int year)
 #define RTC_STA_BUSY					(1 << 2)
 #define RTC_STA_SLEEPMODE				(1 << 3)
 #define RTC_STA_ISOFORCE				(1 << 6)
+#define RTC_STA_OSC32K_SEL				(1 << 7)
 #define RTC_STA_GPIO0ACT				(1 << 9)
 #define RTC_STA_GPIO1ACT				(1 << 10)
 #define RTC_STA_CAN0ACT					(1 << 11)
@@ -113,7 +114,7 @@ int RTC_ReadSleepData(void);
 //	Real-time clock related
 int rtc_time_init(void);
 int iGetLocalTime(SystemTime_t *tm);
-void vSetLocalTime(SystemTime_t *tm);
+int iSetLocalTime(SystemTime_t *tm);
 uint32_t CpuGetLocalTime(void);
 void CpuSetLocalTime(uint32_t time);
 #endif /* __AMT630HV160_RTC_H */

+ 33 - 8
amt630hv160-mcu/amt630hv160-mcu-sram-nos/src/ArkmicroFiles/libcpu-amt630hv160/source/amt630hv160_rtc.c

@@ -37,6 +37,8 @@ static const unsigned short rtc_ydays[2][13] = {
 	{ 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
 };
 
+static u32 rtc_residual;
+
 #define LEAPS_THRU_END_OF(y) ((y)/4 - (y)/100 + (y)/400)
 
 static int RTC_WaitNotBusy(void)
@@ -450,13 +452,20 @@ uint32_t rtc_tm_to_time(struct rtc_time *tm)
 static void rtc_update_time(unsigned int time)
 {
 	int timeout = 100000;
+	u32 cnth;
+
+	if (RTC->RTCSTA & RTC_STA_OSC32K_SEL) {
+		cnth = time;
+	} else {
+		cnth = (u64)time * 46875 / 65536;
+		rtc_residual = ((u64)time * 46875) & (65536 - 1);
+	}
 
-	RTC_WriteReg(RTC_CNT, &RTC->CNT_L, 0);
-	RTC_WriteReg(RTC_CNT, &RTC->CNT_H, time);
+	RTC_WriteReg(RTC_CNT, &RTC->CNT_H, cnth);
 
 	//wait rtc_busy;
 	RTC_WaitNotBusy();
-	while (RTC->CNT_H != time) {
+	while(RTC->CNT_H != cnth) {
 		if (timeout-- == 0)
 			break;
 	}
@@ -475,9 +484,10 @@ static int rtc_read_time(struct rtc_time *tm)
 
 	/* we don't report wday/yday/isdst ... */
 	RTC_WaitNotBusy();
-
-	time = RTC->CNT_H;
-
+	if (RTC->RTCSTA & RTC_STA_OSC32K_SEL)
+		time = RTC->CNT_H;
+	else
+		time = ((u64)RTC->CNT_H * 65536 + rtc_residual) / 46875;
 	rtc_time_to_tm(time, tm);
 
 	return 0;
@@ -519,6 +529,11 @@ int rtc_time_init(void)
 
 int iGetLocalTime(SystemTime_t *tm)
 {
+	if (tm == NULL) {
+		printf("%s invalid tm.\n", __func__);
+		return -1;
+	}
+
 	rtc_read_time(tm);
 	tm->tm_year += 1900;
 	tm->tm_mon += 1;
@@ -526,18 +541,28 @@ int iGetLocalTime(SystemTime_t *tm)
 	return 0;
 }
 
-void vSetLocalTime(SystemTime_t *tm)
+int iSetLocalTime(SystemTime_t *tm)
 {
+	if (tm == NULL) {
+		printf("%s invalid tm.\n", __func__);
+		return -1;
+	}
+
 	tm->tm_year -= 1900;
 	tm->tm_mon -= 1;
 	rtc_set_time(tm);
+
+	return 0;
 }
 
 uint32_t CpuGetLocalTime(void)
 {
 	RTC_WaitNotBusy();
 
-	return RTC->CNT_H;
+	if (RTC->RTCSTA & RTC_STA_OSC32K_SEL)
+		return RTC->CNT_H;
+	else
+		return ((u64)RTC->CNT_H * 65536 + rtc_residual) / 46875;
 }
 
 void CpuSetLocalTime(uint32_t time)

+ 10 - 3
amt630hv160-mcu/amt630hv160-mcu-sram-nos/src/amt630hv160_conf.h

@@ -142,9 +142,16 @@
 #define _ADC0
 #define _ADC1
 #define _ADC2
-#define ADC0_THR_SEL	1	/* 阈值:0->  0.2v,1->1.6V(adc电源1.8v时) */
-#define ADC1_THR_SEL	1	/* 阈值:0->  0.2v,1->1.6V(adc电源1.8v时) */
-#define ADC2_THR_SEL	1	/* 阈值:0->  0.2v,1->1.6V(adc电源1.8v时) */
+/*
+	阈值与adc_det_high_valid_enable函数联合使用(4种模式)
+	ADCx_THR_SEL为0时,阈值为0.2V。
+	ADCx_THR_SEL为1时,阈值为1.6V
+	adc_det_high_valid_enable函数传参enable = 1,ADC值大于阈值时,触发中断。
+	adc_det_high_valid_enable函数传参enable = 0,ADC值小于阈值时,触发中断。
+*/
+#define ADC0_THR_SEL	1
+#define ADC1_THR_SEL	1
+#define ADC2_THR_SEL	1
 #ifdef _ADC0
 #define _ADC0_0
 #define _ADC0_1