From 4979f7565cbd5f093e280f9f277ae29bb450acb8 Mon Sep 17 00:00:00 2001
From: zhyinch <zhyinch@gmail.com>
Date: 星期六, 15 九月 2018 17:28:24 +0800
Subject: [PATCH] 增加低功耗标签

---
 源码/核心板/Src/OnChipDevices/RTC.h              |    4 +
 源码/核心板/Src/OnChipDevices/Spi.h              |    2 
 源码/核心板/Src/main.c                           |   17 +++++
 源码/核心板/Src/OnChipDevices/Rcc_Nvic_Systick.h |    2 
 源码/核心板/Src/application/serial_at_cmd_app.c  |    2 
 .gitignore                                  |   15 +++++
 源码/核心板/Src/stm32f10x_it.c                   |   13 ++++
 源码/核心板/Src/OnChipDevices/Rcc_Nvic_Systick.c |   15 ++++
 源码/核心板/Src/OnChipDevices/RTC.c              |   54 ++++++++++++++++++
 源码/核心板/Src/application/dw_app.c             |   22 ++++++-
 10 files changed, 138 insertions(+), 8 deletions(-)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..ed50861
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,15 @@
+
+*.txt
+*.crf
+*.d
+*.o
+*.ini
+*.dbgconf
+*.lst
+*.map
+*.iex
+*.dep
+*.axf
+*.htm
+*.lnp
+*.sct
diff --git "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/OnChipDevices/RTC.c" "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/OnChipDevices/RTC.c"
new file mode 100644
index 0000000..6b53987
--- /dev/null
+++ "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/OnChipDevices/RTC.c"
@@ -0,0 +1,54 @@
+#include "RTC.h"
+void RTC_SET_ALARM(u32 sec)
+{
+//DEBUG_COM_STREAM("-??-",NULL);
+RTC_SetAlarm(RTC_GetCounter()+sec);
+//DEBUG_COM_STREAM("-??1-",NULL);
+RTC_WaitForLastTask();
+//DEBUG_COM_STREAM("-??2-",NULL);
+RTC_ITConfig(RTC_FLAG_ALR,ENABLE);
+}
+void RTC_Configuration(void)
+{
+	/* Enable PWR and BKP clocks */
+	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
+
+	/* Allow access to BKP Domain */
+	PWR_BackupAccessCmd(ENABLE);
+
+	/* Reset Backup Domain */
+	//BKP_DeInit();
+
+	RCC_LSICmd(ENABLE);
+	/* Enable LSE */
+	//RCC_LSEConfig(RCC_LSE_ON);
+	/* Wait till LSE is ready */
+	while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET){}
+
+	/* Select LSE as RTC Clock Source */
+	RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
+
+	/* Enable RTC Clock */
+	RCC_RTCCLKCmd(ENABLE);
+
+	/* Wait for RTC registers synchronization */
+	RTC_WaitForSynchro();
+
+	/* Wait until last write operation on RTC registers has finished */
+	RTC_WaitForLastTask();
+
+	/* Enable the RTC Second */
+	//RTC_ITConfig(RTC_IT_SEC, ENABLE);
+
+	/* Wait until last write operation on RTC registers has finished */
+	RTC_WaitForLastTask();
+		
+	RTC_ITConfig(RTC_IT_ALR,ENABLE); //?? RTC ???  
+		RTC_WaitForLastTask();
+	/* Set RTC prescaler: set RTC period to 1sec */
+	RTC_SetPrescaler(40000); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */
+
+	/* Wait until last write operation on RTC registers has finished */
+	RTC_WaitForLastTask();
+	RTC_SET_ALARM(1);
+}
\ No newline at end of file
diff --git "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/OnChipDevices/RTC.h" "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/OnChipDevices/RTC.h"
new file mode 100644
index 0000000..953178f
--- /dev/null
+++ "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/OnChipDevices/RTC.h"
@@ -0,0 +1,4 @@
+#include "stm32f10x.h"
+
+
+void RTC_Configuration(void);
\ No newline at end of file
diff --git "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/OnChipDevices/Rcc_Nvic_Systick.c" "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/OnChipDevices/Rcc_Nvic_Systick.c"
index e021ee3..14e9205 100644
--- "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/OnChipDevices/Rcc_Nvic_Systick.c"
+++ "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/OnChipDevices/Rcc_Nvic_Systick.c"
@@ -44,7 +44,7 @@
 void Nvic_Init(void)
 {
 	NVIC_InitTypeDef NVIC_InitStructure;
-	
+		EXTI_InitTypeDef EXTI_InitStructure;
 	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
 	
 	/* Enable and set EXTI Interrupt to the lowest priority */
@@ -53,6 +53,17 @@
     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
 
+		EXTI_InitStructure.EXTI_Line = EXTI_Line17;
+    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
+    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
+    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
+    EXTI_Init(&EXTI_InitStructure); 
+	
+		NVIC_InitStructure.NVIC_IRQChannel = RTCAlarm_IRQn;
+    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
+    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
+		NVIC_InitStructure.NVIC_IRQChannelCmd =ENABLE;
+	
     NVIC_Init(&NVIC_InitStructure);
 }
 
