From 08187305f2971d0e46fc919bb16ffd7d0a27b81a Mon Sep 17 00:00:00 2001
From: guanjiao <sqrgj@163.com>
Date: 星期六, 15 九月 2018 16:20:26 +0800
Subject: [PATCH] 1. 增加Flash读写功能 2. 增加map表读写功能 3. 修改一些全局变量的名称

---
 源码/核心板/Src/application/global_param.c      |   26 ++++
 源码/核心板/Src/main.c                          |   12 +
 源码/核心板/Src/application/serial_at_cmd_app.c |   24 +--
 源码/核心板/Src/OnChipDevices/Flash.c           |  162 +++++++++++++++++++++++++++
 源码/核心板/Src/application/global_param.h      |   27 ++++
 源码/核心板/Src/OnChipDevices/Usart.c           |   59 ---------
 源码/核心板/Src/OnChipDevices/Flash.h           |   25 ++++
 源码/核心板/Src/application/serial_at_cmd_app.h |    3 
 源码/核心板/Src/OnChipDevices/Usart.h           |    2 
 9 files changed, 259 insertions(+), 81 deletions(-)

diff --git "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/OnChipDevices/Flash.c" "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/OnChipDevices/Flash.c"
new file mode 100644
index 0000000..891d65c
--- /dev/null
+++ "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/OnChipDevices/Flash.c"
@@ -0,0 +1,162 @@
+#include "Flash.h"
+
+/**
+  * @brief  Calculate the number of pages
+  * @param  Size: The image size,the units of Size is Byte
+  * @retval The number of pages or error
+  */
+uint32_t FLASH_Pages_Calculate(uint32_t Size)
+{
+	uint32_t Flash_Page_Number = 0;
+	
+	if( Size % PAGE_SIZE != 0)
+	{
+		Flash_Page_Number = ( Size / PAGE_SIZE) + 1;
+	}
+	else 
+		Flash_Page_Number =   Size / PAGE_SIZE;
+	
+	if( Flash_Page_Number > MAX_FPAGE_NUM)
+		return 0;
+	else 
+		return Flash_Page_Number;
+}
+
+
+/**
+  * @brief  erase the block before  write Data
+  * @param  Address: the start address to erase
+			Len:	 the length need to erase,uints is Byte
+  * @retval 0:error; 1:success
+  */
+uint32_t FLASH_Prepare(uint32_t Address, uint32_t Len)      //起始地址和字长
+{
+	uint32_t NbrOFPage = 0;
+	uint32_t EraseCount = 0;
+	FLASH_Status FLASHStatus = FLASH_COMPLETE;
+	
+	FLASH_Unlock();
+	NbrOFPage = FLASH_Pages_Calculate( Len );
+	
+	for(; EraseCount < NbrOFPage; EraseCount++)
+	{
+		FLASHStatus = FLASH_ErasePage( Address + ( PAGE_SIZE * EraseCount) );
+		if( FLASHStatus != FLASH_COMPLETE)
+			break;
+	}
+	
+	FLASH_Lock();
+
+	if( EraseCount != NbrOFPage)
+		return 0;
+	else
+		return 1;
+}
+
+/**
+  * @brief  read the data from flash
+  * @param  Address: the start address to read
+			Len:	 the length need to read,uints is Byte
+  * @retval 
+  */
+void FLASH_Read( uint32_t Address, uint8_t* Readbuff, uint32_t Len)
+{
+	uint32_t ReadCount = 0;
+	
+	for( ; ReadCount < Len; ReadCount++)
+	{
+		Readbuff[ ReadCount ] = ((uint8_t *)Address)[ ReadCount ];
+	}
+}
+
+/**
+  * @brief  write data into flash
+  * @param  Address: the start address to write
+			Len:	 the length need to write,uints is Byte
+  * @retval 0:error  ; 1:success
+  */
+uint32_t FLASH_Write( uint32_t Address, const uint8_t* pData, uint32_t Len)
+{
+	uint32_t WriteCount = 0;
+	uint32_t FlashDestination = Address;
+	uint32_t FlashSource = (uint32_t)pData;
+	FLASH_Status FLASHStatus = FLASH_COMPLETE;
+		
+	FLASH_Unlock();
+	
+	for( ; WriteCount < Len; WriteCount+=2)
+	{
+		FLASHStatus = FLASH_ProgramHalfWord(FlashDestination, *(uint16_t*)FlashSource);
+		
+		if( FLASHStatus != FLASH_COMPLETE)
+		{
+			break;
+		}
+		if (*(uint16_t*)FlashDestination != *(uint16_t*)FlashSource)
+		{
+			break;
+		}
+		FlashDestination += 2;
+		FlashSource += 2;
+	}
+	
+	FLASH_Lock();
+	
+	if( WriteCount < Len)
+		return 0;
+	else
+		return 1;
+	
+}
+
+///**
+//  * @brief  Disable the write protection of desired pages
+//  * @param  None
+//  * @retval None
+//  */
+//void FLASH_Dis_WriteProt_Pages(void)
+//{
+//	uint32_t UserOptionByte = 0, WRPR = 0;
+//	uint16_t Var1 = OB_IWDG_SW, Var2 = OB_STOP_NoRST, Var3 = OB_STDBY_NoRST;
+//	FLASH_Status Status = FLASH_BUSY;
+//	static uint32_t UserMemoryMask = 0;
+
+//	WRPR = FLASH_GetWriteProtectionOptionByte();
+
+//	  /* Test if user memory is write protected */
+//	  if ((WRPR & UserMemoryMask) != UserMemoryMask)
+//	  {
+//		UserOptionByte = FLASH_GetUserOptionByte();
+
+//		UserMemoryMask |= WRPR;
+
+//		Status = FLASH_EraseOptionBytes();   //解除保护
+
+//		if (UserMemoryMask != 0xFFFFFFFF)
+//		{
+//		  Status = FLASH_EnableWriteProtection((uint32_t)~UserMemoryMask);
+//		}
+
+//		/* Test if user Option Bytes are programmed */
+//		if ((UserOptionByte & 0x07) != 0x07)
+//		{ 
+//		  /* Restore user Option Bytes */
+//		  if ((UserOptionByte & 0x01) == 0x0)
+//		  {
+//			Var1 = OB_IWDG_HW;
+//		  }
+//		  if ((UserOptionByte & 0x02) == 0x0)
+//		  {
+//			Var2 = OB_STOP_RST;
+//		  }
+//		  if ((UserOptionByte & 0x04) == 0x0)
+//		  {
+//			Var3 = OB_STDBY_RST;
+//		  }
+
+//		  FLASH_UserOptionByteConfig(Var1, Var2, Var3);
+//		}
+//		
+//	  }
+
+//}
diff --git "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/OnChipDevices/Flash.h" "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/OnChipDevices/Flash.h"
new file mode 100644
index 0000000..b46612c
--- /dev/null
+++ "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/OnChipDevices/Flash.h"
@@ -0,0 +1,25 @@
+#include "stm32f10x.h"
+
+#define FLASH_SIZE       		(uint32_t)0x10000
+#define PAGE_SIZE   			(0x400)	   		        //C8T6 Flash is 64KB ,each page is 1KB 
+#define MAX_FPAGE_NUM   		64
+  
+#define FLASH_BASE_ADDR   		(uint32_t)0x08000000	//the start address of APP,keep the 0x8000000-0x8004FFF forIAP(20K)
+#define FLASH_APP_FORMAL_ADDR  	(uint32_t)0x08001000    //the start address of formal software
+#define FLASH_APP_TEMP_ADDR     (uint32_t)0x08008000    //the start address of temp software,wait for checks
+#define FLASH_IAP_CTRL_MAP		(uint32_t)0x0800F000    //the control map start address,
+#define MAX_APP_ADDR      		(uint32_t)0x0800FFFF    //the max array of Flash address 
+
+#define MAX_APP_SIZE      		(uint32_t)0x7000      //the max size of software is 28K
+#define MAX_APPPAGE_NUM    		28
+
+#define NVIC_VectTab_FLASH           ((uint32_t)0x08000000)
+
+//parameters position
+uint32_t FLASH_Pages_Calculate(uint32_t Size);
+uint32_t FLASH_Prepare( uint32_t Address, uint32_t Len);
+void FLASH_Read( uint32_t Address, uint8_t *Readbuff, uint32_t Len);
+uint32_t FLASH_Write( uint32_t Address, const uint8_t* pData, uint32_t Len);
+void FLASH_Dis_WriteProt_Pages(void);
+
+
diff --git "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/OnChipDevices/Usart.c" "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/OnChipDevices/Usart.c"
index 1d8320b..6ed2bf7 100644
--- "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/OnChipDevices/Usart.c"
+++ "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/OnChipDevices/Usart.c"
@@ -1,62 +1,5 @@
 #include "Usart.h"
