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.

98 lines
2.7 KiB

  1. #include "config.h"
  2. #include <lwip/sockets.h>
  3. #include "lwip/api.h"
  4. #include "lwip/opt.h"
  5. #include "lwip/sys.h"
  6. #include "zflash.h"
  7. #include "zboard.h"
  8. #define FLASH_WRITE(data) \
  9. status = FlashWrite(config_flash_write_start_address, data); \
  10. if (status != HAL_OK) \
  11. { \
  12. printf("flash write error, status:%d\r\n", status); \
  13. return -2; \
  14. } \
  15. config_flash_write_start_address += 4;
  16. #define FLASH_READ(data) \
  17. data = FlashRead(config_flash_write_start_address); \
  18. config_flash_write_start_address += 4;
  19. #define CONFIG_SIZE ((sizeof(config_t) / 4) + ((sizeof(config_t) % 4) > 0 ? 1 : 0))
  20. static config_t s_config;
  21. static bool s_is_first_init = false;
  22. void config_init(void)
  23. {
  24. IP4_ADDR((ip4_addr_t *)&s_config.ip, 192, 168, 8, 10);
  25. IP4_ADDR((ip4_addr_t *)&s_config.gw, 192, 168, 8, 1);
  26. IP4_ADDR((ip4_addr_t *)&s_config.netmask, 255, 255, 255, 0);
  27. s_config.localport = SERVICE_PORT;
  28. s_config.obtaining_ip_mode = OBTAINING_IP_MODE_DHCP; // dhcp
  29. s_config.mask = 1; //
  30. if (config_flash_write() < 0)
  31. {
  32. printf("flash write config error\r\n");
  33. }
  34. else
  35. {
  36. printf("flash write config success\r\n");
  37. }
  38. config_flash_read();
  39. /*dumpconfig*/
  40. config_dump_config();
  41. }
  42. bool config_is_first_init(void) { return s_is_first_init; }
  43. void config_dump_config(void)
  44. {
  45. printf("=================config================\r\n");
  46. printf("= config obtaining_ip_mode %u\r\n", s_config.obtaining_ip_mode);
  47. printf("= config ip %s\r\n", inet_ntoa(s_config.ip));
  48. printf("= config gw: %s\r\n", inet_ntoa(s_config.gw));
  49. printf("= config netmask: %s\r\n", inet_ntoa(s_config.netmask));
  50. printf("= config localport: %u\r\n", s_config.localport);
  51. }
  52. void config_flash_read(void)
  53. {
  54. uint32_t config_flash_write_start_address = FLASH_START_ADDRESS;
  55. FLASH_READ(s_config.mask);
  56. FLASH_READ(s_config.obtaining_ip_mode);
  57. FLASH_READ(s_config.ip);
  58. FLASH_READ(s_config.gw);
  59. FLASH_READ(s_config.netmask);
  60. FLASH_READ(s_config.localport);
  61. }
  62. config_t *config_get(void) { return &s_config; }
  63. int8_t config_flash_write(void)
  64. {
  65. HAL_StatusTypeDef status;
  66. uint32_t config_flash_write_start_address = FLASH_START_ADDRESS;
  67. status = FlashErase();
  68. if (status != HAL_OK)
  69. {
  70. printf("flash erase error, status:%d\r\n", status);
  71. return -1;
  72. }
  73. FLASH_WRITE(s_config.mask);
  74. FLASH_WRITE(s_config.obtaining_ip_mode);
  75. FLASH_WRITE(s_config.ip);
  76. FLASH_WRITE(s_config.gw);
  77. FLASH_WRITE(s_config.netmask);
  78. FLASH_WRITE(s_config.localport);
  79. return 0;
  80. }