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.

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