Pārlūkot izejas kodu

add write_json_to_flash

haitao 10 mēneši atpakaļ
vecāks
revīzija
a30a81e85c

BIN
app/MDK/Objects/dtu_simple.axf


Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 129 - 126
app/MDK/dtu_simple.uvoptx


+ 6 - 1
app/MDK/dtu_simple.uvprojx

@@ -275,7 +275,7 @@
               <OCR_RVCT4>
                 <Type>1</Type>
                 <StartAddress>0x8000000</StartAddress>
-                <Size>0x40000</Size>
+                <Size>0x318800</Size>
               </OCR_RVCT4>
               <OCR_RVCT5>
                 <Type>1</Type>
@@ -432,6 +432,11 @@
               <FileType>1</FileType>
               <FilePath>..\System\source\udp.c</FilePath>
             </File>
+            <File>
+              <FileName>gd32_flash.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>..\System\source\gd32_flash.c</FilePath>
+            </File>
           </Files>
         </Group>
         <Group>

+ 10 - 0
app/System/include/gd32_flash.h

@@ -0,0 +1,10 @@
+#ifndef GD32_FLASH_H
+#define GD32_FLASH_H
+
+
+#define FMC_PAGE_SIZE           ((uint16_t)0x1000U)
+#define FMC_WRITE_START_ADDR    ((uint32_t)0x08038800U)
+#define FMC_WRITE_END_ADDR      ((uint32_t)0x0803FFFFU)
+void gd32_flash_test();
+int read_data_from_flash(void *buffer);
+#endif

+ 105 - 0
app/System/source/gd32_flash.c

@@ -0,0 +1,105 @@
+#include "gd32f30x.h"
+#include "gd32_flash.h"
+#include "string.h"
+#include "FreeRTOS.h"
+#include "task.h"
+/*
+ *gd32f307RCT6的flash内存总共有256K的空间现在要对齐进行相应的分区
+ *前30K空间用于BOOTLOADER区
+ *后30K空间用于json字符串数据保存,其中前4字节保存数据长度
+ *剩下空间用于正常程序
+ */
+#define FMC_READ(addrx) (*(volatile uint32_t *)(uint32_t)(addrx))
+void GD32_WriteFlash(uint32_t saddr, uint32_t *wdata, uint32_t wnum);
+/*-------------------------------------------------*/
+/*函数名:保存数据到flash中                        */
+/*参  数:params保存的字符串数据								   */
+/*返回值:无 																			 */
+/*前4个字节用于保存字符串长度后面全是字符串数据   TODO:最大长度的限制 	*/
+/*-------------------------------------------------*/
+int save_config_params(char *params)
+{
+	uint32_t paramsLen = strlen(params);
+	volatile uint32_t flashDataLen = FMC_READ(FMC_WRITE_START_ADDR);
+	if (paramsLen % 4 != 0)
+	{
+		paramsLen = (paramsLen / 4 + 1) * 4;
+	}
+
+	if (flashDataLen != paramsLen)
+	{
+		GD32_WriteFlash(FMC_WRITE_START_ADDR, (uint32_t *)&paramsLen, 4);
+		GD32_WriteFlash(FMC_WRITE_START_ADDR + 4, (uint32_t *)params, paramsLen);
+		return 1;
+	}
+	for (int i = 1; i < paramsLen / 4; i++)
+	{
+		uint32_t flashWord = FMC_READ(FMC_WRITE_START_ADDR + i * 4);
+		uint32_t paramsWord = *(uint32_t *)(params + (i - 1) * 4);
+		vTaskDelay(100); // 解决flash速度缓慢判断错误问题
+		if (flashWord != paramsWord)
+		{
+			GD32_WriteFlash(FMC_WRITE_START_ADDR + i * 4, &paramsWord, 4);
+		}
+	}
+}
+/*-------------------------------------------------*/
+/*函数名:从flash中读出字符串数据                       */
+/*参  数:params保存的字符串数据								   */
+/*返回值:无 																			 */
+/*前4个字节用于保存字符串长度后面全是字符串数据 	*/
+/*-------------------------------------------------*/
+int read_data_from_flash(void *buffer)
+{
+	uint32_t length = FMC_READ(FMC_WRITE_START_ADDR);
+	vTaskDelay(100);
+	if (length == 0xFFFFFFFF)
+	{
+		return -1;
+	}
+	for (uint32_t i = 0; i < length; i += 4)
+	{
+		*(uint32_t *)((uint8_t *)buffer + i) = FMC_READ(FMC_WRITE_START_ADDR + 4 + i);
+	}
+	return 0;
+}
+
+/*-------------------------------------------------*/
+/*函数名:擦除FLASH                                */
+/*参  数:start:擦除起始扇区   num:擦几个扇区    */
+/*返回值:无                                       */
+/*-------------------------------------------------*/
+void GD32_EraseFlash(uint16_t start, uint16_t num)
+{
+	uint16_t i;
+
+	fmc_unlock();
+	for (i = 0; i < num; i++)
+	{
+		fmc_page_erase((0x08000000 + start * 1024) + (1024 * i));
+	}
+	fmc_lock();
+}
+/*---------------------------------------------------------------------*/
+/*函数名:写入FLASH                                                    */
+/*参  数:saddr:写入地址 wdata:写入数据指针  wnum:写入多少个字节    */
+/*返回值:无                                                           */
+/*---------------------------------------------------------------------*/
+void GD32_WriteFlash(uint32_t saddr, uint32_t *wdata, uint32_t wnum)
+{
+	fmc_unlock();
+	while (wnum > 0)
+	{
+		fmc_word_program(saddr, *wdata);
+		wnum -= 4;
+		saddr += 4;
+		wdata++;
+	}
+	fmc_lock();
+}
+
+void gd32_flash_test()
+{
+	char *p = "hello,word/r/n";
+	save_config_params(p);
+}

