ChibiOS 21.11.5
hal_efl.c
Go to the documentation of this file.
1/*
2 ChibiOS - Copyright (C) 2006-2026 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_efl.c
19 * @brief Embedded Flash Driver code.
20 *
21 * @addtogroup HAL_EFL
22 * @{
23 */
24
25#include "hal.h"
26
27#if (HAL_USE_EFL == TRUE) || defined(__DOXYGEN__)
28
29/*===========================================================================*/
30/* Driver local definitions. */
31/*===========================================================================*/
32
33/*===========================================================================*/
34/* Driver exported variables. */
35/*===========================================================================*/
36
37/*===========================================================================*/
38/* Driver local functions. */
39/*===========================================================================*/
40
41static flash_error_t efl_acquire_exclusive(void *instance) {
42#if (EFL_USE_MUTUAL_EXCLUSION == TRUE)
43 EFlashDriver *devp = (EFlashDriver *)instance;
44
45 osalMutexLock(&devp->mutex);
46 return FLASH_NO_ERROR;
47#else
48 (void)instance;
49 osalDbgAssert(false, "mutual exclusion not enabled");
51#endif
52}
53
54static flash_error_t efl_release_exclusive(void *instance) {
55#if (EFL_USE_MUTUAL_EXCLUSION == TRUE)
56 EFlashDriver *devp = (EFlashDriver *)instance;
57
58 osalMutexUnlock(&devp->mutex);
59 return FLASH_NO_ERROR;
60#else
61 (void)instance;
62 osalDbgAssert(false, "mutual exclusion not enabled");
64#endif
65}
66
67/*===========================================================================*/
68/* Driver local variables and types. */
69/*===========================================================================*/
70
83
84/*===========================================================================*/
85/* Driver exported functions. */
86/*===========================================================================*/
87
88/**
89 * @brief Embedded Flash Driver initialization.
90 * @note This function is implicitly invoked by @p halInit(), there is
91 * no need to explicitly initialize the driver.
92 *
93 * @init
94 */
95void eflInit(void) {
96
98}
99
100/**
101 * @brief Initializes a generic @p EFlashDriver object.
102 *
103 * @param[out] eflp pointer to a @p EFlashDriver structure
104 *
105 * @init
106 */
108
109 eflp->vmt = &vmt;
110 eflp->state = FLASH_STOP;
111 eflp->config = NULL;
112#if EFL_USE_MUTUAL_EXCLUSION == TRUE
114#endif
115}
116
117/**
118 * @brief Configures and starts the driver.
119 *
120 * @param[in] eflp pointer to a @p EFlashDriver structure
121 * @param[in] config pointer to a configuration structure.
122 * If this parameter is set to @p NULL then a default
123 * configuration is used.
124 * @return The operation status.
125 *
126 * @api
127 */
129 msg_t msg;
130
131 osalDbgCheck(eflp != NULL);
132
133 osalSysLock();
134
135 osalDbgAssert((eflp->state == FLASH_STOP) || (eflp->state == FLASH_READY),
136 "invalid state");
137
138 eflp->config = config;
139
140#if defined(EFL_LLD_ENHANCED_API)
141 msg = efl_lld_start(eflp);
142 if (msg == HAL_RET_SUCCESS) {
143 eflp->state = FLASH_READY;
144 }
145 else {
146 eflp->state = FLASH_STOP;
147 }
148#else
149 efl_lld_start(eflp);
150 eflp->state = FLASH_READY;
151 msg = HAL_RET_SUCCESS;
152#endif
153
155
156 return msg;
157}
158
159/**
160 * @brief Stops the driver.
161 *
162 * @param[in] eflp pointer to a @p EFlashDriver structure
163 *
164 * @api
165 */
167
168 osalDbgCheck(eflp != NULL);
169
170 osalSysLock();
171
172 osalDbgAssert((eflp->state == FLASH_STOP) || (eflp->state == FLASH_READY),
173 "invalid state");
174
175 efl_lld_stop(eflp);
176 eflp->config = NULL;
177 eflp->state = FLASH_STOP;
178
180}
181
182#endif /* HAL_USE_EFL == TRUE */
183
184/** @} */
static flash_error_t efl_release_exclusive(void *instance)
Definition hal_efl.c:54
flash_error_t efl_lld_start_erase_sector(void *instance, flash_sector_t sector)
Starts an sector erase operation.
struct hal_efl_config EFlashConfig
Type of a structure representing a flash driver configuration.
flash_error_t efl_lld_program(void *instance, flash_offset_t offset, size_t n, const uint8_t *pp)
Program operation.
void efl_lld_init(void)
Low level Embedded Flash driver initialization.
Definition hal_efl_lld.c:78
static const struct EFlashDriverVMT vmt
Definition hal_efl.c:71
flash_error_t efl_lld_start_erase_all(void *instance)
Starts a whole-device erase operation.
static flash_error_t efl_acquire_exclusive(void *instance)
Definition hal_efl.c:41
void efl_lld_start(EFlashDriver *eflp)
Configures and activates the Embedded Flash peripheral.
Definition hal_efl_lld.c:93
struct hal_efl_driver EFlashDriver
Type of embedded flash driver class.
Definition hal_efl.h:60
flash_error_t efl_lld_verify_erase(void *instance, flash_sector_t sector)
Returns the erase state of a sector.
void eflInit(void)
Embedded Flash Driver initialization.
Definition hal_efl.c:95
const flash_descriptor_t * efl_lld_get_descriptor(void *instance)
Gets the flash descriptor structure.
msg_t eflStart(EFlashDriver *eflp, const EFlashConfig *config)
Configures and starts the driver.
Definition hal_efl.c:128
void efl_lld_stop(EFlashDriver *eflp)
Deactivates the Embedded Flash peripheral.
void eflObjectInit(EFlashDriver *eflp)
Initializes a generic EFlashDriver object.
Definition hal_efl.c:107
void eflStop(EFlashDriver *eflp)
Stops the driver.
Definition hal_efl.c:166
flash_error_t efl_lld_query_erase(void *instance, uint32_t *msec)
Queries the driver for erase operation progress.
flash_error_t efl_lld_read(void *instance, flash_offset_t offset, size_t n, uint8_t *rp)
Read operation.
flash_error_t
Type of a flash error code.
Definition hal_flash.h:98
@ FLASH_READY
Definition hal_flash.h:89
@ FLASH_STOP
Definition hal_flash.h:88
@ FLASH_ERROR_UNIMPLEMENTED
Definition hal_flash.h:106
@ FLASH_NO_ERROR
Definition hal_flash.h:99
#define HAL_RET_SUCCESS
Definition hal.h:93
static void osalSysLock(void)
Enters a critical zone from thread context.
Definition osal.h:601
static void osalMutexObjectInit(mutex_t *mp)
Initializes a mutex_t object.
Definition osal.h:753
static void osalSysUnlock(void)
Leaves a critical zone from thread context.
Definition osal.h:611
void osalMutexLock(mutex_t *mp)
Locks the specified mutex.
Definition osal.c:380
#define osalDbgAssert(c, remark)
Condition assertion.
Definition osal.h:264
void osalMutexUnlock(mutex_t *mp)
Unlocks the specified mutex.
Definition osal.c:400
#define osalDbgCheck(c)
Function parameters check.
Definition osal.h:284
int32_t msg_t
Definition chearly.h:87
HAL subsystem header.
EFlash virtual methods table.
Definition hal_efl.h:89
const struct EFlashDriverVMT * vmt
EFlashDriver Virtual Methods Table.
Definition hal_efl.h:110
_efl_driver_data mutex_t mutex
Mutex protecting EFL.
Definition hal_efl.h:116