|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2013, 2014 Damien P. George |
| 7 | + * Copyright (c) 2019 Nick Moore for Adafruit Industries |
| 8 | + * |
| 9 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | + * of this software and associated documentation files (the "Software"), to deal |
| 11 | + * in the Software without restriction, including without limitation the rights |
| 12 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | + * copies of the Software, and to permit persons to whom the Software is |
| 14 | + * furnished to do so, subject to the following conditions: |
| 15 | + * |
| 16 | + * The above copyright notice and this permission notice shall be included in |
| 17 | + * all copies or substantial portions of the Software. |
| 18 | + * |
| 19 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | + * THE SOFTWARE. |
| 26 | + */ |
| 27 | + |
| 28 | +#include "py/runtime.h" |
| 29 | + |
| 30 | +#include <stdio.h> |
| 31 | +#include <string.h> |
| 32 | + |
| 33 | +#include "nrf_nvmc.h" |
| 34 | + |
| 35 | +#define FLASH_PAGE_SIZE (4096) |
| 36 | + |
| 37 | +#ifdef BLUETOOTH_SD |
| 38 | +#include "ble_drv.h" |
| 39 | +#include "nrf_sdm.h" |
| 40 | + |
| 41 | +STATIC void sd_flash_operation_start(void) { |
| 42 | + sd_flash_operation_status = SD_FLASH_OPERATION_IN_PROGRESS; |
| 43 | +} |
| 44 | + |
| 45 | +STATIC sd_flash_operation_status_t sd_flash_operation_wait_until_done(void) { |
| 46 | + while (sd_flash_operation_status == SD_FLASH_OPERATION_IN_PROGRESS) { |
| 47 | + sd_app_evt_wait(); |
| 48 | + } |
| 49 | + return sd_flash_operation_status; |
| 50 | +} |
| 51 | +#endif |
| 52 | + |
| 53 | +void nrf_nvm_safe_flash_page_write(uint32_t page_addr, uint8_t *data) { |
| 54 | + #ifdef BLUETOOTH_SD |
| 55 | + uint8_t sd_en = 0; |
| 56 | + (void) sd_softdevice_is_enabled(&sd_en); |
| 57 | + if (sd_en) { |
| 58 | + uint32_t err_code; |
| 59 | + sd_flash_operation_status_t status; |
| 60 | + |
| 61 | + sd_flash_operation_start(); |
| 62 | + err_code = sd_flash_page_erase(page_addr / FLASH_PAGE_SIZE); |
| 63 | + if (err_code != NRF_SUCCESS) { |
| 64 | + mp_raise_OSError_msg_varg(translate("Flash erase failed to start, err 0x%04x"), err_code); |
| 65 | + } |
| 66 | + status = sd_flash_operation_wait_until_done(); |
| 67 | + if (status == SD_FLASH_OPERATION_ERROR) { |
| 68 | + mp_raise_OSError_msg(translate("Flash erase failed")); |
| 69 | + } |
| 70 | + |
| 71 | + // Divide a full page into parts, because writing a full page causes an assertion failure. |
| 72 | + // See https://devzone.nordicsemi.com/f/nordic-q-a/40088/sd_flash_write-cause-nrf_fault_id_sd_assert/ |
| 73 | + const size_t BLOCK_PARTS = 2; |
| 74 | + size_t words_to_write = FLASH_PAGE_SIZE / sizeof(uint32_t) / BLOCK_PARTS; |
| 75 | + for (size_t i = 0; i < BLOCK_PARTS; i++) { |
| 76 | + sd_flash_operation_start(); |
| 77 | + err_code = sd_flash_write(((uint32_t *)page_addr) + i * words_to_write, |
| 78 | + (uint32_t *)data + i * words_to_write, |
| 79 | + words_to_write); |
| 80 | + if (err_code != NRF_SUCCESS) { |
| 81 | + mp_raise_OSError_msg_varg(translate("Flash write failed to start, err 0x%04x"), err_code); |
| 82 | + } |
| 83 | + status = sd_flash_operation_wait_until_done(); |
| 84 | + if (status == SD_FLASH_OPERATION_ERROR) { |
| 85 | + mp_raise_OSError_msg(translate("Flash write failed")); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return; |
| 90 | + } |
| 91 | + #endif |
| 92 | + |
| 93 | + nrf_nvmc_page_erase(page_addr); |
| 94 | + nrf_nvmc_write_bytes(page_addr, data, FLASH_PAGE_SIZE); |
| 95 | +} |
0 commit comments