sys_hal.c 2.1 KB

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