MySensors Library & Examples  2.3.2
Flash.h
Go to the documentation of this file.
1 /*
2  * Flash.h - Flash library
3  * Original Copyright (c) 2017 Frank Holtz. All right reserved.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  */
28 #pragma once
29 
30 #include <Arduino.h>
31 #include <stdio.h> // for size_t
32 
33 /*
34  * Define characteristics of Flash
35  *
36  * @def FLASH_ERASE_CYCLES
37  * @brief Specified number of erase cycles
38  *
39  * @def FLASH_PAGE_SIZE
40  * @brief Used/supported Flash page size
41  *
42  * @def FLASH_ERASE_PAGE_TIME
43  * @brief Time in ms to delete a page
44  *
45  * @def FLASH_WRITES_PER_WORD
46  * @brief How often a dataword (32 bit) can be written
47  *
48  * @def FLASH_WRITES_PER_PAGE
49  * @brief How many writes are allowed into a page
50  *
51  * @def FLASH_SUPPORTS_RANDOM_WRITE
52  * @brief Set this if it is allowed to write to a page in random order.
53  */
54 
55 #if defined(NRF51)
56 #define FLASH_ERASE_CYCLES 20000
57 #define FLASH_PAGE_SIZE 1024
58 #define FLASH_ERASE_PAGE_TIME 23
59 #define FLASH_SUPPORTS_RANDOM_WRITE true
60 #define FLASH_WRITES_PER_WORD 2
61 #define FLASH_WRITES_PER_PAGE 512
62 #elif defined(NRF52)
63 #define FLASH_ERASE_CYCLES 10000
64 #define FLASH_PAGE_SIZE 4096
65 #define FLASH_ERASE_PAGE_TIME 90
66 #define FLASH_SUPPORTS_RANDOM_WRITE true
67 #define FLASH_WRITES_PER_WORD 32
68 #define FLASH_WRITES_PER_PAGE 181
69 #elif defined(NRF52840)
70 #define FLASH_ERASE_CYCLES 10000
71 #define FLASH_PAGE_SIZE 4096
72 #define FLASH_ERASE_PAGE_TIME 90
73 #define FLASH_SUPPORTS_RANDOM_WRITE true
74 #define FLASH_WRITES_PER_WORD 2
75 #define FLASH_WRITES_PER_PAGE 403
76 #else
77 #define FLASH_ERASE_CYCLES 10000
78 #define FLASH_PAGE_SIZE 4096
79 #define FLASH_ERASE_PAGE_TIME 100
80 //#define FLASH_SUPPORTS_RANDOM_WRITE true
81 #define FLASH_WRITES_PER_WORD 1
82 #warning "Unknown platform. Please check the code."
83 #endif
84 
90 {
91 public:
92  //----------------------------------------------------------------------------
94  FlashClass() {};
95  //----------------------------------------------------------------------------
97  void begin() {};
99  void end() {};
100  //----------------------------------------------------------------------------
101  /*
102  * Physical flash geometry
103  */
104  //----------------------------------------------------------------------------
108  uint32_t page_size() const;
109  //----------------------------------------------------------------------------
113  uint8_t page_size_bits() const;
114  //----------------------------------------------------------------------------
118  uint32_t page_count() const;
119  //----------------------------------------------------------------------------
123  uint32_t specified_erase_cycles() const;
124  //----------------------------------------------------------------------------
129  uint32_t *page_address(size_t page);
133  uint32_t *top_app_page_address();
134  //----------------------------------------------------------------------------
135  /*
136  * Accessing flash memory
137  */
138  //----------------------------------------------------------------------------
144  void erase(uint32_t *address, size_t size);
145  //----------------------------------------------------------------------------
148  void erase_all();
149  //----------------------------------------------------------------------------
154  void write(uint32_t *address, uint32_t value);
155  //----------------------------------------------------------------------------
161  void write_block(uint32_t *dst_address, uint32_t *src_address,
162  uint16_t word_count);
163 
164 private:
165  // Wait until flash is ready
166  void wait_for_ready();
167 };
168 
169 extern FlashClass Flash;
170 
172 #ifdef NRF5
173 #include "hal/architecture/NRF5/drivers/Flash.cpp"
174 #else
175 #error "Unsupported platform."
176 #endif
177 
FlashClass
This class provides low-level access to internal Flash memory.
Definition: Flash.h:89
FlashClass::FlashClass
FlashClass()
Definition: Flash.h:94
Flash
FlashClass Flash
extern FlashClass
FlashClass::write
void write(uint32_t *address, uint32_t value)
FlashClass::page_address
uint32_t * page_address(size_t page)
FlashClass::page_size
uint32_t page_size() const
FlashClass::end
void end()
Definition: Flash.h:99
FlashClass::begin
void begin()
Definition: Flash.h:97
FlashClass::specified_erase_cycles
uint32_t specified_erase_cycles() const
FlashClass::page_size_bits
uint8_t page_size_bits() const
FlashClass::write_block
void write_block(uint32_t *dst_address, uint32_t *src_address, uint16_t word_count)
FlashClass::erase_all
void erase_all()
FlashClass::page_count
uint32_t page_count() const
FlashClass::top_app_page_address
uint32_t * top_app_page_address()
FlashClass::erase
void erase(uint32_t *address, size_t size)