FlashStorage.cpp 3.61 KB
Newer Older
Jake Read's avatar
Jake Read committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
  Copyright (c) 2015 Arduino LLC.  All right reserved.
  Written by Cristian Maglie

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  See the GNU Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "FlashStorage.h"

static const uint32_t pageSizes[] = { 8, 16, 32, 64, 128, 256, 512, 1024 };

FlashClass::FlashClass(const void *flash_addr, uint32_t size) :
  PAGE_SIZE(pageSizes[NVMCTRL->PARAM.bit.PSZ]),
  PAGES(NVMCTRL->PARAM.bit.NVMP),
  MAX_FLASH(PAGE_SIZE * PAGES),
  ROW_SIZE(MAX_FLASH / 64),
  flash_address((volatile void *)flash_addr),
  flash_size(size)
{
}

34
static inline uint32_t read_unaligned_uint32(const void *data){
Jake Read's avatar
Jake Read committed
35
36
37
38
39
40
41
42
43
44
45
46
47
  union {
    uint32_t u32;
    uint8_t u8[4];
  } res;
  const uint8_t *d = (const uint8_t *)data;
  res.u8[0] = d[0];
  res.u8[1] = d[1];
  res.u8[2] = d[2];
  res.u8[3] = d[3];
  return res.u32;
}

// Invalidate all CMCC cache entries if CMCC cache is enabled.
48
static void invalidate_CMCC_cache(){
Jake Read's avatar
Jake Read committed
49
50
51
52
53
54
55
56
  if (CMCC->SR.bit.CSTS) { // CR -> SR 
    CMCC->CTRL.bit.CEN = 0;
    while (CMCC->SR.bit.CSTS) {}
    CMCC->MAINT0.bit.INVALL = 1;
    CMCC->CTRL.bit.CEN = 1;
  }
}

57
void FlashClass::write(const volatile void *flash_ptr, const void *data, uint32_t size){
Jake Read's avatar
Jake Read committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
  // Calculate data boundaries
  size = (size + 3) / 4;
  volatile uint32_t *dst_addr = (volatile uint32_t *)flash_ptr;
  const uint8_t *src_addr = (uint8_t *)data;

  // Disable automatic page write
  NVMCTRL->CTRLA.bit.WMODE = 0;
  while (NVMCTRL->STATUS.bit.READY == 0) { }
  // Disable NVMCTRL cache while writing, per SAMD51 errata.
  bool original_CACHEDIS0 = NVMCTRL->CTRLA.bit.CACHEDIS0;
  bool original_CACHEDIS1 = NVMCTRL->CTRLA.bit.CACHEDIS1;
  NVMCTRL->CTRLA.bit.CACHEDIS0 = true;
  NVMCTRL->CTRLA.bit.CACHEDIS1 = true;

  // Do writes in pages
  while (size) {
    // Execute "PBC" Page Buffer Clear
    NVMCTRL->CTRLB.reg = NVMCTRL_CTRLB_CMDEX_KEY | NVMCTRL_CTRLB_CMD_PBC;
    while (NVMCTRL->INTFLAG.bit.DONE == 0) { }

    // Fill page buffer
    uint32_t i;
    for (i=0; i<(PAGE_SIZE/4) && size; i++) {
      *dst_addr = read_unaligned_uint32(src_addr);
      src_addr += 4;
      dst_addr++;
      size--;
    }

    // Execute "WP" Write Page
    NVMCTRL->CTRLB.reg = NVMCTRL_CTRLB_CMDEX_KEY | NVMCTRL_CTRLB_CMD_WP;
    while (NVMCTRL->INTFLAG.bit.DONE == 0) { }
    invalidate_CMCC_cache();
    // Restore original NVMCTRL cache settings.
    NVMCTRL->CTRLA.bit.CACHEDIS0 = original_CACHEDIS0;
    NVMCTRL->CTRLA.bit.CACHEDIS1 = original_CACHEDIS1;
  }
}

97
void FlashClass::erase(const volatile void *flash_ptr, uint32_t size){
Jake Read's avatar
Jake Read committed
98
99
100
101
102
103
104
105
106
  const uint8_t *ptr = (const uint8_t *)flash_ptr;
  while (size > ROW_SIZE) {
    erase(ptr);
    ptr += ROW_SIZE;
    size -= ROW_SIZE;
  }
  erase(ptr);
}

107
void FlashClass::erase(const volatile void *flash_ptr){
Jake Read's avatar
Jake Read committed
108
109
110
111
112
113
  NVMCTRL->ADDR.reg = ((uint32_t)flash_ptr);
  NVMCTRL->CTRLB.reg = NVMCTRL_CTRLB_CMDEX_KEY | NVMCTRL_CTRLB_CMD_EB;
  while (!NVMCTRL->INTFLAG.bit.DONE) { }
  invalidate_CMCC_cache();
}

114
void FlashClass::read(const volatile void *flash_ptr, void *data, uint32_t size){
Jake Read's avatar
Jake Read committed
115
116
117
  memcpy(data, (const void *)flash_ptr, size);
}