FastArduino  v1.9
C++ library to build fast but small Arduino/AVR projects
flash.h
Go to the documentation of this file.
1 // Copyright 2016-2022 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 
49  template<typename T = uint8_t>
50  T* read_flash(uint16_t address, T* buffer, uint8_t size)
51  {
52  uint8_t* ptr = (uint8_t*) buffer;
53  for (size_t i = 0; i < (size * sizeof(T)); ++i) *ptr++ = pgm_read_byte(address++);
54  return buffer;
55  }
56 
67  template<typename T> T& read_flash(uint16_t address, T& item)
68  {
69  uint8_t* ptr = (uint8_t*) &item;
70  for (size_t i = 0; i < sizeof(T); ++i) *ptr++ = pgm_read_byte(address++);
71  return item;
72  }
73 
100  template<typename T> T& read_flash(const T* address, T& item)
101  {
102  return read_flash((uint16_t) address, item);
103  }
104 }
105 
118 #define F(ptr) \
119  (__extension__({ \
120  static const char __c[] PROGMEM = (ptr); \
121  (const flash::FlashStorage*) &__c[0]; \
122  }))
123 
124 #endif /* FLASH_HH */
Defines API to handle flash memory storage.
Definition: flash.h:31
T * read_flash(uint16_t address, T *buffer, uint8_t size)
Read flash memory content at given address into buffer.
Definition: flash.h:50