sys_hal.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include <stdint.h>
  2. #include "sys_hal.h"
  3. #include "sys_api.h"
  4. #include "common_config.h"
  5. #include "common.h"
  6. //#include "hal.h"
  7. #ifdef PRINTF_STDLIB
  8. #include <stdio.h>
  9. #endif
  10. #ifdef PRINTF_CUSTOM
  11. #include "tinystdio.h"
  12. #endif
  13. /**
  14. * @brief
  15. */
  16. void SYS_ReadFromFlash(uint8_t *data, uint32_t size, uint32_t baseAddress)
  17. {
  18. for (uint32_t i = 0; i < size; i++)
  19. *data++ = (*(uint32_t*)baseAddress++);
  20. }
  21. /**
  22. * @brief
  23. */
  24. void SYS_WriteToFlash(uint8_t *data, uint32_t size, uint32_t crc)
  25. {
  26. uint32_t baseAddress = SYS_SECTOR;
  27. uint32_t checkCrc = 0;
  28. bool fAlarm = 0;
  29. flash_status_type status;
  30. uint8_t *pdata = data;
  31. for (uint8_t i = 0; i < 3; i++)
  32. {
  33. fAlarm = 0;
  34. flash_unlock();
  35. SYS_EraseFlashSector();
  36. for (uint32_t i = 0; i < size; i++) {
  37. if ((status = flash_byte_program(baseAddress++, *pdata++)) != FLASH_OPERATE_DONE) {
  38. SDBG printf("FLASH_ProgramByte error: %d\r\n", status);
  39. break;
  40. }
  41. }
  42. if ((status = flash_word_program((uint32_t)SYS_CRC_ADDRESS, crc)) != FLASH_OPERATE_DONE) {
  43. SDBG printf("FLASH_ProgramWord error: %d\r\n", status);
  44. }
  45. flash_lock();
  46. checkCrc = SYS_GetCRC((SYS_t *)SYS_SECTOR);
  47. // Проверяем CRC того, что было записано
  48. if (checkCrc == crc) {
  49. SDBG printf("SYS write OK\r\n");
  50. break;
  51. }
  52. else
  53. fAlarm = 1; // Авария
  54. }
  55. // Произошел сбой при записи - перезагружаемся
  56. if (fAlarm) {
  57. SDBG printf("Flash write error: 1%d", (uint8_t)status);;
  58. NVIC_SystemReset();
  59. }
  60. }
  61. /**
  62. * @brief Очистка сектора настроек
  63. * @retval
  64. */
  65. void SYS_EraseFlashSector(void)
  66. {
  67. flash_status_type status;
  68. if ((status = flash_sector_erase(SYS_SECTOR)) != FLASH_OPERATE_DONE) {
  69. SDBG printf("SYS_EraseFlashSector error: %d\r\n", status);
  70. }
  71. }