first submission
This commit is contained in:
@@ -0,0 +1,164 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : ModuleConfig.c
|
||||
* Author : WCH
|
||||
* Version : V1.0.0
|
||||
* Date : 2022/06/10
|
||||
* Description : Module configuration related functions.
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "ModuleConfig.h"
|
||||
#include "wchnet.h"
|
||||
|
||||
/*********************************************************************
|
||||
* @fn CFG_ERASE
|
||||
*
|
||||
* @brief erase Data-Flash block, minimal block is 256B
|
||||
*
|
||||
* @param Page_Address - the address of the page being erased.
|
||||
* Length - Erased data length
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void CFG_ERASE(uint32_t Page_Address, u32 Length) {
|
||||
u32 NbrOfPage, EraseCounter;
|
||||
|
||||
FLASH_Unlock_Fast();
|
||||
NbrOfPage = Length / FLASH_PAGE_SIZE;
|
||||
|
||||
for (EraseCounter = 0; EraseCounter < NbrOfPage; EraseCounter++) {
|
||||
FLASH_ErasePage_Fast( Page_Address + (FLASH_PAGE_SIZE * EraseCounter));
|
||||
}
|
||||
FLASH_Lock_Fast();
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn CFG_WRITE
|
||||
*
|
||||
* @brief write Data-Flash data block
|
||||
*
|
||||
* @param StartAddr - the address of the page being written.
|
||||
* Buffer - data buff
|
||||
* Length - written data length
|
||||
*
|
||||
* @return FLASH_Status
|
||||
*/
|
||||
FLASH_Status CFG_WRITE( u32 StartAddr, u8 *Buffer, u32 Length )
|
||||
{
|
||||
u32 address = StartAddr;
|
||||
u32 *p_buff = (u32 *)Buffer;
|
||||
FLASH_Status FLASHStatus = FLASH_COMPLETE;
|
||||
|
||||
FLASH_Unlock();
|
||||
while((address < (StartAddr + Length)) && (FLASHStatus == FLASH_COMPLETE))
|
||||
{
|
||||
FLASHStatus = FLASH_ProgramWord(address, *p_buff);
|
||||
address += 4;
|
||||
p_buff++;
|
||||
}
|
||||
FLASH_Lock();
|
||||
return FLASHStatus;
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn CFG_READ
|
||||
*
|
||||
* @brief read Data-Flash data block
|
||||
*
|
||||
* @param StartAddr - the address of the page being read.
|
||||
* Buffer - data buff
|
||||
* Length - read data length
|
||||
*
|
||||
* @return none
|
||||
*/
|
||||
void CFG_READ( u32 StartAddr, u8 *Buffer, u32 Length )
|
||||
{
|
||||
u32 address = StartAddr;
|
||||
u32 *p_buff = (u32 *)Buffer;
|
||||
|
||||
while(address < (StartAddr + Length))
|
||||
{
|
||||
*p_buff = (*(u32 *)address);
|
||||
address += 4;
|
||||
p_buff++;
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************
|
||||
* @fn ParseConfigbuf
|
||||
*
|
||||
* @brief Parse Configuration command
|
||||
*
|
||||
* @param buf - data buff.
|
||||
*
|
||||
* @return opcode
|
||||
*/
|
||||
u8 ParseConfigbuf(u8 *buf)
|
||||
{
|
||||
u16 i;
|
||||
u32 len;
|
||||
pnet_comm net = (pnet_comm)buf;
|
||||
|
||||
i = memcmp(net->flag,NET_MODULE_FLAG,sizeof(NET_MODULE_FLAG));
|
||||
if(i) return 0;
|
||||
switch(net->cmd)
|
||||
{
|
||||
case NET_MODULE_CMD_SEARCH : //search module
|
||||
memcpy(net->id,MACAddr,6);
|
||||
net->cmd = NET_MODULE_ACK_SEARCH ;
|
||||
sprintf((char *)net->dat,"%d.%d.%d.%d",(u16)CFG->src_ip[0],
|
||||
(u16)CFG->src_ip[1],(u16)CFG->src_ip[2],(u16)CFG->src_ip[3]);
|
||||
net->len = strlen((char *)net->dat);
|
||||
sprintf((char*)&net->dat[net->len + 1],"%s",CFG->module_name);
|
||||
net->len += strlen((char*)&net->dat[net->len + 1]) + 1;
|
||||
len = sizeof(net_comm);
|
||||
WCHNET_SocketUdpSendTo(0,buf,&len,brocastIp,brocastPort);
|
||||
printf("CMD_SEARCH********************\n");
|
||||
return 0;
|
||||
|
||||
case NET_MODULE_CMD_SET : //Configure module parameters
|
||||
i = memcmp(net->id,MACAddr,6);
|
||||
if(i) return 0;
|
||||
net->cmd = NET_MODULE_ACK_SET ;
|
||||
net->len = 0;
|
||||
memcpy(Configbuf,net->dat,MODULE_CFG_LEN);
|
||||
CFG->cfg_flag[0]=checkcode1;
|
||||
CFG->cfg_flag[1]=checkcode2;
|
||||
CFG_ERASE(PAGE_WRITE_START_ADDR,FLASH_PAGE_SIZE);
|
||||
CFG_WRITE(PAGE_WRITE_START_ADDR,Configbuf,MODULE_CFG_LEN );
|
||||
len = sizeof(net_comm);
|
||||
WCHNET_SocketUdpSendTo(0,buf,&len,brocastIp,brocastPort);
|
||||
printf("CMD_SET***********************\n");
|
||||
return 1;
|
||||
|
||||
case NET_MODULE_CMD_GET : //Get module configuration
|
||||
i = memcmp(net->id,MACAddr,6);
|
||||
if(i) return 0;
|
||||
net->cmd = NET_MODULE_ACK_GET ;
|
||||
/*When returning the configuration information
|
||||
* to the host computer, remove the last two
|
||||
* bytes of the verification code*/
|
||||
net->len = (MODULE_CFG_LEN-2);
|
||||
memcpy(net->dat,Configbuf,(MODULE_CFG_LEN-2)) ;
|
||||
len = sizeof(net_comm);
|
||||
WCHNET_SocketUdpSendTo(0,buf,&len,brocastIp,brocastPort);
|
||||
printf("CMD_GET***********************\n");
|
||||
return 0;
|
||||
|
||||
case NET_MODULE_CMD_RESET : //reset module
|
||||
i = memcmp(net->id,MACAddr,6);
|
||||
if(i) return 0;
|
||||
CFG_ERASE(PAGE_WRITE_START_ADDR,FLASH_PAGE_SIZE);
|
||||
CFG_WRITE(PAGE_WRITE_START_ADDR,Default_cfg,MODULE_CFG_LEN );
|
||||
net->cmd = NET_MODULE_ACK_RESET ;
|
||||
len = sizeof(net_comm);
|
||||
printf("CMD_RESET*********************\n");
|
||||
return 1;
|
||||
default:
|
||||
return 0 ;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
/********************************** (C) COPYRIGHT *******************************
|
||||
* File Name : ModuleConfig.h
|
||||
* Author : WCH
|
||||
* Version : V1.0.0
|
||||
* Date : 2022/06/10
|
||||
* Description : Module configuration related command codes
|
||||
* and configuration structures.
|
||||
*********************************************************************************
|
||||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||||
* Attention: This software (modified or not) and binary are used for
|
||||
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
|
||||
*******************************************************************************/
|
||||
#ifndef __MODULECONFIG_H__
|
||||
#define __MODULECONFIG_H__
|
||||
|
||||
#include "debug.h"
|
||||
|
||||
/*Address where configuration information is stored*/
|
||||
#define PAGE_WRITE_START_ADDR ((uint32_t)0x0801FF00) /* Start from (128K - 256B) */
|
||||
#define PAGE_WRITE_END_ADDR ((uint32_t)0x08020000) /* End at 128K */
|
||||
#define FLASH_PAGE_SIZE 256
|
||||
|
||||
#define NET_MODULE_DATA_LENGTH 60 //Maximum length of the data area
|
||||
#define MODULE_CFG_LEN 44 //The length of configuration buff
|
||||
|
||||
/*Communication command code*/
|
||||
#define NET_MODULE_CMD_SET 0X01 //Configure module parameters
|
||||
#define NET_MODULE_CMD_GET 0X02 //Get module configuration
|
||||
#define NET_MODULE_CMD_RESET 0X03 //reset module
|
||||
#define NET_MODULE_CMD_SEARCH 0X04 //search module
|
||||
|
||||
/*ACK command code*/
|
||||
#define NET_MODULE_ACK_SET 0X81 //Respond to configuration command
|
||||
#define NET_MODULE_ACK_GET 0X82 //Response to get configure command
|
||||
#define NET_MODULE_ACK_RESET 0X83 //Respond to reset command
|
||||
#define NET_MODULE_ACK_SEARCH 0X84 //Respond to search commands
|
||||
|
||||
#define NET_MODULE_FLAG "WCHNET_MODULE" //identify the host computer communication
|
||||
#define checkcode1 0X11 //Identity configuration information
|
||||
#define checkcode2 0x22
|
||||
|
||||
/*communication structure*/
|
||||
typedef struct NET_COMM {
|
||||
/*The communication identifier,
|
||||
* because they are all communicated
|
||||
* by broadcasting, a fixed value is
|
||||
* added here.*/
|
||||
unsigned char flag[16];
|
||||
unsigned char cmd; //command header
|
||||
unsigned char id[6]; //ID, WCHNET MAC address
|
||||
unsigned char len; //data length
|
||||
unsigned char dat[NET_MODULE_DATA_LENGTH]; //data buffer
|
||||
}net_comm,*pnet_comm;
|
||||
|
||||
/*Module Communication Mode Identification*/
|
||||
#define NET_MODULE_TYPE_TCP_S 0X00 //TCP SERVER
|
||||
#define NET_MODULE_TYPE_TCP_C 0X01 //TCP CLIENT
|
||||
#define NET_MODULE_TYPE_UDP_S 0X02 //UDP SERVER
|
||||
#define NET_MODULE_TYPE_UDP_C 0X03 //UDP CLIENT
|
||||
#define NET_MODULE_TYPE_NONE 0X04 //no mode is enabled
|
||||
|
||||
/*Configuration structure*/
|
||||
typedef struct MODULE_CFG {
|
||||
unsigned char module_name[21]; //module name
|
||||
unsigned char type; //The module is in default mode (no mode is enabled by default)
|
||||
unsigned char src_ip[4]; //The IP address of the module
|
||||
unsigned char mask[4]; //The subnet mask of the module
|
||||
unsigned char getway[4]; //The gateway address of the module
|
||||
unsigned char src_port[2]; //Module source port
|
||||
unsigned char dest_ip[4]; //destination IP address
|
||||
unsigned char dest_port[2]; //destination port
|
||||
unsigned char cfg_flag[2]; //Verification code, used to verify configuration information
|
||||
}module_cfg,*pmodule_cfg;
|
||||
|
||||
extern u8 MACAddr[6];
|
||||
extern u8 brocastIp[4];
|
||||
extern u8 Configbuf[MODULE_CFG_LEN];
|
||||
extern u8 Default_cfg[MODULE_CFG_LEN];
|
||||
extern u16 brocastPort;
|
||||
extern pmodule_cfg CFG;
|
||||
|
||||
extern u8 ParseConfigbuf(u8 *buf);
|
||||
|
||||
extern void CFG_ERASE(uint32_t Page_Address, u32 Length );
|
||||
|
||||
extern void CFG_READ( u32 StartAddr, u8 *Buffer, u32 Length );
|
||||
|
||||
extern FLASH_Status CFG_WRITE( u32 StartAddr, u8 *Buffer, u32 Length );
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user