-
-//void Usart_Init(void)
-//{
-//	USART_InitTypeDef USART_InitStructure;
-//    GPIO_InitTypeDef GPIO_InitStructure;
-//	
-//	/* Enable GPIO clock */
-//    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
-//	/* Enable USART clock */
-//    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
-//	
-//	/* Configure USART Tx as alternate function push-pull */
-//    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
-//    GPIO_InitStructure.GPIO_Pin = USART_TX_pin;
-//    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
-//    GPIO_Init(USART_GPIO, &GPIO_InitStructure);
-
-//    /* Configure USART Rx as input floating */
-//    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
-//    GPIO_InitStructure.GPIO_Pin = USART_RX_pin;
-//    GPIO_Init(USART_GPIO, &GPIO_InitStructure);
-
-//    /* USARTx configured as follow:
-//    	  - BaudRate = 115200 baud
-//    	  - Word Length = 8 Bits
-//    	  - One Stop Bit
-//    	  - No parity
-//    	  - Hardware flow control disabled (RTS and CTS signals)
-//    	  - Receive and transmit enabled
-//    */
-//    USART_InitStructure.USART_BaudRate = BAUD_RATE ;
-//    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
-//    USART_InitStructure.USART_StopBits = USART_StopBits_1;
-//    USART_InitStructure.USART_Parity = USART_Parity_No;
-//    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
-//    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
-
-//    /* USART configuration */
-//    USART_Init(EXT_USART, &USART_InitStructure);
-
-//    /* Enable USART */
-//    USART_Cmd(EXT_USART, ENABLE);
-
-//}
-
-//uint16_t Checksum_u16(uint8_t *pdata, uint32_t len)
-//{
-//    uint16_t sum = 0;
-//    uint32_t i;
-//    for(i = 0; i < len; i++)
-//        sum += pdata[i];
-//    sum = ~sum;
-//    return sum;
-//}
-
-
-
-///////////////////////////////////
+#include <string.h>
 
 //数据发送队列变量
 EUART_Frame m_EUART_TxFrames[EUART_TX_FRM_SIZE];	//数据发送帧队列	
