You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
3.0 KiB
105 lines
3.0 KiB
#include "config.h"
|
|
|
|
#include <lwip/sockets.h>
|
|
|
|
#include "lwip/api.h"
|
|
#include "lwip/opt.h"
|
|
#include "lwip/sys.h"
|
|
#include "zflash.h"
|
|
#include "zboard.h"
|
|
|
|
#define FLASH_WRITE(data) \
|
|
status = FlashWrite(config_flash_write_start_address, data); \
|
|
if (status != HAL_OK) \
|
|
{ \
|
|
printf("flash write error, status:%d\r\n", status); \
|
|
return -2; \
|
|
} \
|
|
config_flash_write_start_address += 4;
|
|
|
|
#define FLASH_READ(data) \
|
|
data = FlashRead(config_flash_write_start_address); \
|
|
config_flash_write_start_address += 4;
|
|
|
|
#define CONFIG_SIZE ((sizeof(config_t) / 4) + ((sizeof(config_t) % 4) > 0 ? 1 : 0))
|
|
#define COINFIG_WRITED_FLAG 0X88888888
|
|
|
|
static config_t s_config;
|
|
static bool s_is_first_init = false;
|
|
|
|
void config_init(void)
|
|
{
|
|
config_flash_read();
|
|
|
|
if (s_config.config_writed_flag != COINFIG_WRITED_FLAG)
|
|
{
|
|
IP4_ADDR((ip4_addr_t *)&s_config.ip, 192, 168, 8, 10);
|
|
IP4_ADDR((ip4_addr_t *)&s_config.gw, 192, 168, 8, 1);
|
|
IP4_ADDR((ip4_addr_t *)&s_config.netmask, 255, 255, 255, 0);
|
|
s_config.localport = SERVICE_PORT;
|
|
s_config.obtaining_ip_mode = OBTAINING_IP_MODE_DHCP; // dhcp
|
|
s_config.mask = 1; //
|
|
s_config.config_writed_flag = COINFIG_WRITED_FLAG;
|
|
|
|
if (config_flash_write() < 0)
|
|
{
|
|
printf("flash write config error\r\n");
|
|
}
|
|
else
|
|
{
|
|
printf("flash write config success\r\n");
|
|
}
|
|
}
|
|
|
|
/*dumpconfig*/
|
|
config_dump_config();
|
|
}
|
|
|
|
bool config_is_first_init(void) { return s_is_first_init; }
|
|
|
|
void config_dump_config(void)
|
|
{
|
|
printf("=================config================\r\n");
|
|
printf("= config obtaining_ip_mode %u\r\n", s_config.obtaining_ip_mode);
|
|
printf("= config ip %s\r\n", inet_ntoa(s_config.ip));
|
|
printf("= config gw: %s\r\n", inet_ntoa(s_config.gw));
|
|
printf("= config netmask: %s\r\n", inet_ntoa(s_config.netmask));
|
|
printf("= config localport: %u\r\n", s_config.localport);
|
|
}
|
|
|
|
void config_flash_read(void)
|
|
{
|
|
uint32_t config_flash_write_start_address = FLASH_START_ADDRESS;
|
|
|
|
FLASH_READ(s_config.mask);
|
|
FLASH_READ(s_config.obtaining_ip_mode);
|
|
FLASH_READ(s_config.ip);
|
|
FLASH_READ(s_config.gw);
|
|
FLASH_READ(s_config.netmask);
|
|
FLASH_READ(s_config.localport);
|
|
FLASH_READ(s_config.config_writed_flag);
|
|
}
|
|
|
|
config_t *config_get(void) { return &s_config; }
|
|
|
|
int8_t config_flash_write(void)
|
|
{
|
|
HAL_StatusTypeDef status;
|
|
uint32_t config_flash_write_start_address = FLASH_START_ADDRESS;
|
|
status = FlashErase();
|
|
if (status != HAL_OK)
|
|
{
|
|
printf("flash erase error, status:%d\r\n", status);
|
|
return -1;
|
|
}
|
|
|
|
FLASH_WRITE(s_config.mask);
|
|
FLASH_WRITE(s_config.obtaining_ip_mode);
|
|
FLASH_WRITE(s_config.ip);
|
|
FLASH_WRITE(s_config.gw);
|
|
FLASH_WRITE(s_config.netmask);
|
|
FLASH_WRITE(s_config.localport);
|
|
FLASH_WRITE(s_config.config_writed_flag);
|
|
|
|
return 0;
|
|
}
|