ChibiOS/HAL 9.0.0
hal_flash.c
Go to the documentation of this file.
1/*
2 ChibiOS - Copyright (C) 2006..2021 Giovanni Di Sirio
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17/**
18 * @file hal_flash.c
19 * @brief Generic flash driver class code.
20 *
21 * @addtogroup HAL_FLASH
22 * @{
23 */
24
25#include "hal.h"
26
27#include "hal_flash.h"
28
29/*===========================================================================*/
30/* Driver local definitions. */
31/*===========================================================================*/
32
33/*===========================================================================*/
34/* Driver exported variables. */
35/*===========================================================================*/
36
37/*===========================================================================*/
38/* Driver local variables and types. */
39/*===========================================================================*/
40
41/*===========================================================================*/
42/* Driver local functions. */
43/*===========================================================================*/
44
45/*===========================================================================*/
46/* Driver exported functions. */
47/*===========================================================================*/
48
49/**
50 * @brief Waits until the current erase operation is finished.
51 *
52 * @param[in] devp pointer to a @p BaseFlash object
53 *
54 * @return An error code.
55 * @retval FLASH_NO_ERROR if there is no erase operation in progress.
56 * @retval FLASH_ERROR_ERASE if the erase operation failed.
57 * @retval FLASH_ERROR_HW_FAILURE if access to the memory failed.
58 *
59 * @api
60 */
62
63 while (true) {
64 flash_error_t err;
65 uint32_t msec;
66
67 /* Checking operation state.*/
68 err = flashQueryErase(devp, &msec);
69 if (err != FLASH_BUSY_ERASING) {
70 return err;
71 }
72
73 /* Interval because nice waiting.*/
75 }
76}
77
78/**
79 * @brief Returns the offset of a sector.
80 *
81 * @param[in] devp pointer to a @p BaseFlash object
82 * @param[in] sector flash sector number
83 * @return Sector offset.
84 *
85 * @api
86 */
88 flash_sector_t sector) {
89 flash_offset_t offset;
90 const flash_descriptor_t *descriptor = flashGetDescriptor(devp);
91
92 osalDbgAssert(sector < descriptor->sectors_count, "invalid sector");
93
94 if (descriptor->sectors != NULL) {
95 offset = descriptor->sectors[sector].offset;
96 }
97 else {
98 offset = (flash_offset_t)sector * (flash_offset_t)descriptor->sectors_size;
99 }
100
101 return offset;
102}
103
104/**
105 * @brief Returns the size of a sector.
106 *
107 * @param[in] devp pointer to a @p BaseFlash object
108 * @param[in] sector flash sector number
109 * @return Sector size.
110 *
111 * @api
112 */
114 flash_sector_t sector) {
115 uint32_t size;
116 const flash_descriptor_t *descriptor = flashGetDescriptor(devp);
117
118 osalDbgAssert(sector < descriptor->sectors_count, "invalid sector");
119
120 if (descriptor->sectors != NULL) {
121 size = descriptor->sectors[sector].size;
122 }
123 else {
124 size = descriptor->sectors_size;
125 }
126
127 return size;
128}
129
130/**
131 * @brief Returns the sector of an offset.
132 *
133 * @param[in] devp pointer to a @p BaseFlash object
134 * @param[in] offset flash offset
135 * @return Flash sector.
136 *
137 * @api
138 */
140 flash_sector_t sector, i;
141 const flash_descriptor_t *descriptor = flashGetDescriptor(devp);
142
143 osalDbgAssert(offset < descriptor->size, "invalid offset");
144
145 if (descriptor->sectors != NULL) {
146 flash_offset_t sector_start;
147 flash_offset_t sector_end;
148 for (i = 0; i < descriptor->sectors_count; i++) {
149 sector_start = descriptor->sectors[i].offset;
150 sector_end = sector_start + descriptor->sectors[i].size - 1U;
151 if ((offset >= sector_start) && (offset <= sector_end)) {
152 sector = i;
153 return sector;
154 }
155 }
156 }
157 else {
158 sector = offset / descriptor->sectors_size;
159 return sector;
160 }
161
162 osalDbgAssert(FALSE, "invalid offset");
163
164 return 0;
165}
166
167/**
168 * @brief Get absolute address from offset
169 *
170 * @param[in] devp pointer to a @p BaseFlash object
171 * @param[in] offset flash offset
172 * @return A pointer to the offset.
173 *
174 * @deprecated
175 */
177 const flash_descriptor_t *descriptor = flashGetDescriptor(devp);
178
179 osalDbgAssert(offset < descriptor->size, "invalid offset");
180
181 return (void *)(descriptor->address + offset);
182}
183
184/**
185 * @brief Get offset from absolute address
186 *
187 * @param[in] devp pointer to a @p BaseFlash object
188 * @param[in] addr pointer
189 * @return flash offset
190 *
191 * @deprecated
192 */
194 const flash_descriptor_t *descriptor = flashGetDescriptor(devp);
195
196 osalDbgAssert(((flash_offset_t)addr >= (flash_offset_t)descriptor->address) &&
197 ((flash_offset_t)addr <= ((flash_offset_t)descriptor->address +
198 descriptor->size)),
199 "invalid address");
200
201 return (flash_offset_t)addr - (flash_offset_t)descriptor->address;
202}
203
204/** @} */
flash_offset_t flashGetSectorOffset(BaseFlash *devp, flash_sector_t sector)
Returns the offset of a sector.
Definition hal_flash.c:87
uint32_t flashGetSectorSize(BaseFlash *devp, flash_sector_t sector)
Returns the size of a sector.
Definition hal_flash.c:113
flash_sector_t flashGetOffsetSector(BaseFlash *devp, flash_offset_t offset)
Returns the sector of an offset.
Definition hal_flash.c:139
uint32_t flash_sector_t
Type of a flash sector number.
Definition hal_flash.h:117
flash_error_t flashWaitErase(BaseFlash *devp)
Waits until the current erase operation is finished.
Definition hal_flash.c:61
uint32_t flash_offset_t
Type of a flash offset.
Definition hal_flash.h:112
void * flashGetOffsetAddress(BaseFlash *devp, flash_offset_t offset)
Get absolute address from offset.
Definition hal_flash.c:176
flash_error_t
Type of a flash error code.
Definition hal_flash.h:98
flash_offset_t flashGetAddressOffset(BaseFlash *devp, void *addr)
Get offset from absolute address.
Definition hal_flash.c:193
#define flashQueryErase(ip, msec)
Queries the driver for erase operation progress.
Definition hal_flash.h:336
#define flashGetDescriptor(ip)
Gets the flash descriptor structure.
Definition hal_flash.h:253
@ FLASH_BUSY_ERASING
Definition hal_flash.h:100
#define FALSE
Definition osal.h:41
#define osalDbgAssert(c, remark)
Condition assertion.
Definition osal.h:264
#define osalThreadSleepMilliseconds(msecs)
Delays the invoking thread for the specified number of milliseconds.
Definition osal.h:522
HAL subsystem header.
Generic flash driver class header.
Base flash class.
Definition hal_flash.h:224
Type of a flash device descriptor.
Definition hal_flash.h:136
const flash_sector_descriptor_t * sectors
List of sectors for devices with non-uniform sector sizes.
Definition hal_flash.h:154
uint32_t sectors_size
Size of sectors for devices with uniform sector size.
Definition hal_flash.h:160
uint32_t size
Flash size.
Definition hal_flash.h:169
flash_sector_t sectors_count
Number of sectors in the device.
Definition hal_flash.h:148
uint8_t * address
Flash address if memory mapped or zero.
Definition hal_flash.h:165
flash_offset_t offset
Sector offset.
Definition hal_flash.h:126
uint32_t size
Sector size.
Definition hal_flash.h:130