+ 1 - 6
app/System/source/tcp_server.c

@@ -41,21 +41,16 @@ tcp_server_begin:
         goto tcp_server_begin;
     }
 		
-		
-		
-		
-		
-		
     while (1)
     {
 				vTaskDelay(1000);
         socklen_t len = sizeof(client_sock);
         int client_socket = accept(sockfd, (struct sockaddr*)&client_sock,&len);
+				LogPrint(LOG_INFO,__FILE__,__FUNCTION__,__LINE__,"ÉÏλ»ú³É¹¦Á¬½Ó%s");
         if (client_socket<0)
         {
             printf("error");
         }
-//        LogPrint(LOG_INFO,);
     }
     
 }

+ 2 - 1
app/System/source/udp.c

@@ -166,7 +166,7 @@ int udp_log_start()
 {
 	int ret;
 	struct sockaddr_in local_addr;
-	    // ´´½¨UDPÌ×½Ó×Ö
+	// ´´½¨UDPÌ×½Ó×Ö
   sockfd = socket(AF_INET, SOCK_DGRAM, 0);
   if (sockfd < -1) {
     perror("Error creating socket");
@@ -206,6 +206,7 @@ int fputc(int ch, FILE *f)
     return ch;
 	}
 }
+
 int udp_send_printf(char *p)
 {
 	if(sockfd>=0)

+ 1 - 1
app/USR/include/FreeRTOSConfig.h

@@ -82,7 +82,7 @@ extern uint32_t SystemCoreClock;
 #define configTICK_RATE_HZ                            ( ( TickType_t ) 1000 )
 #define configMAX_PRIORITIES                          ( 6 )
 #define configMINIMAL_STACK_SIZE                      ( ( unsigned short ) 128 )
-#define configTOTAL_HEAP_SIZE                         ( ( size_t ) ( 50 * 1024 ) )
+#define configTOTAL_HEAP_SIZE                         ( ( size_t ) ( 45 * 1024 ) )
 #define configMAX_TASK_NAME_LEN                       ( 16 )
 #define configUSE_16_BIT_TICKS                        0
 #define configIDLE_SHOULD_YIELD                       1

+ 4 - 2
app/USR/source/main.c

@@ -52,6 +52,7 @@ OF SUCH DAMAGE.
 #include "sys_mqtt.h"
 #include "tcp_server.h"
 #include "log.h"
+#include "gd32_flash.h"
 /*
 *freertos优先级管理
 *管理启动流程任务 init_task        	优先级 1
@@ -106,6 +107,7 @@ int main(void)
 
 void init_task(void * pvParameters)
 {
+		gd32_flash_test();
     /* configure ethernet (GPIOs, clocks, MAC, DMA) */ 
     enet_system_setup();
 		log_init();
@@ -150,9 +152,9 @@ void led_task(void * pvParameters)
 
 void vApplicationStackOverflowHook(TaskHandle_t xTask, char *pcTaskName )
 {
-	printf("task :%s 栈溢出\r\n",pcTaskName);
+	LogPrint(LOG_ERROR,__FILE__,__FUNCTION__,__LINE__,"task :%s 栈溢出\r\n",pcTaskName);
 }
 void vApplicationMallocFailedHook()
 {
-		printf("malloc error\r\n");
+		LogPrint(LOG_ERROR,__FILE__,__FUNCTION__,__LINE__,"malloc error");
 }