ChibiOS/HAL 9.0.0
hal_xsnor_base.h
Go to the documentation of this file.
1/*
2 ChibiOS - Copyright (C) 2006..2025 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_xsnor_base.h
19 * @brief Generated SNOR Base Driver header.
20 * @note This is a generated file, do not edit directly.
21 *
22 * @addtogroup HAL_XSNOR_BASE
23 * @brief SNOR abstract driver.
24 * @details Base class for SNOR flash devices.
25 * @{
26 */
27
28#ifndef HAL_XSNOR_BASE_H
29#define HAL_XSNOR_BASE_H
30
31#include "oop_base_object.h"
32#include "oop_base_interface.h"
33#include "hal_flash_interface.h"
34
35/*===========================================================================*/
36/* Module constants. */
37/*===========================================================================*/
38
39/**
40 * @name Bus type and width options
41 * @{
42 */
43#define XSNOR_BUS_MODE_SPI 0U
44#define XSNOR_BUS_MODE_WSPI_1LINE 1U
45#define XSNOR_BUS_MODE_WSPI_2LINES 2U
46#define XSNOR_BUS_MODE_WSPI_4LINES 3U
47#define XSNOR_BUS_MODE_WSPI_8LINES 4U
48/** @} */
49
50/**
51 * @brief Hint to use 4 bytes addresses in SPI protocol.
52 * @note TODO: To be moved into the flash interface module.
53 */
54#define FLASH_ATTR_SPI_4BYTES_ADDR_HINT 0x00008000U
55
56/*===========================================================================*/
57/* Module pre-compile time settings. */
58/*===========================================================================*/
59
60/**
61 * @name Configuration options
62 * @{
63 */
64/**
65 * @brief Non-cacheable operations buffer.
66 */
67#if !defined(XSNOR_BUFFER_SIZE) || defined(__DOXYGEN__)
68#define XSNOR_BUFFER_SIZE 32
69#endif
70
71/**
72 * @brief SPI support enable switch.
73 */
74#if !defined(XSNOR_USE_SPI) || defined(__DOXYGEN__)
75#define XSNOR_USE_SPI TRUE
76#endif
77
78/**
79 * @brief WSPI support enable switch.
80 */
81#if !defined(XSNOR_USE_WSPI) || defined(__DOXYGEN__)
82#define XSNOR_USE_WSPI TRUE
83#endif
84
85/**
86 * @brief Bus share support enable switch.
87 */
88#if !defined(XSNOR_SHARED_BUS) || defined(__DOXYGEN__)
89#define XSNOR_SHARED_BUS TRUE
90#endif
91/** @} */
92
93/*===========================================================================*/
94/* Derived constants and error checks. */
95/*===========================================================================*/
96
97/* Checks on XSNOR_BUFFER_SIZE configuration.*/
98#if XSNOR_BUFFER_SIZE < 32
99#error "XSNOR_BUFFER_SIZE minimum is 32"
100#endif
101
102#if (XSNOR_BUFFER_SIZE & (XSNOR_BUFFER_SIZE - 1)) != 0
103#error "XSNOR_BUFFER_SIZE must be a power of 2"
104#endif
105
106/* Checks on XSNOR_USE_SPI configuration.*/
107#if (XSNOR_USE_SPI != FALSE) && (XSNOR_USE_SPI != TRUE)
108#error "XSNOR_USE_SPI invalid value"
109#endif
110
111/* Checks on XSNOR_USE_WSPI configuration.*/
112#if (XSNOR_USE_WSPI != FALSE) && (XSNOR_USE_WSPI != TRUE)
113#error "XSNOR_USE_WSPI invalid value"
114#endif
115
116/* Checks on XSNOR_SHARED_BUS configuration.*/
117#if (XSNOR_SHARED_BUS != FALSE) && (XSNOR_SHARED_BUS != TRUE)
118#error "XSNOR_SHARED_BUS invalid value"
119#endif
120
121/* Other consistency checks.*/
122#if (XSNOR_USE_SPI == FALSE) && (XSNOR_USE_WSPI == FALSE)
123#error "XSNOR_USE_SPI or XSNOR_USE_WSPI must be enabled"
124#endif
125#if (XSNOR_USE_SPI == TRUE) && (HAL_USE_SPI == FALSE)
126#error "XSNOR_USE_SPI requires HAL_USE_SPI"
127#endif
128#if (XSNOR_USE_WSPI == TRUE) && (HAL_USE_WSPI == FALSE)
129#error "XSNOR_USE_WSPI requires HAL_USE_WSPI"
130#endif
131
132/**
133 * @brief This switch is @p TRUE if both SPI and WSPI are in use.
134 */
135#define XSNOR_USE_BOTH ((XSNOR_USE_SPI == TRUE) && (XSNOR_USE_WSPI == TRUE))
136
137/*===========================================================================*/
138/* Module macros. */
139/*===========================================================================*/
140
141#if (XSNOR_SHARED_BUS == FALSE) || defined (__DOXYGEN__)
142/**
143 * @name Bus mutex macros when sharing is disabled
144 * @{
145 */
146#define __xsnor_bus_acquire(self)
147#define __xsnor_bus_release(self)
148/** @} */
149#endif /* XSNOR_SHARED_BUS == FALSE */
150
151/*===========================================================================*/
152/* Module data structures and types. */
153/*===========================================================================*/
154
155/**
156 * @brief Type of a non-cacheable buffer.
157 */
159
160/**
161 * @brief Type of a SNOR configuration structure.
162 */
164
165/**
166 * @brief Type of a commands configuration structure.
167 */
169
170/**
171 * @brief SNOR driver configuration.
172 */
174 /**
175 * @brief Non-cacheable data buffer.
176 */
178#if (XSNOR_USE_WSPI == TRUE) || defined (__DOXYGEN__)
179 /**
180 * @brief Non-cacheable WSPI command buffer.
181 */
183#endif /* XSNOR_USE_WSPI == TRUE */
184#if (XSNOR_USE_SPI == TRUE) || defined (__DOXYGEN__)
185 /**
186 * @brief Non-cacheable SPI buffer.
187 */
188 uint8_t spibuf[8];
189#endif /* XSNOR_USE_SPI == TRUE */
190};
191
192/**
193 * @brief SNOR command configuration structure.
194 */
196 /**
197 * @brief Command only.
198 */
199 uint32_t cmd;
200 /**
201 * @brief Command and address.
202 */
203 uint32_t cmd_addr;
204 /**
205 * @brief Command and data.
206 */
207 uint32_t cmd_data;
208 /**
209 * @brief Command, address and data.
210 */
212};
213
214#if (XSNOR_USE_WSPI == TRUE) || defined (__DOXYGEN__)
215/**
216 * @brief WSPI-specific configuration fields.
217 */
219 /**
220 * @brief WSPI driver to be used for physical communication.
221 */
223 /**
224 * @brief WSPI driver configuration.
225 */
227};
228#endif /* XSNOR_USE_WSPI == TRUE */
229
230#if (XSNOR_USE_SPI == TRUE) || defined (__DOXYGEN__)
231/**
232 * @brief SPI-specific configuration fields.
233 */
235 /**
236 * @brief SPI driver to be used for physical communication.
237 */
239 /**
240 * @brief SPI driver configuration.
241 */
243};
244#endif /* XSNOR_USE_SPI == TRUE */
245
246/**
247 * @brief Union of possible bus configurations.
248 */
250#if (XSNOR_USE_WSPI == TRUE) || defined (__DOXYGEN__)
252#endif /* XSNOR_USE_WSPI == TRUE */
253#if (XSNOR_USE_SPI == TRUE) || defined (__DOXYGEN__)
255#endif /* XSNOR_USE_SPI == TRUE */
256};
257
258/**
259 * @brief SNOR driver configuration.
260 */
262 /**
263 * @brief Bus type selection switch.
264 */
266 /**
267 * @brief WSPI driver configuration.
268 */
270 /**
271 * @brief Pointer to the non-cacheable buffers.
272 */
274 /**
275 * @brief Device-dependent options, used by subclasses only.
276 */
278};
279
280/**
281 * @class hal_xsnor_base_c
282 * @extends base_object_c
283 * @implements flash_interface_i
284 *
285 * @brief Base class of all SNOR drivers.
286 *
287 * @name Class @p hal_xsnor_base_c structures
288 * @{
289 */
290
291/**
292 * @brief Type of a XSNOR base driver class.
293 */
295
296/**
297 * @brief Class @p hal_xsnor_base_c virtual methods table.
298 */
300 /* From base_object_c.*/
301 void (*dispose)(void *ip);
302 /* From hal_xsnor_base_c.*/
303 flash_error_t (*init)(void *ip);
304 flash_error_t (*read)(void *ip, flash_offset_t offset, size_t n, uint8_t *rp);
305 flash_error_t (*program)(void *ip, flash_offset_t offset, size_t n, const uint8_t *pp);
308 flash_error_t (*query_erase)(void *ip, unsigned *msec);
310 flash_error_t (*mmap_on)(void *ip, uint8_t **addrp);
311 void (*mmap_off)(void *ip);
312};
313
314/**
315 * @brief Structure representing a XSNOR base driver class.
316 */
318 /**
319 * @brief Virtual Methods Table.
320 */
321 const struct hal_xsnor_base_vmt *vmt;
322 /**
323 * @brief Implemented interface @p flash_interface_i.
324 */
326 /**
327 * @brief Driver state.
328 */
330 /**
331 * @brief Driver configuration.
332 */
334#if (XSNOR_USE_WSPI == TRUE) || defined (__DOXYGEN__)
335 /**
336 * @brief Current commands configuration.
337 * @note This field is meant to be initialized by subclasses on object
338 * creation.
339 */
341#endif /* XSNOR_USE_WSPI == TRUE */
342 /**
343 * @brief Flash access mutex.
344 */
346 /**
347 * @brief Flash descriptor.
348 * @note This field is meant to be initialized by subclasses on memory
349 * initialization.
350 */
352};
353/** @} */
354
355/*===========================================================================*/
356/* External declarations. */
357/*===========================================================================*/
358
359#ifdef __cplusplus
360extern "C" {
361#endif
362 /* Methods of hal_xsnor_base_c.*/
363 void *__xsnor_objinit_impl(void *ip, const void *vmt);
364 void __xsnor_dispose_impl(void *ip);
365#if (XSNOR_USE_SPI == TRUE) || defined (__DOXYGEN__)
366 void __xsnor_spi_cmd_addr(void *ip, uint32_t cmd, flash_offset_t offset);
367#endif /* XSNOR_USE_SPI == TRUE */
368#if (XSNOR_SHARED_BUS == TRUE) || defined (__DOXYGEN__)
369 void __xsnor_bus_acquire(void *ip);
370 void __xsnor_bus_release(void *ip);
371#endif /* XSNOR_SHARED_BUS == TRUE */
372 void __xsnor_bus_cmd(void *ip, uint32_t cmd);
373 void __xsnor_bus_cmd_send(void *ip, uint32_t cmd, size_t n, const uint8_t *p);
374 void __xsnor_bus_cmd_receive(void *ip, uint32_t cmd, size_t n, uint8_t *p);
375 void __xsnor_bus_cmd_addr(void *ip, uint32_t cmd, flash_offset_t offset);
376 void __xsnor_bus_cmd_addr_send(void *ip, uint32_t cmd, flash_offset_t offset,
377 size_t n, const uint8_t *p);
378 void __xsnor_bus_cmd_addr_receive(void *ip, uint32_t cmd,
379 flash_offset_t offset, size_t n,
380 uint8_t *p);
381 void __xsnor_bus_cmd_dummy_receive(void *ip, uint32_t cmd, uint32_t dummy,
382 size_t n, uint8_t *p);
383 void __xsnor_bus_cmd_addr_dummy_receive(void *ip, uint32_t cmd,
384 flash_offset_t offset,
385 uint32_t dummy, size_t n, uint8_t *p);
386 flash_error_t xsnorStart(void *ip, const xsnor_config_t *config);
387 void xsnorStop(void *ip);
388#if (WSPI_SUPPORTS_MEMMAP == TRUE) || defined (__DOXYGEN__)
389 flash_error_t xsnorMemoryMap(void *ip, uint8_t **addrp);
390 void xsnorMemoryUnmap(void *ip);
391#endif /* WSPI_SUPPORTS_MEMMAP == TRUE */
392 /* Regular functions.*/
393#ifdef __cplusplus
394}
395#endif
396
397/*===========================================================================*/
398/* Module inline functions. */
399/*===========================================================================*/
400
401/**
402 * @name Virtual methods of hal_xsnor_base_c
403 * @{
404 */
406static inline flash_error_t xsnor_device_init(void *ip) {
408
409 return self->vmt->init(ip);
410}
411
412/**
413 * @brief Read operation.
414 *
415 * @param[in,out] ip Pointer to a @p hal_xsnor_base_c instance.
416 * @param[in] offset Flash offset.
417 * @param[in] n Number of bytes to be read.
418 * @param[out] rp Pointer to the data buffer.
419 * @return An error code.
420 * @retval FLASH_NO_ERROR Operation successful.
421 * @retval FLASH_ERROR_READ If the read operation failed.
422 * @retval FLASH_ERROR_HW_FAILURE If access to the memory failed.
423 *
424 * @notapi
425 */
427static inline flash_error_t xsnor_device_read(void *ip, flash_offset_t offset,
428 size_t n, uint8_t *rp) {
430
431 return self->vmt->read(ip, offset, n, rp);
432}
433
434/**
435 * @brief Program operation.
436 *
437 * @param[in,out] ip Pointer to a @p hal_xsnor_base_c instance.
438 * @param[in] offset Flash offset.
439 * @param[in] n Number of bytes to be programmed.
440 * @param[in] pp Pointer to the data buffer.
441 * @return An error code.
442 * @retval FLASH_NO_ERROR Operation successful.
443 * @retval FLASH_ERROR_PROGRAM If the program operation failed.
444 * @retval FLASH_ERROR_HW_FAILURE If access to the memory failed.
445 *
446 * @notapi
447 */
449static inline flash_error_t xsnor_device_program(void *ip,
450 flash_offset_t offset,
451 size_t n, const uint8_t *pp) {
453
454 return self->vmt->program(ip, offset, n, pp);
455}
456
457/**
458 * @brief Starts a whole-device erase operation.
459 *
460 * @param[in,out] ip Pointer to a @p hal_xsnor_base_c instance.
461 * @return An error code.
462 * @retval FLASH_NO_ERROR Operation successful.
463 * @retval FLASH_ERROR_HW_FAILURE If access to the memory failed.
464 *
465 * @notapi
466 */
470
471 return self->vmt->start_erase_all(ip);
472}
473
474/**
475 * @brief Starts an sector erase operation.
476 *
477 * @param[in,out] ip Pointer to a @p hal_xsnor_base_c instance.
478 * @param[in] sector Sector to be erased.
479 * @return An error code.
480 * @retval FLASH_NO_ERROR Operation successful.
481 * @retval FLASH_ERROR_HW_FAILURE If access to the memory failed.
482 *
483 * @notapi
484 */
487 flash_sector_t sector) {
489
490 return self->vmt->start_erase_sector(ip, sector);
491}
492
493/**
494 * @brief Queries the driver for erase operation progress.
495 *
496 * @param[in,out] ip Pointer to a @p hal_xsnor_base_c instance.
497 * @param[out] msec Recommended time, in milliseconds, that should
498 * be spent before calling this function again,
499 * can be @p NULL
500 * @return An error code.
501 * @retval FLASH_NO_ERROR If there is no erase operation in progress.
502 * @retval FLASH_ERROR_HW_FAILURE If access to the memory failed.
503 *
504 * @notapi
505 */
507static inline flash_error_t xsnor_device_query_erase(void *ip, unsigned *msec) {
509
510 return self->vmt->query_erase(ip, msec);
511}
512
513/**
514 * @brief Returns the erase state of a sector.
515 *
516 * @param[in,out] ip Pointer to a @p hal_xsnor_base_c instance.
517 * @param[in] sector Sector to be verified.
518 * @return An error code.
519 * @retval FLASH_NO_ERROR Operation successful.
520 * @retval FLASH_ERROR_VERIFY If the verify operation failed.
521 * @retval FLASH_ERROR_HW_FAILURE If access to the memory failed.
522 *
523 * @notapi
524 */
527 flash_sector_t sector) {
529
530 return self->vmt->verify_erase(ip, sector);
531}
532
534static inline flash_error_t xsnor_device_mmap_on(void *ip, uint8_t **addrp) {
536
537 return self->vmt->mmap_on(ip, addrp);
538}
539
541static inline void xsnor_device_mmap_off(void *ip) {
543
544 self->vmt->mmap_off(ip);
545}
546/** @} */
547
548#endif /* HAL_XSNOR_BASE_H */
549
550/** @} */
Base class of all SNOR drivers.
#define CC_FORCE_INLINE
Enforces a function inline.
Definition ccportab.h:108
static const struct EFlashDriverVMT vmt
Definition hal_efl.c:71
uint32_t flash_sector_t
Type of a flash sector number.
Definition hal_flash.h:117
flash_state_t
Driver state machine possible states.
Definition hal_flash.h:86
uint32_t flash_offset_t
Type of a flash offset.
Definition hal_flash.h:112
flash_error_t
Type of a flash error code.
Definition hal_flash.h:98
struct xsnor_buffers xsnor_buffers_t
Type of a non-cacheable buffer.
void __xsnor_dispose_impl(void *ip)
Implementation of object finalization.
void __xsnor_bus_cmd_send(void *ip, uint32_t cmd, size_t n, const uint8_t *p)
Sends a command followed by a data transmit phase.
#define XSNOR_BUFFER_SIZE
Non-cacheable operations buffer.
void __xsnor_spi_cmd_addr(void *ip, uint32_t cmd, flash_offset_t offset)
Sends command and address over SPI.
#define __xsnor_bus_release(self)
static CC_FORCE_INLINE void xsnor_device_mmap_off(void *ip)
void * __xsnor_objinit_impl(void *ip, const void *vmt)
Implementation of object creation.
static CC_FORCE_INLINE flash_error_t xsnor_device_mmap_on(void *ip, uint8_t **addrp)
void __xsnor_bus_cmd_addr_send(void *ip, uint32_t cmd, flash_offset_t offset, size_t n, const uint8_t *p)
Sends a command followed by a flash address and a data transmit phase.
static CC_FORCE_INLINE flash_error_t xsnor_device_start_erase_sector(void *ip, flash_sector_t sector)
Starts an sector erase operation.
static CC_FORCE_INLINE flash_error_t xsnor_device_read(void *ip, flash_offset_t offset, size_t n, uint8_t *rp)
Read operation.
void __xsnor_bus_cmd_addr(void *ip, uint32_t cmd, flash_offset_t offset)
Sends a command followed by a flash address.
void xsnorMemoryUnmap(void *ip)
Leaves the memory mapped mode.
void __xsnor_bus_cmd(void *ip, uint32_t cmd)
Sends a naked command.
flash_error_t xsnorStart(void *ip, const xsnor_config_t *config)
Configures and activates a SNOR driver.
struct xsnor_config xsnor_config_t
Type of a SNOR configuration structure.
void __xsnor_bus_cmd_addr_dummy_receive(void *ip, uint32_t cmd, flash_offset_t offset, uint32_t dummy, size_t n, uint8_t *p)
Sends a complete header followed by a data receive phase.
static CC_FORCE_INLINE flash_error_t xsnor_device_query_erase(void *ip, unsigned *msec)
Queries the driver for erase operation progress.
void __xsnor_bus_cmd_dummy_receive(void *ip, uint32_t cmd, uint32_t dummy, size_t n, uint8_t *p)
Sends a command followed by dummy cycles and a data receive phase.
static CC_FORCE_INLINE flash_error_t xsnor_device_init(void *ip)
static CC_FORCE_INLINE flash_error_t xsnor_device_program(void *ip, flash_offset_t offset, size_t n, const uint8_t *pp)
Program operation.
void __xsnor_bus_cmd_receive(void *ip, uint32_t cmd, size_t n, uint8_t *p)
Sends a command followed by a data receive phase.
struct xsnor_commands xsnor_commands_t
Type of a commands configuration structure.
void __xsnor_bus_cmd_addr_receive(void *ip, uint32_t cmd, flash_offset_t offset, size_t n, uint8_t *p)
Sends a command followed by a flash address and a data receive phase.
void xsnorStop(void *ip)
Deactivates a SNOR driver.
#define __xsnor_bus_acquire(self)
static CC_FORCE_INLINE flash_error_t xsnor_device_verify_erase(void *ip, flash_sector_t sector)
Returns the erase state of a sector.
flash_error_t xsnorMemoryMap(void *ip, uint8_t **addrp)
Enters the memory mapped mode.
static CC_FORCE_INLINE flash_error_t xsnor_device_start_erase_all(void *ip)
Starts a whole-device erase operation.
uint32_t mutex_t
Type of a mutex.
Definition osal.h:229
struct hal_spi_config SPIConfig
Type of a SPI driver configuration structure.
Definition hal_spi_v1.h:121
struct hal_spi_driver SPIDriver
Type of a structure representing an SPI driver.
Definition hal_spi_v1.h:117
struct hal_wspi_driver WSPIDriver
Type of a structure representing an WSPI driver.
Definition hal_wspi.h:83
struct hal_wspi_config WSPIConfig
Type of a structure representing an WSPI driver configuration.
Definition hal_wspi.h:88
Generated Flash Interface header.
Generic flash interface.
Type of a flash device descriptor.
Definition hal_flash.h:136
Class hal_xsnor_base_c virtual methods table.
flash_error_t(* init)(void *ip)
flash_error_t(* program)(void *ip, flash_offset_t offset, size_t n, const uint8_t *pp)
flash_error_t(* verify_erase)(void *ip, flash_sector_t sector)
flash_error_t(* mmap_on)(void *ip, uint8_t **addrp)
void(* dispose)(void *ip)
void(* mmap_off)(void *ip)
flash_error_t(* query_erase)(void *ip, unsigned *msec)
flash_error_t(* start_erase_sector)(void *ip, flash_sector_t sector)
flash_error_t(* start_erase_all)(void *ip)
flash_error_t(* read)(void *ip, flash_offset_t offset, size_t n, uint8_t *rp)
Structure representing a XSNOR base driver class.
flash_state_t state
Driver state.
flash_interface_i fls
Implemented interface flash_interface_i.
const struct hal_xsnor_base_vmt * vmt
Virtual Methods Table.
const xsnor_commands_t * commands
Current commands configuration.
flash_descriptor_t descriptor
Flash descriptor.
mutex_t mutex
Flash access mutex.
const xsnor_config_t * config
Driver configuration.
Type of a WSPI command descriptor.
Definition hal_wspi.h:101
SNOR driver configuration.
uint8_t spibuf[8]
Non-cacheable SPI buffer.
uint8_t databuf[XSNOR_BUFFER_SIZE]
Non-cacheable data buffer.
wspi_command_t cmdbuf
Non-cacheable WSPI command buffer.
SPI-specific configuration fields.
SPIDriver * drv
SPI driver to be used for physical communication.
const SPIConfig * cfg
SPI driver configuration.
WSPI-specific configuration fields.
const WSPIConfig * cfg
WSPI driver configuration.
WSPIDriver * drv
WSPI driver to be used for physical communication.
SNOR command configuration structure.
uint32_t cmd_data
Command and data.
uint32_t cmd_addr
Command and address.
uint32_t cmd
Command only.
uint32_t cmd_addr_data
Command, address and data.
SNOR driver configuration.
int bus_type
Bus type selection switch.
int options
Device-dependent options, used by subclasses only.
union xsnor_bus_configs bus
WSPI driver configuration.
xsnor_buffers_t * buffers
Pointer to the non-cacheable buffers.
Union of possible bus configurations.
struct xsnor_bus_wspi wspi
struct xsnor_bus_spi spi