diff --git "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/OnChipDevices/Usart.h" "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/OnChipDevices/Usart.h"
index 3041a96..0cb0059 100644
--- "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/OnChipDevices/Usart.h"
+++ "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/OnChipDevices/Usart.h"
@@ -3,8 +3,6 @@
 #define __USART_H__
 
 #include "stm32f10x.h"
-#include <string.h>
-
 
 //#define UART_GPIO_REMAP_ENABLE
 #ifdef UART_GPIO_REMAP_ENABLE
diff --git "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/application/global_param.c" "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/application/global_param.c"
new file mode 100644
index 0000000..6934c80
--- /dev/null
+++ "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/application/global_param.c"
@@ -0,0 +1,26 @@
+#include "global_param.h"
+#include "Flash.h"
+
+uint16_t g_com_map[COM_MAP_SIZE];
+
+uint32_t save_com_map_to_flash(void)
+{
+	uint32_t result = 0;
+
+	__disable_irq();
+	result = FLASH_Prepare(FLASH_IAP_CTRL_MAP, (COM_MAP_SIZE<<1));
+	if(result)
+		result = FLASH_Write(FLASH_IAP_CTRL_MAP, (const uint8_t*)g_com_map, (COM_MAP_SIZE<<1)); 
+	__enable_irq();
+	return result;
+}
+
+void parameter_init(void)
+{
+	FLASH_Read(FLASH_IAP_CTRL_MAP, (uint8_t*)&g_com_map, (COM_MAP_SIZE<<1));
+	
+	g_com_map[ALARM_DISTANCE] = 200;	//默认报警距离200cm
+	g_com_map[ALARM_DEV] = 0;
+	
+}
+
diff --git "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/application/global_param.h" "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/application/global_param.h"
new file mode 100644
index 0000000..977da01
--- /dev/null
+++ "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/application/global_param.h"
@@ -0,0 +1,27 @@
+#ifndef __GLOBAL_PARAM_H__
+#define __GLOBAL_PARAM_H__
+
+#include "stm32f10x.h"
+
+#define COM_MAP_SIZE	1024
+
+
+/*---------------Map Definition-------------------*/
+#define 	DEV_ROLE				0x10		//该设备是anchor还是tag	
+#define 	DEV_ID					0x11		//设备的Id号
+#define 	COM_INTERVAL			0x12		//通讯间隔
+#define 	DEV_GROUP_ID			0x13		//通讯组ID
+
+#define 	ALARM_DEV				0x20		//是哪个设备报警
+#define		ALARM_DISTANCE			0x21		//小于多少距离报警
+
+/*------------END Map Definition-------------------*/
+
+extern uint16_t g_com_map[COM_MAP_SIZE];
+
+
+extern uint32_t save_com_map_to_flash(void);
+extern void parameter_init(void);
+
+#endif
+
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..913b7c5 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"
@@ -1,21 +1,19 @@
 #include "serial_at_cmd_app.h"
