BootloaderAPI.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2018.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2018 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaims all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. /** \file
  27. *
  28. * Bootloader user application API functions.
  29. */
  30. #include "BootloaderAPI.h"
  31. static bool IsPageAddressValid(const uint32_t Address)
  32. {
  33. /* Determine if the given page address is correctly aligned to the
  34. start of a flash page. */
  35. bool PageAddressIsAligned = !(Address & (SPM_PAGESIZE - 1));
  36. return (Address < BOOT_START_ADDR) && PageAddressIsAligned;
  37. }
  38. void BootloaderAPI_ErasePage(const uint32_t Address)
  39. {
  40. if (! IsPageAddressValid(Address))
  41. return;
  42. ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
  43. {
  44. boot_page_erase_safe(Address);
  45. boot_spm_busy_wait();
  46. boot_rww_enable();
  47. }
  48. }
  49. void BootloaderAPI_WritePage(const uint32_t Address)
  50. {
  51. if (! IsPageAddressValid(Address))
  52. return;
  53. ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
  54. {
  55. boot_page_write_safe(Address);
  56. boot_spm_busy_wait();
  57. boot_rww_enable();
  58. }
  59. }
  60. void BootloaderAPI_FillWord(const uint32_t Address, const uint16_t Word)
  61. {
  62. boot_page_fill_safe(Address, Word);
  63. }
  64. uint8_t BootloaderAPI_ReadSignature(const uint16_t Address)
  65. {
  66. return boot_signature_byte_get(Address);
  67. }
  68. uint8_t BootloaderAPI_ReadFuse(const uint16_t Address)
  69. {
  70. return boot_lock_fuse_bits_get(Address);
  71. }
  72. uint8_t BootloaderAPI_ReadLock(void)
  73. {
  74. return boot_lock_fuse_bits_get(GET_LOCK_BITS);
  75. }
  76. void BootloaderAPI_WriteLock(const uint8_t LockBits)
  77. {
  78. ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
  79. {
  80. boot_lock_bits_set_safe(LockBits);
  81. }
  82. }