@@ -89,4 +100,4 @@
 {  
     uint32_t i=1000*nTimer;  
     delay_us(i);  
-}  
+}  
\ No newline at end of file
diff --git "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/OnChipDevices/Rcc_Nvic_Systick.h" "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/OnChipDevices/Rcc_Nvic_Systick.h"
index 161a76f..fcd4f82 100644
--- "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/OnChipDevices/Rcc_Nvic_Systick.h"
+++ "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/OnChipDevices/Rcc_Nvic_Systick.h"
@@ -11,5 +11,5 @@
 int Systick_Init(void);
 void delay_us(uint32_t nTimer);
 void delay_ms(uint32_t nTimer);
-
+void RTC_Configuration(void);
 #endif
diff --git "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/OnChipDevices/Spi.h" "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/OnChipDevices/Spi.h"
index bdeaa69..59081e3 100644
--- "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/OnChipDevices/Spi.h"
+++ "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/OnChipDevices/Spi.h"
@@ -5,7 +5,7 @@
 #include "stm32f10x.h"
 
 #define SPIx_PRESCALER_SLOW			SPI_BaudRatePrescaler_32
-#define SPIx_PRESCALER_FAST			SPI_BaudRatePrescaler_8
+#define SPIx_PRESCALER_FAST			SPI_BaudRatePrescaler_16
 
 #define SPIx						SPI1
 #define SPIx_GPIO					GPIOA
diff --git "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/application/dw_app.c" "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/application/dw_app.c"
index bd1960c..d82f45e 100644
--- "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/application/dw_app.c"
+++ "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/application/dw_app.c"
@@ -47,13 +47,13 @@
 #define POLL_TX_TO_RESP_RX_DLY_UUS 150
 /* This is the delay from Frame RX timestamp to TX reply timestamp used for calculating/setting the DW1000's delayed TX function. This includes the
  * frame length of approximately 2.66 ms with above configuration. */
-#define RESP_RX_TO_FINAL_TX_DLY_UUS 4100
+#define RESP_RX_TO_FINAL_TX_DLY_UUS 5100
 /* Receive response timeout. See NOTE 5 below. */
 #define RESP_RX_TIMEOUT_UUS 14700
 
 #define POLL_RX_TO_RESP_TX_DLY_UUS 3600
 /* This is the delay from the end of the frame transmission to the enable of the receiver, as programmed for the DW1000's wait for response feature. */
-#define RESP_TX_TO_FINAL_RX_DLY_UUS 500
+#define RESP_TX_TO_FINAL_RX_DLY_UUS 1500
 /* Receive final timeout. See NOTE 5 below. */
 #define FINAL_RX_TIMEOUT_UUS 4300
 
@@ -223,7 +223,9 @@
 
     /* Configure DW1000. See NOTE 6 below. */
     dwt_configure(&config);//配置DW1000
+	
 
+	
     /* Apply default antenna delay value. See NOTE 1 below. */
     dwt_setrxantennadelay(RX_ANT_DLY);		//设置接收天线延迟
     dwt_settxantennadelay(TX_ANT_DLY);		//设置发射天线延迟