+#include "global_param.h"
 #include <string.h>
 
-uint8_t g_frame_data[MAX_FRAME_LEN] = {0};
-uint8_t g_frame_data_len = 0;
-
-uint32_t g_alarm_dist = 200;
-uint8_t g_alarm_dev = 0;
+uint8_t m_frame_data[MAX_FRAME_LEN] = {0};
+uint8_t m_frame_data_len = 0;
 
 void ParseFrame(void)
 {
-	if(!memcmp(g_frame_data, "SET", 3))
+	if(!memcmp(m_frame_data, "SET", 3))
 	{
-		g_alarm_dist = (g_frame_data[3]-'0')*1000 + (g_frame_data[4]-'0')*100 +  (g_frame_data[5]-'0')*10 + (g_frame_data[6]-'0');
+		g_com_map[ALARM_DISTANCE] = (m_frame_data[3]-'0')*1000 + (m_frame_data[4]-'0')*100 +  (m_frame_data[5]-'0')*10 + (m_frame_data[6]-'0');
 	}
-	else if(!memcmp(g_frame_data, "DEV", 3))
+	else if(!memcmp(m_frame_data, "DEV", 3))
 	{
-		g_alarm_dev = g_frame_data[3]-'0';
+		g_com_map[ALARM_DEV] = m_frame_data[3]-'0';
 	}
 }
 
@@ -39,20 +37,20 @@
 			{ 
 				s_usart_state = 3;
 				s_data_pos = 0;
-				g_frame_data_len = 0;
+				m_frame_data_len = 0;
 			}
 		break;
 			
 		case 3:
-			g_frame_data_len = s_data_pos;
-			g_frame_data[s_data_pos++] = data;
+			m_frame_data_len = s_data_pos;
+			m_frame_data[s_data_pos++] = data;
 			if(data == 0x0d)
 			{
 				s_usart_state = 4;
 			}
 			if(s_data_pos >= MAX_FRAME_LEN) //如果超过最大长度
 			{
-				g_frame_data_len = MAX_FRAME_LEN;
+				m_frame_data_len = MAX_FRAME_LEN;
 				ParseFrame();
 				s_usart_state = 0;
 			}
diff --git "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/application/serial_at_cmd_app.h" "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/application/serial_at_cmd_app.h"
index b9b061f..83635fd 100644
--- "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/application/serial_at_cmd_app.h"
+++ "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/application/serial_at_cmd_app.h"
@@ -6,9 +6,6 @@
 
 #define MAX_FRAME_LEN		50
 
-extern uint32_t g_alarm_dist;
-extern uint8_t g_alarm_dev;
-
 void UsartParseDataHandler(uint8_t data);
 
 
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..b0e5590 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 "global_param.h"
 
 //#define WORK_MODE_TAG
 #define WORK_MODE_ANCHOR
@@ -33,9 +34,10 @@
 	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
 }
 
-void Parameter_Init(void)
+void Program_Init(void)
 {
 	Usart1ParseDataCallback = UsartParseDataHandler;
+	parameter_init();
 }
 
 /*! ------------------------------------------------------------------------------------------------------------------
@@ -52,7 +54,7 @@
 	LPFilter_Frac* p_Dis_Filter;
 	
 	Device_Init();
-	Parameter_Init();
+	Program_Init();
 	Dw1000_Init();
 	p_Dis_Filter = New_LP_Frac(0.7);
 
@@ -69,13 +71,13 @@
 		//除UWB之外的其他代码...
 		dis_after_filter = LP_Frac_Update(p_Dis_Filter, dist_cm);
 		
-		if(dis_after_filter <= g_alarm_dist)
+		if(dis_after_filter <= g_com_map[ALARM_DISTANCE])
 		{
 //			g_beep_off_time = (uint32_t)dis_after_filter * 10;
 //			if(g_beep_off_time <= 0) 
 //				g_beep_off_time = 0;
 //			g_beep_on_time = BEEM_ON_TIME_MS;
-			switch(g_alarm_dev) 
+			switch(g_com_map[ALARM_DEV]) 
 			{
 				case 0:
 					BEEP2_ON;
@@ -103,7 +105,7 @@
 		{
 //			g_beep_on_time = 0;
 //			g_beep_off_time = 0;
-			switch(g_alarm_dev) 
+			switch(g_com_map[ALARM_DEV]) 
 			{
 				case 0:
 					BEEP2_OFF;

--
Gitblit v1.9.3