ChibiOS/HAL 9.0.0
hal_efl.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_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#if EFL_USE_MUTUAL_EXCLUSION == TRUE
113#endif
114}
115
116/**
117 * @brief Configures and starts the driver.
118 *
119 * @param[in] eflp pointer to a @p EFlashDriver structure
120 * @param[in] config pointer to a configuration structure.
121 * If this parameter is set to @p NULL then a default
122 * configuration is used.
123 * @return The operation status.
124 *
125 * @api
126 */
128 msg_t msg;
129
130 osalDbgCheck(eflp != NULL);
131
132 osalSysLock();
133
134 osalDbgAssert((eflp->state == FLASH_STOP) || (eflp->state == FLASH_READY),
135 "invalid state");
136
137 eflp->config = config;
138
139#if defined(EFL_LLD_ENHANCED_API)
140 msg = efl_lld_start(eflp);
141 if (msg == HAL_RET_SUCCESS) {
142 eflp->state = FLASH_READY;
143 }
144 else {
145 eflp->state = FLASH_STOP;
146 }
147#else
148 efl_lld_start(eflp);
149 eflp->state = FLASH_READY;
150 msg = HAL_RET_SUCCESS;
151#endif
152
154
155 return msg;
156}
157
158/**
159 * @brief Stops the driver.
160 *
161 * @param[in] eflp pointer to a @p EFlashDriver structure
162 *
163 * @api
164 */
166
167 osalDbgCheck(eflp != NULL);
168
169 osalSysLock();
170
171 osalDbgAssert((eflp->state == FLASH_STOP) || (eflp->state == FLASH_READY),
172 "invalid state");
173
174 efl_lld_stop(eflp);
175 eflp->state = FLASH_STOP;
176
178}
179
180#endif /* HAL_USE_EFL == TRUE */
181
182/** @} */
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 external flash driver class.
Definition hal_efl.h:59
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:127
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:165
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
int32_t msg_t
Type of a message.
Definition osal.h:159
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
HAL subsystem header.
EFlash virtual methods table.
Definition hal_efl.h:88
const struct EFlashDriverVMT * vmt
EFlashDriver Virtual Methods Table.
Definition hal_efl.h:109
_efl_driver_data mutex_t mutex
Mutex protecting EFL.
Definition hal_efl.h:115