ChibiOS/HAL  6.1.0
hal_flash.c
Go to the documentation of this file.
1 /*
2  ChibiOS - Copyright (C) 2006..2018 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  */
60 
61  while (true) {
62  flash_error_t err;
63  uint32_t msec;
64 
65  /* Checking operation state.*/
66  err = flashQueryErase(devp, &msec);
67  if (err != FLASH_BUSY_ERASING) {
68  return err;
69  }
70 
71  /* Interval because nice waiting.*/
73  }
74 }
75 
76 /**
77  * @brief Returns the offset of a sector.
78  *
79  * @param[in] devp pointer to a @p BaseFlash object
80  * @param[in] sector flash sector number
81  *
82  * @return the offset of the sector
83  */
85  flash_sector_t sector) {
86  flash_offset_t offset;
87  const flash_descriptor_t *descriptor = flashGetDescriptor(devp);
88 
89  osalDbgAssert(sector < descriptor->sectors_count, "invalid sector");
90 
91  if (descriptor->sectors != NULL) {
92  offset = descriptor->sectors[sector].offset;
93  }
94  else {
95  offset = (flash_offset_t)sector * (flash_offset_t)descriptor->sectors_size;
96  }
97 
98  return offset;
99 }
100 
101 /**
102  * @brief Returns the size of a sector.
103  *
104  * @param[in] devp pointer to a @p BaseFlash object
105  * @param[in] sector flash sector number
106  *
107  * @return the size of the sector
108  */
110  flash_sector_t sector) {
111  uint32_t size;
112  const flash_descriptor_t *descriptor = flashGetDescriptor(devp);
113 
114  osalDbgAssert(sector < descriptor->sectors_count, "invalid sector");
115 
116  if (descriptor->sectors != NULL) {
117  size = descriptor->sectors[sector].size;
118  }
119  else {
120  size = descriptor->sectors_size;
121  }
122 
123  return size;
124 }
125 /** @} */
uint32_t flash_sector_t
Type of a flash sector number.
Definition: hal_flash.h:88
uint32_t size
Sector size.
Definition: hal_flash.h:101
flash_offset_t offset
Sector offset.
Definition: hal_flash.h:97
HAL subsystem header.
Base flash class.
Definition: hal_flash.h:187
uint32_t sectors_size
Size of sectors for devices with uniform sector size.
Definition: hal_flash.h:131
#define osalThreadSleepMilliseconds(msecs)
Delays the invoking thread for the specified number of milliseconds.
Definition: osal.h:451
Type of a flash device descriptor.
Definition: hal_flash.h:107
uint32_t flashGetSectorSize(BaseFlash *devp, flash_sector_t sector)
Returns the size of a sector.
Definition: hal_flash.c:109
flash_error_t
Type of a flash error code.
Definition: hal_flash.h:70
#define flashGetDescriptor(ip)
Gets the flash descriptor structure.
Definition: hal_flash.h:216
flash_error_t flashWaitErase(BaseFlash *devp)
Waits until the current erase operation is finished.
Definition: hal_flash.c:59
uint32_t flash_offset_t
Type of a flash offset.
Definition: hal_flash.h:83
Generic flash driver class header.
#define flashQueryErase(ip, msec)
Queries the driver for erase operation progress.
Definition: hal_flash.h:298
#define osalDbgAssert(c, remark)
Condition assertion.
Definition: osal.h:258
flash_offset_t flashGetSectorOffset(BaseFlash *devp, flash_sector_t sector)
Returns the offset of a sector.
Definition: hal_flash.c:84
const flash_sector_descriptor_t * sectors
List of sectors for devices with non-uniform sector sizes.
Definition: hal_flash.h:125