1 // Copyright 2022 The Pigweed Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 4 // use this file except in compliance with the License. You may obtain a copy of 5 // the License at 6 // 7 // https://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, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations under 13 // the License. 14 #pragma once 15 16 #include <cstddef> 17 #include <cstdint> 18 19 #include "pw_kvs/flash_memory.h" 20 21 namespace pw::kvs { 22 23 // FlashPartition that supports combining multiple FlashMemory sectors in to a 24 // single logical FlashPartition sector. The number of FlashMemory sectors per 25 // logical sector is specified by flash_sectors_per_logical_sector. 26 // 27 // If the number of FlashMemory sectors is not a multiple of 28 // flash_sectors_per_logical_sector, then the FlashMemory sectors used in the 29 // partition is rounded down to the nearest multiple. 30 class FlashPartitionWithLogicalSectors : public FlashPartition { 31 public: 32 FlashPartitionWithLogicalSectors( 33 FlashMemory* flash, 34 size_t flash_sectors_per_logical_sector, 35 uint32_t flash_start_sector_index, 36 uint32_t flash_sector_count, 37 uint32_t alignment_bytes = 0, // Defaults to flash alignment 38 PartitionPermission permission = PartitionPermission::kReadAndWrite) FlashPartition(flash,flash_start_sector_index,flash_sector_count,alignment_bytes,permission)39 : FlashPartition(flash, 40 flash_start_sector_index, 41 flash_sector_count, 42 alignment_bytes, 43 permission), 44 flash_sectors_per_logical_sector_(flash_sectors_per_logical_sector) {} 45 FlashPartitionWithLogicalSectors(FlashMemory * flash,size_t flash_sectors_per_logical_sector)46 FlashPartitionWithLogicalSectors(FlashMemory* flash, 47 size_t flash_sectors_per_logical_sector) 48 : FlashPartitionWithLogicalSectors(flash, 49 flash_sectors_per_logical_sector, 50 0, 51 flash->sector_count(), 52 flash->alignment_bytes()) {} 53 sector_size_bytes()54 size_t sector_size_bytes() const override { 55 return flash_.sector_size_bytes() * flash_sectors_per_logical_sector_; 56 } 57 sector_count()58 size_t sector_count() const override { 59 return flash_sector_count_ / flash_sectors_per_logical_sector_; 60 } 61 Erase(Address address,size_t num_sectors)62 Status Erase(Address address, size_t num_sectors) override { 63 return flash_.Erase(address, 64 num_sectors * flash_sectors_per_logical_sector_); 65 } 66 67 private: 68 size_t flash_sectors_per_logical_sector_; 69 }; 70 71 } // namespace pw::kvs 72