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.

134 lines
3.6 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 CONFIG_SIZE ((sizeof(config_t) / 4) + ((sizeof(config_t) % 4) > 0 ? 1 : 0))
  9. static config_t s_config;
  10. static bool s_is_first_init = false;
  11. void config_init(void)
  12. {
  13. #if 1
  14. IP4_ADDR((ip4_addr_t *)&s_config.ip, 192, 168, 8, 10);
  15. IP4_ADDR((ip4_addr_t *)&s_config.gw, 192, 168, 8, 1);
  16. IP4_ADDR((ip4_addr_t *)&s_config.netmask, 255, 255, 255, 0);
  17. s_config.localport = SERVICE_PORT;
  18. s_config.obtaining_ip_mode = OBTAINING_IP_MODE_DHCP; // dhcp
  19. s_config.mask = 1; //
  20. #endif
  21. if (config_flash_write() < 0)
  22. {
  23. printf("flash write config error\r\n");
  24. }
  25. else
  26. {
  27. printf("flash write config success\r\n");
  28. }
  29. config_flash_read();
  30. /*dumpconfig*/
  31. config_dump_config();
  32. }
  33. bool config_is_first_init(void) { return s_is_first_init; }
  34. void config_dump_config(void)
  35. {
  36. printf("=================config================\r\n");
  37. printf("= config obtaining_ip_mode %u\r\n", s_config.obtaining_ip_mode);
  38. printf("= config ip %s\r\n", inet_ntoa(s_config.ip));
  39. printf("= config gw: %s\r\n", inet_ntoa(s_config.gw));
  40. printf("= config netmask: %s\r\n", inet_ntoa(s_config.netmask));
  41. printf("= config localport: %u\r\n", s_config.localport);
  42. }
  43. bool config_update(void)
  44. {
  45. // if (stmflash_write(FLASH_SAVE_ADDR, (uint32_t *)&s_config, CONFIG_SIZE) < 0)
  46. // {
  47. // printf("flash write config error\r\n");
  48. // return false;
  49. // }
  50. // config_dump_config();
  51. return true;
  52. }
  53. void config_flash_read(void)
  54. {
  55. uint32_t config_flash_write_start_address = FLASH_START_ADDRESS;
  56. printf("=================read================\r\n");
  57. printf("mask: %d\r\n", FlashRead(config_flash_write_start_address + 0));
  58. printf("obtaining_ip_mode: %d\r\n", FlashRead(config_flash_write_start_address + 4));
  59. printf("ip: %d\r\n", FlashRead(config_flash_write_start_address + 8));
  60. printf("gw: %d\r\n", FlashRead(config_flash_write_start_address + 12));
  61. printf("netmask: %d\r\n", FlashRead(config_flash_write_start_address + 16));
  62. printf("localport: %d\r\n", FlashRead(config_flash_write_start_address + 20));
  63. }
  64. config_t *config_get(void) { return &s_config; }
  65. int8_t config_flash_write(void)
  66. {
  67. HAL_StatusTypeDef status;
  68. uint32_t config_flash_write_start_address = FLASH_START_ADDRESS;
  69. status = FlashErase();
  70. if (status != HAL_OK)
  71. {
  72. printf("flash erase error, status:%d\r\n", status);
  73. return -1;
  74. }
  75. status = FlashWrite(config_flash_write_start_address + 0, s_config.mask);
  76. if (status != HAL_OK)
  77. {
  78. printf("flash write error, status:%d\r\n", status);
  79. return -2;
  80. }
  81. status = FlashWrite(config_flash_write_start_address + 4, s_config.obtaining_ip_mode);
  82. if (status != HAL_OK)
  83. {
  84. printf("flash write error, status:%d\r\n", status);
  85. return -3;
  86. }
  87. status = FlashWrite(config_flash_write_start_address + 8, s_config.ip);
  88. if (status != HAL_OK)
  89. {
  90. printf("flash write error, status:%d\r\n", status);
  91. return -4;
  92. }
  93. status = FlashWrite(config_flash_write_start_address + 12, s_config.gw);
  94. if (status != HAL_OK)
  95. {
  96. printf("flash write error, status:%d\r\n", status);
  97. return -5;
  98. }
  99. status = FlashWrite(config_flash_write_start_address + 16, s_config.netmask);
  100. if (status != HAL_OK)
  101. {
  102. printf("flash write error, status:%d\r\n", status);
  103. return -6;
  104. }
  105. status = FlashWrite(config_flash_write_start_address + 20, s_config.localport);
  106. if (status != HAL_OK)
  107. {
  108. printf("flash write error, status:%d\r\n", status);
  109. return -7;
  110. }
  111. return 0;
  112. }