Storage.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * This file is free software: you can redistribute it and/or modify it
  3. * under the terms of the GNU General Public License as published by the
  4. * Free Software Foundation, either version 3 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This file is distributed in the hope that it will be useful, but
  8. * WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. * See the GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License along
  13. * with this program. If not, see <http://www.gnu.org/licenses/>.
  14. *
  15. * Code by Andrew Tridgell and Siddharth Bharat Purohit
  16. */
  17. #pragma once
  18. #include <AP_HAL/AP_HAL.h>
  19. #include "AP_HAL_ChibiOS_Namespace.h"
  20. #include <AP_Common/Bitmask.h>
  21. #include <AP_FlashStorage/AP_FlashStorage.h>
  22. #include "hwdef/common/flash.h"
  23. #include <AP_RAMTRON/AP_RAMTRON.h>
  24. #define CH_STORAGE_SIZE HAL_STORAGE_SIZE
  25. #ifndef HAL_USE_EMPTY_STORAGE
  26. // when using flash storage we use a small line size to make storage
  27. // compact and minimise the number of erase cycles needed
  28. #define CH_STORAGE_LINE_SHIFT 3
  29. #define CH_STORAGE_LINE_SIZE (1<<CH_STORAGE_LINE_SHIFT)
  30. #define CH_STORAGE_NUM_LINES (CH_STORAGE_SIZE/CH_STORAGE_LINE_SIZE)
  31. class ChibiOS::Storage : public AP_HAL::Storage {
  32. public:
  33. void init() override {}
  34. void read_block(void *dst, uint16_t src, size_t n) override;
  35. void write_block(uint16_t dst, const void* src, size_t n) override;
  36. void _timer_tick(void) override;
  37. bool healthy(void) override;
  38. private:
  39. volatile bool _initialised;
  40. void _storage_create(void);
  41. void _storage_open(void);
  42. void _save_backup(void);
  43. void _mark_dirty(uint16_t loc, uint16_t length);
  44. uint8_t _buffer[CH_STORAGE_SIZE] __attribute__((aligned(4)));
  45. Bitmask<CH_STORAGE_NUM_LINES> _dirty_mask;
  46. bool _flash_write_data(uint8_t sector, uint32_t offset, const uint8_t *data, uint16_t length);
  47. bool _flash_read_data(uint8_t sector, uint32_t offset, uint8_t *data, uint16_t length);
  48. bool _flash_erase_sector(uint8_t sector);
  49. bool _flash_erase_ok(void);
  50. uint8_t _flash_page;
  51. bool _flash_failed;
  52. uint32_t _last_re_init_ms;
  53. uint32_t _last_empty_ms;
  54. #ifdef STORAGE_FLASH_PAGE
  55. AP_FlashStorage _flash{_buffer,
  56. stm32_flash_getpagesize(STORAGE_FLASH_PAGE),
  57. FUNCTOR_BIND_MEMBER(&Storage::_flash_write_data, bool, uint8_t, uint32_t, const uint8_t *, uint16_t),
  58. FUNCTOR_BIND_MEMBER(&Storage::_flash_read_data, bool, uint8_t, uint32_t, uint8_t *, uint16_t),
  59. FUNCTOR_BIND_MEMBER(&Storage::_flash_erase_sector, bool, uint8_t),
  60. FUNCTOR_BIND_MEMBER(&Storage::_flash_erase_ok, bool)};
  61. #endif
  62. void _flash_load(void);
  63. void _flash_write(uint16_t line);
  64. #if HAL_WITH_RAMTRON
  65. AP_RAMTRON fram;
  66. bool using_fram;
  67. #endif
  68. #ifdef USE_POSIX
  69. bool using_filesystem;
  70. int log_fd;
  71. #endif
  72. };
  73. #endif // HAL_USE_EMPTY_STORAGE