FastArduino  v1.7
C++ library to build fast but small Arduino/AVR projects
flash.h
Go to the documentation of this file.
1 // Copyright 2016-2021 Jean-Francois Poilpret
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
16 
21 #ifndef FLASH_HH
22 #define FLASH_HH
23 
24 #include "boards/io.h"
25 #include <avr/pgmspace.h>
26 
30 namespace flash
31 {
33  // Utility to handle PROGMEM storage for strings
34  class FlashStorage;
36 
47  template<typename T> T& read_flash(uint16_t address, T& item)
48  {
49  uint8_t* ptr = (uint8_t*) &item;
50  for (size_t i = 0; i < sizeof(T); ++i) *ptr++ = pgm_read_byte(address++);
51  return item;
52  }
53 
80  template<typename T> T& read_flash(const T* address, T& item)
81  {
82  return read_flash((uint16_t) address, item);
83  }
84 }
85 
98 #define F(ptr) \
99  (__extension__({ \
100  static const char __c[] PROGMEM = (ptr); \
101  (const flash::FlashStorage*) &__c[0]; \
102  }))
103 
104 #endif /* FLASH_HH */
105 
flash::read_flash
T & read_flash(uint16_t address, T &item)
Read flash memory content at given address into item.
Definition: flash.h:47
flash
Defines API to handle flash memory storage.
Definition: flash.h:31