flash.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2018 Ayke van Laethem
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "py/mpconfig.h"
  27. #if MICROPY_MBFS && BLUETOOTH_SD
  28. #include "drivers/flash.h"
  29. #include "drivers/bluetooth/ble_drv.h"
  30. #include "nrf_soc.h"
  31. // Rotates bits in `value` left `shift` times.
  32. STATIC inline uint32_t rotate_left(uint32_t value, uint32_t shift) {
  33. return (value << shift) | (value >> (32 - shift));
  34. }
  35. STATIC volatile flash_state_t flash_operation_state = FLASH_STATE_BUSY;
  36. STATIC void operation_init(void) {
  37. flash_operation_state = FLASH_STATE_BUSY;
  38. }
  39. void flash_operation_finished(flash_state_t result) {
  40. flash_operation_state = result;
  41. }
  42. STATIC bool operation_wait(uint32_t result) {
  43. if (ble_drv_stack_enabled() != 1) {
  44. // SoftDevice is not enabled, no event will be generated.
  45. return result == NRF_SUCCESS;
  46. }
  47. if (result != NRF_SUCCESS) {
  48. // In all other (non-success) cases, the command hasn't been
  49. // started and no event will be generated.
  50. return false;
  51. }
  52. // Wait until the event has been generated.
  53. while (flash_operation_state == FLASH_STATE_BUSY) {
  54. sd_app_evt_wait();
  55. }
  56. // Now we can safely continue, flash operation has completed.
  57. return flash_operation_state == FLASH_STATE_SUCCESS;
  58. }
  59. void flash_write_byte(uint32_t address, uint8_t b) {
  60. uint32_t address_aligned = address & ~3;
  61. // Value to write - leave all bits that should not change at 0xff.
  62. uint32_t value = 0xffffff00 | b;
  63. // Rotate bits in value to an aligned position.
  64. value = rotate_left(value, (address & 3) * 8);
  65. while (1) {
  66. operation_init();
  67. uint32_t result = sd_flash_write((uint32_t*)address_aligned, &value, 1);
  68. if (operation_wait(result)) break;
  69. }
  70. }
  71. void flash_page_erase(uint32_t pageaddr) {
  72. while (1) {
  73. operation_init();
  74. uint32_t result = sd_flash_page_erase(pageaddr / FLASH_PAGESIZE);
  75. if (operation_wait(result)) break;
  76. }
  77. }
  78. void flash_write_bytes(uint32_t dst, const uint8_t *src, uint32_t num_bytes) {
  79. const uint8_t *src_end = src + num_bytes;
  80. // sd_flash_write does not accept unaligned addresses so we have to
  81. // work around that by writing all unaligned addresses byte-by-byte.
  82. // Write first bytes to align the write address.
  83. while (src != src_end && (dst & 0b11)) {
  84. flash_write_byte(dst, *src);
  85. dst++;
  86. src++;
  87. }
  88. // Write as many words as possible.
  89. // dst is now aligned, src possibly not.
  90. while (src_end - src >= 4) {
  91. uint8_t buf[4] __attribute__((aligned(4)));
  92. for (int i = 0; i < 4; i++) {
  93. buf[i] = ((uint8_t*)src)[i];
  94. }
  95. operation_init();
  96. uint32_t result = sd_flash_write((uint32_t*)dst, (const uint32_t*)&buf, 1);
  97. if (operation_wait(result)) {
  98. // If it is successfully written, go to the next word.
  99. src += 4;
  100. dst += 4;
  101. }
  102. }
  103. // Write remaining unaligned bytes.
  104. while (src != src_end) {
  105. flash_write_byte(dst, *src);
  106. dst++;
  107. src++;
  108. }
  109. }
  110. #endif // MICROPY_MBFS