|
| 1 | +/* |
| 2 | + Copyright (c) 2015 Arduino LLC. All right reserved. |
| 3 | +
|
| 4 | + This library is free software; you can redistribute it and/or |
| 5 | + modify it under the terms of the GNU Lesser General Public |
| 6 | + License as published by the Free Software Foundation; either |
| 7 | + version 2.1 of the License, or (at your option) any later version. |
| 8 | +
|
| 9 | + This library is distributed in the hope that it will be useful, |
| 10 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | + See the GNU Lesser General Public License for more details. |
| 13 | +
|
| 14 | + You should have received a copy of the GNU Lesser General Public |
| 15 | + License along with this library; if not, write to the Free Software |
| 16 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | +*/ |
| 18 | + |
| 19 | +#if defined (ARDUINO_SAMD_ZERO) |
| 20 | + |
| 21 | +#include "FlashStorage.h" |
| 22 | + |
| 23 | +static const uint32_t pageSizes[] = { 8, 16, 32, 64, 128, 256, 512, 1024 }; |
| 24 | + |
| 25 | +FlashClass::FlashClass(const void *flash_addr, uint32_t size) : |
| 26 | + PAGE_SIZE(pageSizes[NVMCTRL->PARAM.bit.PSZ]), |
| 27 | + PAGES(NVMCTRL->PARAM.bit.NVMP), |
| 28 | + MAX_FLASH(PAGE_SIZE * PAGES), |
| 29 | + ROW_SIZE(PAGE_SIZE * 4), |
| 30 | + flash_address((volatile void *)flash_addr), |
| 31 | + flash_size(size) |
| 32 | +{ |
| 33 | +} |
| 34 | + |
| 35 | +static inline uint32_t read_unaligned_uint32(const void *data) |
| 36 | +{ |
| 37 | + union { |
| 38 | + uint32_t u32; |
| 39 | + uint8_t u8[4]; |
| 40 | + } res; |
| 41 | + const uint8_t *d = (const uint8_t *)data; |
| 42 | + res.u8[0] = d[0]; |
| 43 | + res.u8[1] = d[1]; |
| 44 | + res.u8[2] = d[2]; |
| 45 | + res.u8[3] = d[3]; |
| 46 | + return res.u32; |
| 47 | +} |
| 48 | + |
| 49 | +void FlashClass::write(const volatile void *flash_ptr, const void *data, uint32_t size) |
| 50 | +{ |
| 51 | + // Calculate data boundaries |
| 52 | + size = (size + 3) / 4; |
| 53 | + volatile uint32_t *dst_addr = (volatile uint32_t *)flash_ptr; |
| 54 | + const uint8_t *src_addr = (uint8_t *)data; |
| 55 | + |
| 56 | + // Disable automatic page write |
| 57 | + NVMCTRL->CTRLB.bit.MANW = 1; |
| 58 | + |
| 59 | + // Do writes in pages |
| 60 | + while (size) { |
| 61 | + // Execute "PBC" Page Buffer Clear |
| 62 | + NVMCTRL->CTRLA.reg = NVMCTRL_CTRLA_CMDEX_KEY | NVMCTRL_CTRLA_CMD_PBC; |
| 63 | + while (NVMCTRL->INTFLAG.bit.READY == 0) { } |
| 64 | + |
| 65 | + // Fill page buffer |
| 66 | + uint32_t i; |
| 67 | + for (i=0; i<(PAGE_SIZE/4) && size; i++) { |
| 68 | + *dst_addr = read_unaligned_uint32(src_addr); |
| 69 | + src_addr += 4; |
| 70 | + dst_addr++; |
| 71 | + size--; |
| 72 | + } |
| 73 | + |
| 74 | + // Execute "WP" Write Page |
| 75 | + NVMCTRL->CTRLA.reg = NVMCTRL_CTRLA_CMDEX_KEY | NVMCTRL_CTRLA_CMD_WP; |
| 76 | + while (NVMCTRL->INTFLAG.bit.READY == 0) { } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +void FlashClass::erase(const volatile void *flash_ptr, uint32_t size) |
| 81 | +{ |
| 82 | + const uint8_t *ptr = (const uint8_t *)flash_ptr; |
| 83 | + while (size > ROW_SIZE) { |
| 84 | + erase(ptr); |
| 85 | + ptr += ROW_SIZE; |
| 86 | + size -= ROW_SIZE; |
| 87 | + } |
| 88 | + erase(ptr); |
| 89 | +} |
| 90 | + |
| 91 | +void FlashClass::erase(const volatile void *flash_ptr) |
| 92 | +{ |
| 93 | + NVMCTRL->ADDR.reg = ((uint32_t)flash_ptr) / 2; |
| 94 | + NVMCTRL->CTRLA.reg = NVMCTRL_CTRLA_CMDEX_KEY | NVMCTRL_CTRLA_CMD_ER; |
| 95 | + while (!NVMCTRL->INTFLAG.bit.READY) { } |
| 96 | +} |
| 97 | + |
| 98 | +void FlashClass::read(const volatile void *flash_ptr, void *data, uint32_t size) |
| 99 | +{ |
| 100 | + memcpy(data, (const void *)flash_ptr, size); |
| 101 | +} |
| 102 | + |
| 103 | +#endif // defined (ARDUINO_SAMD_ZERO) |
0 commit comments