@@ -233,11 +235,24 @@
     dwt_setrxaftertxdelay(POLL_TX_TO_RESP_RX_DLY_UUS);			//设置发送后开启接收,并设定延迟时间
     dwt_setrxtimeout(RESP_RX_TIMEOUT_UUS);						//设置接收超时时间
 }
+void Delayus(uint16_t time) 
+{
+  uint16_t i = 0;
+  while (time--) {
+    i = 10;
+    while (i--)
+      ;
 
+  }
+}
 void Tag_App(void)//发送模式(TAG标签)
 {
 	uint32_t frame_len;
 	uint32_t final_tx_time;
+	
+	GPIO_ResetBits(SPIx_GPIO, SPIx_CS);
+	Delayus(2000);
+	GPIO_SetBits(SPIx_GPIO, SPIx_CS);
 	
 	/* Write frame data to DW1000 and prepare transmission. See NOTE 7 below. */
 	tx_poll_msg[ALL_MSG_SN_IDX] = frame_seq_nb;
@@ -319,7 +334,8 @@
 	}
 	LED0_BLINK;
 	/* Execute a delay between ranging exchanges. */
-	deca_sleep(RNG_DELAY_MS + random_delay_tim); //休眠固定时间
+	dwt_entersleep();
+	
 }
 
 void Anchor_App(void)
diff --git "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/application/serial_at_cmd_app.c" "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/application/serial_at_cmd_app.c"
index c8864cb..a3d0408 100644
--- "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/application/serial_at_cmd_app.c"
+++ "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/application/serial_at_cmd_app.c"
@@ -4,7 +4,7 @@
 uint8_t g_frame_data[MAX_FRAME_LEN] = {0};
 uint8_t g_frame_data_len = 0;
 
-uint32_t g_alarm_dist = 200;
+uint32_t g_alarm_dist = 50;
 uint8_t g_alarm_dev = 0;
 
 void ParseFrame(void)
diff --git "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/main.c" "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/main.c"
index 2f3ac86..b2dd27c 100644
--- "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/main.c"
+++ "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/main.c"
@@ -9,6 +9,7 @@
 #include "filters.h"
 #include "stm32f10x_it.h"
 #include "serial_at_cmd_app.h"
+#include "deca_device_api.h"
 
 //#define WORK_MODE_TAG
 #define WORK_MODE_ANCHOR
@@ -23,6 +24,9 @@
 	SystemInit();
 	Nvic_Init();
 	Systick_Init();
+#ifdef WORK_MODE_TAG	
+	RTC_Configuration();
+#endif
 	Led_Init();
 	Beep_Init();
 	DW_GPIO_Init();
@@ -47,6 +51,7 @@
  *
  * @return none
  */
+extern uint8_t send_start;
 int main(void)
 {
 	LPFilter_Frac* p_Dis_Filter;
@@ -56,11 +61,23 @@
 	Dw1000_Init();
 	p_Dis_Filter = New_LP_Frac(0.7);
 
+	#ifdef WORK_MODE_TAG
+   dwt_configuresleep(0x940, 0x7);
+	 _dwt_aonarrayupload();
+	//dwt_entersleep();
+	#endif
+	
     /* Loop forever initiating ranging exchanges. */
 	while(1)
 	{
 #ifdef WORK_MODE_TAG
+		
+	if(send_start)
+	{send_start=0;
 		Tag_App();
+	}
+	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR,ENABLE);
+PWR_EnterSTOPMode(PWR_Regulator_LowPower,PWR_STOPEntry_WFI);	
 #else 
 		Anchor_App();
 
diff --git "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/stm32f10x_it.c" "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/stm32f10x_it.c"
index 8de836d..03523c6 100644
--- "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/stm32f10x_it.c"
+++ "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/stm32f10x_it.c"
@@ -62,5 +62,18 @@
 
   }   
 }
+uint8_t tt=0;
+uint8_t send_start=0;
+void RTCAlarm_IRQHandler(void)
+{
+   EXTI_ClearITPendingBit(EXTI_Line17);
+	RTC_ClearITPendingBit(RTC_FLAG_ALR);
+	send_start=1;
+	RTC_SET_ALARM(1);
+	tt++;
+	//GPIO_Toggle(GPIOA,LED_PIN);
+	
+  //SYS.wake_id|=1<<17;
+}
 
 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/

--
Gitblit v1.9.3